public void TestGenerateAccessTokenForServiceAccountsWithSecretsFile() { MockAppConfig config = new Mocks.MockAppConfig(); config.MockReadSettings(tblSettingsWithSecretJson); provider = new OAuth2ProviderForServiceAccounts(config); TestUtils.ValidateRequiredParameters(provider, new string[] { "ServiceAccountEmail", "Scope", "JwtPrivateKey" }, delegate() { provider.GenerateAccessTokenForServiceAccount(); } ); oauth2RequestInterceptor.RequestType = OAuth2RequestInterceptor.OAuth2RequestType.FetchAccessTokenForServiceAccount; WebRequestInterceptor.OnBeforeSendResponse callback = delegate(Uri uri, WebHeaderCollection headers, String body) { Assert.AreEqual(SERVICE_ACCOUNT_REQUEST_WITH_SECRETS_FILE, body); }; try { oauth2RequestInterceptor.BeforeSendResponse += callback; provider.GenerateAccessTokenForServiceAccount(); Assert.AreEqual(provider.AccessToken, OAuth2RequestInterceptor.TEST_ACCESS_TOKEN); Assert.AreEqual(provider.TokenType, OAuth2RequestInterceptor.ACCESS_TOKEN_TYPE); Assert.AreEqual(provider.ExpiresIn.ToString(), OAuth2RequestInterceptor.EXPIRES_IN); } finally { oauth2RequestInterceptor.BeforeSendResponse -= callback; } }
public void TestPropertySettersAndGetters() { MockAppConfig config = new MockAppConfig(); config.MockReadSettings(dictSettings); provider = new OAuth2ProviderForServiceAccounts(config) { HttpClientFactory = mockHttpClientFactory, Clock = mockClock, ClientId = TEST_CLIENT_ID }; Assert.AreEqual(provider.ClientId, TEST_CLIENT_ID); provider.ClientSecret = TEST_CLIENT_SECRET; Assert.AreEqual(provider.ClientSecret, TEST_CLIENT_SECRET); provider.AccessToken = TEST_ACCESS_TOKEN; Assert.AreEqual(provider.AccessToken, TEST_ACCESS_TOKEN); provider.Scope = TEST_SCOPE; Assert.AreEqual(provider.Scope, TEST_SCOPE); provider.State = TEST_STATE; Assert.AreEqual(provider.State, TEST_STATE); provider.OnOAuthTokensObtained = TEST_CALLBACK; Assert.AreEqual(provider.OnOAuthTokensObtained, TEST_CALLBACK); }
public void TestConstructor() { MockAppConfig config; // Tests if the constructor works when JSON secrets file is not provided. config = new Mocks.MockAppConfig(); config.MockReadSettings(tblSettingsNoSecretJson); provider = new OAuth2ProviderForServiceAccounts(config); Assert.AreEqual(provider.ClientId, config.OAuth2ClientId); Assert.AreEqual(provider.ClientSecret, config.OAuth2ClientSecret); Assert.AreEqual(provider.AccessToken, config.OAuth2AccessToken); Assert.AreEqual(provider.Scope, config.OAuth2Scope); Assert.AreEqual(provider.ServiceAccountEmail, config.OAuth2ServiceAccountEmail); Assert.AreEqual(provider.PrnEmail, config.OAuth2PrnEmail); Assert.AreEqual(provider.JwtCertificatePath, config.OAuth2CertificatePath); Assert.AreEqual(provider.JwtCertificatePassword, config.OAuth2CertificatePassword); Assert.IsNullOrEmpty(provider.JwtPrivateKey); // Tests if the constructor works when JSON secrets file is provided. config = new Mocks.MockAppConfig(); config.MockReadSettings(tblSettingsWithSecretJson); provider = new OAuth2ProviderForServiceAccounts(config); Assert.AreEqual(provider.ClientId, config.OAuth2ClientId); Assert.AreEqual(provider.ClientSecret, config.OAuth2ClientSecret); Assert.AreEqual(provider.AccessToken, config.OAuth2AccessToken); Assert.AreEqual(provider.Scope, config.OAuth2Scope); Assert.AreEqual(provider.ServiceAccountEmail, config.OAuth2ServiceAccountEmail); Assert.AreEqual(provider.PrnEmail, config.OAuth2PrnEmail); Assert.IsNullOrEmpty(provider.JwtCertificatePath); Assert.IsNullOrEmpty(provider.JwtCertificatePassword); Assert.AreEqual(provider.JwtPrivateKey, config.OAuth2PrivateKey); }
public void TestPropertySettersAndGetters() { provider = new OAuth2ProviderForServiceAccounts(appConfig); provider.ClientId = TEST_CLIENT_ID; Assert.AreEqual(provider.ClientId, TEST_CLIENT_ID); provider.ClientSecret = TEST_CLIENT_SECRET; Assert.AreEqual(provider.ClientSecret, TEST_CLIENT_SECRET); provider.AccessToken = TEST_ACCESS_TOKEN; Assert.AreEqual(provider.AccessToken, TEST_ACCESS_TOKEN); provider.Scope = TEST_SCOPE; Assert.AreEqual(provider.Scope, TEST_SCOPE); provider.State = TEST_STATE; Assert.AreEqual(provider.State, TEST_STATE); provider.OnOAuthTokensObtained = TEST_CALLBACK; Assert.AreEqual(provider.OnOAuthTokensObtained, TEST_CALLBACK); provider.ServiceAccountEmail = TEST_SERVICE_ACCOUNT_EMAIL; Assert.AreEqual(provider.ServiceAccountEmail, TEST_SERVICE_ACCOUNT_EMAIL); provider.PrnEmail = TEST_PRN_EMAIL; Assert.AreEqual(provider.PrnEmail, TEST_PRN_EMAIL); provider.JwtCertificatePath = TEST_JWT_CERTIFICATE_PATH; Assert.AreEqual(provider.JwtCertificatePath, TEST_JWT_CERTIFICATE_PATH); provider.JwtCertificatePassword = TEST_JWT_CERTIFICATE_PASSWORD; Assert.AreEqual(provider.JwtCertificatePassword, TEST_JWT_CERTIFICATE_PASSWORD); }
public void TestGetAuthHeader() { MockAppConfig config = new MockAppConfig(); config.MockReadSettings(tblSettingsNoSecretJson); provider = new OAuth2ProviderForServiceAccounts(config); Assert.AreEqual(AUTHORIZATION_HEADER, provider.GetAuthHeader()); }
public void TestConstructor() { provider = new OAuth2ProviderForServiceAccounts(appConfig); Assert.AreEqual(provider.ClientId, appConfig.OAuth2ClientId); Assert.AreEqual(provider.ClientSecret, appConfig.OAuth2ClientSecret); Assert.AreEqual(provider.AccessToken, appConfig.OAuth2AccessToken); Assert.AreEqual(provider.Scope, appConfig.OAuth2Scope); Assert.AreEqual(provider.ServiceAccountEmail, appConfig.OAuth2ServiceAccountEmail); Assert.AreEqual(provider.PrnEmail, appConfig.OAuth2PrnEmail); Assert.AreEqual(provider.JwtCertificatePath, appConfig.OAuth2CertificatePath); Assert.AreEqual(provider.JwtCertificatePassword, appConfig.OAuth2CertificatePassword); }
public void TestGetAuthHeader() { MockAppConfig config = new MockAppConfig(); config.MockReadSettings(dictSettings); provider = new OAuth2ProviderForServiceAccounts(config) { HttpClientFactory = mockHttpClientFactory, Clock = mockClock }; Assert.AreEqual(AUTHORIZATION_HEADER, provider.GetAuthHeader()); }
public void TestConstructor() { MockAppConfig config = new MockAppConfig(); config.MockReadSettings(dictSettings); provider = new OAuth2ProviderForServiceAccounts(config); Assert.AreEqual(provider.ClientId, config.OAuth2ClientId); Assert.AreEqual(provider.ClientSecret, config.OAuth2ClientSecret); Assert.AreEqual(provider.AccessToken, config.OAuth2AccessToken); Assert.AreEqual(provider.Scope, config.OAuth2Scope); Assert.AreEqual(provider.ServiceAccountEmail, config.OAuth2ServiceAccountEmail); Assert.AreEqual(provider.PrnEmail, config.OAuth2PrnEmail); Assert.AreEqual(provider.JwtPrivateKey, config.OAuth2PrivateKey); Assert.AreEqual(provider.ServiceAccountEmail, config.OAuth2ServiceAccountEmail); }
public void Init() { tblSettings = new Hashtable(); tblSettings.Add("OAuth2ClientId", "OAuth2ClientId"); tblSettings.Add("OAuth2ClientSecret", "OAuth2ClientSecret"); tblSettings.Add("OAuth2ServiceAccountEmail", "OAuth2ServiceAccountEmail"); tblSettings.Add("OAuth2PrnEmail", "OAuth2PrnEmail"); tblSettings.Add("OAuth2AccessToken", "OAuth2AccessToken"); tblSettings.Add("OAuth2Scope", "OAuth2Scope"); tblSettings.Add("OAuth2RedirectUri", "OAuth2RedirectUri"); string tempCertificatePath = Path.GetTempFileName(); using (FileStream fs = File.OpenWrite(tempCertificatePath)) { fs.Write(Resources.certificate, 0, Resources.certificate.Length); } tblSettings.Add("OAuth2JwtCertificatePath", tempCertificatePath); tblSettings.Add("OAuth2JwtCertificatePassword", "notasecret"); appConfig = new MockAppConfig(); appConfig.MockReadSettings(tblSettings); provider = new OAuth2ProviderForServiceAccounts(appConfig); oauth2RequestInterceptor.Intercept = true; }
public void TestGenerateAccessTokenForServiceAccounts() { MockAppConfig config = new Mocks.MockAppConfig(); config.MockReadSettings(dictSettings); provider = new OAuth2ProviderForServiceAccounts(config) { HttpClientFactory = mockHttpClientFactory, Clock = mockClock }; TestUtils.ValidateRequiredParameters(provider, new string[] { "ServiceAccountEmail", "Scope", "JwtPrivateKey" }, delegate() { provider.GenerateAccessTokenForServiceAccount(); } ); provider.GenerateAccessTokenForServiceAccount(); Assert.AreEqual(mockHttpClientFactory.messageHandler.LastRequest, SERVICE_ACCOUNT_REQUEST); Assert.AreEqual(provider.AccessToken, OAuth2RequestInterceptor.TEST_ACCESS_TOKEN); Assert.AreEqual(provider.TokenType, OAuth2RequestInterceptor.ACCESS_TOKEN_TYPE); Assert.AreEqual(provider.ExpiresIn.ToString(), OAuth2RequestInterceptor.EXPIRES_IN); // Test no impersonation with empty string. config.SetPropertyFieldForTests("OAuth2PrnEmail", ""); provider.GenerateAccessTokenForServiceAccount(); Assert.AreEqual(mockHttpClientFactory.messageHandler.LastRequest, SERVICE_ACCOUNT_REQUEST_NO_PRN); // Test no impersonation with null. config.SetPropertyFieldForTests("OAuth2PrnEmail", null); provider.GenerateAccessTokenForServiceAccount(); Assert.AreEqual(mockHttpClientFactory.messageHandler.LastRequest, SERVICE_ACCOUNT_REQUEST_NO_PRN); }
public void TestGetAuthHeader() { provider = new OAuth2ProviderForServiceAccounts(appConfig); Assert.AreEqual(AUTHORIZATION_HEADER, provider.GetAuthHeader()); }