| | | 1 | | using ArturRios.Jwt; |
| | | 2 | | using ArturRios.Util.WebApi.Security.Configuration; |
| | | 3 | | using ArturRios.Util.WebApi.Security.Enums; |
| | | 4 | | using ArturRios.Util.WebApi.Security.Interfaces; |
| | | 5 | | using ArturRios.Util.WebApi.Security.Records; |
| | | 6 | | using Microsoft.AspNetCore.Http; |
| | | 7 | | using Microsoft.Extensions.DependencyInjection; |
| | | 8 | | |
| | | 9 | | namespace ArturRios.Util.WebApi.Security.Authentication; |
| | | 10 | | |
| | | 11 | | /// <summary>Validates the app's own HMAC-signed JWT and resolves the user through the registered |
| | | 12 | | /// <see cref="IAuthenticatedUserMapper"/> — from claims alone or by an <see cref="IAuthenticationProvider"/> |
| | | 13 | | /// lookup, per <see cref="AuthenticationOptions.JwtMode"/>.</summary> |
| | | 14 | | /// <param name="jwtConfig">Provides the key material used to validate the token.</param> |
| | | 15 | | /// <param name="jwtHandler">Validates token signatures.</param> |
| | | 16 | | /// <param name="mapper">Interprets the token's claims as the app's user.</param> |
| | | 17 | | /// <param name="options">Controls how the user is resolved once the signature is valid.</param> |
| | 34 | 18 | | public class JwtTokenValidator( |
| | 34 | 19 | | JwtConfiguration jwtConfig, |
| | 34 | 20 | | JwtHandler jwtHandler, |
| | 34 | 21 | | IAuthenticatedUserMapper mapper, |
| | 34 | 22 | | AuthenticationOptions options) : ITokenValidator |
| | | 23 | | { |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | public async Task<TokenValidationResult> ValidateAsync(string token, HttpContext context) |
| | | 26 | | { |
| | | 27 | | // Every configured key when there are any, so a token signed with a key that is no longer |
| | | 28 | | // the signing key stays valid until it is withdrawn — which is what makes replacing a signing |
| | | 29 | | // secret a rotation rather than a cutover that invalidates every token in flight. Falls back |
| | | 30 | | // to the single secret when no keys are configured, which is what every configuration written |
| | | 31 | | // before ArturRios.Jwt 1.1.0 looks like. |
| | 27 | 32 | | var isValid = jwtConfig.Keys.Count > 0 |
| | 27 | 33 | | ? await jwtHandler.IsTokenValidAsync(token, jwtConfig.Keys) |
| | 27 | 34 | | : await jwtHandler.IsTokenValidAsync(token, jwtConfig.Secret); |
| | | 35 | | |
| | 27 | 36 | | if (!isValid) |
| | | 37 | | { |
| | 9 | 38 | | return new TokenValidationResult(null, "Invalid token"); |
| | | 39 | | } |
| | | 40 | | |
| | 18 | 41 | | var claims = TokenClaimsReader.Read(token); |
| | | 42 | | |
| | 18 | 43 | | if (claims is null) |
| | | 44 | | { |
| | 0 | 45 | | return new TokenValidationResult(null, "Could not read token claims"); |
| | | 46 | | } |
| | | 47 | | |
| | 18 | 48 | | if (options.JwtMode == JwtValidationMode.ClaimsOnly) |
| | | 49 | | { |
| | 13 | 50 | | var claimsUser = MapUser(claims); |
| | | 51 | | |
| | 13 | 52 | | return new TokenValidationResult(claimsUser, claimsUser is null ? "Could not retrieve user from token" : nul |
| | | 53 | | } |
| | | 54 | | |
| | 5 | 55 | | var userId = MapId(claims); |
| | | 56 | | |
| | 5 | 57 | | if (userId is null) |
| | | 58 | | { |
| | 2 | 59 | | return new TokenValidationResult(null, "Could not retrieve user id from token"); |
| | | 60 | | } |
| | | 61 | | |
| | 3 | 62 | | var provider = context.RequestServices.GetRequiredService<IAuthenticationProvider>(); |
| | 3 | 63 | | var user = provider.GetAuthenticatedUserById(userId.Value); |
| | | 64 | | |
| | 3 | 65 | | return new TokenValidationResult(user, user is null ? "User not found" : null); |
| | 27 | 66 | | } |
| | | 67 | | |
| | | 68 | | private IAuthenticatedUser? MapUser(IReadOnlyDictionary<string, string> claims) |
| | | 69 | | { |
| | | 70 | | try |
| | | 71 | | { |
| | 13 | 72 | | return mapper.FromClaims(claims); |
| | | 73 | | } |
| | 1 | 74 | | catch (Exception) |
| | | 75 | | { |
| | 1 | 76 | | return null; |
| | | 77 | | } |
| | 13 | 78 | | } |
| | | 79 | | |
| | | 80 | | private Guid? MapId(IReadOnlyDictionary<string, string> claims) |
| | | 81 | | { |
| | | 82 | | try |
| | | 83 | | { |
| | 5 | 84 | | return mapper.IdFromClaims(claims); |
| | | 85 | | } |
| | 1 | 86 | | catch (Exception) |
| | | 87 | | { |
| | 1 | 88 | | return null; |
| | | 89 | | } |
| | 5 | 90 | | } |
| | | 91 | | } |