PageRenderTime 58ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/SDK/Manager/CredentialManager.cs

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