/Source/SDK/Manager/CredentialManager.cs
C# | 219 lines | 157 code | 15 blank | 47 comment | 30 complexity | db2818ac749edb15e2760c642f89a25b MD5 | raw file
1using System;
2using System.Collections.Generic;
3using PayPal.Authentication;
4using PayPal.Exception;
5using PayPal.Log;
6
7namespace PayPal.Manager
8{
9 /// <summary>
10 /// Reads API credentials to be used with the application
11 /// </summary>
12 public sealed class CredentialManager
13 {
14 /// <summary>
15 /// Logger
16 /// </summary>
17 private static Logger logger = Logger.GetLogger(typeof(CredentialManager));
18
19 private static string accountPrefix = "account";
20
21#if NET_2_0 || NET_3_5
22 /// <summary>
23 /// Singleton instance of ConnectionManager
24 /// </summary>
25 private static readonly CredentialManager singletonInstance = new CredentialManager();
26
27 /// <summary>
28 /// Explicit static constructor to tell C# compiler not to mark type as beforefieldinit
29 /// </summary>
30 static CredentialManager() { }
31
32 /// <summary>
33 /// Gets the Singleton instance of ConnectionManager
34 /// </summary>
35 public static CredentialManager Instance
36 {
37 get
38 {
39 return singletonInstance;
40 }
41 }
42#elif NET_4_0 || NET_4_5 || NET_4_5_1
43 /// <summary>
44 /// System.Lazy type guarantees thread-safe lazy-construction
45 /// static holder for instance, need to use lambda to construct since constructor private
46 /// </summary>
47 private static readonly Lazy<CredentialManager> laze = new Lazy<CredentialManager>(() => new CredentialManager());
48
49 /// <summary>
50 /// Accessor for the Singleton instance of ConnectionManager
51 /// </summary>
52 public static CredentialManager Instance { get { return laze.Value; } }
53#endif
54
55 /// <summary>
56 /// Private constructor, private to prevent direct instantiation
57 /// </summary>
58 private CredentialManager() { }
59
60 /// <summary>
61 /// Returns the default Account Name
62 /// </summary>
63 /// <returns></returns>
64 private Account GetAccount(Dictionary<string, string> config, string apiUserName)
65 {
66 foreach (KeyValuePair<string, string> kvPair in config)
67 {
68 //logger.Info(kvPair.Key + " " + kvPair.Value);
69 if(kvPair.Key.EndsWith(".apiUsername"))
70 {
71 if (apiUserName == null || apiUserName.Equals(kvPair.Value))
72 {
73 int index = Convert.ToInt32(kvPair.Key.Substring(accountPrefix.Length, kvPair.Key.IndexOf('.') - accountPrefix.Length ));
74 Account accnt = new Account();
75 if (config.ContainsKey(accountPrefix + index + ".apiUsername"))
76 {
77 accnt.APIUserName = config[accountPrefix + index + ".apiUsername"];
78 }
79 if(config.ContainsKey(accountPrefix + index + ".apiPassword"))
80 {
81 accnt.APIPassword = config[accountPrefix + index + ".apiPassword"];
82 }
83 if(config.ContainsKey(accountPrefix + index + ".apiSignature"))
84 {
85 accnt.APISignature = config[accountPrefix + index + ".apiSignature"];
86 }
87 if(config.ContainsKey(accountPrefix + index + ".apiCertificate"))
88 {
89 accnt.APICertificate = config[accountPrefix + index + ".apiCertificate"];
90 }
91 if (config.ContainsKey(accountPrefix + index + ".privateKeyPassword"))
92 {
93 accnt.PrivateKeyPassword = config[accountPrefix + index + ".privateKeyPassword"];
94 }
95 if(config.ContainsKey(accountPrefix + index + ".subject"))
96 {
97 accnt.CertificateSubject = config[accountPrefix + index + ".subject"];
98 }
99 if(config.ContainsKey(accountPrefix + index + ".applicationId"))
100 {
101 accnt.ApplicationId = config[accountPrefix + index + ".applicationId"];
102 }
103 return accnt;
104 }
105 }
106 }
107 return null;
108 }
109
110 /// <summary>
111 /// Returns the API Credentials
112 /// </summary>
113 /// <param name="apiUserName"></param>
114 /// <returns></returns>
115 public ICredential GetCredentials(Dictionary<string, string> config, string apiUserName)
116 {
117 ICredential credential = null;
118 Account accnt = GetAccount(config, apiUserName);
119 if (accnt == null)
120 {
121 throw new MissingCredentialException("Missing credentials for " + apiUserName);
122 }
123 if (!string.IsNullOrEmpty(accnt.APICertificate))
124 {
125 CertificateCredential certCredential = new CertificateCredential(accnt.APIUserName, accnt.APIPassword, accnt.APICertificate, accnt.PrivateKeyPassword);
126 certCredential.ApplicationId = accnt.ApplicationId;
127 if (!string.IsNullOrEmpty(accnt.CertificateSubject))
128 {
129 SubjectAuthorization subAuthorization = new SubjectAuthorization(accnt.CertificateSubject);
130 certCredential.ThirdPartyAuthorization = subAuthorization;
131 }
132 credential = certCredential;
133 }
134 else
135 {
136 SignatureCredential signCredential = new SignatureCredential(accnt.APIUserName, accnt.APIPassword, accnt.APISignature);
137 signCredential.ApplicationId = accnt.ApplicationId;
138 if (!string.IsNullOrEmpty(accnt.SignatureSubject))
139 {
140 SubjectAuthorization subjectAuthorization = new SubjectAuthorization(accnt.SignatureSubject);
141 signCredential.ThirdPartyAuthorization = subjectAuthorization;
142 }
143 if (!string.IsNullOrEmpty(accnt.CertificateSubject))
144 {
145 SubjectAuthorization subAuthorization = new SubjectAuthorization(accnt.CertificateSubject);
146 signCredential.ThirdPartyAuthorization = subAuthorization;
147 }
148 credential = signCredential;
149 }
150 ValidateCredentials(credential);
151
152 return credential;
153 }
154
155 /// <summary>
156 /// Validates the API Credentials
157 /// </summary>
158 /// <param name="apiCredentials"></param>
159 private void ValidateCredentials(ICredential apiCredentials)
160 {
161 if (apiCredentials is SignatureCredential)
162 {
163 SignatureCredential credential = (SignatureCredential)apiCredentials;
164 Validate(credential);
165 }
166 else if (apiCredentials is CertificateCredential)
167 {
168 CertificateCredential credential = (CertificateCredential)apiCredentials;
169 Validate(credential);
170 }
171 }
172
173 /// <summary>
174 /// Validates the Signature Credentials
175 /// </summary>
176 /// <param name="apiCredentials"></param>
177 private void Validate(SignatureCredential apiCredentials)
178 {
179 if (string.IsNullOrEmpty(apiCredentials.UserName))
180 {
181 throw new InvalidCredentialException(BaseConstants.ErrorMessages.ErrorUserName);
182 }
183 if (string.IsNullOrEmpty(apiCredentials.Password))
184 {
185 throw new InvalidCredentialException(BaseConstants.ErrorMessages.ErrorPassword);
186 }
187 if (string.IsNullOrEmpty(((SignatureCredential)apiCredentials).Signature))
188 {
189 throw new InvalidCredentialException(BaseConstants.ErrorMessages.ErrorSignature);
190 }
191 }
192
193 /// <summary>
194 /// Validates the Certificate Credentials
195 /// </summary>
196 /// <param name="apiCredentials"></param>
197 private void Validate(CertificateCredential apiCredentials)
198 {
199 if (string.IsNullOrEmpty(apiCredentials.UserName))
200 {
201 throw new InvalidCredentialException(BaseConstants.ErrorMessages.ErrorUserName);
202 }
203 if (string.IsNullOrEmpty(apiCredentials.Password))
204 {
205 throw new InvalidCredentialException(BaseConstants.ErrorMessages.ErrorPassword);
206 }
207
208 if (string.IsNullOrEmpty(((CertificateCredential)apiCredentials).CertificateFile))
209 {
210 throw new InvalidCredentialException(BaseConstants.ErrorMessages.ErrorCertificate);
211 }
212
213 if (string.IsNullOrEmpty(((CertificateCredential)apiCredentials).PrivateKeyPassword))
214 {
215 throw new InvalidCredentialException(BaseConstants.ErrorMessages.ErrorPrivateKeyPassword);
216 }
217 }
218 }
219}