diff options
Diffstat (limited to 'src/DotNetOpenAuth.Test/OAuth2')
4 files changed, 128 insertions, 182 deletions
diff --git a/src/DotNetOpenAuth.Test/OAuth2/AuthorizationServerTests.cs b/src/DotNetOpenAuth.Test/OAuth2/AuthorizationServerTests.cs index e8f7172..b280e76 100644 --- a/src/DotNetOpenAuth.Test/OAuth2/AuthorizationServerTests.cs +++ b/src/DotNetOpenAuth.Test/OAuth2/AuthorizationServerTests.cs @@ -8,7 +8,9 @@ namespace DotNetOpenAuth.Test.OAuth2 { using System; using System.Collections.Generic; using System.Linq; + using System.Net.Http; using System.Text; + using System.Threading; using System.Threading.Tasks; using DotNetOpenAuth.OAuth2; using DotNetOpenAuth.OAuth2.ChannelElements; @@ -25,38 +27,42 @@ namespace DotNetOpenAuth.Test.OAuth2 { /// Verifies that authorization server responds with an appropriate error response. /// </summary> [Test] - public void ErrorResponseTest() { - var coordinator = new OAuth2Coordinator<UserAgentClient>( - AuthorizationServerDescription, - AuthorizationServerMock, - new UserAgentClient(AuthorizationServerDescription), - client => { + public async Task ErrorResponseTest() { + var coordinator = new CoordinatorBase( + async (hostFactories, ct) => { var request = new AccessTokenAuthorizationCodeRequestC(AuthorizationServerDescription) { ClientIdentifier = ClientId, ClientSecret = ClientSecret, AuthorizationCode = "foo" }; - - var response = client.Channel.Request<AccessTokenFailedResponse>(request); + var client = new UserAgentClient(AuthorizationServerDescription, hostFactories: hostFactories); + var response = await client.Channel.RequestAsync<AccessTokenFailedResponse>(request, CancellationToken.None); Assert.That(response.Error, Is.Not.Null.And.Not.Empty); Assert.That(response.Error, Is.EqualTo(Protocol.AccessTokenRequestErrorCodes.InvalidRequest)); }, - server => { - server.HandleTokenRequest().Respond(); - }); - coordinator.Run(); + CoordinatorBase.Handle(AuthorizationServerDescription.TokenEndpoint).By(async (req, ct) => { + var server = new AuthorizationServer(AuthorizationServerMock); + return await server.HandleTokenRequestAsync(req, ct); + })); + await coordinator.RunAsync(); } [Test] - public void DecodeRefreshToken() { + public async Task DecodeRefreshToken() { var refreshTokenSource = new TaskCompletionSource<string>(); - var coordinator = new OAuth2Coordinator<WebServerClient>( - AuthorizationServerDescription, - AuthorizationServerMock, - new WebServerClient(AuthorizationServerDescription), - client => { + var coordinator = new CoordinatorBase( + async (hostFactories, ct) => { + var client = new WebServerClient(AuthorizationServerDescription); try { - var authState = new AuthorizationState(TestScopes) { - Callback = ClientCallback, - }; - client.PrepareRequestUserAuthorization(authState).Respond(); - var result = client.ProcessUserAuthorization(); + var authState = new AuthorizationState(TestScopes) { Callback = ClientCallback, }; + var authRedirectResponse = await client.PrepareRequestUserAuthorizationAsync(authState, ct); + Uri authCompleteUri; + using (var httpClient = hostFactories.CreateHttpClient()) { + using (var response = await httpClient.GetAsync(authRedirectResponse.Headers.Location)) { + response.EnsureSuccessStatusCode(); + authCompleteUri = response.Headers.Location; + } + } + + var authCompleteRequest = new HttpRequestMessage(HttpMethod.Get, authCompleteUri); + authCompleteRequest.Headers.Add("Cookie", string.Join("; ", authRedirectResponse.Headers.GetValues("Set-Cookie"))); + var result = await client.ProcessUserAuthorizationAsync(authCompleteRequest, ct); Assert.That(result.AccessToken, Is.Not.Null.And.Not.Empty); Assert.That(result.RefreshToken, Is.Not.Null.And.Not.Empty); refreshTokenSource.SetResult(result.RefreshToken); @@ -64,20 +70,28 @@ namespace DotNetOpenAuth.Test.OAuth2 { refreshTokenSource.TrySetCanceled(); } }, - server => { - var request = server.ReadAuthorizationRequest(); - Assert.That(request, Is.Not.Null); - server.ApproveAuthorizationRequest(request, ResourceOwnerUsername); - server.HandleTokenRequest().Respond(); - var authorization = server.DecodeRefreshToken(refreshTokenSource.Task.Result); - Assert.That(authorization, Is.Not.Null); - Assert.That(authorization.User, Is.EqualTo(ResourceOwnerUsername)); - }); - coordinator.Run(); + CoordinatorBase.Handle(AuthorizationServerDescription.AuthorizationEndpoint).By( + async (req, ct) => { + var server = new AuthorizationServer(AuthorizationServerMock); + var request = await server.ReadAuthorizationRequestAsync(req, ct); + Assert.That(request, Is.Not.Null); + var response = server.PrepareApproveAuthorizationRequest(request, ResourceOwnerUsername); + return await server.Channel.PrepareResponseAsync(response); + }), + CoordinatorBase.Handle(AuthorizationServerDescription.TokenEndpoint).By( + async (req, ct) => { + var server = new AuthorizationServer(AuthorizationServerMock); + var response = await server.HandleTokenRequestAsync(req, ct); + var authorization = server.DecodeRefreshToken(refreshTokenSource.Task.Result); + Assert.That(authorization, Is.Not.Null); + Assert.That(authorization.User, Is.EqualTo(ResourceOwnerUsername)); + return response; + })); + await coordinator.RunAsync(); } [Test] - public void ResourceOwnerScopeOverride() { + public async Task ResourceOwnerScopeOverride() { var clientRequestedScopes = new[] { "scope1", "scope2" }; var serverOverriddenScopes = new[] { "scope1", "differentScope" }; var authServerMock = CreateAuthorizationServerMock(); @@ -89,25 +103,26 @@ namespace DotNetOpenAuth.Test.OAuth2 { response.ApprovedScope.UnionWith(serverOverriddenScopes); return response; }); - var coordinator = new OAuth2Coordinator<WebServerClient>( - AuthorizationServerDescription, - authServerMock.Object, - new WebServerClient(AuthorizationServerDescription), - client => { - var authState = new AuthorizationState(TestScopes) { - Callback = ClientCallback, - }; - var result = client.ExchangeUserCredentialForToken(ResourceOwnerUsername, ResourceOwnerPassword, clientRequestedScopes); - Assert.That(result.Scope, Is.EquivalentTo(serverOverriddenScopes)); - }, - server => { - server.HandleTokenRequest().Respond(); - }); - coordinator.Run(); + + // AuthorizationServerDescription, + //authServerMock.Object, + //new WebServerClient(AuthorizationServerDescription), + var coordinator = new CoordinatorBase( + async (hostFactories, ct) => { + var client = new WebServerClient(AuthorizationServerDescription, hostFactories: hostFactories); + var result = await client.ExchangeUserCredentialForTokenAsync(ResourceOwnerUsername, ResourceOwnerPassword, clientRequestedScopes, ct); + Assert.That(result.Scope, Is.EquivalentTo(serverOverriddenScopes)); + }, + CoordinatorBase.Handle(AuthorizationServerDescription.TokenEndpoint).By( + async (req, ct) => { + var server = new AuthorizationServer(authServerMock.Object); + return await server.HandleTokenRequestAsync(req, ct); + })); + await coordinator.RunAsync(); } [Test] - public void CreateAccessTokenSeesAuthorizingUserResourceOwnerGrant() { + public async Task CreateAccessTokenSeesAuthorizingUserResourceOwnerGrant() { var authServerMock = CreateAuthorizationServerMock(); authServerMock .Setup(a => a.CheckAuthorizeResourceOwnerCredentialGrant(ResourceOwnerUsername, ResourceOwnerPassword, It.IsAny<IAccessTokenRequest>())) @@ -116,25 +131,22 @@ namespace DotNetOpenAuth.Test.OAuth2 { Assert.That(req.UserName, Is.EqualTo(ResourceOwnerUsername)); return response; }); - var coordinator = new OAuth2Coordinator<WebServerClient>( - AuthorizationServerDescription, - authServerMock.Object, - new WebServerClient(AuthorizationServerDescription), - client => { - var authState = new AuthorizationState(TestScopes) { - Callback = ClientCallback, - }; - var result = client.ExchangeUserCredentialForToken(ResourceOwnerUsername, ResourceOwnerPassword, TestScopes); + var coordinator = new CoordinatorBase( + async (hostFactories, ct) => { + var client = new WebServerClient(AuthorizationServerDescription, hostFactories: hostFactories); + var result = await client.ExchangeUserCredentialForTokenAsync(ResourceOwnerUsername, ResourceOwnerPassword, TestScopes, ct); Assert.That(result.AccessToken, Is.Not.Null); }, - server => { - server.HandleTokenRequest().Respond(); - }); - coordinator.Run(); + CoordinatorBase.Handle(AuthorizationServerDescription.TokenEndpoint).By( + async (req, ct) => { + var server = new AuthorizationServer(authServerMock.Object); + return await server.HandleTokenRequestAsync(req, ct); + })); + await coordinator.RunAsync(); } [Test] - public void CreateAccessTokenSeesAuthorizingUserClientCredentialGrant() { + public async Task CreateAccessTokenSeesAuthorizingUserClientCredentialGrant() { var authServerMock = CreateAuthorizationServerMock(); authServerMock .Setup(a => a.CheckAuthorizeClientCredentialsGrant(It.IsAny<IAccessTokenRequest>())) @@ -142,25 +154,22 @@ namespace DotNetOpenAuth.Test.OAuth2 { Assert.That(req.UserName, Is.Null); return new AutomatedAuthorizationCheckResponse(req, true); }); - var coordinator = new OAuth2Coordinator<WebServerClient>( - AuthorizationServerDescription, - authServerMock.Object, - new WebServerClient(AuthorizationServerDescription), - client => { - var authState = new AuthorizationState(TestScopes) { - Callback = ClientCallback, - }; - var result = client.GetClientAccessToken(TestScopes); + var coordinator = new CoordinatorBase( + async (hostFactories, ct) => { + var client = new WebServerClient(AuthorizationServerDescription, hostFactories: hostFactories); + var result = await client.GetClientAccessTokenAsync(TestScopes, ct); Assert.That(result.AccessToken, Is.Not.Null); }, - server => { - server.HandleTokenRequest().Respond(); - }); - coordinator.Run(); + CoordinatorBase.Handle(AuthorizationServerDescription.TokenEndpoint).By( + async (req, ct) => { + var server = new AuthorizationServer(authServerMock.Object); + return await server.HandleTokenRequestAsync(req, ct); + })); + await coordinator.RunAsync(); } [Test] - public void CreateAccessTokenSeesAuthorizingUserAuthorizationCodeGrant() { + public async Task CreateAccessTokenSeesAuthorizingUserAuthorizationCodeGrant() { var authServerMock = CreateAuthorizationServerMock(); authServerMock .Setup(a => a.IsAuthorizationValid(It.IsAny<IAuthorizationDescription>())) @@ -168,30 +177,44 @@ namespace DotNetOpenAuth.Test.OAuth2 { Assert.That(req.User, Is.EqualTo(ResourceOwnerUsername)); return true; }); - var coordinator = new OAuth2Coordinator<WebServerClient>( - AuthorizationServerDescription, - authServerMock.Object, - new WebServerClient(AuthorizationServerDescription), - client => { + var coordinator = new CoordinatorBase( + async (hostFactories, ct) => { + var client = new WebServerClient(AuthorizationServerDescription, hostFactories: hostFactories); var authState = new AuthorizationState(TestScopes) { Callback = ClientCallback, }; - client.PrepareRequestUserAuthorization(authState).Respond(); - var result = client.ProcessUserAuthorization(); + var authRedirectResponse = await client.PrepareRequestUserAuthorizationAsync(authState, ct); + Uri authCompleteUri; + using (var httpClient = hostFactories.CreateHttpClient()) { + using (var response = await httpClient.GetAsync(authRedirectResponse.Headers.Location)) { + response.EnsureSuccessStatusCode(); + authCompleteUri = response.Headers.Location; + } + } + + var authCompleteRequest = new HttpRequestMessage(HttpMethod.Get, authCompleteUri); + var result = await client.ProcessUserAuthorizationAsync(authCompleteRequest, ct); Assert.That(result.AccessToken, Is.Not.Null.And.Not.Empty); Assert.That(result.RefreshToken, Is.Not.Null.And.Not.Empty); }, - server => { - var request = server.ReadAuthorizationRequest(); - Assert.That(request, Is.Not.Null); - server.ApproveAuthorizationRequest(request, ResourceOwnerUsername); - server.HandleTokenRequest().Respond(); - }); - coordinator.Run(); + CoordinatorBase.Handle(AuthorizationServerDescription.TokenEndpoint).By( + async (req, ct) => { + var server = new AuthorizationServer(authServerMock.Object); + var request = await server.ReadAuthorizationRequestAsync(req, ct); + Assert.That(request, Is.Not.Null); + var response = server.PrepareApproveAuthorizationRequest(request, ResourceOwnerUsername); + return await server.Channel.PrepareResponseAsync(response); + }), + CoordinatorBase.Handle(AuthorizationServerDescription.TokenEndpoint).By( + async (req, ct) => { + var server = new AuthorizationServer(authServerMock.Object); + return await server.HandleTokenRequestAsync(req, ct); + })); + await coordinator.RunAsync(); } [Test] - public void ClientCredentialScopeOverride() { + public async Task ClientCredentialScopeOverride() { var clientRequestedScopes = new[] { "scope1", "scope2" }; var serverOverriddenScopes = new[] { "scope1", "differentScope" }; var authServerMock = CreateAuthorizationServerMock(); @@ -203,21 +226,21 @@ namespace DotNetOpenAuth.Test.OAuth2 { response.ApprovedScope.UnionWith(serverOverriddenScopes); return response; }); - var coordinator = new OAuth2Coordinator<WebServerClient>( - AuthorizationServerDescription, - authServerMock.Object, - new WebServerClient(AuthorizationServerDescription), - client => { + var coordinator = new CoordinatorBase( + async (hostFactories, ct) => { + var client = new WebServerClient(AuthorizationServerDescription, hostFactories: hostFactories); var authState = new AuthorizationState(TestScopes) { Callback = ClientCallback, }; - var result = client.GetClientAccessToken(clientRequestedScopes); + var result = await client.GetClientAccessTokenAsync(clientRequestedScopes, ct); Assert.That(result.Scope, Is.EquivalentTo(serverOverriddenScopes)); }, - server => { - server.HandleTokenRequest().Respond(); - }); - coordinator.Run(); + CoordinatorBase.Handle(AuthorizationServerDescription.TokenEndpoint).By( + async (req, ct) => { + var server = new AuthorizationServer(authServerMock.Object); + return await server.HandleTokenRequestAsync(req, ct); + })); + await coordinator.RunAsync(); } } } diff --git a/src/DotNetOpenAuth.Test/OAuth2/MessageFactoryTests.cs b/src/DotNetOpenAuth.Test/OAuth2/MessageFactoryTests.cs index 52b5371..810d830 100644 --- a/src/DotNetOpenAuth.Test/OAuth2/MessageFactoryTests.cs +++ b/src/DotNetOpenAuth.Test/OAuth2/MessageFactoryTests.cs @@ -31,7 +31,7 @@ namespace DotNetOpenAuth.Test.OAuth2 { var authServerChannel = new OAuth2AuthorizationServerChannel(new Mock<IAuthorizationServerHost>().Object, new Mock<ClientAuthenticationModule>().Object); this.authServerMessageFactory = authServerChannel.MessageFactoryTestHook; - var clientChannel = new OAuth2ClientChannel(); + var clientChannel = new OAuth2ClientChannel(null); this.clientMessageFactory = clientChannel.MessageFactoryTestHook; } diff --git a/src/DotNetOpenAuth.Test/OAuth2/OAuth2Coordinator.cs b/src/DotNetOpenAuth.Test/OAuth2/OAuth2Coordinator.cs deleted file mode 100644 index eeda125..0000000 --- a/src/DotNetOpenAuth.Test/OAuth2/OAuth2Coordinator.cs +++ /dev/null @@ -1,74 +0,0 @@ -//----------------------------------------------------------------------- -// <copyright file="OAuth2Coordinator.cs" company="Outercurve Foundation"> -// Copyright (c) Outercurve Foundation. All rights reserved. -// </copyright> -//----------------------------------------------------------------------- - -namespace DotNetOpenAuth.Test.OAuth2 { - using System; - using System.Collections.Generic; - using System.Linq; - using System.Net; - using System.Text; - using DotNetOpenAuth.OAuth2; - using DotNetOpenAuth.Test.Mocks; - using Validation; - - internal class OAuth2Coordinator<TClient> : CoordinatorBase<TClient, AuthorizationServer> - where TClient : ClientBase { - private readonly AuthorizationServerDescription serverDescription; - private readonly IAuthorizationServerHost authServerHost; - private readonly TClient client; - - internal OAuth2Coordinator( - AuthorizationServerDescription serverDescription, - IAuthorizationServerHost authServerHost, - TClient client, - Action<TClient> clientAction, - Action<AuthorizationServer> authServerAction) - : base(clientAction, authServerAction) { - Requires.NotNull(serverDescription, "serverDescription"); - Requires.NotNull(authServerHost, "authServerHost"); - Requires.NotNull(client, "client"); - - this.serverDescription = serverDescription; - this.authServerHost = authServerHost; - this.client = client; - - this.client.ClientIdentifier = OAuth2TestBase.ClientId; - this.client.ClientCredentialApplicator = ClientCredentialApplicator.PostParameter(OAuth2TestBase.ClientSecret); - } - - internal override void Run() { - var authServer = new AuthorizationServer(this.authServerHost); - - var rpCoordinatingChannel = new CoordinatingOAuth2ClientChannel(this.client.Channel, this.IncomingMessageFilter, this.OutgoingMessageFilter); - var opCoordinatingChannel = new CoordinatingOAuth2AuthServerChannel(authServer.Channel, this.IncomingMessageFilter, this.OutgoingMessageFilter); - rpCoordinatingChannel.RemoteChannel = opCoordinatingChannel; - opCoordinatingChannel.RemoteChannel = rpCoordinatingChannel; - - this.client.Channel = rpCoordinatingChannel; - authServer.Channel = opCoordinatingChannel; - - this.RunCore(this.client, authServer); - } - - private static Action<WebServerClient> WrapAction(Action<WebServerClient> action) { - Requires.NotNull(action, "action"); - - return client => { - action(client); - ((CoordinatingChannel)client.Channel).Close(); - }; - } - - private static Action<AuthorizationServer> WrapAction(Action<AuthorizationServer> action) { - Requires.NotNull(action, "action"); - - return authServer => { - action(authServer); - ((CoordinatingChannel)authServer.Channel).Close(); - }; - } - } -} diff --git a/src/DotNetOpenAuth.Test/OAuth2/OAuth2TestBase.cs b/src/DotNetOpenAuth.Test/OAuth2/OAuth2TestBase.cs index 395b18c..f01b5b7 100644 --- a/src/DotNetOpenAuth.Test/OAuth2/OAuth2TestBase.cs +++ b/src/DotNetOpenAuth.Test/OAuth2/OAuth2TestBase.cs @@ -37,10 +37,7 @@ namespace DotNetOpenAuth.Test.OAuth2 { TokenEndpoint = new Uri("https://authserver/token"), }; - protected static readonly IClientDescription ClientDescription = new ClientDescription( - ClientSecret, - ClientCallback, - ClientType.Confidential); + protected static readonly IClientDescription ClientDescription = new ClientDescription(ClientSecret, ClientCallback); protected static readonly IAuthorizationServerHost AuthorizationServerMock = CreateAuthorizationServerMock().Object; |