Skip to content

Commit d9bf9d3

Browse files
author
Keegan Caruso
committed
Update Wilson7 branch
Default to using new handlers Changes from API review
1 parent 65595cb commit d9bf9d3

12 files changed

+1827
-28
lines changed

src/Security/Authentication/JwtBearer/src/JwtBearerHandler.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using Microsoft.AspNetCore.Http;
1010
using Microsoft.Extensions.Logging;
1111
using Microsoft.Extensions.Options;
12-
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
1312
using Microsoft.IdentityModel.Tokens;
1413
using Microsoft.Net.Http.Headers;
1514

@@ -20,8 +19,6 @@ namespace Microsoft.AspNetCore.Authentication.JwtBearer;
2019
/// </summary>
2120
public class JwtBearerHandler : AuthenticationHandler<JwtBearerOptions>
2221
{
23-
private OpenIdConnectConfiguration? _configuration;
24-
2522
/// <summary>
2623
/// Initializes a new instance of <see cref="JwtBearerHandler"/>.
2724
/// </summary>
@@ -101,7 +98,7 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
10198
SecurityToken? validatedToken = null;
10299
ClaimsPrincipal? principal = null;
103100

104-
if (Options.UseTokenHandlers)
101+
if (!Options.UseSecurityTokenValidators)
105102
{
106103
foreach (var tokenHandler in Options.TokenHandlers)
107104
{
@@ -123,7 +120,7 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
123120
catch (Exception ex)
124121
{
125122
validationFailures ??= new List<Exception>(1);
126-
RecordTokenValidationError(new SecurityTokenValidationException($"TokenHandler: '{tokenHandler}', threw an exception (see inner exception).", ex), validationFailures);
123+
RecordTokenValidationError(ex, validationFailures);
127124
}
128125
}
129126
}
@@ -194,7 +191,7 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
194191
return AuthenticateResult.Fail(authenticationFailedContext.Exception);
195192
}
196193

197-
if (Options.UseTokenHandlers)
194+
if (!Options.UseSecurityTokenValidators)
198195
{
199196
return AuthenticateResults.TokenHandlerUnableToValidate;
200197
}
@@ -251,10 +248,10 @@ private async Task<TokenValidationParameters> SetupTokenValidationParametersAsyn
251248
if (Options.ConfigurationManager != null)
252249
{
253250
// GetConfigurationAsync has a time interval that must pass before new http request will be issued.
254-
_configuration = await Options.ConfigurationManager.GetConfigurationAsync(Context.RequestAborted);
255-
var issuers = new[] { _configuration.Issuer };
251+
var configuration = await Options.ConfigurationManager.GetConfigurationAsync(Context.RequestAborted);
252+
var issuers = new[] { configuration.Issuer };
256253
tokenValidationParameters.ValidIssuers = (tokenValidationParameters.ValidIssuers == null ? issuers : tokenValidationParameters.ValidIssuers.Concat(issuers));
257-
tokenValidationParameters.IssuerSigningKeys = (tokenValidationParameters.IssuerSigningKeys == null ? _configuration.SigningKeys : tokenValidationParameters.IssuerSigningKeys.Concat(_configuration.SigningKeys));
254+
tokenValidationParameters.IssuerSigningKeys = (tokenValidationParameters.IssuerSigningKeys == null ? configuration.SigningKeys : tokenValidationParameters.IssuerSigningKeys.Concat(configuration.SigningKeys));
258255
}
259256
}
260257

src/Security/Authentication/JwtBearer/src/JwtBearerOptions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ public JwtBearerOptions()
111111
/// <summary>
112112
/// Gets the ordered list of <see cref="ISecurityTokenValidator"/> used to validate access tokens.
113113
/// </summary>
114+
[Obsolete("SecurityTokenValidators is no longer used by default. Use TokenHandlers instead. To continue using SecurityTokenValidators, set UseSecurityTokenValidators to true.")]
114115
public IList<ISecurityTokenValidator> SecurityTokenValidators { get; private set; }
115116

