This repository was archived by the owner on Dec 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 591
Save tokens in auth properties instead of claims #698
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f0b16e5
Save tokens in auth properties instead of claims
HaoK 66c9882
Update samples and rename property
HaoK 1fc8d35
Switch to extension methods
HaoK 8973833
Update rest of the auth middleware add tests
HaoK f914567
PR feedback
HaoK 73614d1
Move SaveTokens into Remote/JwtBearerOptions
HaoK 1d984be
Switch to token api for OIDC IdTokenHint
HaoK 7809f0a
Rename Bearer token name to access_token
HaoK 5309445
Remove review comment
HaoK 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,9 +105,7 @@ protected override async Task HandleSignOutAsync(SignOutContext signout) | |
message.PostLogoutRedirectUri = logoutRedirectUri; | ||
} | ||
|
||
var principal = await Context.Authentication.AuthenticateAsync(Options.SignInScheme); | ||
message.IdTokenHint = principal?.FindFirst(OpenIdConnectParameterNames.IdToken)?.Value; | ||
|
||
message.IdTokenHint = await Context.Authentication.GetTokenAsync(OpenIdConnectParameterNames.IdToken); | ||
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. Nope, it's not required. |
||
var redirectContext = new RedirectContext(Context, Options, properties) | ||
{ | ||
ProtocolMessage = message | ||
|
@@ -513,9 +511,9 @@ protected override async Task<AuthenticateResult> HandleRemoteAuthenticateAsync( | |
tokenEndpointResponse = authenticationValidatedContext.TokenEndpointResponse; | ||
ticket = authenticationValidatedContext.Ticket; | ||
|
||
if (Options.SaveTokensAsClaims) | ||
if (Options.SaveTokens) | ||
{ | ||
SaveTokens(ticket.Principal, tokenEndpointResponse ?? authorizationResponse, jwt.Issuer); | ||
SaveTokens(ticket.Properties, tokenEndpointResponse ?? authorizationResponse); | ||
} | ||
|
||
if (Options.GetClaimsFromUserInfoEndpoint) | ||
|
@@ -693,32 +691,28 @@ protected virtual async Task<AuthenticateResult> GetUserInformationAsync(OpenIdC | |
/// </summary> | ||
/// <param name="principal">The principal in which tokens are saved.</param> | ||
/// <param name="message">The OpenID Connect response.</param> | ||
private void SaveTokens(ClaimsPrincipal principal, OpenIdConnectMessage message, string issuer) | ||
private void SaveTokens(AuthenticationProperties properties, OpenIdConnectMessage message) | ||
{ | ||
var identity = (ClaimsIdentity)principal.Identity; | ||
var tokens = new List<AuthenticationToken>(); | ||
|
||
if (!string.IsNullOrEmpty(message.AccessToken)) | ||
{ | ||
identity.AddClaim(new Claim(OpenIdConnectParameterNames.AccessToken, message.AccessToken, | ||
ClaimValueTypes.String, issuer)); | ||
tokens.Add(new AuthenticationToken { Name = OpenIdConnectParameterNames.AccessToken, Value = message.AccessToken }); | ||
} | ||
|
||
if (!string.IsNullOrEmpty(message.IdToken)) | ||
{ | ||
identity.AddClaim(new Claim(OpenIdConnectParameterNames.IdToken, message.IdToken, | ||
ClaimValueTypes.String, issuer)); | ||
tokens.Add(new AuthenticationToken { Name = OpenIdConnectParameterNames.IdToken, Value = message.IdToken }); | ||
} | ||
|
||
if (!string.IsNullOrEmpty(message.RefreshToken)) | ||
{ | ||
identity.AddClaim(new Claim(OpenIdConnectParameterNames.RefreshToken, message.RefreshToken, | ||
ClaimValueTypes.String, issuer)); | ||
tokens.Add(new AuthenticationToken { Name = OpenIdConnectParameterNames.RefreshToken, Value = message.RefreshToken }); | ||
} | ||
|
||
if (!string.IsNullOrEmpty(message.TokenType)) | ||
{ | ||
identity.AddClaim(new Claim(OpenIdConnectParameterNames.TokenType, message.TokenType, | ||
ClaimValueTypes.String, issuer)); | ||
tokens.Add(new AuthenticationToken { Name = OpenIdConnectParameterNames.TokenType, Value = message.TokenType }); | ||
} | ||
|
||
if (!string.IsNullOrEmpty(message.ExpiresIn)) | ||
|
@@ -729,8 +723,7 @@ private void SaveTokens(ClaimsPrincipal principal, OpenIdConnectMessage message, | |
var expiresAt = Options.SystemClock.UtcNow + TimeSpan.FromSeconds(value); | ||
// https://www.w3.org/TR/xmlschema-2/#dateTime | ||
// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx | ||
identity.AddClaim(new Claim("expires_at", expiresAt.ToString("o", CultureInfo.InvariantCulture), | ||
ClaimValueTypes.DateTime, issuer)); | ||
tokens.Add(new AuthenticationToken { Name = "expires_at", Value = expiresAt.ToString("o", CultureInfo.InvariantCulture) }); | ||
} | ||
} | ||
} | ||
|
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
11 changes: 11 additions & 0 deletions
11
src/Microsoft.AspNetCore.Authentication/AuthenticationToken.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,11 @@ | ||
// 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.AspNetCore.Authentication | ||
{ | ||
public class AuthenticationToken | ||
{ | ||
public string Name { get; set; } | ||
public string Value { get; set; } | ||
} | ||
} |
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
Oops, something went wrong.
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.
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.
So, no plan to make it a property on
AuthenticationToken
?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.
Doesn't sound like you've convinced @Tratcher and it definitely makes things more complicated, so my default is no for now as well, you can file an issue to consider adding it later
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.
Okay, will do.