mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-07-16 17:47:22 +00:00
Format ./web/backend/Infrastructure/Infrastructure.Jwt
This commit is contained in:
@@ -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.
|
||||||
|
|||||||
@@ -7,17 +7,11 @@
|
|||||||
</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>
|
||||||
<ProjectReference Include="..\..\Domain\Domain.Exceptions\Domain.Exceptions.csproj"/>
|
<ProjectReference Include="..\..\Domain\Domain.Exceptions\Domain.Exceptions.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user