diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2012-02-21 10:21:10 -0800 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2012-02-21 10:21:10 -0800 |
commit | 4fc95a91caf2caa216a5080f151e24ad1d6fcba4 (patch) | |
tree | 2024ba45599cb8ef9b92d8e652d6dae4f7057e92 /src | |
parent | d7c4c22f7a35d884c0f0c1f8fb0b517b7f68f54b (diff) | |
download | DotNetOpenAuth-4fc95a91caf2caa216a5080f151e24ad1d6fcba4.zip DotNetOpenAuth-4fc95a91caf2caa216a5080f151e24ad1d6fcba4.tar.gz DotNetOpenAuth-4fc95a91caf2caa216a5080f151e24ad1d6fcba4.tar.bz2 |
Added two more OAuth2 unit tests, for the UserAgentClient class.
Diffstat (limited to 'src')
-rw-r--r-- | src/DotNetOpenAuth.OAuth2.Client/OAuth2/UserAgentClient.cs | 54 | ||||
-rw-r--r-- | src/DotNetOpenAuth.Test/DotNetOpenAuth.Test.csproj | 3 | ||||
-rw-r--r-- | src/DotNetOpenAuth.Test/OAuth2/OAuth2Coordinator.cs | 27 | ||||
-rw-r--r-- | src/DotNetOpenAuth.Test/OAuth2/UserAgentClientAuthorizeTests.cs | 74 | ||||
-rw-r--r-- | src/DotNetOpenAuth.Test/OAuth2/WebServerClientAuthorizeTests.cs (renamed from src/DotNetOpenAuth.Test/OAuth2/AuthorizeTests.cs) | 14 |
5 files changed, 142 insertions, 30 deletions
diff --git a/src/DotNetOpenAuth.OAuth2.Client/OAuth2/UserAgentClient.cs b/src/DotNetOpenAuth.OAuth2.Client/OAuth2/UserAgentClient.cs index ded0e97..d2ede6f 100644 --- a/src/DotNetOpenAuth.OAuth2.Client/OAuth2/UserAgentClient.cs +++ b/src/DotNetOpenAuth.OAuth2.Client/OAuth2/UserAgentClient.cs @@ -72,17 +72,7 @@ namespace DotNetOpenAuth.OAuth2 { Requires.NotNull(authorization, "authorization"); Requires.ValidState(!string.IsNullOrEmpty(this.ClientIdentifier)); - if (authorization.Callback == null) { - authorization.Callback = new Uri("http://localhost/"); - } - - var request = new EndUserAuthorizationRequest(this.AuthorizationServer) { - ClientIdentifier = this.ClientIdentifier, - Callback = authorization.Callback, - ClientState = state, - }; - request.Scope.ResetContents(authorization.Scope); - + var request = this.PrepareRequestUserAuthorization(authorization, state); return this.Channel.PrepareResponse(request).GetDirectUriRequest(this.Channel); } @@ -105,6 +95,21 @@ namespace DotNetOpenAuth.OAuth2 { return null; } + return this.ProcessUserAuthorization(authorizationState, response); + } + + /// <summary> + /// Scans the incoming request for an authorization response message. + /// </summary> + /// <param name="authorizationState">The authorization.</param> + /// <param name="response">The incoming authorization response message.</param> + /// <returns> + /// The granted authorization, or <c>null</c> if the incoming HTTP request did not contain an authorization server response or authorization was rejected. + /// </returns> + internal IAuthorizationState ProcessUserAuthorization(IAuthorizationState authorizationState, IDirectedProtocolMessage response) { + Requires.NotNull(authorizationState, "authorizationState"); + Requires.NotNull(response, "response"); + EndUserAuthorizationSuccessAccessTokenResponse accessTokenSuccess; EndUserAuthorizationSuccessAuthCodeResponse authCodeSuccess; EndUserAuthorizationFailedResponse failure; @@ -119,5 +124,32 @@ namespace DotNetOpenAuth.OAuth2 { return authorizationState; } + + /// <summary> + /// Generates a URL that the user's browser can be directed to in order to authorize + /// this client to access protected data at some resource server. + /// </summary> + /// <param name="authorization">The authorization state that is tracking this particular request. Optional.</param> + /// <param name="state">The client state that should be returned with the authorization response.</param> + /// <returns> + /// A message to send to the authorization server. + /// </returns> + internal EndUserAuthorizationRequest PrepareRequestUserAuthorization(IAuthorizationState authorization, string state = null) { + Requires.NotNull(authorization, "authorization"); + Requires.ValidState(!string.IsNullOrEmpty(this.ClientIdentifier)); + + if (authorization.Callback == null) { + authorization.Callback = new Uri("http://localhost/"); + } + + var request = new EndUserAuthorizationRequest(this.AuthorizationServer) { + ClientIdentifier = this.ClientIdentifier, + Callback = authorization.Callback, + ClientState = state, + }; + request.Scope.ResetContents(authorization.Scope); + + return request; + } } } diff --git a/src/DotNetOpenAuth.Test/DotNetOpenAuth.Test.csproj b/src/DotNetOpenAuth.Test/DotNetOpenAuth.Test.csproj index 8fc4183..af0e09a 100644 --- a/src/DotNetOpenAuth.Test/DotNetOpenAuth.Test.csproj +++ b/src/DotNetOpenAuth.Test/DotNetOpenAuth.Test.csproj @@ -243,9 +243,10 @@ <Compile Include="Mocks\TestMessage.cs" /> <Compile Include="Mocks\TestMessageFactory.cs" /> <Compile Include="OAuth2\MessageFactoryTests.cs" /> - <Compile Include="OAuth2\AuthorizeTests.cs" /> + <Compile Include="OAuth2\UserAgentClientAuthorizeTests.cs" /> <Compile Include="OAuth2\OAuth2Coordinator.cs" /> <Compile Include="OAuth2\OAuth2TestBase.cs" /> + <Compile Include="OAuth2\WebServerClientAuthorizeTests.cs" /> <Compile Include="OAuth\ChannelElements\HmacSha1SigningBindingElementTests.cs" /> <Compile Include="OAuth\ChannelElements\OAuthChannelTests.cs" /> <Compile Include="OAuth\ChannelElements\PlaintextSigningBindingElementTest.cs" /> diff --git a/src/DotNetOpenAuth.Test/OAuth2/OAuth2Coordinator.cs b/src/DotNetOpenAuth.Test/OAuth2/OAuth2Coordinator.cs index 5af7fe8..993cad5 100644 --- a/src/DotNetOpenAuth.Test/OAuth2/OAuth2Coordinator.cs +++ b/src/DotNetOpenAuth.Test/OAuth2/OAuth2Coordinator.cs @@ -12,34 +12,43 @@ namespace DotNetOpenAuth.Test.OAuth2 { using DotNetOpenAuth.OAuth2; using DotNetOpenAuth.Test.Mocks; - internal class OAuth2Coordinator : CoordinatorBase<WebServerClient, AuthorizationServer> { + internal class OAuth2Coordinator<TClient> : CoordinatorBase<TClient, AuthorizationServer> + where TClient : ClientBase { private readonly AuthorizationServerDescription serverDescription; private readonly IAuthorizationServer authServerHost; + private readonly TClient client; - internal OAuth2Coordinator(AuthorizationServerDescription serverDescription, IAuthorizationServer authServerHost, Action<WebServerClient> clientAction, Action<AuthorizationServer> authServerAction) + internal OAuth2Coordinator( + AuthorizationServerDescription serverDescription, + IAuthorizationServer 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.ClientSecret = OAuth2TestBase.ClientSecret; } internal override void Run() { - var client = new WebServerClient(this.serverDescription) { - ClientIdentifier = OAuth2TestBase.ClientId, - ClientSecret = OAuth2TestBase.ClientSecret, - }; var authServer = new AuthorizationServer(this.authServerHost); - var rpCoordinatingChannel = new CoordinatingChannel(client.Channel, this.IncomingMessageFilter, this.OutgoingMessageFilter); + var rpCoordinatingChannel = new CoordinatingChannel(this.client.Channel, this.IncomingMessageFilter, this.OutgoingMessageFilter); var opCoordinatingChannel = new CoordinatingOAuth2AuthServerChannel(authServer.Channel, this.IncomingMessageFilter, this.OutgoingMessageFilter); rpCoordinatingChannel.RemoteChannel = opCoordinatingChannel; opCoordinatingChannel.RemoteChannel = rpCoordinatingChannel; - client.Channel = rpCoordinatingChannel; + this.client.Channel = rpCoordinatingChannel; authServer.Channel = opCoordinatingChannel; - this.RunCore(client, authServer); + this.RunCore(this.client, authServer); } private static Action<WebServerClient> WrapAction(Action<WebServerClient> action) { diff --git a/src/DotNetOpenAuth.Test/OAuth2/UserAgentClientAuthorizeTests.cs b/src/DotNetOpenAuth.Test/OAuth2/UserAgentClientAuthorizeTests.cs new file mode 100644 index 0000000..82c8b66 --- /dev/null +++ b/src/DotNetOpenAuth.Test/OAuth2/UserAgentClientAuthorizeTests.cs @@ -0,0 +1,74 @@ +//----------------------------------------------------------------------- +// <copyright file="UserAgentClientAuthorizeTests.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.Text; + using DotNetOpenAuth.Messaging; + using DotNetOpenAuth.Messaging.Bindings; + using DotNetOpenAuth.OAuth2; + using DotNetOpenAuth.OAuth2.ChannelElements; + using DotNetOpenAuth.OAuth2.Messages; + using Moq; + using NUnit.Framework; + + [TestFixture] + public class UserAgentClientAuthorizeTests : OAuth2TestBase { + [TestCase] + public void AuthorizationCodeGrantAuthorization() { + var coordinator = new OAuth2Coordinator<UserAgentClient>( + AuthorizationServerDescription, + AuthorizationServerMock, + new UserAgentClient(AuthorizationServerDescription), + client => { + var authState = new AuthorizationState { + Callback = ClientCallback, + }; + var request = client.PrepareRequestUserAuthorization(authState); + client.Channel.Respond(request); + var incoming = client.Channel.ReadFromRequest(); + var result = client.ProcessUserAuthorization(authState, incoming); + Assert.IsNotNullOrEmpty(result.AccessToken); + Assert.IsNotNullOrEmpty(result.RefreshToken); + }, + server => { + var request = server.ReadAuthorizationRequest(); + server.ApproveAuthorizationRequest(request, Username); + var tokenRequest = server.ReadAccessTokenRequest(); + var tokenResponse = server.PrepareAccessTokenResponse(tokenRequest); + server.Channel.Respond(tokenResponse); + }); + coordinator.Run(); + } + + [TestCase] + public void ImplicitGrantAuthorization() { + var coordinator = new OAuth2Coordinator<UserAgentClient>( + AuthorizationServerDescription, + AuthorizationServerMock, + new UserAgentClient(AuthorizationServerDescription), + client => { + var authState = new AuthorizationState { + Callback = ClientCallback, + }; + var request = client.PrepareRequestUserAuthorization(authState); + request.ResponseType = EndUserAuthorizationResponseType.AccessToken; + client.Channel.Respond(request); + var incoming = client.Channel.ReadFromRequest(); + var result = client.ProcessUserAuthorization(authState, incoming); + Assert.IsNotNullOrEmpty(result.AccessToken); + Assert.IsNull(result.RefreshToken); + }, + server => { + var request = server.ReadAuthorizationRequest(); + server.ApproveAuthorizationRequest(request, Username); + }); + coordinator.Run(); + } + } +} diff --git a/src/DotNetOpenAuth.Test/OAuth2/AuthorizeTests.cs b/src/DotNetOpenAuth.Test/OAuth2/WebServerClientAuthorizeTests.cs index d4e09fa..ec466f8 100644 --- a/src/DotNetOpenAuth.Test/OAuth2/AuthorizeTests.cs +++ b/src/DotNetOpenAuth.Test/OAuth2/WebServerClientAuthorizeTests.cs @@ -1,5 +1,5 @@ //----------------------------------------------------------------------- -// <copyright file="AuthorizeTests.cs" company="Outercurve Foundation"> +// <copyright file="WebServerClientAuthorizeTests.cs" company="Outercurve Foundation"> // Copyright (c) Outercurve Foundation. All rights reserved. // </copyright> //----------------------------------------------------------------------- @@ -9,21 +9,17 @@ namespace DotNetOpenAuth.Test.OAuth2 { using System.Collections.Generic; using System.Linq; using System.Text; - using DotNetOpenAuth.Messaging; - using DotNetOpenAuth.Messaging.Bindings; using DotNetOpenAuth.OAuth2; - using DotNetOpenAuth.OAuth2.ChannelElements; - using DotNetOpenAuth.OAuth2.Messages; - using Moq; using NUnit.Framework; [TestFixture] - public class AuthorizeTests : OAuth2TestBase { + public class WebServerClientAuthorizeTests : OAuth2TestBase { [TestCase] - public void AuthCodeGrantAuthorization() { - var coordinator = new OAuth2Coordinator( + public void AuthorizationCodeGrantAuthorization() { + var coordinator = new OAuth2Coordinator<WebServerClient>( AuthorizationServerDescription, AuthorizationServerMock, + new WebServerClient(AuthorizationServerDescription), client => { var authState = new AuthorizationState { Callback = ClientCallback, |