-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
OAuth - Creating event for ExchangeCode #9448
Changes from all commits
280f4a4
d3e3b81
1e60e82
fca8b60
0b57464
43e3c22
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// 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.Collections.Generic; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Microsoft.AspNetCore.Authentication.OAuth | ||
{ | ||
/// <summary> | ||
/// Contains information about the context of exchanging code for access token . | ||
/// </summary> | ||
public class OAuthExchangeCodeContext : PropertiesContext<OAuthOptions> | ||
{ | ||
/// <summary> | ||
/// Initializes a new <see cref="OAuthExchangeCodeContext"/>. | ||
/// </summary> | ||
/// <param name="properties">The <see cref="AuthenticationProperties"/>.</param> | ||
/// <param name="context">The HTTP environment.</param> | ||
/// <param name="scheme">The authentication scheme.</param> | ||
/// <param name="options">The options used by the authentication middleware.</param> | ||
/// <param name="tokenRequestParameters">The parameters that will be sent as query string for the token request</param> | ||
public OAuthExchangeCodeContext( | ||
AuthenticationProperties properties, | ||
HttpContext context, | ||
AuthenticationScheme scheme, | ||
OAuthOptions options, | ||
IDictionary<string, string> tokenRequestParameters) | ||
: base(context, scheme, options, properties) | ||
{ | ||
TokenRequestParameters = tokenRequestParameters; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the request parameters for the token request | ||
/// </summary> | ||
public IDictionary<string, string> TokenRequestParameters { get; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,7 +99,7 @@ protected override async Task<HandleRequestResult> HandleRemoteAuthenticateAsync | |
return HandleRequestResult.Fail("Code was not found.", properties); | ||
} | ||
|
||
using (var tokens = await ExchangeCodeAsync(code, BuildRedirectUri(Options.CallbackPath))) | ||
using (var tokens = await ExchangeCodeAsync(code, BuildRedirectUri(Options.CallbackPath), properties)) | ||
{ | ||
if (tokens.Error != null) | ||
{ | ||
|
@@ -159,7 +159,7 @@ protected override async Task<HandleRequestResult> HandleRemoteAuthenticateAsync | |
} | ||
} | ||
|
||
protected virtual async Task<OAuthTokenResponse> ExchangeCodeAsync(string code, string redirectUri) | ||
protected virtual async Task<OAuthTokenResponse> ExchangeCodeAsync(string code, string redirectUri, AuthenticationProperties properties) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a non-breaking way to add this new parameter? E.g. with a new overload? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Tratcher hi, I was trying to make this non-breaking but I really don't see how... An overload won't work because now it is called from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers/search?q=ExchangeCodeAsync&unscoped_q=ExchangeCodeAsync There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, the first one about simply augmenting the Dictionary, I totally agree with you and I already changed it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Tratcher is it possible to add a new API ExchangeCodeAsnyc(ExchangeCodeContext context)? This would future proof us a bit. We have used this in other places. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @brentschmaltz yes, that's plausible. Either way it's a breaking change for many providers. If we added PKCE support directly we might be able to avoid this break. #9448 (comment) |
||
{ | ||
var tokenRequestParameters = new Dictionary<string, string>() | ||
{ | ||
|
@@ -170,6 +170,9 @@ protected virtual async Task<OAuthTokenResponse> ExchangeCodeAsync(string code, | |
{ "grant_type", "authorization_code" }, | ||
}; | ||
|
||
var exchangeCodeContext = new OAuthExchangeCodeContext(properties, Context, Scheme, Options, tokenRequestParameters); | ||
await Events.OnExchangeCode(exchangeCodeContext); | ||
|
||
var requestContent = new FormUrlEncodedContent(tokenRequestParameters); | ||
|
||
var requestMessage = new HttpRequestMessage(HttpMethod.Post, Options.TokenEndpoint); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PrepareCodeExchange?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think BeforeExchangeCode is better, what you think?