public static async Task ClientAssertionWithX509TestAsync(Sts sts) { var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority, TokenCacheType.Null); AuthenticationResultProxy result = null; var certificate = new ClientAssertionCertificate(sts.ValidConfidentialClientId, new X509Certificate2(sts.ConfidentialClientCertificateName, sts.ConfidentialClientCertificatePassword)); RecorderJwtId.JwtIdIndex = 2; result = await context.AcquireTokenAsync(sts.ValidResource, certificate); Verify.IsNotNullOrEmptyString(result.AccessToken); result = await context.AcquireTokenAsync(null, certificate); VerifyErrorResult(result, Sts.InvalidArgumentError, "resource"); result = await context.AcquireTokenAsync(sts.ValidResource, (ClientAssertionCertificate)null); VerifyErrorResult(result, Sts.InvalidArgumentError, "clientCertificate"); var invalidCertificate = new ClientAssertionCertificate(sts.ValidConfidentialClientId, new X509Certificate2(sts.InvalidConfidentialClientCertificateName, sts.InvalidConfidentialClientCertificatePassword)); RecorderJwtId.JwtIdIndex = 3; result = await context.AcquireTokenAsync(sts.ValidResource, invalidCertificate); VerifyErrorResult(result, Sts.InvalidClientError, "50012"); // AADSTS50012: Client assertion contains an invalid signature. invalidCertificate = new ClientAssertionCertificate(sts.InvalidClientId, new X509Certificate2(sts.ConfidentialClientCertificateName, sts.ConfidentialClientCertificatePassword)); RecorderJwtId.JwtIdIndex = 4; result = await context.AcquireTokenAsync(sts.ValidResource, invalidCertificate); VerifyErrorResult(result, Sts.UnauthorizedClient, "70001"); // AADSTS70001: Application '87002806-c87a-41cd-896b-84ca5690d29e' is not registered for the account. }
public static async Task ConfidentialClientWithX509TestAsync(Sts sts) { SetCredential(sts); var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority, TokenCacheType.Null); string authorizationCode = await context.AcquireAccessCodeAsync(sts.ValidResource, sts.ValidConfidentialClientId, sts.ValidRedirectUriForConfidentialClient, sts.ValidUserId); var certificate = new ClientAssertionCertificate(sts.ValidConfidentialClientId, ExportX509Certificate(sts.ConfidentialClientCertificateName, sts.ConfidentialClientCertificatePassword), sts.ConfidentialClientCertificatePassword); RecorderJwtId.JwtIdIndex = 1; AuthenticationResultProxy result = await context.AcquireTokenByAuthorizationCodeAsync(authorizationCode, sts.ValidRedirectUriForConfidentialClient, certificate, sts.ValidResource); VerifySuccessResult(sts, result); result = await context.AcquireTokenByAuthorizationCodeAsync(authorizationCode, sts.ValidRedirectUriForConfidentialClient, certificate); VerifySuccessResult(sts, result); result = await context.AcquireTokenByAuthorizationCodeAsync(authorizationCode, sts.ValidRedirectUriForConfidentialClient, certificate, null); VerifySuccessResult(sts, result); result = await context.AcquireTokenByAuthorizationCodeAsync(null, sts.ValidRedirectUriForConfidentialClient, certificate, sts.ValidResource); VerifyErrorResult(result, Sts.InvalidArgumentError, "authorizationCode"); result = await context.AcquireTokenByAuthorizationCodeAsync(string.Empty, sts.ValidRedirectUriForConfidentialClient, certificate, sts.ValidResource); VerifyErrorResult(result, Sts.InvalidArgumentError, "authorizationCode"); // Send null for redirect result = await context.AcquireTokenByAuthorizationCodeAsync(authorizationCode, null, certificate, sts.ValidResource); VerifyErrorResult(result, Sts.InvalidArgumentError, "redirectUri"); result = await context.AcquireTokenByAuthorizationCodeAsync(authorizationCode, sts.ValidRedirectUriForConfidentialClient, (ClientAssertionCertificate)null, sts.ValidResource); VerifyErrorResult(result, Sts.InvalidArgumentError, "clientCertificate"); }
internal static void AcquireTokenWithPromptBehaviorNeverTest(Sts sts) { // Should not be able to get a token silently on first try. var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority); AuthenticationResultProxy result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Never); VerifyErrorResult(result, Sts.UserInteractionRequired, null); AuthenticationContextProxy.SetCredentials(sts.ValidUserName, sts.ValidPassword); // Obtain a token interactively. result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); VerifySuccessResult(sts, result); AuthenticationContextProxy.SetCredentials(null, null); // Now there should be a token available in the cache so token should be available silently. result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Never); VerifySuccessResult(sts, result); // Clear the cache and silent auth should work via session cookies. AuthenticationContextProxy.ClearDefaultCache(); result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Never); VerifySuccessResult(sts, result); // Clear the cache and cookies and silent auth should fail. AuthenticationContextProxy.ClearDefaultCache(); EndBrowserDialogSession(); result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Never); VerifyErrorResult(result, Sts.UserInteractionRequired, null); }
public static async Task CorrelationIdTestAsync(Sts sts) { SetCredential(sts); var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority); Guid correlationId = Guid.NewGuid(); AuthenticationResultProxy result = null; var eventListener = new SampleEventListener(); eventListener.EnableEvents(AdalOption.AdalEventSource, EventLevel.Verbose); context.SetCorrelationId(correlationId); result = await context.AcquireTokenAsync(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PlatformParameters, sts.ValidUserId); VerifySuccessResult(sts, result); Verify.IsTrue(eventListener.TraceBuffer.Contains(correlationId.ToString())); eventListener.TraceBuffer = string.Empty; context.SetCorrelationId(Guid.Empty); AuthenticationResultProxy result2 = await context.AcquireTokenSilentAsync(sts.ValidResource, sts.ValidClientId); Verify.IsNotNullOrEmptyString(result2.AccessToken); Verify.IsFalse(eventListener.TraceBuffer.Contains(correlationId.ToString())); }
public static async Task AcquireTokenPositiveByRefreshTokenTestAsync(Sts sts) { SetCredential(sts); var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority); AuthenticationResultProxy result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); VerifySuccessResult(sts, result); result = await context.AcquireTokenByRefreshTokenAsync(result.RefreshToken, sts.ValidClientId, (string)null); VerifySuccessResult(sts, result, true, false); AuthenticationResultProxy result2 = await context.AcquireTokenByRefreshTokenAsync(result.RefreshToken + "x", sts.ValidClientId, (string)null); VerifyErrorResult(result2, "invalid_grant", "Refresh Token", 400); result = await context.AcquireTokenByRefreshTokenAsync(result.RefreshToken, sts.ValidClientId, sts.ValidResource); if (sts.Type == StsType.ADFS) { VerifyErrorResult(result, Sts.InvalidArgumentError, "multiple resource"); } else { VerifySuccessResult(sts, result, true, false); } }
internal static void SwitchUserTest(Sts sts) { Log.Comment("Acquire token for user1 interactively"); AuthenticationContextProxy.SetCredentials(null, sts.ValidPassword); var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority); AuthenticationResultProxy result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); VerifySuccessResultAndTokenContent(sts, result); Verify.AreEqual(sts.ValidUserName, result.UserInfo.DisplayableId); Log.Comment("Acquire token via cookie for user1 without user"); AuthenticationResultProxy result2 = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri); VerifySuccessResultAndTokenContent(sts, result); Verify.AreEqual(sts.ValidUserName, result.UserInfo.DisplayableId); Log.Comment("Acquire token for user2 via force prompt and user"); AuthenticationContextProxy.SetCredentials(sts.ValidUserName2, sts.ValidPassword2); result2 = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Always, sts.ValidRequiredUserId2); VerifySuccessResultAndTokenContent(sts, result2); Verify.AreEqual(sts.ValidUserName2, result2.UserInfo.DisplayableId); Log.Comment("Acquire token for user2 via force prompt"); AuthenticationContextProxy.SetCredentials(sts.ValidUserName2, sts.ValidPassword2); result2 = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Always); VerifySuccessResultAndTokenContent(sts, result2); Verify.AreEqual(sts.ValidUserName2, result2.UserInfo.DisplayableId); Log.Comment("Fail to acquire token without user while tokens for two users in the cache"); result2 = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri); VerifyErrorResult(result2, "multiple_matching_tokens_detected", null); }
public static void ExtraQueryParametersTest(Sts sts) { SetCredential(sts); var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority, TokenCacheType.Null); AuthenticationResultProxy result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId, null); VerifySuccessResult(sts, result); result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId, "redirect_uri=123"); VerifyErrorResult(result, "duplicate_query_parameter", "redirect_uri"); result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId, "resource=123&dummy=dummy_value#$%^@%^^%"); VerifyErrorResult(result, "duplicate_query_parameter", "resource"); result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId, "client_id=123"); VerifyErrorResult(result, "duplicate_query_parameter", "client_id"); EndBrowserDialogSession(); result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId, "login_hint=123"); VerifyErrorResult(result, "duplicate_query_parameter", "login_hint"); EndBrowserDialogSession(); result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId, "login_hintx=123"); VerifySuccessResult(sts, result); EndBrowserDialogSession(); result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, UserIdentifier.AnyUser, "login_hint=" + sts.ValidUserName); VerifySuccessResult(sts, result); result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId, string.Empty); VerifySuccessResult(sts, result); }
public static async Task CorrelationIdTestAsync(Sts sts) { SetCredential(sts); var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority); Guid correlationId = Guid.NewGuid(); AuthenticationResultProxy result = null; MemoryStream stream = new MemoryStream(); using (var listener = new TextWriterTraceListener(stream)) { Trace.Listeners.Add(listener); context.SetCorrelationId(correlationId); result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); VerifySuccessResult(sts, result); listener.Flush(); string trace = Encoding.UTF8.GetString(stream.ToArray(), 0, (int)stream.Position); Verify.IsTrue(trace.Contains(correlationId.ToString())); Trace.Listeners.Remove(listener); } stream = new MemoryStream(); using (var listener = new TextWriterTraceListener(stream)) { Trace.Listeners.Add(listener); context.SetCorrelationId(Guid.Empty); AuthenticationResultProxy result2 = await context.AcquireTokenByRefreshTokenAsync(result.RefreshToken, sts.ValidClientId); Verify.IsNotNull(result2.AccessToken); listener.Flush(); string trace = Encoding.UTF8.GetString(stream.ToArray(), 0, (int)stream.Position); Verify.IsFalse(trace.Contains(correlationId.ToString())); Verify.IsTrue(trace.Contains("Correlation ID")); Trace.Listeners.Remove(listener); } }
public static List <AuthenticationResultProxy> AcquireTokenPositiveWithCache(Sts sts, AuthenticationContextProxy context) { AuthenticationResultProxy result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); VerifySuccessResult(sts, result); Log.Comment("Waiting 2 seconds before next token request..."); AuthenticationContextProxy.Delay(2000); // 2 seconds delay AuthenticationResultProxy result2; if (result.UserInfo != null) { result2 = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, new UserIdentifier(result.UserInfo.DisplayableId, UserIdentifierType.OptionalDisplayableId), SecondCallExtraQueryParameter); } else { result2 = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri); } VerifySuccessResult(sts, result2); return(new List <AuthenticationResultProxy> { result, result2 }); }
internal static async Task MultiThreadedClientAssertionWithX509Test(Sts sts) { var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority); const int ParallelCount = 20; AuthenticationResultProxy[] result = new AuthenticationResultProxy[ParallelCount]; var certificate = new ClientAssertionCertificate(sts.ValidConfidentialClientId, new X509Certificate2(sts.ConfidentialClientCertificateName, sts.ConfidentialClientCertificatePassword)); RecorderJwtId.JwtIdIndex = 8; AuthenticationContextProxy.CallSync = true; Parallel.For(0, ParallelCount, async(i) => { result[i] = await context.AcquireTokenAsync(sts.ValidResource, certificate); Log.Comment("Error: " + result[i].Error); Log.Comment("Error Description: " + result[i].ErrorDescription); Verify.IsNotNullOrEmptyString(result[i].AccessToken); }); result[0] = await context.AcquireTokenAsync(sts.ValidResource, certificate); Log.Comment("Error: " + result[0].Error); Log.Comment("Error Description: " + result[0].ErrorDescription); Verify.IsNotNullOrEmptyString(result[0].AccessToken); }
internal static void CacheExpirationMarginTest(Sts sts) { SetCredential(sts); var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority); AuthenticationResultProxy result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); VerifySuccessResult(sts, result); AuthenticationContextProxy.Delay(2000); // 2 seconds delay AuthenticationContextProxy.SetCredentials(null, null); var userId = (result.UserInfo != null) ? new UserIdentifier(result.UserInfo.DisplayableId, UserIdentifierType.OptionalDisplayableId) : UserIdentifier.AnyUser; AuthenticationResultProxy result2 = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, userId, SecondCallExtraQueryParameter); VerifySuccessResult(sts, result2); VerifyExpiresOnAreEqual(result, result2); var dummyContext = new AuthenticationContext("https://dummy/dummy", false); AdalFriend.UpdateTokenExpiryOnTokenCache(dummyContext.TokenCache, DateTime.UtcNow + TimeSpan.FromSeconds(4 * 60 + 50)); result2 = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, userId); VerifySuccessResult(sts, result2); Verify.AreNotEqual(result.AccessToken, result2.AccessToken); }
public static async Task AcquireTokenPositiveTestAsync(Sts sts) { SetCredential(sts); var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority); AuthenticationResultProxy result = await context.AcquireTokenAsync(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PlatformParameters, sts.ValidUserId); VerifySuccessResult(sts, result); }
internal static async Task ConfidentialClientTokenRefreshWithMRRTTest(Sts sts) { SetCredential(sts); var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority); string authorizationCode = context.AcquireAccessCode(sts.ValidResource, sts.ValidConfidentialClientId, sts.ValidRedirectUriForConfidentialClient, sts.ValidUserId); var credential = new ClientCredential(sts.ValidConfidentialClientId, sts.ValidConfidentialClientSecret); AuthenticationResultProxy result = await context.AcquireTokenByAuthorizationCodeAsync(authorizationCode, sts.ValidRedirectUriForConfidentialClient, credential); VerifySuccessResult(sts, result); AuthenticationResultProxy result2 = await context.AcquireTokenByRefreshTokenAsync(result.RefreshToken, credential, sts.ValidResource2); VerifySuccessResult(sts, result2, true, false); AuthenticationContextProxy.ClearDefaultCache(); result = await context.AcquireTokenByAuthorizationCodeAsync(authorizationCode, sts.ValidRedirectUriForConfidentialClient, credential); VerifySuccessResult(sts, result); result2 = await context.AcquireTokenSilentAsync(sts.ValidResource, credential, UserIdentifier.AnyUser); VerifySuccessResult(sts, result2, true, false); result2 = await context.AcquireTokenSilentAsync(sts.ValidResource2, sts.ValidConfidentialClientId); VerifyErrorResult(result2, AdalError.FailedToAcquireTokenSilently, null); result2 = await context.AcquireTokenSilentAsync(sts.ValidResource2, credential, UserIdentifier.AnyUser); VerifySuccessResult(sts, result2, true, false); }
internal static async Task TokenSubjectTypeTest(Sts sts) { SetCredential(sts); var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority); string authorizationCode = context.AcquireAccessCode(sts.ValidResource, sts.ValidConfidentialClientId, sts.ValidRedirectUriForConfidentialClient, sts.ValidUserId); var credential = new ClientCredential(sts.ValidConfidentialClientId, sts.ValidConfidentialClientSecret); AuthenticationResultProxy result = await context.AcquireTokenByAuthorizationCodeAsync(authorizationCode, sts.ValidRedirectUriForConfidentialClient, credential); VerifySuccessResult(sts, result); AuthenticationResultProxy result2 = await context.AcquireTokenSilentAsync(sts.ValidResource, credential, sts.ValidUserId); VerifySuccessResult(sts, result2); VerifyExpiresOnAreEqual(result, result2); AuthenticationResultProxy result3 = await context.AcquireTokenAsync(sts.ValidResource, credential); VerifySuccessResult(sts, result3, false, false); AuthenticationResultProxy result4 = await context.AcquireTokenAsync(sts.ValidResource, credential); VerifySuccessResult(sts, result4, false, false); VerifyExpiresOnAreEqual(result3, result4); VerifyExpiresOnAreNotEqual(result, result3); var cacheItems = TokenCache.DefaultShared.ReadItems().ToList(); Verify.AreEqual(cacheItems.Count, 2); }
internal static void GetAuthorizationRequestURLTest(Sts sts) { var context = new AuthenticationContext(sts.Authority, sts.ValidateAuthority); Uri uri = null; try { uri = context.GetAuthorizationRequestURL(null, sts.ValidClientId, sts.ValidDefaultRedirectUri, sts.ValidUserId, "extra=123"); } catch (ArgumentNullException ex) { Verify.AreEqual(ex.ParamName, "resource"); } uri = context.GetAuthorizationRequestURL(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, sts.ValidUserId, "extra=123"); Verify.IsNotNull(uri); Verify.IsTrue(uri.AbsoluteUri.Contains("login_hint")); Verify.IsTrue(uri.AbsoluteUri.Contains("extra=123")); uri = context.GetAuthorizationRequestURL(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, UserIdentifier.AnyUser, null); Verify.IsNotNull(uri); Verify.IsFalse(uri.AbsoluteUri.Contains("login_hint")); Verify.IsFalse(uri.AbsoluteUri.Contains("client-request-id=")); context.CorrelationId = Guid.NewGuid(); uri = context.GetAuthorizationRequestURL(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, sts.ValidUserId, "extra"); Verify.IsNotNull(uri); Verify.IsTrue(uri.AbsoluteUri.Contains("client-request-id=")); }
public static void AcquireTokenPositiveTest(Sts sts) { SetCredential(sts); var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority); AuthenticationResultProxy result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); VerifySuccessResult(sts, result); }
public static async Task ClientCredentialTestAsync(Sts sts) { var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority, TokenCacheType.Null); AuthenticationResultProxy result = null; var credential = new ClientCredential(sts.ValidConfidentialClientId, sts.ValidConfidentialClientSecret); result = await context.AcquireTokenAsync(sts.ValidResource, credential); Verify.IsNotNullOrEmptyString(result.AccessToken); result = await context.AcquireTokenAsync(null, credential); VerifyErrorResult(result, Sts.InvalidArgumentError, "resource"); result = await context.AcquireTokenAsync(sts.ValidResource, (ClientCredential)null); VerifyErrorResult(result, Sts.InvalidArgumentError, "clientCredential"); var invalidCredential = new ClientCredential(sts.ValidConfidentialClientId, sts.ValidConfidentialClientSecret + "x"); result = await context.AcquireTokenAsync(sts.ValidResource, invalidCredential); VerifyErrorResult(result, Sts.InvalidClientError, null, 0, "50012"); invalidCredential = new ClientCredential(sts.ValidConfidentialClientId.Replace("0", "1"), sts.ValidConfidentialClientSecret + "x"); result = await context.AcquireTokenAsync(sts.ValidResource, invalidCredential); VerifyErrorResult(result, Sts.UnauthorizedClient, null, 400, "70001"); }
public static async Task InstanceDiscoveryTestAsync(Sts sts) { SetCredential(sts); var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority); AuthenticationResultProxy result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); VerifySuccessResult(sts, result); AuthenticationContextProxy.SetEnvironmentVariable("ExtraQueryParameter", string.Empty); // PROD discovery endpoint knows about PPE as well, so this passes discovery and fails later as refresh token is invalid for PPE. context = new AuthenticationContextProxy(sts.Authority.Replace("windows.net", "windows-ppe.net"), sts.ValidateAuthority); result = await context.AcquireTokenByRefreshTokenAsync(result.RefreshToken, sts.ValidClientId, sts.ValidResource); VerifyErrorResult(result, "invalid_grant", "Refresh Token"); try { context = new AuthenticationContextProxy(sts.Authority.Replace("windows.net", "windows.unknown"), sts.ValidateAuthority); result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); VerifyErrorResult(result, "authority_not_in_valid_list", "authority"); } #if TEST_ADAL_WINPHONE_UNIT catch (AdalServiceException ex) { Verify.AreNotEqual(sts.Type, StsType.ADFS); Verify.AreEqual(ex.ErrorCode, Sts.AuthorityNotInValidList); Verify.IsTrue(ex.Message.Contains("authority")); } #endif finally { } }
public static async Task AcquireTokenFromCacheTestAsync(Sts sts) { AuthenticationContext context = new AuthenticationContext(sts.Authority, sts.ValidateAuthority); try { await context.AcquireTokenSilentAsync(sts.ValidResource, sts.ValidClientId, sts.ValidUserId); Verify.Fail("AdalSilentTokenAcquisitionException was expected"); } catch (AdalSilentTokenAcquisitionException ex) { Verify.AreEqual(AdalError.FailedToAcquireTokenSilently, ex.ErrorCode); } catch { Verify.Fail("AdalSilentTokenAcquisitionException was expected"); } AuthenticationContextProxy.SetCredentials(sts.Type == StsType.ADFS ? sts.ValidUserName : null, sts.ValidPassword); var contextProxy = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority); AuthenticationResultProxy resultProxy = contextProxy.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); VerifySuccessResult(sts, resultProxy); AuthenticationResult result = await context.AcquireTokenSilentAsync(sts.ValidResource, sts.ValidClientId, (sts.Type == StsType.ADFS)?UserIdentifier.AnyUser : sts.ValidUserId); VerifySuccessResult(result); result = await context.AcquireTokenSilentAsync(sts.ValidResource, sts.ValidClientId); VerifySuccessResult(result); }
public static async Task MultiResourceRefreshTokenTestAsync(Sts sts) { SetCredential(sts); var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority); AuthenticationResultProxy result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); VerifySuccessResult(sts, result); AuthenticationResultProxy result2 = await context.AcquireTokenByRefreshTokenAsync(result.RefreshToken, sts.ValidClientId, sts.ValidResource2); if (sts.Type == StsType.AAD) { VerifySuccessResult(sts, result2, true, false); Verify.IsTrue(result.IsMultipleResourceRefreshToken); Verify.IsTrue(result2.IsMultipleResourceRefreshToken); } result2 = context.AcquireToken(sts.ValidResource2, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); VerifySuccessResult(sts, result2); if (sts.Type == StsType.ADFS) { Verify.IsFalse(result.IsMultipleResourceRefreshToken); } else { Verify.IsTrue(result.IsMultipleResourceRefreshToken); } if (sts.Type == StsType.AAD) { result2 = context.AcquireToken(sts.ValidResource3, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); VerifySuccessResult(sts, result2); Verify.IsTrue(result.IsMultipleResourceRefreshToken); } }
public static void AcquireTokenPositiveWithInMemoryCacheTest(Sts sts) { SetCredential(sts); var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority, TokenCacheType.InMemory); List <AuthenticationResultProxy> results = AcquireTokenPositiveWithCacheExpectingEqualResults(sts, context); VerifyExpiresOnAreEqual(results[0], results[1]); }
public static void AcquireTokenWithAuthenticationCanceledTest(Sts sts) { AuthenticationContextProxy.SetCredentials(null, null); var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority); AuthenticationResultProxy result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, new UserIdentifier("*****@*****.**", UserIdentifierType.OptionalDisplayableId)); VerifyErrorResult(result, Sts.AuthenticationCanceledError, "canceled"); }
public static void AcquireTokenWithIncorrectUserCredentialTest(Sts sts) { AuthenticationContextProxy.SetCredentials(sts.InvalidUserName, "invalid_password"); var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority); AuthenticationResultProxy result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, UserIdentifier.AnyUser, "incorrect_user"); VerifyErrorResult(result, Sts.AuthenticationCanceledError, "canceled"); }
public static void AcquireTokenWithInvalidClientIdTest(Sts sts) { SetCredential(sts); var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority); AuthenticationResultProxy result = context.AcquireToken(sts.ValidResource, sts.InvalidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); VerifyErrorResult(result, Sts.AuthenticationCanceledError, null); }
public MainPage() { this.InitializeComponent(); this.NavigationCacheMode = NavigationCacheMode.Required; this.sts = StsFactory.CreateSts(StsType.AAD); }
public static async Task AcquireTokenPositiveWithInMemoryCacheTestAsync(Sts sts) { SetCredential(sts); var context = new AuthenticationContextProxy(TestConstants.DefaultAuthorityCommonTenant, sts.ValidateAuthority, TokenCacheType.InMemory); List <AuthenticationResultProxy> results = await AcquireTokenPositiveWithCacheExpectingEqualResultsAsync(sts, context); VerifyExpiresOnAreEqual(results[0], results[1]); }
public static async Task AcquireTokenWithAuthenticationCanceledTestAsync(Sts sts) { AuthenticationContextProxy.SetCredentials(null, null); var context = new AuthenticationContextProxy(TestConstants.DefaultAuthorityCommonTenant, sts.ValidateAuthority); AuthenticationResultProxy result = await context.AcquireTokenAsync(TestConstants.DefaultResource, TestConstants.DefaultClientId, TestConstants.DefaultResource, PlatformParameters, new UserIdentifier("*****@*****.**", UserIdentifierType.OptionalDisplayableId)); VerifyErrorResult(result, Sts.AuthenticationCanceledError, "canceled"); }
private static void VerifySuccessResultAndTokenContent(Sts sts, AuthenticationResultProxy result, bool supportRefreshToken = true, bool supportUserInfo = true) { VerifySuccessResult(sts, result, supportRefreshToken, supportUserInfo); if (supportUserInfo) { VerifyTokenContent(result); } }
public static async Task ConfidentialClientTestAsync(Sts sts) { SetCredential(sts); var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority); string authorizationCode = context.AcquireAccessCode(sts.ValidResource, sts.ValidConfidentialClientId, sts.ValidRedirectUriForConfidentialClient, sts.ValidUserId); var credential = new ClientCredential(sts.ValidConfidentialClientId, sts.ValidConfidentialClientSecret); AuthenticationResultProxy result = await context.AcquireTokenByAuthorizationCodeAsync(authorizationCode, sts.ValidRedirectUriForConfidentialClient, credential); VerifySuccessResult(sts, result); AuthenticationContextProxy.Delay(2000); // 2 seconds delay context.SetCorrelationId(new Guid("2ddbba59-1a04-43fb-b363-7fb0ae785031")); // Test cache usage in AcquireTokenByAuthorizationCodeAsync // There is no cache lookup, so the results should be different. AuthenticationResultProxy result2 = await context.AcquireTokenByAuthorizationCodeAsync(authorizationCode, sts.ValidRedirectUriForConfidentialClient, credential); VerifySuccessResult(sts, result2); Verify.AreNotEqual(result.AccessToken, result2.AccessToken); AuthenticationContextProxy.ClearDefaultCache(); result = await context.AcquireTokenByRefreshTokenAsync(result.RefreshToken, credential); VerifySuccessResult(sts, result, true, false); result = await context.AcquireTokenByRefreshTokenAsync(result.RefreshToken, sts.ValidConfidentialClientId, sts.ValidResource); VerifyErrorResult(result, "invalid_request", null, 400, "90014"); // ACS90014: The request body must contain the following parameter: 'client_secret or client_assertion'. result = await context.AcquireTokenByAuthorizationCodeAsync(null, sts.ValidRedirectUriForConfidentialClient, credential); VerifyErrorResult(result, "invalid_argument", "authorizationCode"); result = await context.AcquireTokenByAuthorizationCodeAsync(string.Empty, sts.ValidRedirectUriForConfidentialClient, credential); VerifyErrorResult(result, "invalid_argument", "authorizationCode"); result = await context.AcquireTokenByAuthorizationCodeAsync(authorizationCode + "x", sts.ValidRedirectUriForConfidentialClient, credential); VerifyErrorResult(result, "invalid_grant", "authorization code"); result = await context.AcquireTokenByAuthorizationCodeAsync(authorizationCode, new Uri(sts.ValidRedirectUriForConfidentialClient.AbsoluteUri + "x"), credential); VerifyErrorResult(result, "invalid_grant", "does not match the reply address", 400, "70002"); result = await context.AcquireTokenByAuthorizationCodeAsync(authorizationCode, sts.ValidRedirectUriForConfidentialClient, (ClientCredential)null); VerifyErrorResult(result, "invalid_argument", "credential"); var invalidCredential = new ClientCredential(sts.ValidConfidentialClientId, sts.ValidConfidentialClientSecret + "x"); result = await context.AcquireTokenByAuthorizationCodeAsync(authorizationCode, sts.ValidRedirectUriForConfidentialClient, invalidCredential); VerifyErrorResult(result, "invalid_client", "client secret", 401); }
public static async Task AuthenticationParametersDiscoveryTestAsync(Sts sts) { const string RelyingPartyWithDiscoveryUrl = "http://localhost:8080"; using (Microsoft.Owin.Hosting.WebApp.Start<RelyingParty>(RelyingPartyWithDiscoveryUrl)) { Log.Comment("Relying Party Started"); HttpWebResponse response = null; AuthenticationParametersProxy authParams = null; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(RelyingPartyWithDiscoveryUrl); request.ContentType = "application/x-www-form-urlencoded"; response = (HttpWebResponse)request.GetResponse(); } catch (WebException ex) { response = (HttpWebResponse)ex.Response; if (response.StatusCode == HttpStatusCode.Unauthorized) { authParams = AuthenticationParametersProxy.CreateFromResponseAuthenticateHeader(response.Headers["WWW-authenticate"]); } } finally { response.Close(); } SetCredential(sts); var context = new AuthenticationContextProxy(authParams.Authority, sts.ValidateAuthority, TokenCacheType.Null); var result = await context.AcquireTokenAsync(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PlatformParameters, sts.ValidUserId); VerifySuccessResult(sts, result); // ADAL WinRT does not support AuthenticationParameters.CreateFromUnauthorizedResponse API if (TestType != TestType.WinRT) { using (HttpClient client = new HttpClient()) { HttpResponseMessage responseMessage = await client.GetAsync(RelyingPartyWithDiscoveryUrl); authParams = await AuthenticationParametersProxy.CreateFromUnauthorizedResponseAsync(responseMessage); } context = new AuthenticationContextProxy(authParams.Authority, sts.ValidateAuthority, TokenCacheType.Null); result = await context.AcquireTokenAsync(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PlatformParameters, sts.ValidUserId); VerifySuccessResult(sts, result); } authParams = await AuthenticationParametersProxy.CreateFromResourceUrlAsync(new Uri(RelyingPartyWithDiscoveryUrl)); context = new AuthenticationContextProxy(authParams.Authority, sts.ValidateAuthority, TokenCacheType.Null); result = await context.AcquireTokenAsync(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PlatformParameters, sts.ValidUserId); AdalTests.VerifySuccessResult(sts, result); Log.Comment("Relying Party Terminating..."); } }
public static void VerifySuccessResult(Sts sts, AuthenticationResultProxy result, bool supportRefreshToken = true, bool supportUserInfo = true) { Log.Comment("Verifying success result..."); if (result.Status != AuthenticationStatusProxy.Success) { Log.Comment(string.Format("Unexpected '{0}' error from service: {1}", result.Error, result.ErrorDescription)); } Verify.AreEqual(AuthenticationStatusProxy.Success, result.Status, "AuthenticationResult.Status"); Verify.IsNotNullOrEmptyString(result.AccessToken, "AuthenticationResult.AccessToken"); if (supportRefreshToken) { Verify.IsNotNullOrEmptyString(result.RefreshToken, "AuthenticationResult.RefreshToken"); } else { Verify.IsNullOrEmptyString(result.RefreshToken, "AuthenticationResult.RefreshToken"); } Verify.IsNullOrEmptyString(result.Error, "AuthenticationResult.Error"); Verify.IsNullOrEmptyString(result.ErrorDescription, "AuthenticationResult.ErrorDescription"); if (sts.Type != StsType.ADFS && supportUserInfo) { Action <string, string, bool> ValidateUserInfo = (string field, string caption, bool required) => { if (string.IsNullOrEmpty(field)) { if (required) { Log.Error("No " + caption); } else { Log.Warning("No " + caption); } } else { Log.Comment(field, caption); } }; ValidateUserInfo(result.TenantId, "tenant id", true); ValidateUserInfo(result.UserInfo.UniqueId, "user unique id", true); ValidateUserInfo(result.UserInfo.DisplayableId, "user displayable id", true); ValidateUserInfo(result.UserInfo.IdentityProvider, "identity provider", true); ValidateUserInfo(result.UserInfo.GivenName, "given name", false); ValidateUserInfo(result.UserInfo.FamilyName, "family name", false); } long expiresIn = (long)(result.ExpiresOn - DateTime.UtcNow).TotalSeconds; Log.Comment("Verifying token expiration..."); Verify.IsGreaterThanOrEqual(expiresIn, (long)0, "Token ExpiresOn"); }
public static void NonHttpsURLNegativeTest(Sts sts) { AuthenticationContextProxy.SetCredentials(sts.ValidUserName, sts.ValidPassword); var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority); // Obtain a token interactively. AuthenticationResultProxy result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); VerifyErrorDescriptionContains(result.ErrorDescription, "Non-HTTPS url redirect is not supported in webview"); }
public static void AcquireTokenPositiveWithNullCacheTest(Sts sts) { AuthenticationContextProxy.SetCredentials(sts.ValidUserName, sts.ValidPassword); var context = new AuthenticationContextProxy( sts.Authority, sts.ValidateAuthority, TokenCacheType.Null); List <AuthenticationResultProxy> results = AcquireTokenPositiveWithCache(sts, context); VerifyExpiresOnAreNotEqual(results[0], results[1]); }
public static async Task AcquireTokenPositiveWithNullCacheTestAsync(Sts sts) { AuthenticationContextProxy.SetCredentials(sts.ValidUserName, sts.ValidPassword); var context = new AuthenticationContextProxy( TestConstants.DefaultAuthorityCommonTenant, sts.ValidateAuthority, TokenCacheType.Null); List <AuthenticationResultProxy> results = await AcquireTokenPositiveWithCacheAsync(sts, context); VerifyExpiresOnAreNotEqual(results[0], results[1]); }
public static async Task UserInfoTestAsync(Sts sts) { SetCredential(sts); var context = new AuthenticationContextProxy(TestConstants.DefaultAuthorityCommonTenant, sts.ValidateAuthority); AuthenticationResultProxy result = await context.AcquireTokenAsync(TestConstants.DefaultResource, TestConstants.DefaultClientId, TestConstants.DefaultResource, PlatformParameters, sts.ValidUserId); VerifySuccessResult(sts, result); AuthenticationResultProxy result2; if (sts.Type == StsType.AAD) { Verify.AreEqual(sts.ValidUserName, result.UserInfo.DisplayableId); Verify.IsNotNullOrEmptyString(result.UserInfo.UniqueId); Verify.IsNotNullOrEmptyString(result.UserInfo.GivenName); Verify.IsNotNullOrEmptyString(result.UserInfo.FamilyName); EndBrowserDialogSession(); Log.Comment("Waiting 2 seconds before next token request..."); AuthenticationContextProxy.Delay(2000); // 2 seconds delay AuthenticationContextProxy.SetCredentials(null, null); result2 = await context.AcquireTokenAsync(TestConstants.DefaultResource, TestConstants.DefaultClientId, TestConstants.DefaultResource, PlatformParameters, new UserIdentifier(result.UserInfo.DisplayableId, UserIdentifierType.OptionalDisplayableId), SecondCallExtraQueryParameter); ValidateAuthenticationResultsAreEqual(result, result2); } AuthenticationContextProxy.SetCredentials(null, null); result2 = await context.AcquireTokenAsync(TestConstants.DefaultResource, TestConstants.DefaultClientId, TestConstants.DefaultResource, PlatformParameters); Verify.AreEqual(result.AccessToken, result2.AccessToken); SetCredential(sts); result2 = await context.AcquireTokenAsync(TestConstants.DefaultResource, TestConstants.DefaultClientId, TestConstants.DefaultResource, PlatformParameters, sts.ValidUserId, ThirdCallExtraQueryParameter); VerifySuccessResult(sts, result2); if (result.UserInfo != null) { ValidateAuthenticationResultsAreEqual(result, result2); } else { VerifyExpiresOnAreNotEqual(result, result2); } EndBrowserDialogSession(); Log.Comment("Waiting 2 seconds before next token request..."); AuthenticationContextProxy.Delay(2000); // 2 seconds delay AuthenticationContextProxy.SetCredentials(sts.ValidUserName, sts.ValidPassword); result2 = await context.AcquireTokenAsync(TestConstants.DefaultResource, TestConstants.DefaultClientId, TestConstants.DefaultResource, PlatformParameters, sts.InvalidRequiredUserId, SecondCallExtraQueryParameter); VerifyErrorResult(result2, "user_mismatch", null); }
// Constructor public MainPage() { InitializeComponent(); this.sts = StsFactory.CreateSts(StsType.AAD); this.fls = new FileLoggingSession("Test File Logging Session"); this.fls.AddLoggingChannel(AdalTrace.AdalLoggingChannel, LoggingLevel.Verbose); this.ls = new LoggingSession("Test Logging Session"); this.ls.AddLoggingChannel(AdalTrace.AdalLoggingChannel, LoggingLevel.Verbose); }
public static async Task AcquireTokenPositiveWithoutRedirectUriOrUserIdTestAsync(Sts sts) { AuthenticationContextProxy.SetCredentials(sts.ValidUserName, sts.ValidPassword); var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority); AuthenticationResultProxy result = await context.AcquireTokenAsync(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PlatformParameters); VerifySuccessResult(sts, result); result = await context.AcquireTokenAsync(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PlatformParameters, null); VerifyErrorResult(result, Sts.InvalidArgumentError, "userId"); VerifyErrorResult(result, Sts.InvalidArgumentError, "UserIdentifier.AnyUser"); result = await context.AcquireTokenAsync(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PlatformParameters, UserIdentifier.AnyUser); VerifySuccessResult(sts, result); }
public static void AcquireTokenPositiveWithoutRedirectUriOrUserIdTest(Sts sts) { AuthenticationContextProxy.SetCredentials(sts.ValidUserName, sts.ValidPassword); var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority); AuthenticationResultProxy result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri); VerifySuccessResult(sts, result); result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, null); VerifyErrorResult(result, Sts.InvalidArgumentError, "userId"); VerifyErrorResult(result, Sts.InvalidArgumentError, "UserIdentifier.AnyUser"); result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, UserIdentifier.AnyUser); VerifySuccessResult(sts, result); }
public static void AcquireTokenWithCallbackTest(Sts sts) { SetCredential(sts); var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority) { AuthenticationContextDelegate = AuthenticationContextPositiveDelegate }; positiveCalled = false; AuthenticationResultProxy result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); Verify.IsTrue(positiveCalled); VerifySuccessResult(sts, result); context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority) { AuthenticationContextDelegate = AuthenticationContextNegativeDelegate }; negativeCalled = false; result = context.AcquireToken(sts.ValidResource, sts.InvalidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); Verify.IsTrue(negativeCalled); VerifyErrorResult(result, Sts.AuthenticationCanceledError, null); }
public static async Task ConfidentialClientWithX509TestAsync(Sts sts) { SetCredential(sts); var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority, TokenCacheType.Null); string authorizationCode = context.AcquireAccessCode(sts.ValidResource, sts.ValidConfidentialClientId, sts.ValidRedirectUriForConfidentialClient, sts.ValidUserId); var certificate = new ClientAssertionCertificate(sts.ValidConfidentialClientId, new X509Certificate2(sts.ConfidentialClientCertificateName, sts.ConfidentialClientCertificatePassword)); RecorderJwtId.JwtIdIndex = 1; AuthenticationResultProxy result = await context.AcquireTokenByAuthorizationCodeAsync(authorizationCode, sts.ValidRedirectUriForConfidentialClient, certificate, sts.ValidResource); VerifySuccessResult(sts, result); result = await context.AcquireTokenByAuthorizationCodeAsync(authorizationCode, sts.ValidRedirectUriForConfidentialClient, certificate); VerifySuccessResult(sts, result); result = await context.AcquireTokenByRefreshTokenAsync(result.RefreshToken, certificate, sts.ValidResource); VerifySuccessResult(sts, result, true, false); result = await context.AcquireTokenByRefreshTokenAsync(result.RefreshToken, sts.ValidConfidentialClientId, sts.ValidResource); VerifyErrorResult(result, Sts.InvalidRequest, null, 400, "90014"); // The request body must contain the following parameter: 'client_secret or client_assertion'. result = await context.AcquireTokenByAuthorizationCodeAsync(authorizationCode, sts.ValidRedirectUriForConfidentialClient, certificate, null); VerifySuccessResult(sts, result); result = await context.AcquireTokenByAuthorizationCodeAsync(null, sts.ValidRedirectUriForConfidentialClient, certificate, sts.ValidResource); VerifyErrorResult(result, Sts.InvalidArgumentError, "authorizationCode"); result = await context.AcquireTokenByAuthorizationCodeAsync(string.Empty, sts.ValidRedirectUriForConfidentialClient, certificate, sts.ValidResource); VerifyErrorResult(result, Sts.InvalidArgumentError, "authorizationCode"); // Send null for redirect result = await context.AcquireTokenByAuthorizationCodeAsync(authorizationCode, null, certificate, sts.ValidResource); VerifyErrorResult(result, Sts.InvalidArgumentError, "redirectUri"); result = await context.AcquireTokenByAuthorizationCodeAsync(authorizationCode, sts.ValidRedirectUriForConfidentialClient, (ClientAssertionCertificate)null, sts.ValidResource); VerifyErrorResult(result, Sts.InvalidArgumentError, "clientCertificate"); }
public static void VerifySuccessResult(Sts sts, AuthenticationResultProxy result, bool supportRefreshToken = true, bool supportUserInfo = true) { Log.Comment("Verifying success result..."); if (result.Status != AuthenticationStatusProxy.Success) { Log.Comment(string.Format("Unexpected '{0}' error from service: {1}", result.Error, result.ErrorDescription)); } Verify.AreEqual(AuthenticationStatusProxy.Success, result.Status, "AuthenticationResult.Status"); Verify.IsNotNullOrEmptyString(result.AccessToken, "AuthenticationResult.AccessToken"); if (supportRefreshToken) { Verify.IsNotNullOrEmptyString(result.RefreshToken, "AuthenticationResult.RefreshToken"); } else { Verify.IsNullOrEmptyString(result.RefreshToken, "AuthenticationResult.RefreshToken"); } Verify.IsNullOrEmptyString(result.Error, "AuthenticationResult.Error"); Verify.IsNullOrEmptyString(result.ErrorDescription, "AuthenticationResult.ErrorDescription"); if (sts.Type != StsType.ADFS && supportUserInfo) { Action<string, string, bool> ValidateUserInfo = (string field, string caption, bool required) => { if (string.IsNullOrEmpty(field)) { if (required) { Log.Error("No " + caption); } else { Log.Warning("No " + caption); } } else { Log.Comment(field, caption); } }; ValidateUserInfo(result.TenantId, "tenant id", true); ValidateUserInfo(result.UserInfo.UniqueId, "user unique id", true); ValidateUserInfo(result.UserInfo.DisplayableId, "user displayable id", true); ValidateUserInfo(result.UserInfo.IdentityProvider, "identity provider", true); ValidateUserInfo(result.UserInfo.GivenName, "given name", false); ValidateUserInfo(result.UserInfo.FamilyName, "family name", false); } long expiresIn = (long)(result.ExpiresOn - DateTime.UtcNow).TotalSeconds; Log.Comment("Verifying token expiration..."); Verify.IsGreaterThanOrEqual(expiresIn, (long)0, "Token ExpiresOn"); }
public static List<AuthenticationResultProxy> AcquireTokenPositiveWithCache(Sts sts, AuthenticationContextProxy context) { AuthenticationResultProxy result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); VerifySuccessResult(sts, result); Log.Comment("Waiting 2 seconds before next token request..."); AuthenticationContextProxy.Delay(2000); // 2 seconds delay AuthenticationResultProxy result2; if (result.UserInfo != null) result2 = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, new UserIdentifier(result.UserInfo.DisplayableId, UserIdentifierType.OptionalDisplayableId), SecondCallExtraQueryParameter); else result2 = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri); VerifySuccessResult(sts, result2); return new List<AuthenticationResultProxy> { result, result2 }; }
public static async Task MixedCaseUserNameTestAsync(Sts sts) { var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority); UserCredentialProxy credential = new UserCredentialProxy(sts.ValidUserName3, sts.ValidPassword3); AuthenticationResultProxy result = await context.AcquireTokenAsync(sts.ValidResource, sts.ValidClientId, credential); VerifySuccessResult(sts, result); Verify.IsNotNull(result.UserInfo); Verify.AreNotEqual(result.UserInfo.DisplayableId, result.UserInfo.DisplayableId.ToLower()); AuthenticationContextProxy.Delay(2000); // 2 seconds delay AuthenticationResultProxy result2 = await context.AcquireTokenAsync(sts.ValidResource, sts.ValidClientId, credential); VerifySuccessResult(sts, result2); Verify.IsTrue(AreDateTimeOffsetsEqual(result.ExpiresOn, result2.ExpiresOn)); }
internal static void MultiUserCacheTest(Sts sts) { Log.Comment("Acquire token for user1 interactively"); AuthenticationContextProxy.SetCredentials(null, sts.ValidPassword); var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority); AuthenticationResultProxy result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); VerifySuccessResultAndTokenContent(sts, result); Verify.AreEqual(sts.ValidUserName, result.UserInfo.DisplayableId); Log.Comment("Acquire token for user1 returning cached token"); AuthenticationContextProxy.SetCredentials(null, null); result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); VerifySuccessResultAndTokenContent(sts, result); Verify.AreEqual(sts.ValidUserName, result.UserInfo.DisplayableId); Log.Comment("Clear cookie and acquire token for user2 interactively"); EndBrowserDialogSession(); AuthenticationContextProxy.SetCredentials(null, sts.ValidPassword2); AuthenticationResultProxy result2 = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidRequiredUserId2); VerifySuccessResultAndTokenContent(sts, result2); Verify.AreEqual(sts.ValidUserName2, result2.UserInfo.DisplayableId); Log.Comment("Acquire token for user1 returning cached token"); AuthenticationContextProxy.SetCredentials(null, null); result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); VerifySuccessResultAndTokenContent(sts, result); Verify.AreEqual(sts.ValidUserName, result.UserInfo.DisplayableId); Log.Comment("Acquire token for user2 returning cached token"); AuthenticationContextProxy.SetCredentials(null, null); result2 = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidRequiredUserId2); VerifySuccessResultAndTokenContent(sts, result2); Verify.AreEqual(sts.ValidUserName2, result2.UserInfo.DisplayableId); Log.Comment("Acquire token for user1 and resource2 using cached multi resource refresh token"); AuthenticationContextProxy.SetCredentials(null, null); result = context.AcquireToken(sts.ValidResource2, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); VerifySuccessResultAndTokenContent(sts, result); Verify.AreEqual(sts.ValidUserName, result.UserInfo.DisplayableId); Log.Comment("Acquire token for user2 and resource2 using cached multi resource refresh token"); AuthenticationContextProxy.SetCredentials(null, null); result2 = context.AcquireToken(sts.ValidResource2, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidRequiredUserId2); VerifySuccessResultAndTokenContent(sts, result2); Verify.AreEqual(sts.ValidUserName2, result2.UserInfo.DisplayableId); }
public static void ForcePromptTest(Sts sts) { SetCredential(sts); var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority); AuthenticationResultProxy result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); VerifySuccessResult(sts, result); AuthenticationContextProxy.SetCredentials(null, null); AuthenticationResultProxy result2 = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, (sts.Type == StsType.ADFS) ? null : sts.ValidUserId); VerifySuccessResult(sts, result2); Verify.AreEqual(result2.AccessToken, result.AccessToken); AuthenticationContextProxy.SetCredentials(sts.ValidUserName, sts.ValidPassword); result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Always); VerifySuccessResult(sts, result); Verify.AreNotEqual(result2.AccessToken, result.AccessToken); }
public static void AcquireTokenAndRefreshSessionTest(Sts sts) { var userId = sts.ValidUserId; AuthenticationContextProxy.SetCredentials(userId.Id, sts.ValidPassword); var context = new AuthenticationContextProxy(sts.Authority, false, TokenCacheType.InMemory); AuthenticationResultProxy result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, userId); VerifySuccessResult(sts, result); AuthenticationContextProxy.Delay(2000); AuthenticationResultProxy result2 = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.RefreshSession, userId); VerifySuccessResult(sts, result2); Verify.AreNotEqual(result.AccessToken, result2.AccessToken); }
public static async Task AcquireTokenNonInteractivePositiveTestAsync(Sts sts) { var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority); UserCredentialProxy credential = new UserCredentialProxy(sts.ValidUserName, sts.ValidPassword); AuthenticationResultProxy result = await context.AcquireTokenAsync(sts.ValidResource, sts.ValidClientId, credential); VerifySuccessResult(sts, result); Verify.IsNotNull(result.UserInfo); Verify.IsNotNullOrEmptyString(result.UserInfo.UniqueId); Verify.IsNotNullOrEmptyString(result.UserInfo.DisplayableId); AuthenticationContextProxy.Delay(2000); // Test token cache AuthenticationResultProxy result2 = await context.AcquireTokenAsync(sts.ValidResource, sts.ValidClientId, credential); VerifySuccessResult(sts, result2); VerifyExpiresOnAreEqual(result, result2); }
public static void AcquireTokenPositiveWithFederatedTenantTest(Sts sts) { var userId = sts.ValidUserId; AuthenticationContextProxy.SetCredentials(userId.Id, sts.ValidPassword); var context = new AuthenticationContextProxy(sts.Authority, false, TokenCacheType.Null); AuthenticationResultProxy result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, userId); VerifySuccessResult(sts, result); result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, UserIdentifier.AnyUser); VerifySuccessResult(sts, result); }
private static List<AuthenticationResultProxy> AcquireTokenPositiveWithCacheExpectingEqualResults(Sts sts, AuthenticationContextProxy context) { List<AuthenticationResultProxy> results = AcquireTokenPositiveWithCache(sts, context); Verify.AreEqual(results[0].AccessToken, results[1].AccessToken, "AuthenticationResult.AccessToken"); Log.Comment(string.Format("First ExpiresOn: {0}", results[0].ExpiresOn)); Log.Comment(string.Format("Second ExpiresOn: {0}", results[1].ExpiresOn)); return results; }
private static void SetCredential(Sts sts) { AuthenticationContextProxy.SetCredentials(sts.Type == StsType.ADFS ? sts.ValidUserName : null, sts.ValidPassword); }
public static void AuthenticationContextAuthorityValidationTest(Sts sts) { SetCredential(sts); AuthenticationContextProxy context = null; AuthenticationResultProxy result = null; try { context = new AuthenticationContextProxy(sts.InvalidAuthority, true); Verify.AreNotEqual(sts.Type, StsType.ADFS); result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); VerifyErrorResult(result, Sts.AuthorityNotInValidList, "authority"); } catch (ArgumentException ex) { Verify.AreEqual(sts.Type, StsType.ADFS); Verify.AreEqual(ex.ParamName, "validateAuthority"); } #if TEST_ADAL_WINPHONE_UNIT catch (AdalServiceException ex) { Verify.AreNotEqual(sts.Type, StsType.ADFS); Verify.AreEqual(ex.ErrorCode, Sts.AuthorityNotInValidList); Verify.IsTrue(ex.Message.Contains("authority")); } #endif context = new AuthenticationContextProxy(sts.InvalidAuthority, false); result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); VerifyErrorResult(result, Sts.AuthenticationUiFailedError, "authentication dialog"); context = new AuthenticationContextProxy(sts.Authority, false); result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); VerifySuccessResult(sts, result); if (sts.Type != StsType.ADFS) { context = new AuthenticationContextProxy(sts.Authority, true); result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); VerifySuccessResult(sts, result); } try { context = new AuthenticationContextProxy(sts.InvalidAuthority); Verify.AreNotEqual(sts.Type, StsType.ADFS); result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); VerifyErrorResult(result, Sts.AuthorityNotInValidList, "authority"); } catch (ArgumentException ex) { Verify.AreEqual(sts.Type, StsType.ADFS); Verify.AreEqual(ex.ParamName, "validateAuthority"); } #if TEST_ADAL_WINPHONE_UNIT catch (AdalServiceException ex) { Verify.AreNotEqual(sts.Type, StsType.ADFS); Verify.AreEqual(ex.ErrorCode, Sts.AuthorityNotInValidList); Verify.IsTrue(ex.Message.Contains("authority")); } #endif context = new AuthenticationContextProxy(sts.Authority + "/extraPath1/extraPath2", sts.ValidateAuthority); result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); VerifySuccessResult(sts, result); }
public static async Task WebExceptionAccessTestAsync(Sts sts) { SetCredential(sts); var context = new AuthenticationContextProxy(sts.Authority, sts.ValidateAuthority); AuthenticationResultProxy result = context.AcquireToken(sts.ValidResource, sts.ValidClientId, sts.ValidDefaultRedirectUri, PromptBehaviorProxy.Auto, sts.ValidUserId); VerifySuccessResult(sts, result); result = await context.AcquireTokenByRefreshTokenAsync(result.RefreshToken, sts.InvalidClientId); VerifyErrorResult(result, "unauthorized_client", "AADSTS70001"); Verify.IsNotNull(result.Exception); Verify.IsNotNull(result.Exception.InnerException); Verify.IsTrue(result.Exception.InnerException is WebException); using (StreamReader sr = new StreamReader(((WebException)(result.Exception.InnerException)).Response.GetResponseStream())) { string streamBody = sr.ReadToEnd(); Verify.IsTrue(streamBody.Contains("AADSTS70001")); } }