diff options
Diffstat (limited to 'src/DotNetOpenAuth.Test/OpenId/RelyingParty')
3 files changed, 114 insertions, 94 deletions
diff --git a/src/DotNetOpenAuth.Test/OpenId/RelyingParty/AuthenticationRequestTests.cs b/src/DotNetOpenAuth.Test/OpenId/RelyingParty/AuthenticationRequestTests.cs index cd72fdb..333169f 100644 --- a/src/DotNetOpenAuth.Test/OpenId/RelyingParty/AuthenticationRequestTests.cs +++ b/src/DotNetOpenAuth.Test/OpenId/RelyingParty/AuthenticationRequestTests.cs @@ -10,6 +10,8 @@ namespace DotNetOpenAuth.Test.OpenId.RelyingParty { using System.Collections.Specialized; using System.Linq; using System.Text; + using System.Threading; + using System.Threading.Tasks; using System.Web; using DotNetOpenAuth.Messaging; using DotNetOpenAuth.OpenId; @@ -70,54 +72,51 @@ namespace DotNetOpenAuth.Test.OpenId.RelyingParty { /// Verifies RedirectingResponse. /// </summary> [Test] - public void CreateRequestMessage() { - OpenIdCoordinator coordinator = new OpenIdCoordinator( - rp => { - Identifier id = this.GetMockIdentifier(ProtocolVersion.V20); - IAuthenticationRequest authRequest = rp.CreateRequest(id, this.realm, this.returnTo); - - // Add some callback arguments - authRequest.AddCallbackArguments("a", "b"); - authRequest.AddCallbackArguments(new Dictionary<string, string> { { "c", "d" }, { "e", "f" } }); - - // Assembly an extension request. - ClaimsRequest sregRequest = new ClaimsRequest(); - sregRequest.Nickname = DemandLevel.Request; - authRequest.AddExtension(sregRequest); - - // Construct the actual authentication request message. - var authRequestAccessor = (AuthenticationRequest)authRequest; - var req = authRequestAccessor.CreateRequestMessageTestHook(); - Assert.IsNotNull(req); - - // Verify that callback arguments were included. - NameValueCollection callbackArguments = HttpUtility.ParseQueryString(req.ReturnTo.Query); - Assert.AreEqual("b", callbackArguments["a"]); - Assert.AreEqual("d", callbackArguments["c"]); - Assert.AreEqual("f", callbackArguments["e"]); - - // Verify that extensions were included. - Assert.AreEqual(1, req.Extensions.Count); - Assert.IsTrue(req.Extensions.Contains(sregRequest)); - }, - AutoProvider); - coordinator.Run(); + public async Task CreateRequestMessage() { + this.RegisterAutoProvider(); + var rp = this.CreateRelyingParty(); + Identifier id = this.GetMockIdentifier(ProtocolVersion.V20); + IAuthenticationRequest authRequest = await rp.CreateRequestAsync(id, this.realm, this.returnTo); + + // Add some callback arguments + authRequest.AddCallbackArguments("a", "b"); + authRequest.AddCallbackArguments(new Dictionary<string, string> { { "c", "d" }, { "e", "f" } }); + + // Assembly an extension request. + var sregRequest = new ClaimsRequest(); + sregRequest.Nickname = DemandLevel.Request; + authRequest.AddExtension(sregRequest); + + // Construct the actual authentication request message. + var authRequestAccessor = (AuthenticationRequest)authRequest; + var req = await authRequestAccessor.CreateRequestMessageTestHookAsync(CancellationToken.None); + Assert.IsNotNull(req); + + // Verify that callback arguments were included. + NameValueCollection callbackArguments = HttpUtility.ParseQueryString(req.ReturnTo.Query); + Assert.AreEqual("b", callbackArguments["a"]); + Assert.AreEqual("d", callbackArguments["c"]); + Assert.AreEqual("f", callbackArguments["e"]); + + // Verify that extensions were included. + Assert.AreEqual(1, req.Extensions.Count); + Assert.IsTrue(req.Extensions.Contains(sregRequest)); } /// <summary> /// Verifies that delegating authentication requests are filtered out when configured to do so. /// </summary> [Test] - public void CreateFiltersDelegatingIdentifiers() { + public async Task CreateFiltersDelegatingIdentifiers() { Identifier id = GetMockIdentifier(ProtocolVersion.V20, false, true); var rp = CreateRelyingParty(); // First verify that delegating identifiers work - Assert.IsTrue(AuthenticationRequest.Create(id, rp, this.realm, this.returnTo, false).Any(), "The delegating identifier should have not generated any results."); + Assert.IsTrue((await AuthenticationRequest.CreateAsync(id, rp, this.realm, this.returnTo, false, CancellationToken.None)).Any(), "The delegating identifier should have not generated any results."); // Now disable them and try again. rp.SecuritySettings.RejectDelegatingIdentifiers = true; - Assert.IsFalse(AuthenticationRequest.Create(id, rp, this.realm, this.returnTo, false).Any(), "The delegating identifier should have not generated any results."); + Assert.IsFalse((await AuthenticationRequest.CreateAsync(id, rp, this.realm, this.returnTo, false, CancellationToken.None)).Any(), "The delegating identifier should have not generated any results."); } /// <summary> @@ -135,11 +134,12 @@ namespace DotNetOpenAuth.Test.OpenId.RelyingParty { /// Verifies that AddCallbackArguments adds query arguments to the return_to URL of the message. /// </summary> [Test] - public void AddCallbackArgument() { + public async Task AddCallbackArgument() { var authRequest = this.CreateAuthenticationRequest(this.claimedId, this.claimedId); Assert.AreEqual(this.returnTo, authRequest.ReturnToUrl); authRequest.AddCallbackArguments("p1", "v1"); - var req = (SignedResponseRequest)authRequest.RedirectingResponse.OriginalMessage; + var response = (HttpResponseMessageWithOriginal)await authRequest.GetRedirectingResponseAsync(CancellationToken.None); + var req = (SignedResponseRequest)response.OriginalMessage; NameValueCollection query = HttpUtility.ParseQueryString(req.ReturnTo.Query); Assert.AreEqual("v1", query["p1"]); } @@ -149,13 +149,14 @@ namespace DotNetOpenAuth.Test.OpenId.RelyingParty { /// rather than appending them. /// </summary> [Test] - public void AddCallbackArgumentClearsPreviousArgument() { + public async Task AddCallbackArgumentClearsPreviousArgument() { UriBuilder returnToWithArgs = new UriBuilder(this.returnTo); returnToWithArgs.AppendQueryArgs(new Dictionary<string, string> { { "p1", "v1" } }); this.returnTo = returnToWithArgs.Uri; var authRequest = this.CreateAuthenticationRequest(this.claimedId, this.claimedId); authRequest.AddCallbackArguments("p1", "v2"); - var req = (SignedResponseRequest)authRequest.RedirectingResponse.OriginalMessage; + var response = (HttpResponseMessageWithOriginal)await authRequest.GetRedirectingResponseAsync(CancellationToken.None); + var req = (SignedResponseRequest)response.OriginalMessage; NameValueCollection query = HttpUtility.ParseQueryString(req.ReturnTo.Query); Assert.AreEqual("v2", query["p1"]); } @@ -164,11 +165,12 @@ namespace DotNetOpenAuth.Test.OpenId.RelyingParty { /// Verifies identity-less checkid_* request behavior. /// </summary> [Test] - public void NonIdentityRequest() { + public async Task NonIdentityRequest() { var authRequest = this.CreateAuthenticationRequest(this.claimedId, this.claimedId); authRequest.IsExtensionOnly = true; Assert.IsTrue(authRequest.IsExtensionOnly); - var req = (SignedResponseRequest)authRequest.RedirectingResponse.OriginalMessage; + var response = (HttpResponseMessageWithOriginal)await authRequest.GetRedirectingResponseAsync(CancellationToken.None); + var req = (SignedResponseRequest)response.OriginalMessage; Assert.IsNotInstanceOf<CheckIdRequest>(req, "An unexpected SignedResponseRequest derived type was generated."); } @@ -177,15 +179,15 @@ namespace DotNetOpenAuth.Test.OpenId.RelyingParty { /// only generate OP Identifier auth requests. /// </summary> [Test] - public void DualIdentifierUsedOnlyAsOPIdentifierForAuthRequest() { + public async Task DualIdentifierUsedOnlyAsOPIdentifierForAuthRequest() { var rp = this.CreateRelyingParty(true); - var results = AuthenticationRequest.Create(GetMockDualIdentifier(), rp, this.realm, this.returnTo, false).ToList(); + var results = (await AuthenticationRequest.CreateAsync(GetMockDualIdentifier(), rp, this.realm, this.returnTo, false, CancellationToken.None)).ToList(); Assert.AreEqual(1, results.Count); Assert.IsTrue(results[0].IsDirectedIdentity); // Also test when dual identiifer support is turned on. rp.SecuritySettings.AllowDualPurposeIdentifiers = true; - results = AuthenticationRequest.Create(GetMockDualIdentifier(), rp, this.realm, this.returnTo, false).ToList(); + results = (await AuthenticationRequest.CreateAsync(GetMockDualIdentifier(), rp, this.realm, this.returnTo, false, CancellationToken.None)).ToList(); Assert.AreEqual(1, results.Count); Assert.IsTrue(results[0].IsDirectedIdentity); } diff --git a/src/DotNetOpenAuth.Test/OpenId/RelyingParty/OpenIdRelyingPartyTests.cs b/src/DotNetOpenAuth.Test/OpenId/RelyingParty/OpenIdRelyingPartyTests.cs index a2a4efa..2d9413d 100644 --- a/src/DotNetOpenAuth.Test/OpenId/RelyingParty/OpenIdRelyingPartyTests.cs +++ b/src/DotNetOpenAuth.Test/OpenId/RelyingParty/OpenIdRelyingPartyTests.cs @@ -7,11 +7,18 @@ namespace DotNetOpenAuth.Test.OpenId.RelyingParty { using System; using System.Linq; + using System.Net.Http; + using System.Threading.Tasks; + using System.Web; + using DotNetOpenAuth.Messaging; using DotNetOpenAuth.OpenId; using DotNetOpenAuth.OpenId.Extensions; using DotNetOpenAuth.OpenId.Messages; + using DotNetOpenAuth.OpenId.Provider; using DotNetOpenAuth.OpenId.RelyingParty; + using DotNetOpenAuth.Test.Mocks; + using NUnit.Framework; [TestFixture] @@ -22,12 +29,13 @@ namespace DotNetOpenAuth.Test.OpenId.RelyingParty { } [Test] - public void CreateRequestDumbMode() { + public async Task CreateRequestDumbMode() { var rp = this.CreateRelyingParty(true); Identifier id = this.GetMockIdentifier(ProtocolVersion.V20); - var authReq = rp.CreateRequest(id, RPRealmUri, RPUri); - CheckIdRequest requestMessage = (CheckIdRequest)authReq.RedirectingResponse.OriginalMessage; - Assert.IsNull(requestMessage.AssociationHandle); + var authReq = await rp.CreateRequestAsync(id, RPRealmUri, RPUri); + var httpMessage = await authReq.GetRedirectingResponseAsync(); + var data = HttpUtility.ParseQueryString(httpMessage.GetDirectUriRequest().Query); + Assert.IsNull(data[Protocol.Default.openid.assoc_handle]); } [Test, ExpectedException(typeof(ArgumentNullException))] @@ -46,52 +54,52 @@ namespace DotNetOpenAuth.Test.OpenId.RelyingParty { } [Test] - public void CreateRequest() { + public async Task CreateRequest() { var rp = this.CreateRelyingParty(); StoreAssociation(rp, OPUri, HmacShaAssociation.Create("somehandle", new byte[20], TimeSpan.FromDays(1))); Identifier id = Identifier.Parse(GetMockIdentifier(ProtocolVersion.V20)); - var req = rp.CreateRequest(id, RPRealmUri, RPUri); + var req = await rp.CreateRequestAsync(id, RPRealmUri, RPUri); Assert.IsNotNull(req); } [Test] - public void CreateRequests() { + public async Task CreateRequests() { var rp = this.CreateRelyingParty(); StoreAssociation(rp, OPUri, HmacShaAssociation.Create("somehandle", new byte[20], TimeSpan.FromDays(1))); Identifier id = Identifier.Parse(GetMockIdentifier(ProtocolVersion.V20)); - var requests = rp.CreateRequests(id, RPRealmUri, RPUri); + var requests = await rp.CreateRequestsAsync(id, RPRealmUri, RPUri); Assert.AreEqual(1, requests.Count()); } [Test] - public void CreateRequestsWithEndpointFilter() { + public async Task CreateRequestsWithEndpointFilter() { var rp = this.CreateRelyingParty(); StoreAssociation(rp, OPUri, HmacShaAssociation.Create("somehandle", new byte[20], TimeSpan.FromDays(1))); Identifier id = Identifier.Parse(GetMockIdentifier(ProtocolVersion.V20)); rp.EndpointFilter = opendpoint => true; - var requests = rp.CreateRequests(id, RPRealmUri, RPUri); + var requests = await rp.CreateRequestsAsync(id, RPRealmUri, RPUri); Assert.AreEqual(1, requests.Count()); rp.EndpointFilter = opendpoint => false; - requests = rp.CreateRequests(id, RPRealmUri, RPUri); + requests = await rp.CreateRequestsAsync(id, RPRealmUri, RPUri); Assert.AreEqual(0, requests.Count()); } [Test, ExpectedException(typeof(ProtocolException))] - public void CreateRequestOnNonOpenID() { - Uri nonOpenId = new Uri("http://www.microsoft.com/"); + public async Task CreateRequestOnNonOpenID() { + var nonOpenId = new Uri("http://www.microsoft.com/"); + Handle(nonOpenId).By("<html/>", "text/html"); var rp = this.CreateRelyingParty(); - this.MockResponder.RegisterMockResponse(nonOpenId, "text/html", "<html/>"); - rp.CreateRequest(nonOpenId, RPRealmUri, RPUri); + await rp.CreateRequestAsync(nonOpenId, RPRealmUri, RPUri); } [Test] - public void CreateRequestsOnNonOpenID() { - Uri nonOpenId = new Uri("http://www.microsoft.com/"); + public async Task CreateRequestsOnNonOpenID() { + var nonOpenId = new Uri("http://www.microsoft.com/"); + Handle(nonOpenId).By("<html/>", "text/html"); var rp = this.CreateRelyingParty(); - this.MockResponder.RegisterMockResponse(nonOpenId, "text/html", "<html/>"); - var requests = rp.CreateRequests(nonOpenId, RPRealmUri, RPUri); + var requests = await rp.CreateRequestsAsync(nonOpenId, RPRealmUri, RPUri); Assert.AreEqual(0, requests.Count()); } @@ -100,25 +108,32 @@ namespace DotNetOpenAuth.Test.OpenId.RelyingParty { /// OPs that are not approved by <see cref="OpenIdRelyingParty.EndpointFilter"/>. /// </summary> [Test] - public void AssertionWithEndpointFilter() { - var coordinator = new OpenIdCoordinator( - rp => { - // register with RP so that id discovery passes - rp.Channel.WebRequestHandler = this.MockResponder.MockWebRequestHandler; + public async Task AssertionWithEndpointFilter() { + var opStore = new StandardProviderApplicationStore(); + Handle(RPUri).By( + async req => { + var rp = new OpenIdRelyingParty(new StandardRelyingPartyApplicationStore(), this.HostFactories); // Rig it to always deny the incoming OP rp.EndpointFilter = op => false; // Receive the unsolicited assertion - var response = rp.GetResponse(); + var response = await rp.GetResponseAsync(req); + Assert.That(response, Is.Not.Null); Assert.AreEqual(AuthenticationStatus.Failed, response.Status); - }, - op => { - Identifier id = GetMockIdentifier(ProtocolVersion.V20); - op.SendUnsolicitedAssertion(OPUri, GetMockRealm(false), id, id); - AutoProvider(op); + return new HttpResponseMessage(); }); - coordinator.Run(); + this.RegisterAutoProvider(); + { + var op = new OpenIdProvider(opStore, this.HostFactories); + Identifier id = GetMockIdentifier(ProtocolVersion.V20); + var assertion = await op.PrepareUnsolicitedAssertionAsync(OPUri, GetMockRealm(false), id, id); + using (var httpClient = this.HostFactories.CreateHttpClient()) { + using (var response = await httpClient.GetAsync(assertion.Headers.Location)) { + response.EnsureSuccessStatusCode(); + } + } + } } } } diff --git a/src/DotNetOpenAuth.Test/OpenId/RelyingParty/PositiveAuthenticationResponseTests.cs b/src/DotNetOpenAuth.Test/OpenId/RelyingParty/PositiveAuthenticationResponseTests.cs index 91318f5..6ce74fd 100644 --- a/src/DotNetOpenAuth.Test/OpenId/RelyingParty/PositiveAuthenticationResponseTests.cs +++ b/src/DotNetOpenAuth.Test/OpenId/RelyingParty/PositiveAuthenticationResponseTests.cs @@ -7,6 +7,9 @@ namespace DotNetOpenAuth.Test.OpenId.RelyingParty { using System; using System.Collections.Generic; + using System.Threading; + using System.Threading.Tasks; + using DotNetOpenAuth.Messaging; using DotNetOpenAuth.OpenId; using DotNetOpenAuth.OpenId.Extensions.SimpleRegistration; @@ -29,12 +32,12 @@ namespace DotNetOpenAuth.Test.OpenId.RelyingParty { /// Verifies good, positive assertions are accepted. /// </summary> [Test] - public void Valid() { + public async Task Valid() { PositiveAssertionResponse assertion = this.GetPositiveAssertion(); ClaimsResponse extension = new ClaimsResponse(); assertion.Extensions.Add(extension); var rp = CreateRelyingParty(); - var authResponse = new PositiveAuthenticationResponse(assertion, rp); + var authResponse = await PositiveAuthenticationResponse.CreateAsync(assertion, rp, CancellationToken.None); Assert.AreEqual(AuthenticationStatus.Authenticated, authResponse.Status); Assert.IsNull(authResponse.Exception); Assert.AreEqual((string)assertion.ClaimedIdentifier, (string)authResponse.ClaimedIdentifier); @@ -49,13 +52,13 @@ namespace DotNetOpenAuth.Test.OpenId.RelyingParty { /// Verifies that discovery verification of a positive assertion can match a dual identifier. /// </summary> [Test] - public void DualIdentifierMatchesInAssertionVerification() { + public async Task DualIdentifierMatchesInAssertionVerification() { PositiveAssertionResponse assertion = this.GetPositiveAssertion(true); ClaimsResponse extension = new ClaimsResponse(); assertion.Extensions.Add(extension); var rp = CreateRelyingParty(); rp.SecuritySettings.AllowDualPurposeIdentifiers = true; - new PositiveAuthenticationResponse(assertion, rp); // this will throw if it fails to find a match + await PositiveAuthenticationResponse.CreateAsync(assertion, rp, CancellationToken.None); // this will throw if it fails to find a match } /// <summary> @@ -63,12 +66,12 @@ namespace DotNetOpenAuth.Test.OpenId.RelyingParty { /// if the default settings are in place. /// </summary> [Test, ExpectedException(typeof(ProtocolException))] - public void DualIdentifierNoMatchInAssertionVerificationByDefault() { + public async Task DualIdentifierNoMatchInAssertionVerificationByDefault() { PositiveAssertionResponse assertion = this.GetPositiveAssertion(true); ClaimsResponse extension = new ClaimsResponse(); assertion.Extensions.Add(extension); var rp = CreateRelyingParty(); - new PositiveAuthenticationResponse(assertion, rp); // this will throw if it fails to find a match + await PositiveAuthenticationResponse.CreateAsync(assertion, rp, CancellationToken.None); // this will throw if it fails to find a match } /// <summary> @@ -77,11 +80,11 @@ namespace DotNetOpenAuth.Test.OpenId.RelyingParty { /// that the OP has no authority to assert positively regarding. /// </summary> [Test, ExpectedException(typeof(ProtocolException))] - public void SpoofedClaimedIdDetectionSolicited() { + public async Task SpoofedClaimedIdDetectionSolicited() { PositiveAssertionResponse assertion = this.GetPositiveAssertion(); assertion.ProviderEndpoint = new Uri("http://rogueOP"); var rp = CreateRelyingParty(); - var authResponse = new PositiveAuthenticationResponse(assertion, rp); + var authResponse = await PositiveAuthenticationResponse.CreateAsync(assertion, rp, CancellationToken.None); Assert.AreEqual(AuthenticationStatus.Failed, authResponse.Status); } @@ -90,22 +93,22 @@ namespace DotNetOpenAuth.Test.OpenId.RelyingParty { /// Cdentifiers when RequireSsl is set to true. /// </summary> [Test, ExpectedException(typeof(ProtocolException))] - public void InsecureIdentifiersRejectedWithRequireSsl() { + public async Task InsecureIdentifiersRejectedWithRequireSsl() { PositiveAssertionResponse assertion = this.GetPositiveAssertion(); var rp = CreateRelyingParty(); rp.SecuritySettings.RequireSsl = true; - var authResponse = new PositiveAuthenticationResponse(assertion, rp); + var authResponse = await PositiveAuthenticationResponse.CreateAsync(assertion, rp, CancellationToken.None); } [Test] - public void GetCallbackArguments() { + public async Task GetCallbackArguments() { PositiveAssertionResponse assertion = this.GetPositiveAssertion(); var rp = CreateRelyingParty(); UriBuilder returnToBuilder = new UriBuilder(assertion.ReturnTo); returnToBuilder.AppendQueryArgs(new Dictionary<string, string> { { "a", "b" } }); assertion.ReturnTo = returnToBuilder.Uri; - var authResponse = new PositiveAuthenticationResponse(assertion, rp); + var authResponse = await PositiveAuthenticationResponse.CreateAsync(assertion, rp, CancellationToken.None); // First pretend that the return_to args were signed. assertion.ReturnToParametersSignatureValidated = true; @@ -124,18 +127,18 @@ namespace DotNetOpenAuth.Test.OpenId.RelyingParty { /// Verifies that certain problematic claimed identifiers pass through to the RP response correctly. /// </summary> [Test] - public void ProblematicClaimedId() { + public async Task ProblematicClaimedId() { var providerEndpoint = new ProviderEndpointDescription(OpenIdTestBase.OPUri, Protocol.Default.Version); string claimed_id = BaseMockUri + "a./b."; var se = IdentifierDiscoveryResult.CreateForClaimedIdentifier(claimed_id, claimed_id, providerEndpoint, null, null); - UriIdentifier identityUri = (UriIdentifier)se.ClaimedIdentifier; - var mockId = new MockIdentifier(identityUri, this.MockResponder, new IdentifierDiscoveryResult[] { se }); + var identityUri = (UriIdentifier)se.ClaimedIdentifier; + this.RegisterMockXrdsResponse(se); + var rp = this.CreateRelyingParty(); var positiveAssertion = this.GetPositiveAssertion(); - positiveAssertion.ClaimedIdentifier = mockId; - positiveAssertion.LocalIdentifier = mockId; - var rp = CreateRelyingParty(); - var authResponse = new PositiveAuthenticationResponse(positiveAssertion, rp); + positiveAssertion.ClaimedIdentifier = claimed_id; + positiveAssertion.LocalIdentifier = claimed_id; + var authResponse = await PositiveAuthenticationResponse.CreateAsync(positiveAssertion, rp, CancellationToken.None); Assert.AreEqual(AuthenticationStatus.Authenticated, authResponse.Status); Assert.AreEqual(claimed_id, authResponse.ClaimedIdentifier.ToString()); } |