diff options
Diffstat (limited to 'src')
7 files changed, 85 insertions, 9 deletions
diff --git a/src/DotNetOpenAuth.OAuth2/OAuth2/OAuthUtilities.cs b/src/DotNetOpenAuth.OAuth2/OAuth2/OAuthUtilities.cs index aeba8d7..1871ad6 100644 --- a/src/DotNetOpenAuth.OAuth2/OAuth2/OAuthUtilities.cs +++ b/src/DotNetOpenAuth.OAuth2/OAuth2/OAuthUtilities.cs @@ -29,7 +29,7 @@ namespace DotNetOpenAuth.OAuth2 { /// <summary> /// The string "Basic ". /// </summary> - private const string HttpBasicAuthScheme = "Basic "; + private const string HttpBasicAuthScheme = "Basic"; /// <summary> /// The delimiter between scope elements. @@ -172,7 +172,7 @@ namespace DotNetOpenAuth.OAuth2 { string concat = userName + ":" + password; byte[] bits = HttpBasicEncoding.GetBytes(concat); string base64 = Convert.ToBase64String(bits); - headers.Authorization = new AuthenticationHeaderValue(HttpBasicAuthScheme.TrimEnd(), base64); + headers.Authorization = new AuthenticationHeaderValue(HttpBasicAuthScheme, base64); } /// <summary> diff --git a/src/DotNetOpenAuth.Test/CookieContainerExtensions.cs b/src/DotNetOpenAuth.Test/CookieContainerExtensions.cs new file mode 100644 index 0000000..5a09d13 --- /dev/null +++ b/src/DotNetOpenAuth.Test/CookieContainerExtensions.cs @@ -0,0 +1,38 @@ +//----------------------------------------------------------------------- +// <copyright file="CookieContainerExtensions.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Test { + using System; + using System.Collections.Generic; + using System.Net; + using System.Net.Http; + + using Validation; + + internal static class CookieContainerExtensions { + internal static void SetCookies(this CookieContainer container, HttpResponseMessage response, Uri requestUri = null) { + Requires.NotNull(container, "container"); + Requires.NotNull(response, "response"); + + IEnumerable<string> cookieHeaders; + if (response.Headers.TryGetValues("Set-Cookie", out cookieHeaders)) { + foreach (string cookie in cookieHeaders) { + container.SetCookies(requestUri ?? response.RequestMessage.RequestUri, cookie); + } + } + } + + internal static void ApplyCookies(this CookieContainer container, HttpRequestMessage request) { + Requires.NotNull(container, "container"); + Requires.NotNull(request, "request"); + + string cookieHeader = container.GetCookieHeader(request.RequestUri); + if (!string.IsNullOrEmpty(cookieHeader)) { + request.Headers.TryAddWithoutValidation("Cookie", cookieHeader); + } + } + } +}
\ No newline at end of file diff --git a/src/DotNetOpenAuth.Test/CookieDelegatingHandler.cs b/src/DotNetOpenAuth.Test/CookieDelegatingHandler.cs new file mode 100644 index 0000000..1b25dc0 --- /dev/null +++ b/src/DotNetOpenAuth.Test/CookieDelegatingHandler.cs @@ -0,0 +1,31 @@ +//----------------------------------------------------------------------- +// <copyright file="CookieDelegatingHandler.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Test { + using System.Linq; + using System.Net; + using System.Net.Http; + using System.Net.Http.Headers; + using System.Text; + using System.Threading; + using System.Threading.Tasks; + + internal class CookieDelegatingHandler : DelegatingHandler { + internal CookieDelegatingHandler(HttpMessageHandler innerHandler, CookieContainer cookieContainer = null) + : base(innerHandler) { + this.Container = cookieContainer ?? new CookieContainer(); + } + + public CookieContainer Container { get; set; } + + protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { + this.Container.ApplyCookies(request); + var response = await base.SendAsync(request, cancellationToken); + this.Container.SetCookies(response); + return response; + } + } +} diff --git a/src/DotNetOpenAuth.Test/DotNetOpenAuth.Test.csproj b/src/DotNetOpenAuth.Test/DotNetOpenAuth.Test.csproj index 07ee8a9..0e5a898 100644 --- a/src/DotNetOpenAuth.Test/DotNetOpenAuth.Test.csproj +++ b/src/DotNetOpenAuth.Test/DotNetOpenAuth.Test.csproj @@ -84,6 +84,7 @@ <RequiredTargetFramework>3.5</RequiredTargetFramework> </Reference> <Reference Include="System.Net.Http" /> + <Reference Include="System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" /> <Reference Include="System.Net.Http.WebRequest" /> <Reference Include="System.Runtime.Serialization"> <RequiredTargetFramework>3.0</RequiredTargetFramework> @@ -126,6 +127,8 @@ <Compile Include="Messaging\StandardMessageFactoryTests.cs" /> <Compile Include="MockingHostFactories.cs" /> <Compile Include="Mocks\AssociateUnencryptedRequestNoSslCheck.cs" /> + <Compile Include="CookieContainerExtensions.cs" /> + <Compile Include="CookieDelegatingHandler.cs" /> <Compile Include="Mocks\IBaseMessageExplicitMembers.cs" /> <Compile Include="Mocks\InMemoryTokenManager.cs" /> <Compile Include="Mocks\MockHttpMessageHandler.cs" /> diff --git a/src/DotNetOpenAuth.Test/MockingHostFactories.cs b/src/DotNetOpenAuth.Test/MockingHostFactories.cs index eb539da..a05fcdf 100644 --- a/src/DotNetOpenAuth.Test/MockingHostFactories.cs +++ b/src/DotNetOpenAuth.Test/MockingHostFactories.cs @@ -10,9 +10,7 @@ namespace DotNetOpenAuth.Test { using System.Net.Http; using System.Threading; using System.Threading.Tasks; - using System.Linq; - using Validation; internal class MockingHostFactories : IHostFactories { @@ -20,14 +18,17 @@ namespace DotNetOpenAuth.Test { public MockingHostFactories(List<TestBase.Handler> handlers = null) { this.handlers = handlers ?? new List<TestBase.Handler>(); + this.CookieContainer = new CookieContainer(); } public List<TestBase.Handler> Handlers { get { return this.handlers; } } + public CookieContainer CookieContainer { get; set; } + public HttpMessageHandler CreateHttpMessageHandler() { - return new ForwardingMessageHandler(this.handlers, this); + return new CookieDelegatingHandler(new ForwardingMessageHandler(this.handlers, this), this.CookieContainer); } public HttpClient CreateHttpClient(HttpMessageHandler handler = null) { diff --git a/src/DotNetOpenAuth.Test/OAuth2/WebServerClientAuthorizeTests.cs b/src/DotNetOpenAuth.Test/OAuth2/WebServerClientAuthorizeTests.cs index 2befd35..59d82d5 100644 --- a/src/DotNetOpenAuth.Test/OAuth2/WebServerClientAuthorizeTests.cs +++ b/src/DotNetOpenAuth.Test/OAuth2/WebServerClientAuthorizeTests.cs @@ -38,11 +38,12 @@ namespace DotNetOpenAuth.Test.OAuth2 { return await server.HandleTokenRequestAsync(req, ct); }); - var client = new WebServerClient(AuthorizationServerDescription); + var client = new WebServerClient(AuthorizationServerDescription, ClientId, ClientSecret, this.HostFactories); var authState = new AuthorizationState(TestScopes) { Callback = ClientCallback, }; var authRequestRedirect = await client.PrepareRequestUserAuthorizationAsync(authState); + this.HostFactories.CookieContainer.SetCookies(authRequestRedirect, ClientCallback); Uri authRequestResponse; using (var httpClient = this.HostFactories.CreateHttpClient()) { using (var httpResponse = await httpClient.GetAsync(authRequestRedirect.Headers.Location)) { @@ -50,7 +51,9 @@ namespace DotNetOpenAuth.Test.OAuth2 { } } - var result = await client.ProcessUserAuthorizationAsync(new HttpRequestMessage(HttpMethod.Get, authRequestResponse), CancellationToken.None); + var authorizationResponse = new HttpRequestMessage(HttpMethod.Get, authRequestResponse); + this.HostFactories.CookieContainer.ApplyCookies(authorizationResponse); + var result = await client.ProcessUserAuthorizationAsync(authorizationResponse, CancellationToken.None); Assert.That(result.AccessToken, Is.Not.Null.And.Not.Empty); Assert.That(result.RefreshToken, Is.Not.Null.And.Not.Empty); } diff --git a/src/DotNetOpenAuth.Test/OpenId/RelyingParty/OpenIdRelyingPartyTests.cs b/src/DotNetOpenAuth.Test/OpenId/RelyingParty/OpenIdRelyingPartyTests.cs index 13049db..1f21ce5 100644 --- a/src/DotNetOpenAuth.Test/OpenId/RelyingParty/OpenIdRelyingPartyTests.cs +++ b/src/DotNetOpenAuth.Test/OpenId/RelyingParty/OpenIdRelyingPartyTests.cs @@ -112,7 +112,7 @@ namespace DotNetOpenAuth.Test.OpenId.RelyingParty { var opStore = new StandardProviderApplicationStore(); Handle(RPRealmUri).By( async req => { - var rp = new OpenIdRelyingParty(new StandardRelyingPartyApplicationStore()); + var rp = new OpenIdRelyingParty(new StandardRelyingPartyApplicationStore(), this.HostFactories); // Rig it to always deny the incoming OP rp.EndpointFilter = op => false; @@ -124,7 +124,7 @@ namespace DotNetOpenAuth.Test.OpenId.RelyingParty { }); this.RegisterAutoProvider(); { - var op = new OpenIdProvider(opStore); + 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()) { |