Skip to content

Commit 711c23f

Browse files
committed
Validate absoluteExpiration is not in the past for JWTs
Add a check to ensure absoluteExpiration is not earlier than the current UTC time when creating a JWT token. Throw an ArgumentException if an invalid expiration is provided to prevent issuing already-expired tokens.
1 parent 00680ec commit 711c23f

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

src/SimpleAuthentication/JwtBearer/JwtBearerService.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,17 @@ public class JwtBearerService(IOptions<JwtBearerSettings> jwtBearerSettingsOptio
2020
/// <inheritdoc />
2121
public virtual Task<string> CreateTokenAsync(string userName, IList<Claim>? claims = null, string? issuer = null, string? audience = null, DateTime? absoluteExpiration = null)
2222
{
23+
var now = DateTime.UtcNow;
24+
25+
if (absoluteExpiration.HasValue && absoluteExpiration.Value < now)
26+
{
27+
throw new ArgumentException("The expiration date must be greater than or equal to the current date and time.", nameof(absoluteExpiration));
28+
}
29+
2330
claims ??= [];
2431
claims.Update(JwtBearerSettings.NameClaimType, userName);
2532
claims.Update(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString());
2633

27-
var now = DateTime.UtcNow;
28-
2934
var securityTokenDescriptor = new SecurityTokenDescriptor()
3035
{
3136
Subject = new ClaimsIdentity(claims, JwtBearerSettings.SchemeName, JwtBearerSettings.NameClaimType, JwtBearerSettings.RoleClaimType),

0 commit comments

Comments
 (0)