116117
/// <summary>
@@ -180,5 +181,5 @@ public bool MapInboundClaims
180181
/// <para>The default token handler is a <see cref="JsonWebTokenHandler"/> which is faster than a <see cref="JwtSecurityTokenHandler"/>.</para>
181182
/// <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>
182183
/// </remarks>
183-
public bool UseTokenHandlers { get; set; }
184+
public bool UseSecurityTokenValidators { get; set; }
184185
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#nullable enable
22
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
33
Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerOptions.TokenHandlers.get -> System.Collections.Generic.IList<Microsoft.IdentityModel.Tokens.TokenHandler!>!
4-
Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerOptions.UseTokenHandlers.get -> bool
5-
Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerOptions.UseTokenHandlers.set -> void
4+
Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerOptions.UseSecurityTokenValidators.get -> bool
5+
Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerOptions.UseSecurityTokenValidators.set -> void
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#nullable enable
22
Microsoft.AspNetCore.Authentication.WsFederation.WsFederationHandler.WsFederationHandler(Microsoft.Extensions.Options.IOptionsMonitor<Microsoft.AspNetCore.Authentication.WsFederation.WsFederationOptions!>! options, Microsoft.Extensions.Logging.ILoggerFactory! logger, System.Text.Encodings.Web.UrlEncoder! encoder) -> void
33
Microsoft.AspNetCore.Authentication.WsFederation.WsFederationOptions.TokenHandlers.get -> System.Collections.Generic.ICollection<Microsoft.IdentityModel.Tokens.TokenHandler!>!
4-
Microsoft.AspNetCore.Authentication.WsFederation.WsFederationOptions.UseTokenHandlers.get -> bool
5-
Microsoft.AspNetCore.Authentication.WsFederation.WsFederationOptions.UseTokenHandlers.set -> void
4+
Microsoft.AspNetCore.Authentication.WsFederation.WsFederationOptions.UseSecurityTokenHandlers.get -> bool
5+
Microsoft.AspNetCore.Authentication.WsFederation.WsFederationOptions.UseSecurityTokenHandlers.set -> void

src/Security/Authentication/WsFederation/src/Resources.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
<value>The service descriptor is missing.</value>
122122
</data>
123123
<data name="Exception_NoTokenValidatorFound" xml:space="preserve">
124-
<value>No token validator was found for the given token.</value>
124+
<value>No token validator or token handler was found for the given token.</value>
125125
</data>
126126
<data name="Exception_OptionMustBeProvided" xml:space="preserve">
127127
<value>The '{0}' option must be provided.</value>

src/Security/Authentication/WsFederation/src/WsFederationHandler.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ protected override async Task<HandleRequestResult> HandleRemoteAuthenticateAsync
245245
var tvp = await SetupTokenValidationParametersAsync();
246246
ClaimsPrincipal? principal = null;
247247
SecurityToken? validatedToken = null;
248-
if (Options.UseTokenHandlers)
248+
if (!Options.UseSecurityTokenHandlers)
249249
{
250250
foreach (var tokenHandler in Options.TokenHandlers)
251251
{
@@ -298,7 +298,6 @@ protected override async Task<HandleRequestResult> HandleRemoteAuthenticateAsync
298298

299299
if (principal == null)
300300
{
301-
// TODO - need new string for TokenHandler
302301
if (validationFailures == null || validationFailures.Count == 0)
303302
{
304303
throw new SecurityTokenException(Resources.Exception_NoTokenValidatorFound);
@@ -375,7 +374,6 @@ private async Task<TokenValidationParameters> SetupTokenValidationParametersAsyn
375374

376375
if (Options.ConfigurationManager is BaseConfigurationManager baseConfigurationManager)
377376
{
378-
// TODO - we need to add a parameter to TokenValidationParameters for the CancellationToken.
379377
tokenValidationParameters.ConfigurationManager = baseConfigurationManager;
380378
}
381379
else

src/Security/Authentication/WsFederation/src/WsFederationOptions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public override void Validate()
105105
/// <summary>
106106
/// Gets or sets the collection of <see cref="ISecurityTokenValidator"/> used to read and validate the <see cref="SecurityToken"/>s.
107107
/// </summary>
108+
[Obsolete("SecurityTokenHandlers is no longer used by default. Use TokenHandlers instead. To continue using SecurityTokenHandlers, set UseSecurityTokenHandlers to true.")]
108109
public ICollection<ISecurityTokenValidator> SecurityTokenHandlers
109110
{
110111
get
@@ -118,7 +119,7 @@ public ICollection<ISecurityTokenValidator> SecurityTokenHandlers
118119
}
119120

120121
/// <summary>
121-
/// Gets or sets the collection of <see cref="ISecurityTokenValidator"/> used to read and validate the <see cref="SecurityToken"/>s.
122+
/// Gets the collection of <see cref="ISecurityTokenValidator"/> used to read and validate the <see cref="SecurityToken"/>s.
122123
/// </summary>
123124
public ICollection<TokenHandler> TokenHandlers
124125
{
@@ -208,5 +209,5 @@ public TokenValidationParameters TokenValidationParameters
208209
/// <para>The default token handler for JsonWebTokens is a <see cref="JsonWebTokenHandler"/> which is faster than a <see cref="JwtSecurityTokenHandler"/>.</para>
209210
/// <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>
210211
/// </remarks>
211-
public bool UseTokenHandlers { get; set; }
212+
public bool UseSecurityTokenHandlers { get; set; }
212213
}

0 commit comments

Comments
 (0)