5
5
using System . Security . Claims ;
6
6
using System . Threading . Tasks ;
7
7
using Microsoft . AspNetCore . Http ;
8
- using Microsoft . Extensions . Options ;
9
8
using Microsoft . IdentityModel . Tokens ;
10
9
11
10
/// <summary>
12
11
/// Encapsulates checks of bearer tokens in HTTP request headers.
13
12
/// </summary>
14
- internal class ApiAuthenticationService : IApiAuthentication
13
+ internal class AuthenticationService : IAuthenticationService
15
14
{
15
+ private readonly TokenValidationParameters _tokenValidationParameters ;
16
16
private readonly IAuthorizationHeaderBearerTokenExtractor _authorizationHeaderBearerTokenExractor ;
17
-
18
17
private readonly IJwtSecurityTokenHandlerWrapper _jwtSecurityTokenHandlerWrapper ;
19
-
20
- private readonly IOidcConfigurationManager _oidcConfigurationManager ;
21
-
22
- private readonly string _issuerUrl ;
23
- private readonly string _issuer ;
24
- private readonly string _audience ;
25
-
26
- private readonly string _nameClaimType ;
27
- private readonly string _roleClaimType ;
28
-
29
- public ApiAuthenticationService (
30
- IOptions < OidcApiAuthSettings > apiAuthorizationSettingsOptions ,
18
+ private readonly IOpenIdConnectConfigurationManager _openIdConnectConfigurationManager ;
19
+
20
+ public AuthenticationService (
21
+ TokenValidationParameters tokenValidationParameters ,
31
22
IAuthorizationHeaderBearerTokenExtractor authorizationHeaderBearerTokenExractor ,
32
23
IJwtSecurityTokenHandlerWrapper jwtSecurityTokenHandlerWrapper ,
33
- IOidcConfigurationManager oidcConfigurationManager )
24
+ IOpenIdConnectConfigurationManager openIdConnectConfigurationManager )
34
25
{
35
- _issuerUrl = apiAuthorizationSettingsOptions ? . Value ? . IssuerUrl ;
36
- _issuer = apiAuthorizationSettingsOptions ? . Value ? . Issuer ?? _issuerUrl ;
37
- _audience = apiAuthorizationSettingsOptions ? . Value ? . Audience ;
38
- _nameClaimType = apiAuthorizationSettingsOptions ? . Value ? . NameClaimType ?? "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier" ;
39
- _roleClaimType = apiAuthorizationSettingsOptions ? . Value ? . RoleClaimType ?? "http://schemas.microsoft.com/ws/2008/06/identity/claims/roleidentifier" ;
40
-
26
+ _tokenValidationParameters = tokenValidationParameters ;
41
27
_authorizationHeaderBearerTokenExractor = authorizationHeaderBearerTokenExractor ;
42
28
43
29
_jwtSecurityTokenHandlerWrapper = jwtSecurityTokenHandlerWrapper ;
44
30
45
- _oidcConfigurationManager = oidcConfigurationManager ;
31
+ _openIdConnectConfigurationManager = openIdConnectConfigurationManager ;
46
32
}
47
33
48
34
/// <summary>
@@ -57,12 +43,11 @@ public ApiAuthenticationService(
57
43
public async Task < ApiAuthenticationResult > AuthenticateAsync (
58
44
IHeaderDictionary httpRequestHeaders )
59
45
{
60
- bool isTokenValid = false ;
46
+ var isTokenValid = false ;
61
47
ClaimsPrincipal principal = new ClaimsPrincipal ( ) ;
62
48
63
- string authorizationBearerToken = _authorizationHeaderBearerTokenExractor . GetToken (
64
- httpRequestHeaders ) ;
65
- if ( authorizationBearerToken == null )
49
+ var bearerToken = _authorizationHeaderBearerTokenExractor . GetToken ( httpRequestHeaders ) ;
50
+ if ( bearerToken == null )
66
51
{
67
52
return new ApiAuthenticationResult ( principal ,
68
53
"Authorization header is missing, invalid format, or is not a Bearer token." ) ;
@@ -80,7 +65,7 @@ public async Task<ApiAuthenticationResult> AuthenticateAsync(
80
65
// then a fresh set of signing keys are retrieved from the OpenID Connect provider
81
66
// (issuer) cached and returned.
82
67
// This method will throw if the configuration cannot be retrieved, instead of returning null.
83
- isserSigningKeys = await _oidcConfigurationManager . GetIssuerSigningKeysAsync ( ) ;
68
+ isserSigningKeys = await _openIdConnectConfigurationManager . GetIssuerSigningKeysAsync ( ) ;
84
69
}
85
70
catch ( Exception ex )
86
71
{
@@ -92,25 +77,12 @@ public async Task<ApiAuthenticationResult> AuthenticateAsync(
92
77
try
93
78
{
94
79
// Try to validate the token.
80
+
95
81
96
- var tokenValidationParameters = new TokenValidationParameters
97
- {
98
- RequireSignedTokens = true ,
99
- ValidAudience = _audience ,
100
- ValidateAudience = true ,
101
- ValidIssuer = _issuer ,
102
- ValidateIssuer = true ,
103
- ValidateIssuerSigningKey = true ,
104
- ValidateLifetime = true ,
105
- IssuerSigningKeys = isserSigningKeys ,
106
- NameClaimType = _nameClaimType ,
107
- RoleClaimType = _roleClaimType
108
- } ;
82
+ _tokenValidationParameters . IssuerSigningKeys = isserSigningKeys ;
109
83
110
84
// Throws if the the token cannot be validated.
111
- principal = _jwtSecurityTokenHandlerWrapper . ValidateToken (
112
- authorizationBearerToken ,
113
- tokenValidationParameters ) ;
85
+ principal = _jwtSecurityTokenHandlerWrapper . ValidateToken ( bearerToken , _tokenValidationParameters ) ;
114
86
115
87
isTokenValid = true ;
116
88
}
@@ -124,7 +96,7 @@ public async Task<ApiAuthenticationResult> AuthenticateAsync(
124
96
// Then we retry by asking for the signing keys and validating the token again.
125
97
// We only retry once.
126
98
127
- _oidcConfigurationManager . RequestRefresh ( ) ;
99
+ _openIdConnectConfigurationManager . RequestRefresh ( ) ;
128
100
129
101
validationRetryCount ++ ;
130
102
}
0 commit comments