Format ./web/backend/Infrastructure/Infrastructure.Jwt

This commit is contained in:
Aaron Po
2026-06-20 15:13:02 -04:00
parent 744d4c7f8c
commit 62766b7638
3 changed files with 12 additions and 34 deletions

View File

@@ -15,12 +15,7 @@ public interface ITokenInfrastructure
/// <param name="expiry">The date and time at which the token expires.</param> /// <param name="expiry">The date and time at which the token expires.</param>
/// <param name="secret">The symmetric secret used to sign the token.</param> /// <param name="secret">The symmetric secret used to sign the token.</param>
/// <returns>The serialized, signed JWT string.</returns> /// <returns>The serialized, signed JWT string.</returns>
string GenerateJwt( string GenerateJwt(Guid userId, string username, DateTime expiry, string secret);
Guid userId,
string username,
DateTime expiry,
string secret
);
/// <summary> /// <summary>
/// Validates a JWT and returns the resulting claims principal. /// Validates a JWT and returns the resulting claims principal.

View File

@@ -7,14 +7,8 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference <PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.2.1" />
Include="Microsoft.IdentityModel.JsonWebTokens" <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.2.1" />
Version="8.2.1"
/>
<PackageReference
Include="System.IdentityModel.Tokens.Jwt"
Version="8.2.1"
/>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -25,12 +25,7 @@ public class JwtInfrastructure : ITokenInfrastructure
/// <param name="expiry">The date and time at which the token expires.</param> /// <param name="expiry">The date and time at which the token expires.</param>
/// <param name="secret">The symmetric secret used to sign the token (encoded as UTF-8 bytes).</param> /// <param name="secret">The symmetric secret used to sign the token (encoded as UTF-8 bytes).</param>
/// <returns>The serialized, signed JWT string.</returns> /// <returns>The serialized, signed JWT string.</returns>
public string GenerateJwt( public string GenerateJwt(Guid userId, string username, DateTime expiry, string secret)
Guid userId,
string username,
DateTime expiry,
string secret
)
{ {
JsonWebTokenHandler handler = new(); JsonWebTokenHandler handler = new();
byte[] key = Encoding.UTF8.GetBytes(secret); byte[] key = Encoding.UTF8.GetBytes(secret);
@@ -46,7 +41,7 @@ public class JwtInfrastructure : ITokenInfrastructure
JwtRegisteredClaimNames.Exp, JwtRegisteredClaimNames.Exp,
new DateTimeOffset(expiry).ToUnixTimeSeconds().ToString() new DateTimeOffset(expiry).ToUnixTimeSeconds().ToString()
), ),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()) new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
}; };
SecurityTokenDescriptor tokenDescriptor = new() SecurityTokenDescriptor tokenDescriptor = new()
@@ -56,13 +51,12 @@ public class JwtInfrastructure : ITokenInfrastructure
SigningCredentials = new SigningCredentials( SigningCredentials = new SigningCredentials(
new SymmetricSecurityKey(key), new SymmetricSecurityKey(key),
SecurityAlgorithms.HmacSha256 SecurityAlgorithms.HmacSha256
) ),
}; };
return handler.CreateToken(tokenDescriptor); return handler.CreateToken(tokenDescriptor);
} }
/// <summary> /// <summary>
/// Validates a JWT's signature and lifetime (issuer and audience validation are disabled), /// Validates a JWT's signature and lifetime (issuer and audience validation are disabled),
/// using the provided secret as the HMAC-SHA256 symmetric signing key. /// using the provided secret as the HMAC-SHA256 symmetric signing key.
@@ -74,21 +68,16 @@ public class JwtInfrastructure : ITokenInfrastructure
/// Thrown when the token is invalid, has no claims identity, is expired, or otherwise fails validation /// Thrown when the token is invalid, has no claims identity, is expired, or otherwise fails validation
/// (including when validation itself throws, e.g. due to a malformed token or signature mismatch). /// (including when validation itself throws, e.g. due to a malformed token or signature mismatch).
/// </exception> /// </exception>
public async Task<ClaimsPrincipal> ValidateJwtAsync( public async Task<ClaimsPrincipal> ValidateJwtAsync(string token, string secret)
string token,
string secret
)
{ {
JsonWebTokenHandler handler = new(); JsonWebTokenHandler handler = new();
byte[] keyBytes = Encoding.UTF8.GetBytes( byte[] keyBytes = Encoding.UTF8.GetBytes(secret);
secret
);
TokenValidationParameters parameters = new() TokenValidationParameters parameters = new()
{ {
ValidateIssuer = false, ValidateIssuer = false,
ValidateAudience = false, ValidateAudience = false,
ValidateLifetime = true, ValidateLifetime = true,
IssuerSigningKey = new SymmetricSecurityKey(keyBytes) IssuerSigningKey = new SymmetricSecurityKey(keyBytes),
}; };
try try