diff options
Diffstat (limited to 'src/DotNetOpenAuth.Test')
10 files changed, 288 insertions, 23 deletions
diff --git a/src/DotNetOpenAuth.Test/Messaging/MessagingUtilitiesTests.cs b/src/DotNetOpenAuth.Test/Messaging/MessagingUtilitiesTests.cs index 8620b93..cf0f9ca 100644 --- a/src/DotNetOpenAuth.Test/Messaging/MessagingUtilitiesTests.cs +++ b/src/DotNetOpenAuth.Test/Messaging/MessagingUtilitiesTests.cs @@ -220,13 +220,17 @@ namespace DotNetOpenAuth.Test.Messaging { Assert.AreEqual("POST", MessagingUtilities.GetHttpVerb(HttpDeliveryMethods.PostRequest)); Assert.AreEqual("HEAD", MessagingUtilities.GetHttpVerb(HttpDeliveryMethods.HeadRequest)); Assert.AreEqual("DELETE", MessagingUtilities.GetHttpVerb(HttpDeliveryMethods.DeleteRequest)); - Assert.AreEqual("PUT", MessagingUtilities.GetHttpVerb(HttpDeliveryMethods.PutRequest)); + Assert.AreEqual("PUT", MessagingUtilities.GetHttpVerb(HttpDeliveryMethods.PutRequest));
+ Assert.AreEqual("PATCH", MessagingUtilities.GetHttpVerb(HttpDeliveryMethods.PatchRequest));
+ Assert.AreEqual("OPTIONS", MessagingUtilities.GetHttpVerb(HttpDeliveryMethods.OptionsRequest)); Assert.AreEqual("GET", MessagingUtilities.GetHttpVerb(HttpDeliveryMethods.GetRequest | HttpDeliveryMethods.AuthorizationHeaderRequest)); Assert.AreEqual("POST", MessagingUtilities.GetHttpVerb(HttpDeliveryMethods.PostRequest | HttpDeliveryMethods.AuthorizationHeaderRequest)); Assert.AreEqual("HEAD", MessagingUtilities.GetHttpVerb(HttpDeliveryMethods.HeadRequest | HttpDeliveryMethods.AuthorizationHeaderRequest)); Assert.AreEqual("DELETE", MessagingUtilities.GetHttpVerb(HttpDeliveryMethods.DeleteRequest | HttpDeliveryMethods.AuthorizationHeaderRequest)); - Assert.AreEqual("PUT", MessagingUtilities.GetHttpVerb(HttpDeliveryMethods.PutRequest | HttpDeliveryMethods.AuthorizationHeaderRequest)); + Assert.AreEqual("PUT", MessagingUtilities.GetHttpVerb(HttpDeliveryMethods.PutRequest | HttpDeliveryMethods.AuthorizationHeaderRequest));
+ Assert.AreEqual("PATCH", MessagingUtilities.GetHttpVerb(HttpDeliveryMethods.PatchRequest | HttpDeliveryMethods.AuthorizationHeaderRequest));
+ Assert.AreEqual("OPTIONS", MessagingUtilities.GetHttpVerb(HttpDeliveryMethods.OptionsRequest | HttpDeliveryMethods.AuthorizationHeaderRequest)); } /// <summary> @@ -246,7 +250,9 @@ namespace DotNetOpenAuth.Test.Messaging { Assert.AreEqual(HttpDeliveryMethods.PostRequest, MessagingUtilities.GetHttpDeliveryMethod("POST")); Assert.AreEqual(HttpDeliveryMethods.HeadRequest, MessagingUtilities.GetHttpDeliveryMethod("HEAD")); Assert.AreEqual(HttpDeliveryMethods.PutRequest, MessagingUtilities.GetHttpDeliveryMethod("PUT")); - Assert.AreEqual(HttpDeliveryMethods.DeleteRequest, MessagingUtilities.GetHttpDeliveryMethod("DELETE")); + Assert.AreEqual(HttpDeliveryMethods.DeleteRequest, MessagingUtilities.GetHttpDeliveryMethod("DELETE"));
+ Assert.AreEqual(HttpDeliveryMethods.PatchRequest, MessagingUtilities.GetHttpDeliveryMethod("PATCH"));
+ Assert.AreEqual(HttpDeliveryMethods.OptionsRequest, MessagingUtilities.GetHttpDeliveryMethod("OPTIONS")); } /// <summary> diff --git a/src/DotNetOpenAuth.Test/Mocks/CoordinatingChannel.cs b/src/DotNetOpenAuth.Test/Mocks/CoordinatingChannel.cs index 2e09943..50eff97 100644 --- a/src/DotNetOpenAuth.Test/Mocks/CoordinatingChannel.cs +++ b/src/DotNetOpenAuth.Test/Mocks/CoordinatingChannel.cs @@ -88,6 +88,11 @@ namespace DotNetOpenAuth.Test.Mocks { private Action<IProtocolMessage> outgoingMessageFilter; /// <summary> + /// The simulated clients cookies. + /// </summary> + private HttpCookieCollection cookies = new HttpCookieCollection(); + + /// <summary> /// Initializes a new instance of the <see cref="CoordinatingChannel"/> class. /// </summary> /// <param name="wrappedChannel">The wrapped channel. Must not be null.</param> @@ -158,15 +163,23 @@ namespace DotNetOpenAuth.Test.Mocks { this.incomingMessageSignal.Set(); } + internal void SaveCookies(HttpCookieCollection cookies) { + Requires.NotNull(cookies, "cookies"); + foreach (string cookieName in cookies) { + var cookie = cookies[cookieName]; + this.cookies.Set(cookie); + } + } + protected internal override HttpRequestBase GetRequestFromContext() { MessageReceivingEndpoint recipient; WebHeaderCollection headers; var messageData = this.AwaitIncomingMessage(out recipient, out headers); CoordinatingHttpRequestInfo result; if (messageData != null) { - result = new CoordinatingHttpRequestInfo(this, this.MessageFactory, messageData, recipient); + result = new CoordinatingHttpRequestInfo(this, this.MessageFactory, messageData, recipient, this.cookies); } else { - result = new CoordinatingHttpRequestInfo(recipient); + result = new CoordinatingHttpRequestInfo(recipient, this.cookies); } if (headers != null) { @@ -207,7 +220,7 @@ namespace DotNetOpenAuth.Test.Mocks { protected override OutgoingWebResponse PrepareDirectResponse(IProtocolMessage response) { this.ProcessMessageFilter(response, true); - return new CoordinatingOutgoingWebResponse(response, this.RemoteChannel); + return new CoordinatingOutgoingWebResponse(response, this.RemoteChannel, this); } protected override OutgoingWebResponse PrepareIndirectResponse(IDirectedProtocolMessage message) { diff --git a/src/DotNetOpenAuth.Test/Mocks/CoordinatingHttpRequestInfo.cs b/src/DotNetOpenAuth.Test/Mocks/CoordinatingHttpRequestInfo.cs index a1f5cf5..2713765 100644 --- a/src/DotNetOpenAuth.Test/Mocks/CoordinatingHttpRequestInfo.cs +++ b/src/DotNetOpenAuth.Test/Mocks/CoordinatingHttpRequestInfo.cs @@ -6,10 +6,12 @@ namespace DotNetOpenAuth.Test.Mocks { using System; -using System.Collections.Generic; -using System.Diagnostics.Contracts; -using System.Net; -using DotNetOpenAuth.Messaging; + using System.Collections.Generic; + using System.Diagnostics.Contracts; + using System.Net; + using System.Web; + + using DotNetOpenAuth.Messaging; internal class CoordinatingHttpRequestInfo : HttpRequestInfo { private readonly Channel channel; @@ -30,12 +32,14 @@ using DotNetOpenAuth.Messaging; /// <param name="messageFactory">The message factory.</param> /// <param name="messageData">The message data.</param> /// <param name="recipient">The recipient.</param> + /// <param name="cookies">Cookies included in the incoming request.</param> internal CoordinatingHttpRequestInfo( Channel channel, IMessageFactory messageFactory, IDictionary<string, string> messageData, - MessageReceivingEndpoint recipient) - : this(recipient) { + MessageReceivingEndpoint recipient, + HttpCookieCollection cookies) + : this(recipient, cookies) { Contract.Requires(channel != null); Contract.Requires(messageFactory != null); Contract.Requires(messageData != null); @@ -49,8 +53,9 @@ using DotNetOpenAuth.Messaging; /// that will not generate any message. /// </summary> /// <param name="recipient">The recipient.</param> - internal CoordinatingHttpRequestInfo(MessageReceivingEndpoint recipient) - : base(GetHttpVerb(recipient), recipient != null ? recipient.Location : new Uri("http://host/path")) { + /// <param name="cookies">Cookies included in the incoming request.</param> + internal CoordinatingHttpRequestInfo(MessageReceivingEndpoint recipient, HttpCookieCollection cookies) + : base(GetHttpVerb(recipient), recipient != null ? recipient.Location : new Uri("http://host/path"), cookies: cookies) { this.recipient = recipient; } diff --git a/src/DotNetOpenAuth.Test/Mocks/CoordinatingOAuth2ClientChannel.cs b/src/DotNetOpenAuth.Test/Mocks/CoordinatingOAuth2ClientChannel.cs index 52f381d..96091ac 100644 --- a/src/DotNetOpenAuth.Test/Mocks/CoordinatingOAuth2ClientChannel.cs +++ b/src/DotNetOpenAuth.Test/Mocks/CoordinatingOAuth2ClientChannel.cs @@ -29,5 +29,9 @@ namespace DotNetOpenAuth.Test.Mocks { get { return this.wrappedChannel.ClientCredentialApplicator; } set { this.wrappedChannel.ClientCredentialApplicator = value; } } + + public System.Xml.XmlDictionaryReaderQuotas JsonReaderQuotas { + get { return this.XmlDictionaryReaderQuotas; } + } } }
\ No newline at end of file diff --git a/src/DotNetOpenAuth.Test/Mocks/CoordinatingOutgoingWebResponse.cs b/src/DotNetOpenAuth.Test/Mocks/CoordinatingOutgoingWebResponse.cs index 8d2c1e7..90dbd7d 100644 --- a/src/DotNetOpenAuth.Test/Mocks/CoordinatingOutgoingWebResponse.cs +++ b/src/DotNetOpenAuth.Test/Mocks/CoordinatingOutgoingWebResponse.cs @@ -16,16 +16,21 @@ namespace DotNetOpenAuth.Test.Mocks { internal class CoordinatingOutgoingWebResponse : OutgoingWebResponse { private CoordinatingChannel receivingChannel; + private CoordinatingChannel sendingChannel; + /// <summary> /// Initializes a new instance of the <see cref="CoordinatingOutgoingWebResponse"/> class. /// </summary> /// <param name="message">The direct response message to send to the remote channel. This message will be cloned.</param> /// <param name="receivingChannel">The receiving channel.</param> - internal CoordinatingOutgoingWebResponse(IProtocolMessage message, CoordinatingChannel receivingChannel) { + /// <param name="sendingChannel">The sending channel.</param> + internal CoordinatingOutgoingWebResponse(IProtocolMessage message, CoordinatingChannel receivingChannel, CoordinatingChannel sendingChannel) { Requires.NotNull(message, "message"); Requires.NotNull(receivingChannel, "receivingChannel"); + Requires.NotNull(sendingChannel, "sendingChannel"); this.receivingChannel = receivingChannel; + this.sendingChannel = sendingChannel; this.OriginalMessage = message; } @@ -35,6 +40,7 @@ namespace DotNetOpenAuth.Test.Mocks { } public override void Respond() { + this.sendingChannel.SaveCookies(this.Cookies); this.receivingChannel.PostMessage(this.OriginalMessage); } } diff --git a/src/DotNetOpenAuth.Test/OAuth/ChannelElements/HmacSha1SigningBindingElementTests.cs b/src/DotNetOpenAuth.Test/OAuth/ChannelElements/HmacSha1SigningBindingElementTests.cs index 487ce56..49260eb 100644 --- a/src/DotNetOpenAuth.Test/OAuth/ChannelElements/HmacSha1SigningBindingElementTests.cs +++ b/src/DotNetOpenAuth.Test/OAuth/ChannelElements/HmacSha1SigningBindingElementTests.cs @@ -5,6 +5,9 @@ //----------------------------------------------------------------------- namespace DotNetOpenAuth.Test.OAuth.ChannelElements { + using DotNetOpenAuth.Messaging; + using DotNetOpenAuth.Messaging.Reflection; + using DotNetOpenAuth.OAuth; using DotNetOpenAuth.OAuth.ChannelElements; using DotNetOpenAuth.OAuth.Messages; using DotNetOpenAuth.Test.Mocks; @@ -20,5 +23,25 @@ namespace DotNetOpenAuth.Test.OAuth.ChannelElements { hmac.Channel = new TestChannel(this.MessageDescriptions); Assert.AreEqual("kR0LhH8UqylaLfR/esXVVlP4sQI=", hmac.GetSignatureTestHook(message)); } + + [Test] + public void LinkedInInteropTest() { + var endpoint = new MessageReceivingEndpoint("https://api.linkedin.com/v1/people/~:(id,first-name,last-name,headline,industry,summary)", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest); + var message = new AccessProtectedResourceRequest(endpoint, Protocol.V10.Version); + message.ConsumerKey = "ub78frzrn0yf"; + message.AccessToken = "852863fd-05da-4d80-a93d-50f64f966de4"; + ((ITamperResistantOAuthMessage)message).ConsumerSecret = "ExJXsYl7Or8OfK98"; + ((ITamperResistantOAuthMessage)message).TokenSecret = "b197333b-470a-43b3-bcd7-49d6d2229c4c"; + var signedMessage = (ITamperResistantOAuthMessage)message; + signedMessage.HttpMethod = "GET"; + signedMessage.SignatureMethod = "HMAC-SHA1"; + MessageDictionary dictionary = this.MessageDescriptions.GetAccessor(message); + dictionary["oauth_timestamp"] = "1353545248"; + dictionary["oauth_nonce"] = "ugEB4bst"; + + var hmac = new HmacSha1SigningBindingElement(); + hmac.Channel = new TestChannel(this.MessageDescriptions); + Assert.That(hmac.GetSignatureTestHook(message), Is.EqualTo("l09yeD9cr4+h1eoUF4WBoGEHrlk=")); + } } } diff --git a/src/DotNetOpenAuth.Test/OAuth2/AuthorizationServerTests.cs b/src/DotNetOpenAuth.Test/OAuth2/AuthorizationServerTests.cs index 3791e28..e8f7172 100644 --- a/src/DotNetOpenAuth.Test/OAuth2/AuthorizationServerTests.cs +++ b/src/DotNetOpenAuth.Test/OAuth2/AuthorizationServerTests.cs @@ -9,8 +9,11 @@ namespace DotNetOpenAuth.Test.OAuth2 { using System.Collections.Generic; using System.Linq; using System.Text; + using System.Threading.Tasks; using DotNetOpenAuth.OAuth2; + using DotNetOpenAuth.OAuth2.ChannelElements; using DotNetOpenAuth.OAuth2.Messages; + using Moq; using NUnit.Framework; /// <summary> @@ -28,8 +31,7 @@ namespace DotNetOpenAuth.Test.OAuth2 { AuthorizationServerMock, new UserAgentClient(AuthorizationServerDescription), client => { - var request = new AccessTokenAuthorizationCodeRequestC(AuthorizationServerDescription) - { ClientIdentifier = ClientId, ClientSecret = ClientSecret, AuthorizationCode = "foo" }; + var request = new AccessTokenAuthorizationCodeRequestC(AuthorizationServerDescription) { ClientIdentifier = ClientId, ClientSecret = ClientSecret, AuthorizationCode = "foo" }; var response = client.Channel.Request<AccessTokenFailedResponse>(request); Assert.That(response.Error, Is.Not.Null.And.Not.Empty); @@ -40,5 +42,182 @@ namespace DotNetOpenAuth.Test.OAuth2 { }); coordinator.Run(); } + + [Test] + public void DecodeRefreshToken() { + var refreshTokenSource = new TaskCompletionSource<string>(); + var coordinator = new OAuth2Coordinator<WebServerClient>( + AuthorizationServerDescription, + AuthorizationServerMock, + new WebServerClient(AuthorizationServerDescription), + client => { + try { + var authState = new AuthorizationState(TestScopes) { + Callback = ClientCallback, + }; + client.PrepareRequestUserAuthorization(authState).Respond(); + var result = client.ProcessUserAuthorization(); + Assert.That(result.AccessToken, Is.Not.Null.And.Not.Empty); + Assert.That(result.RefreshToken, Is.Not.Null.And.Not.Empty); + refreshTokenSource.SetResult(result.RefreshToken); + } catch { + 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(); + } + + [Test] + public void ResourceOwnerScopeOverride() { + var clientRequestedScopes = new[] { "scope1", "scope2" }; + var serverOverriddenScopes = new[] { "scope1", "differentScope" }; + var authServerMock = CreateAuthorizationServerMock(); + authServerMock + .Setup(a => a.CheckAuthorizeResourceOwnerCredentialGrant(ResourceOwnerUsername, ResourceOwnerPassword, It.IsAny<IAccessTokenRequest>())) + .Returns<string, string, IAccessTokenRequest>((un, pw, req) => { + var response = new AutomatedUserAuthorizationCheckResponse(req, true, ResourceOwnerUsername); + response.ApprovedScope.Clear(); + 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(); + } + + [Test] + public void CreateAccessTokenSeesAuthorizingUserResourceOwnerGrant() { + var authServerMock = CreateAuthorizationServerMock(); + authServerMock + .Setup(a => a.CheckAuthorizeResourceOwnerCredentialGrant(ResourceOwnerUsername, ResourceOwnerPassword, It.IsAny<IAccessTokenRequest>())) + .Returns<string, string, IAccessTokenRequest>((un, pw, req) => { + var response = new AutomatedUserAuthorizationCheckResponse(req, true, ResourceOwnerUsername); + 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); + Assert.That(result.AccessToken, Is.Not.Null); + }, + server => { + server.HandleTokenRequest().Respond(); + }); + coordinator.Run(); + } + + [Test] + public void CreateAccessTokenSeesAuthorizingUserClientCredentialGrant() { + var authServerMock = CreateAuthorizationServerMock(); + authServerMock + .Setup(a => a.CheckAuthorizeClientCredentialsGrant(It.IsAny<IAccessTokenRequest>())) + .Returns<IAccessTokenRequest>(req => { + 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); + Assert.That(result.AccessToken, Is.Not.Null); + }, + server => { + server.HandleTokenRequest().Respond(); + }); + coordinator.Run(); + } + + [Test] + public void CreateAccessTokenSeesAuthorizingUserAuthorizationCodeGrant() { + var authServerMock = CreateAuthorizationServerMock(); + authServerMock + .Setup(a => a.IsAuthorizationValid(It.IsAny<IAuthorizationDescription>())) + .Returns<IAuthorizationDescription>(req => { + Assert.That(req.User, Is.EqualTo(ResourceOwnerUsername)); + return true; + }); + var coordinator = new OAuth2Coordinator<WebServerClient>( + AuthorizationServerDescription, + authServerMock.Object, + new WebServerClient(AuthorizationServerDescription), + client => { + var authState = new AuthorizationState(TestScopes) { + Callback = ClientCallback, + }; + client.PrepareRequestUserAuthorization(authState).Respond(); + var result = client.ProcessUserAuthorization(); + 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(); + } + + [Test] + public void ClientCredentialScopeOverride() { + var clientRequestedScopes = new[] { "scope1", "scope2" }; + var serverOverriddenScopes = new[] { "scope1", "differentScope" }; + var authServerMock = CreateAuthorizationServerMock(); + authServerMock + .Setup(a => a.CheckAuthorizeClientCredentialsGrant(It.IsAny<IAccessTokenRequest>())) + .Returns<IAccessTokenRequest>(req => { + var response = new AutomatedAuthorizationCheckResponse(req, true); + response.ApprovedScope.Clear(); + 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.GetClientAccessToken(clientRequestedScopes); + Assert.That(result.Scope, Is.EquivalentTo(serverOverriddenScopes)); + }, + server => { + server.HandleTokenRequest().Respond(); + }); + coordinator.Run(); + } } } diff --git a/src/DotNetOpenAuth.Test/OAuth2/OAuth2TestBase.cs b/src/DotNetOpenAuth.Test/OAuth2/OAuth2TestBase.cs index b9e32fe..395b18c 100644 --- a/src/DotNetOpenAuth.Test/OAuth2/OAuth2TestBase.cs +++ b/src/DotNetOpenAuth.Test/OAuth2/OAuth2TestBase.cs @@ -56,8 +56,9 @@ namespace DotNetOpenAuth.Test.OAuth2 { d => d.ClientIdentifier == ClientId && d.User == ResourceOwnerUsername && MessagingUtilities.AreEquivalent(d.Scope, TestScopes)))).Returns(true); - string canonicalUserName = ResourceOwnerUsername; - authHostMock.Setup(m => m.TryAuthorizeResourceOwnerCredentialGrant(ResourceOwnerUsername, ResourceOwnerPassword, It.IsAny<IAccessTokenRequest>(), out canonicalUserName)).Returns(true); + authHostMock + .Setup(m => m.CheckAuthorizeResourceOwnerCredentialGrant(ResourceOwnerUsername, ResourceOwnerPassword, It.IsAny<IAccessTokenRequest>())) + .Returns<string, string, IAccessTokenRequest>((p1, p2, p3) => new AutomatedUserAuthorizationCheckResponse(p3, true, ResourceOwnerUsername)); authHostMock.Setup(m => m.CreateAccessToken(It.IsAny<IAccessTokenRequest>())).Returns(new AccessTokenResult(new AuthorizationServerAccessToken() { AccessTokenSigningKey = AsymmetricKey })); return authHostMock; } diff --git a/src/DotNetOpenAuth.Test/OAuth2/ResourceServerTests.cs b/src/DotNetOpenAuth.Test/OAuth2/ResourceServerTests.cs index a4d09de..80a8392 100644 --- a/src/DotNetOpenAuth.Test/OAuth2/ResourceServerTests.cs +++ b/src/DotNetOpenAuth.Test/OAuth2/ResourceServerTests.cs @@ -87,8 +87,8 @@ namespace DotNetOpenAuth.Test.OAuth2 { a => a.IsAuthorizationValid(It.Is<IAuthorizationDescription>(d => d.User == null && d.ClientIdentifier == ClientId && MessagingUtilities.AreEquivalent(d.Scope, TestScopes)))) .Returns(true); authServer.Setup( - a => a.TryAuthorizeClientCredentialsGrant(It.Is<IAccessTokenRequest>(d => d.ClientIdentifier == ClientId && MessagingUtilities.AreEquivalent(d.Scope, TestScopes)))) - .Returns(true); + a => a.CheckAuthorizeClientCredentialsGrant(It.Is<IAccessTokenRequest>(d => d.ClientIdentifier == ClientId && MessagingUtilities.AreEquivalent(d.Scope, TestScopes)))) + .Returns<IAccessTokenRequest>(req => new AutomatedAuthorizationCheckResponse(req, true)); var coordinator = new OAuth2Coordinator<WebServerClient>( AuthorizationServerDescription, authServer.Object, diff --git a/src/DotNetOpenAuth.Test/OAuth2/WebServerClientAuthorizeTests.cs b/src/DotNetOpenAuth.Test/OAuth2/WebServerClientAuthorizeTests.cs index 9a9c078..8b8b29c 100644 --- a/src/DotNetOpenAuth.Test/OAuth2/WebServerClientAuthorizeTests.cs +++ b/src/DotNetOpenAuth.Test/OAuth2/WebServerClientAuthorizeTests.cs @@ -84,8 +84,8 @@ namespace DotNetOpenAuth.Test.OAuth2 { a => a.IsAuthorizationValid(It.Is<IAuthorizationDescription>(d => d.User == null && d.ClientIdentifier == ClientId && MessagingUtilities.AreEquivalent(d.Scope, TestScopes)))) .Returns(true); authServer.Setup( - a => a.TryAuthorizeClientCredentialsGrant(It.Is<IAccessTokenRequest>(d => d.ClientIdentifier == ClientId && MessagingUtilities.AreEquivalent(d.Scope, TestScopes)))) - .Returns(true); + a => a.CheckAuthorizeClientCredentialsGrant(It.Is<IAccessTokenRequest>(d => d.ClientIdentifier == ClientId && MessagingUtilities.AreEquivalent(d.Scope, TestScopes)))) + .Returns<IAccessTokenRequest>(req => new AutomatedAuthorizationCheckResponse(req, true)); var coordinator = new OAuth2Coordinator<WebServerClient>( AuthorizationServerDescription, authServer.Object, @@ -102,6 +102,34 @@ namespace DotNetOpenAuth.Test.OAuth2 { } [Test] + public void GetClientAccessTokenReturnsApprovedScope() { + string[] approvedScopes = new[] { "Scope2", "Scope3" }; + var authServer = CreateAuthorizationServerMock(); + authServer.Setup( + a => a.IsAuthorizationValid(It.Is<IAuthorizationDescription>(d => d.User == null && d.ClientIdentifier == ClientId && MessagingUtilities.AreEquivalent(d.Scope, TestScopes)))) + .Returns(true); + authServer.Setup( + a => a.CheckAuthorizeClientCredentialsGrant(It.Is<IAccessTokenRequest>(d => d.ClientIdentifier == ClientId && MessagingUtilities.AreEquivalent(d.Scope, TestScopes)))) + .Returns<IAccessTokenRequest>(req => { + var response = new AutomatedAuthorizationCheckResponse(req, true); + response.ApprovedScope.ResetContents(approvedScopes); + return response; + }); + var coordinator = new OAuth2Coordinator<WebServerClient>( + AuthorizationServerDescription, + authServer.Object, + new WebServerClient(AuthorizationServerDescription), + client => { + var authState = client.GetClientAccessToken(TestScopes); + Assert.That(authState.Scope, Is.EquivalentTo(approvedScopes)); + }, + server => { + server.HandleTokenRequest().Respond(); + }); + coordinator.Run(); + } + + [Test] public void CreateAuthorizingHandlerBearer() { var client = new WebServerClient(AuthorizationServerDescription); string bearerToken = "mytoken"; |