mirror of
https://github.com/azaion/flights.git
synced 2026-04-22 06:56:30 +00:00
0625cd4157
Made-with: Cursor
32 lines
1.1 KiB
C#
32 lines
1.1 KiB
C#
using System.Text;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
namespace Azaion.Flights.Auth;
|
|
|
|
public static class JwtExtensions
|
|
{
|
|
public static IServiceCollection AddJwtAuth(this IServiceCollection services, string jwtSecret)
|
|
{
|
|
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
.AddJwtBearer(options =>
|
|
{
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuerSigningKey = true,
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSecret)),
|
|
ValidateIssuer = false,
|
|
ValidateAudience = false,
|
|
ValidateLifetime = true,
|
|
ClockSkew = TimeSpan.FromMinutes(1)
|
|
};
|
|
});
|
|
|
|
services.AddAuthorizationBuilder()
|
|
.AddPolicy("FL", p => p.RequireClaim("permissions", "FL"))
|
|
.AddPolicy("GPS", p => p.RequireClaim("permissions", "GPS"));
|
|
|
|
return services;
|
|
}
|
|
}
|