-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Add MapIdentityApi<TUser>() #47414
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
Add MapIdentityApi<TUser>() #47414
Conversation
src/Identity/Endpoints/src/IdentityBearerAuthenticationHandler.cs
Outdated
Show resolved
Hide resolved
src/Identity/Endpoints/src/IdentityEndpointServiceCollectionExtensions.cs
Outdated
Show resolved
Hide resolved
364c640
to
9411969
Compare
src/Identity/Endpoints/src/IdentityBearerAuthenticationHandler.cs
Outdated
Show resolved
Hide resolved
src/Identity/Endpoints/src/IdentityBearerAuthenticationHandler.cs
Outdated
Show resolved
Hide resolved
src/Identity/Endpoints/src/IdentityBearerAuthenticationHandler.cs
Outdated
Show resolved
Hide resolved
src/Identity/Endpoints/src/IdentityBearerAuthenticationHandler.cs
Outdated
Show resolved
Hide resolved
src/Identity/Endpoints/src/IdentityBearerAuthenticationHandler.cs
Outdated
Show resolved
Hide resolved
src/Identity/Endpoints/src/IdentityEndpointsServiceCollectionExtensions.cs
Outdated
Show resolved
Hide resolved
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
src/Identity/Endpoints/src/IdentityBearerAuthenticationHandler.cs
Outdated
Show resolved
Hide resolved
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.
Looks good to me
9411969
to
f32a1b4
Compare
f32a1b4
to
2ab66d5
Compare
2ab66d5
to
8bbab02
Compare
/backport to release/8.0-preview4 |
Started backporting to release/8.0-preview4: https://github.com/dotnet/aspnetcore/actions/runs/4812793273 |
@halter73 backporting to release/8.0-preview4 failed, the patch most likely resulted in conflicts: $ git am --3way --ignore-whitespace --keep-non-patch changes.patch
Applying: Add MapIdentityApi<TUser>()
.git/rebase-apply/patch:1330: new blank line at EOF.
+
warning: 1 line adds whitespace errors.
Using index info to reconstruct a base tree...
M src/Identity/Core/src/PublicAPI.Unshipped.txt
M src/Identity/Identity.slnf
M src/Security/Security.slnf
Falling back to patching base and 3-way merge...
Auto-merging src/Security/Security.slnf
Auto-merging src/Identity/Identity.slnf
CONFLICT (content): Merge conflict in src/Identity/Identity.slnf
Auto-merging src/Identity/Core/src/PublicAPI.Unshipped.txt
CONFLICT (content): Merge conflict in src/Identity/Core/src/PublicAPI.Unshipped.txt
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Patch failed at 0001 Add MapIdentityApi<TUser>()
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
Error: The process '/usr/bin/git' failed with exit code 128 Please backport manually! |
@halter73 an error occurred while backporting to release/8.0-preview4, please check the run log for details! Error: git am failed, most likely due to a merge conflict. |
public static class IdentityApiEndpointsIdentityBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Adds configuration ans services needed to support <see cref="IdentityApiEndpointRouteBuilderExtensions.MapIdentityApi{TUser}(IEndpointRouteBuilder)"/> |
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.
ans => and
#pragma warning restore IDE0060 // Remove unused parameter | ||
: SignInAuthenticationHandler<BearerTokenOptions>(optionsMonitor, loggerFactory, urlEncoder) | ||
{ | ||
private const string BearerTokenPurpose = $"Microsoft.AspNetCore.Authentication.BearerToken:v1:BearerToken"; |
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.
- $
|
||
var ticket = BearerTokenProtector.Unprotect(token); | ||
|
||
if (ticket?.Properties?.ExpiresUtc is null) |
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.
store ExpiresUtc in a variable, it's parsed on-demand.
get => GetDateTimeOffset(ExpiresUtcKey); |
|
||
if (properties.ExpiresUtc is null) | ||
{ | ||
properties.ExpiresUtc ??= utcNow + Options.BearerTokenExpiration; |
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.
- ??
|
||
properties ??= new(); | ||
|
||
if (properties.ExpiresUtc is null) |
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.
store locally
{ | ||
var authorization = Request.Headers.Authorization.ToString(); | ||
|
||
return authorization.StartsWith("Bearer ", StringComparison.Ordinal) |
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.
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.
This is new, and I like being strict until we see a client that cannot send "Bearer" with the standard casing.
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.
This is the kind of thing clients are guaranteed to get wrong sooner or later. There's no reason to be strict about it.
/// <summary> | ||
/// Specifies events which the bearer token handler invokes to enable developer control over the authentication process. | ||
/// </summary> | ||
public class BearerTokenEvents |
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.
There should also be an OnSignIn event that would let you take over the response generation, or at least mess with the identity before the token is created.
private static readonly AuthenticateResult FailedUnprotectingToken = AuthenticateResult.Fail("Unprotected token failed"); | ||
private static readonly AuthenticateResult TokenExpired = AuthenticateResult.Fail("Token expired"); | ||
|
||
private ISecureDataFormat<AuthenticationTicket> BearerTokenProtector |
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.
You can do this one in PostConfigure options to avoid creating it per request.
aspnetcore/src/Security/Authentication/Cookies/src/PostConfigureCookieAuthenticationOptions.cs
Lines 40 to 44 in 6d30638
if (options.TicketDataFormat == null) | |
{ | |
// Note: the purpose for the data protector must remain fixed for interop to work. | |
var dataProtector = options.DataProtectionProvider.CreateProtector("Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationMiddleware", name, "v2"); | |
options.TicketDataFormat = new TicketDataFormat(dataProtector); |
Addresses #47227