galaeth-draft/Galaeth.ApiServer/Extensions/JwtConfigurationExtensions.cs

33 lines
1.2 KiB
C#
Raw Permalink Normal View History

2024-11-17 10:31:01 +01:00
using System.Text;
using Galaeth.Core.Configuration;
using Microsoft.IdentityModel.Tokens;
namespace Galaeth.ApiServer.Extensions;
/// <summary>
/// Extensions for <see cref="JwtConfiguration"/>.
/// </summary>
public static class JwtConfigurationExtensions
{
/// <summary>
/// Generate an instance of <see cref="TokenValidationParameters"/> from <see cref="JwtConfiguration"/>.
/// </summary>
/// <param name="config">Instance of <see cref="JwtConfiguration"/>.</param>
/// <returns>Instance of <see cref="TokenValidationParameters"/>.</returns>
public static TokenValidationParameters ToTokenValidationParameters(this JwtConfiguration config)
{
return new TokenValidationParameters()
{
ValidateIssuer = config.ValidateIssuer,
ValidateAudience = config.ValidateAudience,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidAudience = config.Audience,
ValidAudiences =
[config.Audience],
ValidIssuer = config.Issuer,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(config.SecretKey)),
};
}
}