| | 1 | | using Microsoft.IdentityModel.JsonWebTokens; |
| | 2 | | using Microsoft.IdentityModel.Tokens; |
| | 3 | | using System.Security.Claims; |
| | 4 | |
|
| | 5 | | namespace Kestrun.Jwt; |
| | 6 | |
|
| | 7 | | /// <summary> |
| | 8 | | /// Represents the result of building a JWT, including the token, key, builder, issue time, and expiration. |
| | 9 | | /// </summary> |
| | 10 | | /// <param name="token">The JWT compact string.</param> |
| | 11 | | /// <param name="key">The symmetric security key used for signing.</param> |
| | 12 | | /// <param name="builder">The JWT token builder instance.</param> |
| | 13 | | /// <param name="issuedAt">The time at which the token was issued.</param> |
| | 14 | | /// <param name="expires">The expiration time of the token.</param> |
| 24 | 15 | | public sealed class JwtBuilderResult( |
| 24 | 16 | | string token, |
| 24 | 17 | | SymmetricSecurityKey? key, |
| 24 | 18 | | JwtTokenBuilder builder, |
| 24 | 19 | | DateTime issuedAt, |
| 24 | 20 | | DateTime expires) |
| | 21 | | { |
| 24 | 22 | | private readonly string _token = token; |
| 24 | 23 | | private readonly SymmetricSecurityKey? _key = key; |
| 24 | 24 | | private readonly JwtTokenBuilder _builder = builder; |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Gets the time at which the token was issued. |
| | 28 | | /// </summary> |
| 24 | 29 | | public DateTime IssuedAt { get; } = issuedAt; |
| | 30 | | /// <summary> |
| | 31 | | /// Gets the expiration time of the token. |
| | 32 | | /// </summary> |
| 24 | 33 | | public DateTime Expires { get; private set; } = expires; |
| | 34 | |
|
| | 35 | | /// <summary>Get the JWT compact string.</summary> |
| 17 | 36 | | public string Token() => _token; |
| | 37 | |
|
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Gets the <see cref="TokenValidationParameters"/> for validating the JWT token. |
| | 41 | | /// </summary> |
| | 42 | | /// <param name="clockSkew">Optional clock skew to allow when validating token lifetime.</param> |
| | 43 | | /// <returns>The configured <see cref="TokenValidationParameters"/> instance.</returns> |
| | 44 | | public TokenValidationParameters GetValidationParameters(TimeSpan? clockSkew = null) |
| | 45 | | { |
| 10 | 46 | | var tvp = new TokenValidationParameters |
| 10 | 47 | | { |
| 10 | 48 | | ValidateIssuer = _builder.Issuer is not null, |
| 10 | 49 | | ValidIssuer = _builder.Issuer, |
| 10 | 50 | | ValidateAudience = _builder.Audience is not null, |
| 10 | 51 | | ValidAudience = _builder.Audience, |
| 10 | 52 | | ValidateLifetime = true, |
| 10 | 53 | | ClockSkew = clockSkew ?? TimeSpan.FromMinutes(1), |
| 10 | 54 | |
|
| 10 | 55 | | RequireSignedTokens = _key is not null, |
| 10 | 56 | | ValidateIssuerSigningKey = _key is not null, |
| 10 | 57 | | IssuerSigningKey = _key, |
| 10 | 58 | | ValidAlgorithms = _builder.Algorithm != null ? [_builder.Algorithm] : [], |
| 10 | 59 | | NameClaimType = ClaimTypes.Name, |
| 10 | 60 | | // NameClaimType = JwtRegisteredClaimNames.Sub, |
| 10 | 61 | | RoleClaimType = ClaimTypes.Role |
| 10 | 62 | | }; |
| 10 | 63 | | return tvp; |
| | 64 | | } |
| | 65 | |
|
| | 66 | | /// <summary> |
| | 67 | | /// Asynchronously validates the specified JWT using the configured validation parameters. |
| | 68 | | /// </summary> |
| | 69 | | /// <param name="jwt">The JWT compact string to validate.</param> |
| | 70 | | /// <param name="clockSkew">Optional clock skew to allow when validating token lifetime.</param> |
| | 71 | | /// <returns>A task that represents the asynchronous operation, containing the token validation result.</returns> |
| | 72 | | public async Task<TokenValidationResult> ValidateAsync(string jwt, TimeSpan? clockSkew = null) |
| | 73 | | { |
| 3 | 74 | | ArgumentNullException.ThrowIfNull(jwt); |
| 3 | 75 | | ArgumentNullException.ThrowIfNull(_key); |
| | 76 | |
|
| | 77 | | // validate the token using the parameters |
| 1 | 78 | | var validationParameters = GetValidationParameters(clockSkew); |
| | 79 | |
|
| 1 | 80 | | var handler = new JsonWebTokenHandler(); |
| 1 | 81 | | return await handler.ValidateTokenAsync( |
| 1 | 82 | | jwt, validationParameters).ConfigureAwait(false); |
| 1 | 83 | | } |
| | 84 | |
|
| | 85 | | /// <summary> |
| | 86 | | /// Synchronously validates the specified JWT using the configured validation parameters. |
| | 87 | | /// </summary> |
| | 88 | | /// <param name="jwt">The JWT compact string to validate.</param> |
| | 89 | | /// <param name="clockSkew">Optional clock skew to allow when validating token lifetime.</param> |
| | 90 | | /// <returns>The token validation result.</returns> |
| 0 | 91 | | public TokenValidationResult Validate(string jwt, TimeSpan? clockSkew = null) => ValidateAsync(jwt, clockSkew).GetAw |
| | 92 | | } |