/// <summary> /// Returns the API Credentials /// </summary> /// <param name="apiUserName"></param> /// <returns></returns> public ICredential GetCredentials(Dictionary<string, string> config, string apiUserName) { ICredential credential = null; Account accnt = GetAccount(config, apiUserName); if (accnt == null) { throw new MissingCredentialException("Missing credentials for " + apiUserName); } if (!string.IsNullOrEmpty(accnt.APICertificate)) { CertificateCredential certCredential = new CertificateCredential(accnt.APIUsername, accnt.APIPassword, accnt.APICertificate, accnt.PrivateKeyPassword); certCredential.ApplicationID = accnt.ApplicationId; if (!string.IsNullOrEmpty(accnt.CertificateSubject)) { SubjectAuthorization subAuthorization = new SubjectAuthorization(accnt.CertificateSubject); certCredential.ThirdPartyAuthorization = subAuthorization; } credential = certCredential; } else { SignatureCredential signCredential = new SignatureCredential(accnt.APIUsername, accnt.APIPassword, accnt.APISignature); signCredential.ApplicationID = accnt.ApplicationId; if (!string.IsNullOrEmpty(accnt.SignatureSubject)) { SubjectAuthorization subjectAuthorization = new SubjectAuthorization(accnt.SignatureSubject); signCredential.ThirdPartyAuthorization = subjectAuthorization; } credential = signCredential; } ValidateCredentials(credential); return credential; }
/// <summary> /// Processing for TokenAuthorization} using SignatureCredential /// </summary> /// <param name="certCredential"></param> /// <param name="toknAuthorization"></param> /// <returns></returns> protected override Dictionary<string, string> ProcessTokenAuthorization( CertificateCredential certCredential, TokenAuthorization toknAuthorization) { Dictionary<string, string> headers = new Dictionary<string, string>(); try { OAuthGenerator signGenerator = new OAuthGenerator(certCredential.UserName, certCredential.Password); signGenerator.setHTTPMethod(OAuthGenerator.HTTPMethod.POST); signGenerator.setToken(toknAuthorization.AccessToken); signGenerator.setTokenSecret(toknAuthorization.TokenSecret); string tokenTimeStamp = Timestamp; signGenerator.setTokenTimestamp(tokenTimeStamp); log.Debug("token = " + toknAuthorization.AccessToken + " tokenSecret=" + toknAuthorization.TokenSecret + " uri=" + endpointURL); signGenerator.setRequestURI(endpointURL); //Compute Signature string sign = signGenerator.ComputeSignature(); log.Debug("Permissions signature: " + sign); string authorization = "token=" + toknAuthorization.AccessToken + ",signature=" + sign + ",timestamp=" + tokenTimeStamp; log.Debug("Authorization string: " + authorization); headers.Add(BaseConstants.PAYPAL_AUTHORIZATION_MERCHANT, authorization); } catch (OAuthException ae) { throw ae; } return headers; }
/// <summary> /// Processing for TokenAuthorization using SignatureCredential /// </summary> /// <param name="certCredential"></param> /// <param name="tokenAuthorize"></param> /// <returns></returns> protected override Dictionary<string, string> ProcessTokenAuthorization(CertificateCredential certCredential, TokenAuthorization tokenAuthorize) { Dictionary<string, string> headers = new Dictionary<string, string>(); try { OAuthGenerator signGenerator = new OAuthGenerator(certCredential.UserName, certCredential.Password); signGenerator.SetToken(tokenAuthorize.AccessToken); signGenerator.SetTokenSecret(tokenAuthorize.AccessTokenSecret); string tokenTimeStamp = Timestamp; signGenerator.SetTokenTimestamp(tokenTimeStamp); logger.DebugFormat("token = " + tokenAuthorize.AccessToken + " tokenSecret=" + tokenAuthorize.AccessTokenSecret + " uri=" + endpointUrl); signGenerator.SetRequestUri(endpointUrl); //Compute Signature string sign = signGenerator.ComputeSignature(); logger.DebugFormat("Permissions signature: " + sign); string authorization = "token=" + tokenAuthorize.AccessToken + ",signature=" + sign + ",timestamp=" + tokenTimeStamp; logger.DebugFormat("Authorization string: " + authorization); headers.Add(BaseConstants.PayPalAuthorizationPlatformHeader, authorization); } catch (OAuthException oex) { throw oex; } return headers; }
public void GenerateHeaderStrategyToken() { certCredential = new CertificateCredential("testusername", "testpassword", "sdk-cert.p12", "KJAERUGBLVF6Y"); certSOAPHeaderAuthStrategy = new CertificateSOAPHeaderAuthStrategy(); TokenAuthorization toknAuthorization = new TokenAuthorization("accessToken", "tokenSecret"); certSOAPHeaderAuthStrategy.ThirdPartyAuthorization = toknAuthorization; string payload = certSOAPHeaderAuthStrategy.GenerateHeaderStrategy(certCredential); Assert.AreEqual("<ns:RequesterCredentials/>", payload); }
public void GenerateHeaderStrategyWithToken() { certHttpHeaderAuthStrategy = new CertificateHttpHeaderAuthStrategy(Constants.APIEndpointSOAP); TokenAuthorization toknAuthorization = new TokenAuthorization(Constants.AccessToken, Constants.TokenSecret); CertificateCredential certCredential = new CertificateCredential("testusername", "testpassword", "sdk-cert.p12", "KJAERUGBLVF6Y", toknAuthorization); Dictionary<string, string> header = certHttpHeaderAuthStrategy.GenerateHeaderStrategy(certCredential); string authHeader = header[BaseConstants.PayPalAuthorizationMerchantHeader]; string[] headers = authHeader.Split(','); Assert.AreEqual("token=" + Constants.AccessToken, headers[0]); }
public void GenerateHeaderStrategyWithoutToken() { certHttpHeaderAuthStrategy = new CertificateHttpHeaderAuthStrategy(Constants.APIEndpointNVP); certCredential = new CertificateCredential("testusername", "testpassword", "sdk-cert.p12", "KJAERUGBLVF6Y"); Dictionary<string, string> header = certHttpHeaderAuthStrategy.GenerateHeaderStrategy(certCredential); string username = header[BaseConstants.PayPalSecurityUserIdHeader]; string password = header[BaseConstants.PayPalSecurityPasswordHeader]; Assert.AreEqual("testusername", username); Assert.AreEqual("testpassword", password); }
public void GenerateHeaderStrategyWithTokenTest() { certHttpHeaderAuthStrategy = new CertificateHttpHeaderAuthStrategy(UnitTestConstants.APIEndpointSOAP); TokenAuthorization toknAuthorization = new TokenAuthorization(UnitTestConstants.AccessToken, UnitTestConstants.TokenSecret); certCredential = new CertificateCredential("testusername", "testpassword", "sdk-cert.p12", "KJAERUGBLVF6Y", toknAuthorization); Dictionary<string, string> header = certHttpHeaderAuthStrategy.GenerateHeaderStrategy(certCredential); string authHeader = header[BaseConstants.PAYPAL_AUTHORIZATION_PLATFORM_HEADER]; string[] headers = authHeader.Split(','); Assert.AreEqual("token=" + UnitTestConstants.AccessToken, headers[0]); }
public void GenerateHeaderStrategyWithoutTokenTest() { certHttpHeaderAuthStrategy = new CertificateHttpHeaderAuthStrategy(UnitTestConstants.APIEndpointNVP); certCredential = new CertificateCredential("testusername", "testpassword", "sdk-cert.p12", "KJAERUGBLVF6Y"); Dictionary<string, string> header = certHttpHeaderAuthStrategy.GenerateHeaderStrategy(certCredential); string username = header[BaseConstants.PAYPAL_SECURITY_USERID_HEADER]; string password = header[BaseConstants.PAYPAL_SECURITY_PASSWORD_HEADER]; Assert.AreEqual("testusername", username); Assert.AreEqual("testpassword", password); }
public void GenerateHeaderStrategy() { certCredential = new CertificateCredential("testusername", "testpassword", "sdk-cert.p12", "KJAERUGBLVF6Y"); certSOAPHeaderAuthStrategy = new CertificateSOAPHeaderAuthStrategy(); string payload = certSOAPHeaderAuthStrategy.GenerateHeaderStrategy(certCredential); XmlDocument xmlDoc = GetXmlDocument(payload); XmlNodeList xmlNodeListUsername = xmlDoc.GetElementsByTagName("Username"); Assert.IsTrue(xmlNodeListUsername.Count > 0); Assert.AreEqual("testusername", xmlNodeListUsername[0].InnerXml); XmlNodeList xmlNodeListPassword = xmlDoc.GetElementsByTagName("Password"); Assert.IsTrue(xmlNodeListPassword.Count > 0); Assert.AreEqual("testpassword", xmlNodeListPassword[0].InnerXml); }
public ICredential GetCredentials(string apiUserName) { if (apiUserName == null) { apiUserName = GetDefaultAccountName(); } if (this.cachedCredentials.ContainsKey(apiUserName)) { log.Debug("Returning cached credentials for " + apiUserName); return this.cachedCredentials[apiUserName]; } else { ICredential pro = null; ConfigManager configMgr = ConfigManager.Instance; Account acc = configMgr.GetAccount(apiUserName); if (acc == null) { throw new MissingCredentialException("Missing credentials for " + apiUserName); } if (!string.IsNullOrEmpty(acc.APICertificate)) { CertificateCredential cred = new CertificateCredential(); cred.APIUsername = acc.APIUsername; cred.APIPassword = acc.APIPassword; cred.CertificateFile = acc.APICertificate; cred.PrivateKeyPassword = acc.PrivateKeyPassword; cred.ApplicationID = acc.ApplicationId; cred.CertificateSubject = acc.CertificateSubject; pro = cred; } else { SignatureCredential cred = new SignatureCredential(); cred.APIUsername = acc.APIUsername; cred.APIPassword = acc.APIPassword; cred.APISignature = acc.APISignature; cred.ApplicationID = acc.ApplicationId; cred.SignatureSubject = acc.SignatureSubject; pro = cred; } this.cachedCredentials.Add(apiUserName, pro); return pro; } }
public void ObjectCreation() { CertificateCredential cred = new CertificateCredential(); cred.APIUsername = "******"; cred.APIPassword = "******"; cred.ApplicationID = "APP-1234ASDFG"; cred.CertificateFile = "/certs/sdk.p12"; cred.PrivateKeyPassword = "******"; Assert.AreEqual("username_api1.paypal.com", cred.APIUsername); Assert.AreEqual("QWERRTYUIOP", cred.APIPassword); Assert.AreEqual("APP-1234ASDFG", cred.ApplicationID); Assert.AreEqual("/certs/sdk.p12", cred.CertificateFile); Assert.AreEqual("password", cred.PrivateKeyPassword); }
/// <summary> /// Returns the API Credentials /// </summary> /// <param name="apiUserName"></param> /// <returns></returns> public ICredential GetCredentials(string apiUserName) { if (apiUserName == null) { apiUserName = GetDefaultAccountName(); } ICredential credential = null; ConfigManager configMngr = ConfigManager.Instance; Account accnt = configMngr.GetAccount(apiUserName); if (accnt == null) { throw new MissingCredentialException("Missing credentials for " + apiUserName); } if (!string.IsNullOrEmpty(accnt.APICertificate)) { CertificateCredential certCredential = new CertificateCredential(accnt.APIUsername, accnt.APIPassword, accnt.APICertificate, accnt.PrivateKeyPassword); certCredential.ApplicationID = accnt.ApplicationId; if (!string.IsNullOrEmpty(accnt.CertificateSubject)) { SubjectAuthorization subAuthorization = new SubjectAuthorization(accnt.CertificateSubject); certCredential.ThirdPartyAuthorization = subAuthorization; } credential = certCredential; } else { SignatureCredential signCredential = new SignatureCredential(accnt.APIUsername, accnt.APIPassword, accnt.APISignature); signCredential.ApplicationID = accnt.ApplicationId; if (!string.IsNullOrEmpty(accnt.SignatureSubject)) { SubjectAuthorization subjectAuthorization = new SubjectAuthorization(accnt.SignatureSubject); signCredential.ThirdPartyAuthorization = subjectAuthorization; } credential = signCredential; } ValidateCredentials(credential); return credential; }
public void CertificateCredentialArgumentException() { certCredential = new CertificateCredential(null, null, null, null); }
public CertificateCredentialTest() { certCredential = new CertificateCredential("platfo_1255077030_biz_api1.gmail.com", "1255077037", "sdk-cert.p12", "KJAERUGBLVF6Y"); }
/// <summary> /// Validates the Certificate Credentials /// </summary> /// <param name="apiCredentials"></param> private void Validate(CertificateCredential apiCredentials) { if (string.IsNullOrEmpty(apiCredentials.UserName)) { throw new InvalidCredentialException(BaseConstants.ErrorMessages.ErrorUserName); } if (string.IsNullOrEmpty(apiCredentials.Password)) { throw new InvalidCredentialException(BaseConstants.ErrorMessages.ErrorPassword); } if (string.IsNullOrEmpty(((CertificateCredential)apiCredentials).CertificateFile)) { throw new InvalidCredentialException(BaseConstants.ErrorMessages.ErrorCertificate); } if (string.IsNullOrEmpty(((CertificateCredential)apiCredentials).PrivateKeyPassword)) { throw new InvalidCredentialException(BaseConstants.ErrorMessages.ErrorPrivateKeyPassword); } }