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="secret">The symmetric secret used to sign the token.</param>
/// <returns>The serialized, signed JWT string.</returns>
string GenerateJwt(
Guid userId,
string username,
DateTime expiry,
string secret
);
string GenerateJwt(Guid userId, string username, DateTime expiry, string secret);
/// <summary>
/// Validates a JWT and returns the resulting claims principal.

View File

@@ -7,17 +7,11 @@
</PropertyGroup>
<ItemGroup>
<PackageReference
Include="Microsoft.IdentityModel.JsonWebTokens"
Version="8.2.1"
/>
<PackageReference
Include="System.IdentityModel.Tokens.Jwt"
Version="8.2.1"
/>
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.2.1" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.2.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Domain\Domain.Exceptions\Domain.Exceptions.csproj"/>
<ProjectReference Include="..\..\Domain\Domain.Exceptions\Domain.Exceptions.csproj" />
</ItemGroup>
</Project>

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="secret">The symmetric secret used to sign the token (encoded as UTF-8 bytes).</param>
/// <returns>The serialized, signed JWT string.</returns>
public string GenerateJwt(
Guid userId,
string username,
DateTime expiry,
string secret
)
public string GenerateJwt(Guid userId, string username, DateTime expiry, string secret)
{
JsonWebTokenHandler handler = new();
byte[] key = Encoding.UTF8.GetBytes(secret);
@@ -46,7 +41,7 @@ public class JwtInfrastructure : ITokenInfrastructure
JwtRegisteredClaimNames.Exp,
new DateTimeOffset(expiry).ToUnixTimeSeconds().ToString()
),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
};
SecurityTokenDescriptor tokenDescriptor = new()
@@ -56,13 +51,12 @@ public class JwtInfrastructure : ITokenInfrastructure
SigningCredentials = new SigningCredentials(
new SymmetricSecurityKey(key),
SecurityAlgorithms.HmacSha256
)
),
};
return handler.CreateToken(tokenDescriptor);
}
/// <summary>
/// Validates a JWT's signature and lifetime (issuer and audience validation are disabled),
/// 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
/// (including when validation itself throws, e.g. due to a malformed token or signature mismatch).
/// </exception>
public async Task<ClaimsPrincipal> ValidateJwtAsync(
string token,
string secret
)
public async Task<ClaimsPrincipal> ValidateJwtAsync(string token, string secret)
{
JsonWebTokenHandler handler = new();
byte[] keyBytes = Encoding.UTF8.GetBytes(
secret
);
byte[] keyBytes = Encoding.UTF8.GetBytes(secret);
TokenValidationParameters parameters = new()
{
ValidateIssuer = false,
ValidateAudience = false,
ValidateLifetime = true,
IssuerSigningKey = new SymmetricSecurityKey(keyBytes)
IssuerSigningKey = new SymmetricSecurityKey(keyBytes),
};
try