astral-api/Astral.ApiServer/Extensions/JwtOptionsExtensions.cs

37 lines
1.3 KiB
C#
Raw Normal View History

2024-12-11 21:36:30 +01:00
// <copyright file="JwtOptionsExtensions.cs" company="alveus.dev">
// Copyright (c) alveus.dev. All rights reserved. Licensed under the MIT License.
// </copyright>
using System.Text;
using Astral.Core.Options;
using Microsoft.IdentityModel.Tokens;
namespace Astral.ApiServer.Extensions;
/// <summary>
/// Extensions for <see cref="JwtOptions" />.
/// </summary>
public static class JwtOptionsExtensions
{
/// <summary>
/// Generate an instance of <see cref="TokenValidationParameters" /> from <see cref="JwtOptions" />.
/// </summary>
/// <param name="config">Instance of <see cref="JwtOptions" />.</param>
/// <returns>Instance of <see cref="TokenValidationParameters" />.</returns>
public static TokenValidationParameters ToTokenValidationParameters(this JwtOptions 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))
};
}
}