-
Notifications
You must be signed in to change notification settings - Fork 336
Backport ASP.NET Core PKCE Support to OpenIdConnectAuthenticationHandler #389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cb2d127
Add PKCE support to OpenIdConnect Middleware
rzontar c8f4ec8
OpenIdConnectMiddlewareTests
rzontar 1c56e38
Test PKCE implementation in sandbox
rzontar 44ca315
UsePkce defaults to true
rzontar a082946
Condense newlines.
rzontar 079efc0
Update src/Microsoft.Owin.Security.OpenIdConnect/OpenIdConnectAuthent…
Tratcher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/Microsoft.Owin.Security.OpenIdConnect/OAuthConstants.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.Owin.Security.OpenIdConnect | ||
{ | ||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Auth", | ||
Justification = "OAuth2 is a valid word.")] | ||
internal static class OAuthConstants | ||
{ | ||
/// <summary> | ||
/// code_verifier defined in https://tools.ietf.org/html/rfc7636 | ||
/// </summary> | ||
public const string CodeVerifierKey = "code_verifier"; | ||
|
||
/// <summary> | ||
/// code_challenge defined in https://tools.ietf.org/html/rfc7636 | ||
/// </summary> | ||
public const string CodeChallengeKey = "code_challenge"; | ||
|
||
/// <summary> | ||
/// code_challenge_method defined in https://tools.ietf.org/html/rfc7636 | ||
/// </summary> | ||
public const string CodeChallengeMethodKey = "code_challenge_method"; | ||
|
||
/// <summary> | ||
/// S256 defined in https://tools.ietf.org/html/rfc7636 | ||
/// </summary> | ||
public const string CodeChallengeMethodS256 = "S256"; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 182 additions & 0 deletions
182
tests/Microsoft.Owin.Security.Tests/OpenIdConnect/OpenIdConnectMiddlewareTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using System.Xml.Linq; | ||
using Microsoft.IdentityModel.Protocols.OpenIdConnect; | ||
using Microsoft.Owin.Security.Cookies; | ||
using Microsoft.Owin.Security.OpenIdConnect; | ||
using Microsoft.Owin.Testing; | ||
using Owin; | ||
using Xunit; | ||
using Xunit.Extensions; | ||
|
||
namespace Microsoft.Owin.Security.Tests.OpenIdConnect | ||
{ | ||
public class OpenIdConnectMiddlewareTests | ||
{ | ||
[Theory] | ||
[InlineData(true)] | ||
[InlineData(false)] | ||
public async Task ChallengeIncludesPkceIfRequested(bool include) | ||
{ | ||
var options = new OpenIdConnectAuthenticationOptions() | ||
{ | ||
Authority = "https://demo.identityserver.io", | ||
ClientId = "Test Client Id", | ||
ClientSecret = "Test Client Secret", | ||
UsePkce = include, | ||
ResponseType = OpenIdConnectResponseType.Code | ||
}; | ||
var server = CreateServer( | ||
app => app.UseOpenIdConnectAuthentication(options), | ||
context => | ||
{ | ||
context.Authentication.Challenge("OpenIdConnect"); | ||
return true; | ||
}); | ||
|
||
var transaction = await SendAsync(server, "http://example.com/challenge"); | ||
|
||
var res = transaction.Response; | ||
Assert.Equal(HttpStatusCode.Redirect, res.StatusCode); | ||
Assert.NotNull(res.Headers.Location); | ||
|
||
if (include) | ||
{ | ||
Assert.Contains("code_challenge=", res.Headers.Location.Query); | ||
Assert.Contains("code_challenge_method=S256", res.Headers.Location.Query); | ||
} | ||
else | ||
{ | ||
Assert.DoesNotContain("code_challenge=", res.Headers.Location.Query); | ||
Assert.DoesNotContain("code_challenge_method=", res.Headers.Location.Query); | ||
} | ||
} | ||
|
||
[Theory] | ||
[InlineData(OpenIdConnectResponseType.Token)] | ||
[InlineData(OpenIdConnectResponseType.IdToken)] | ||
[InlineData(OpenIdConnectResponseType.CodeIdToken)] | ||
public async Task ChallengeDoesNotIncludePkceForOtherResponseTypes(string responseType) | ||
{ | ||
var options = new OpenIdConnectAuthenticationOptions() | ||
{ | ||
Authority = "https://demo.identityserver.io", | ||
ClientId = "Test Client Id", | ||
ClientSecret = "Test Client Secret", | ||
UsePkce = true, | ||
ResponseType = responseType | ||
}; | ||
var server = CreateServer( | ||
app => app.UseOpenIdConnectAuthentication(options), | ||
context => | ||
{ | ||
context.Authentication.Challenge("OpenIdConnect"); | ||
return true; | ||
}); | ||
|
||
var transaction = await SendAsync(server, "http://example.com/challenge"); | ||
|
||
var res = transaction.Response; | ||
Assert.Equal(HttpStatusCode.Redirect, res.StatusCode); | ||
Assert.NotNull(res.Headers.Location); | ||
|
||
Assert.DoesNotContain("code_challenge=", res.Headers.Location.Query); | ||
Assert.DoesNotContain("code_challenge_method=", res.Headers.Location.Query); | ||
} | ||
|
||
|
||
private static TestServer CreateServer(Action<IAppBuilder> configure, Func<IOwinContext, bool> handler) | ||
{ | ||
return TestServer.Create(app => | ||
{ | ||
app.Properties["host.AppName"] = "OpenIdConnect.Owin.Security.Tests"; | ||
app.UseCookieAuthentication(new CookieAuthenticationOptions | ||
{ | ||
AuthenticationType = "External" | ||
}); | ||
app.SetDefaultSignInAsAuthenticationType("External"); | ||
if (configure != null) | ||
{ | ||
configure(app); | ||
} | ||
app.Use(async (context, next) => | ||
{ | ||
if (handler == null || !handler(context)) | ||
{ | ||
await next(); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
private static async Task<Transaction> SendAsync(TestServer server, string uri, string cookieHeader = null) | ||
{ | ||
var request = new HttpRequestMessage(HttpMethod.Get, uri); | ||
if (!string.IsNullOrEmpty(cookieHeader)) | ||
{ | ||
request.Headers.Add("Cookie", cookieHeader); | ||
} | ||
var transaction = new Transaction | ||
{ | ||
Request = request, | ||
Response = await server.HttpClient.SendAsync(request), | ||
}; | ||
if (transaction.Response.Headers.Contains("Set-Cookie")) | ||
{ | ||
transaction.SetCookie = transaction.Response.Headers.GetValues("Set-Cookie").ToList(); | ||
} | ||
transaction.ResponseText = await transaction.Response.Content.ReadAsStringAsync(); | ||
|
||
if (transaction.Response.Content != null && | ||
transaction.Response.Content.Headers.ContentType != null && | ||
transaction.Response.Content.Headers.ContentType.MediaType == "text/xml") | ||
{ | ||
transaction.ResponseElement = XElement.Parse(transaction.ResponseText); | ||
} | ||
return transaction; | ||
} | ||
|
||
private class Transaction | ||
{ | ||
public HttpRequestMessage Request { get; set; } | ||
public HttpResponseMessage Response { get; set; } | ||
public IList<string> SetCookie { get; set; } | ||
public string ResponseText { get; set; } | ||
public XElement ResponseElement { get; set; } | ||
|
||
public string AuthenticationCookieValue | ||
{ | ||
get | ||
{ | ||
if (SetCookie != null && SetCookie.Count > 0) | ||
{ | ||
var authCookie = SetCookie.SingleOrDefault(c => c.Contains(".AspNet.External=")); | ||
if (authCookie != null) | ||
{ | ||
return authCookie.Substring(0, authCookie.IndexOf(';')); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
|
||
public string FindClaimValue(string claimType) | ||
{ | ||
XElement claim = ResponseElement.Elements("claim").SingleOrDefault(elt => elt.Attribute("type").Value == claimType); | ||
if (claim == null) | ||
{ | ||
return null; | ||
} | ||
return claim.Attribute("value").Value; | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.