-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Update JwtBearer and WsFederation to use updated IdentityModel #48966
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
Changes from all commits
507d241
47ffe32
4153b06
7fa2f96
aca45dd
0402406
df64e72
e63b5ce
4501b82
583728e
65595cb
f22eba9
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 | ||||
---|---|---|---|---|---|---|
|
@@ -5,6 +5,7 @@ | |||||
using System.Net.Http; | ||||||
using Microsoft.IdentityModel.Protocols; | ||||||
using Microsoft.IdentityModel.Protocols.OpenIdConnect; | ||||||
using Microsoft.IdentityModel.JsonWebTokens; | ||||||
using Microsoft.IdentityModel.Tokens; | ||||||
|
||||||
namespace Microsoft.AspNetCore.Authentication.JwtBearer; | ||||||
|
@@ -15,13 +16,22 @@ namespace Microsoft.AspNetCore.Authentication.JwtBearer; | |||||
public class JwtBearerOptions : AuthenticationSchemeOptions | ||||||
{ | ||||||
private readonly JwtSecurityTokenHandler _defaultHandler = new JwtSecurityTokenHandler(); | ||||||
private readonly JsonWebTokenHandler _defaultTokenHandler = new JsonWebTokenHandler | ||||||
{ | ||||||
MapInboundClaims = JwtSecurityTokenHandler.DefaultMapInboundClaims | ||||||
}; | ||||||
|
||||||
private bool _mapInboundClaims = JwtSecurityTokenHandler.DefaultMapInboundClaims; | ||||||
|
||||||
/// <summary> | ||||||
/// Initializes a new instance of <see cref="JwtBearerOptions"/>. | ||||||
/// </summary> | ||||||
public JwtBearerOptions() | ||||||
{ | ||||||
#pragma warning disable CS0618 // Type or member is obsolete | ||||||
SecurityTokenValidators = new List<ISecurityTokenValidator> { _defaultHandler }; | ||||||
#pragma warning restore CS0618 // Type or member is obsolete | ||||||
TokenHandlers = new List<TokenHandler> { _defaultTokenHandler }; | ||||||
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. @kevinchalet i am thinking that creating the interface ITokenValidator that has a couple of methods instead of the abstract class TokenHandler, this would make it easier for users who currently have an implementation of ISecurityTokenValidator. Thoughts? 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.
IM already has WIF only had 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. @kevinchalet OK let's stick with TokenHandler then as this was meant to be the replacement for SecurityTokenHandler. 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. @kevinchalet we could implement ISecurityTokenValidator on JsonWebTokenHandler, but just because we can cast it to TokenHandler doesn't mean that users will not have a runtime break as the SecurityToken will change from JwtSecurityToken to JsonWebToken. 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. Sure, but that would mean an instance of 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. I'm hoping to make it opt-out. 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. |
||||||
} | ||||||
|
||||||
/// <summary> | ||||||
|
@@ -103,8 +113,14 @@ public JwtBearerOptions() | |||||
/// <summary> | ||||||
/// Gets the ordered list of <see cref="ISecurityTokenValidator"/> used to validate access tokens. | ||||||
/// </summary> | ||||||
[Obsolete("SecurityTokenValidators is no longer used by default. Use TokenHandlers instead. To continue using SecurityTokenValidators, set UseSecurityTokenValidators to true. See https://aka.ms/aspnetcore8/security-token-changes")] | ||||||
public IList<ISecurityTokenValidator> SecurityTokenValidators { get; private set; } | ||||||
|
||||||
/// <summary> | ||||||
/// Gets the ordered list of <see cref="TokenHandler"/> used to validate access tokens. | ||||||
/// </summary> | ||||||
public IList<TokenHandler> TokenHandlers { get; private set; } | ||||||
|
||||||
/// <summary> | ||||||
/// Gets or sets the parameters used to validate identity tokens. | ||||||
/// </summary> | ||||||
|
@@ -126,15 +142,20 @@ public JwtBearerOptions() | |||||
public bool IncludeErrorDetails { get; set; } = true; | ||||||
|
||||||
/// <summary> | ||||||
/// Gets or sets the <see cref="MapInboundClaims"/> property on the default instance of <see cref="JwtSecurityTokenHandler"/> in SecurityTokenValidators, which is used when determining | ||||||
/// whether or not to map claim types that are extracted when validating a <see cref="JwtSecurityToken"/>. | ||||||
/// Gets or sets the <see cref="MapInboundClaims"/> property on the default instance of <see cref="JwtSecurityTokenHandler"/> in SecurityTokenValidators, or <see cref="JsonWebTokenHandler"/> in TokenHandlers which is used when determining | ||||||
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.
Suggested change
|
||||||
/// whether or not to map claim types that are extracted when validating a <see cref="JwtSecurityToken"/> or a <see cref="JsonWebToken"/>. | ||||||
/// <para>If this is set to true, the Claim Type is set to the JSON claim 'name' after translating using this mapping. Otherwise, no mapping occurs.</para> | ||||||
/// <para>The default value is true.</para> | ||||||
/// </summary> | ||||||
public bool MapInboundClaims | ||||||
{ | ||||||
get => _defaultHandler.MapInboundClaims; | ||||||
set => _defaultHandler.MapInboundClaims = value; | ||||||
get => _mapInboundClaims; | ||||||
set | ||||||
{ | ||||||
_mapInboundClaims = value; | ||||||
_defaultHandler.MapInboundClaims = value; | ||||||
_defaultTokenHandler.MapInboundClaims = value; | ||||||
} | ||||||
} | ||||||
|
||||||
/// <summary> | ||||||
|
@@ -152,4 +173,15 @@ public bool MapInboundClaims | |||||
/// Defaults to <see cref="ConfigurationManager{OpenIdConnectConfiguration}.DefaultRefreshInterval" />. | ||||||
/// </value> | ||||||
public TimeSpan RefreshInterval { get; set; } = ConfigurationManager<OpenIdConnectConfiguration>.DefaultRefreshInterval; | ||||||
|
||||||
/// <summary> | ||||||
/// Gets or sets whether <see cref="TokenHandlers"/> or <see cref="SecurityTokenValidators"/> will be used to validate the inbound token. | ||||||
/// </summary> | ||||||
/// <remarks> | ||||||
/// The advantage of using TokenHandlers are: | ||||||
/// <para>There is an Async model.</para> | ||||||
/// <para>The default token handler is a <see cref="JsonWebTokenHandler"/> which is faster than a <see cref="JwtSecurityTokenHandler"/>.</para> | ||||||
/// <para>There is an ability to make use of a Last-Known-Good model for metadata that protects applications when metadata is published with errors.</para> | ||||||
Comment on lines
+181
to
+184
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. I wonder if we should rethink these doc comments as they might not make full sense now that we've inverted the boolean to be Maybe we should add why someone would want to use SecurityTokenValidators as well. |
||||||
/// </remarks> | ||||||
public bool UseSecurityTokenValidators { get; set; } | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
#nullable enable | ||
Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.JwtBearerHandler(Microsoft.Extensions.Options.IOptionsMonitor<Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerOptions!>! options, Microsoft.Extensions.Logging.ILoggerFactory! logger, System.Text.Encodings.Web.UrlEncoder! encoder) -> void | ||
Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerOptions.TokenHandlers.get -> System.Collections.Generic.IList<Microsoft.IdentityModel.Tokens.TokenHandler!>! | ||
Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerOptions.UseSecurityTokenValidators.get -> bool | ||
Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerOptions.UseSecurityTokenValidators.set -> void |
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.
Previously we would cache the
_configuration
on the handler. However, we don't cache it anymore, and instead will always callGetConfigurationAsync
. Is this OK? Will it cause any sort of performance issue?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.
GetConfigurationAsync should do its own caching, and handlers are re-created per request anyways. It looks like this is the only place config is read?
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.
Correct, ConfigurationManager does its own caching