Skip to content
This repository was archived by the owner on Mar 20, 2019. It is now read-only.

Commit baef63e

Browse files
committed
Adds a cookie delegating handler, fixes tests.
1 parent 5be8a13 commit baef63e

7 files changed

Lines changed: 85 additions & 9 deletions

File tree

src/DotNetOpenAuth.OAuth2/OAuth2/OAuthUtilities.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static class OAuthUtilities {
2929
/// <summary>
3030
/// The string "Basic ".
3131
/// </summary>
32-
private const string HttpBasicAuthScheme = "Basic ";
32+
private const string HttpBasicAuthScheme = "Basic";
3333

3434
/// <summary>
3535
/// The delimiter between scope elements.
@@ -172,7 +172,7 @@ internal static void ApplyHttpBasicAuth(System.Net.Http.Headers.HttpRequestHeade
172172
string concat = userName + ":" + password;
173173
byte[] bits = HttpBasicEncoding.GetBytes(concat);
174174
string base64 = Convert.ToBase64String(bits);
175-
headers.Authorization = new AuthenticationHeaderValue(HttpBasicAuthScheme.TrimEnd(), base64);
175+
headers.Authorization = new AuthenticationHeaderValue(HttpBasicAuthScheme, base64);
176176
}
177177

178178
/// <summary>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright file="CookieContainerExtensions.cs" company="Andrew Arnott">
3+
// Copyright (c) Andrew Arnott. All rights reserved.
4+
// </copyright>
5+
//-----------------------------------------------------------------------
6+
7+
namespace DotNetOpenAuth.Test {
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Net;
11+
using System.Net.Http;
12+
13+
using Validation;
14+
15+
internal static class CookieContainerExtensions {
16+
internal static void SetCookies(this CookieContainer container, HttpResponseMessage response, Uri requestUri = null) {
17+
Requires.NotNull(container, "container");
18+
Requires.NotNull(response, "response");
19+
20+
IEnumerable<string> cookieHeaders;
21+
if (response.Headers.TryGetValues("Set-Cookie", out cookieHeaders)) {
22+
foreach (string cookie in cookieHeaders) {
23+
container.SetCookies(requestUri ?? response.RequestMessage.RequestUri, cookie);
24+
}
25+
}
26+
}
27+
28+
internal static void ApplyCookies(this CookieContainer container, HttpRequestMessage request) {
29+
Requires.NotNull(container, "container");
30+
Requires.NotNull(request, "request");
31+
32+
string cookieHeader = container.GetCookieHeader(request.RequestUri);
33+
if (!string.IsNullOrEmpty(cookieHeader)) {
34+
request.Headers.TryAddWithoutValidation("Cookie", cookieHeader);
35+
}
36+
}
37+
}
38+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright file="CookieDelegatingHandler.cs" company="Andrew Arnott">
3+
// Copyright (c) Andrew Arnott. All rights reserved.
4+
// </copyright>
5+
//-----------------------------------------------------------------------
6+
7+
namespace DotNetOpenAuth.Test {
8+
using System.Linq;
9+
using System.Net;
10+
using System.Net.Http;
11+
using System.Net.Http.Headers;
12+
using System.Text;
13+
using System.Threading;
14+
using System.Threading.Tasks;
15+
16+
internal class CookieDelegatingHandler : DelegatingHandler {
17+
internal CookieDelegatingHandler(HttpMessageHandler innerHandler, CookieContainer cookieContainer = null)
18+
: base(innerHandler) {
19+
this.Container = cookieContainer ?? new CookieContainer();
20+
}
21+
22+
public CookieContainer Container { get; set; }
23+
24+
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) {
25+
this.Container.ApplyCookies(request);
26+
var response = await base.SendAsync(request, cancellationToken);
27+
this.Container.SetCookies(response);
28+
return response;
29+
}
30+
}
31+
}

src/DotNetOpenAuth.Test/DotNetOpenAuth.Test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<RequiredTargetFramework>3.5</RequiredTargetFramework>
8585
</Reference>
8686
<Reference Include="System.Net.Http" />
87+
<Reference Include="System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
8788
<Reference Include="System.Net.Http.WebRequest" />
8889
<Reference Include="System.Runtime.Serialization">
8990
<RequiredTargetFramework>3.0</RequiredTargetFramework>
@@ -126,6 +127,8 @@
126127
<Compile Include="Messaging\StandardMessageFactoryTests.cs" />
127128
<Compile Include="MockingHostFactories.cs" />
128129
<Compile Include="Mocks\AssociateUnencryptedRequestNoSslCheck.cs" />
130+
<Compile Include="CookieContainerExtensions.cs" />
131+
<Compile Include="CookieDelegatingHandler.cs" />
129132
<Compile Include="Mocks\IBaseMessageExplicitMembers.cs" />
130133
<Compile Include="Mocks\InMemoryTokenManager.cs" />
131134
<Compile Include="Mocks\MockHttpMessageHandler.cs" />

src/DotNetOpenAuth.Test/MockingHostFactories.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,25 @@ namespace DotNetOpenAuth.Test {
1010
using System.Net.Http;
1111
using System.Threading;
1212
using System.Threading.Tasks;
13-
1413
using System.Linq;
15-
1614
using Validation;
1715

1816
internal class MockingHostFactories : IHostFactories {
1917
private readonly List<TestBase.Handler> handlers;
2018

2119
public MockingHostFactories(List<TestBase.Handler> handlers = null) {
2220
this.handlers = handlers ?? new List<TestBase.Handler>();
21+
this.CookieContainer = new CookieContainer();
2322
}
2423

2524
public List<TestBase.Handler> Handlers {
2625
get { return this.handlers; }
2726
}
2827

28+
public CookieContainer CookieContainer { get; set; }
29+
2930
public HttpMessageHandler CreateHttpMessageHandler() {
30-
return new ForwardingMessageHandler(this.handlers, this);
31+
return new CookieDelegatingHandler(new ForwardingMessageHandler(this.handlers, this), this.CookieContainer);
3132
}
3233

3334
public HttpClient CreateHttpClient(HttpMessageHandler handler = null) {

src/DotNetOpenAuth.Test/OAuth2/WebServerClientAuthorizeTests.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,22 @@ public async Task AuthorizationCodeGrant() {
3838
return await server.HandleTokenRequestAsync(req, ct);
3939
});
4040

41-
var client = new WebServerClient(AuthorizationServerDescription);
41+
var client = new WebServerClient(AuthorizationServerDescription, ClientId, ClientSecret, this.HostFactories);
4242
var authState = new AuthorizationState(TestScopes) {
4343
Callback = ClientCallback,
4444
};
4545
var authRequestRedirect = await client.PrepareRequestUserAuthorizationAsync(authState);
46+
this.HostFactories.CookieContainer.SetCookies(authRequestRedirect, ClientCallback);
4647
Uri authRequestResponse;
4748
using (var httpClient = this.HostFactories.CreateHttpClient()) {
4849
using (var httpResponse = await httpClient.GetAsync(authRequestRedirect.Headers.Location)) {
4950
authRequestResponse = httpResponse.Headers.Location;
5051
}
5152
}
5253

53-
var result = await client.ProcessUserAuthorizationAsync(new HttpRequestMessage(HttpMethod.Get, authRequestResponse), CancellationToken.None);
54+
var authorizationResponse = new HttpRequestMessage(HttpMethod.Get, authRequestResponse);
55+
this.HostFactories.CookieContainer.ApplyCookies(authorizationResponse);
56+
var result = await client.ProcessUserAuthorizationAsync(authorizationResponse, CancellationToken.None);
5457
Assert.That(result.AccessToken, Is.Not.Null.And.Not.Empty);
5558
Assert.That(result.RefreshToken, Is.Not.Null.And.Not.Empty);
5659
}

src/DotNetOpenAuth.Test/OpenId/RelyingParty/OpenIdRelyingPartyTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public async Task AssertionWithEndpointFilter() {
112112
var opStore = new StandardProviderApplicationStore();
113113
Handle(RPRealmUri).By(
114114
async req => {
115-
var rp = new OpenIdRelyingParty(new StandardRelyingPartyApplicationStore());
115+
var rp = new OpenIdRelyingParty(new StandardRelyingPartyApplicationStore(), this.HostFactories);
116116

117117
// Rig it to always deny the incoming OP
118118
rp.EndpointFilter = op => false;
@@ -124,7 +124,7 @@ public async Task AssertionWithEndpointFilter() {
124124
});
125125
this.RegisterAutoProvider();
126126
{
127-
var op = new OpenIdProvider(opStore);
127+
var op = new OpenIdProvider(opStore, this.HostFactories);
128128
Identifier id = GetMockIdentifier(ProtocolVersion.V20);
129129
var assertion = await op.PrepareUnsolicitedAssertionAsync(OPUri, GetMockRealm(false), id, id);
130130
using (var httpClient = this.HostFactories.CreateHttpClient()) {

0 commit comments

Comments
 (0)