Вытащил солюшен на уровень выше, чтобы прощё было дотнетить
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
22
Infrastructure/Auth/ServiceCollectionExtensions.cs
Normal file
22
Infrastructure/Auth/ServiceCollectionExtensions.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using ApplicationLayer.InfrastructureServicesInterfaces;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Infrastructure.Auth;
|
||||
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddTokenGenerator(this IServiceCollection services, TokenGeneratorOptions options)
|
||||
{
|
||||
services.AddSingleton<JwtSecurityTokenHandler>();
|
||||
services.AddSingleton<ITokenGenerator, TokenGenerator>(provider =>
|
||||
{
|
||||
var tokenHandler = provider.GetRequiredService<JwtSecurityTokenHandler>();
|
||||
var dateTimeProvider = provider.GetRequiredService<IDateTimeProvider>();
|
||||
|
||||
return new TokenGenerator(options, tokenHandler, dateTimeProvider);
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
34
Infrastructure/Auth/TokenGenerator.cs
Normal file
34
Infrastructure/Auth/TokenGenerator.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using ApplicationLayer.InfrastructureServicesInterfaces;
|
||||
using ApplicationLayer.Services.AuthServices.Common;
|
||||
using Domains.Users;
|
||||
|
||||
namespace Infrastructure.Auth;
|
||||
|
||||
/// <inheritdoc cref="ITokenGenerator"/>
|
||||
/// <param name="options">options kind of one in authorization registration in DI methods</param>
|
||||
/// <param name="tokenHandler">token handler</param>
|
||||
/// <param name="dateTimeProvider">date time provider</param>
|
||||
public class TokenGenerator(TokenGeneratorOptions options, JwtSecurityTokenHandler tokenHandler, IDateTimeProvider dateTimeProvider)
|
||||
: ITokenGenerator
|
||||
{
|
||||
/// <inheritdoc cref="ITokenGenerator.CreateToken"/>
|
||||
public AuthToken CreateToken(User user)
|
||||
{
|
||||
var claims = new List<Claim>
|
||||
{
|
||||
new(ClaimTypes.Role, user.Role.ToString()),
|
||||
new(ClaimTypes.NameIdentifier, user.Id.ToString())
|
||||
};
|
||||
|
||||
var token = new JwtSecurityToken(
|
||||
issuer: options.Issuer,
|
||||
audience: options.Audience,
|
||||
expires: dateTimeProvider.Now().Add(options.ValidTime),
|
||||
signingCredentials: options.Credentials,
|
||||
claims: claims);
|
||||
|
||||
return new AuthToken { Token = tokenHandler.WriteToken(token) };
|
||||
}
|
||||
}
|
||||
5
Infrastructure/Auth/TokenGeneratorOptions.cs
Normal file
5
Infrastructure/Auth/TokenGeneratorOptions.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
namespace Infrastructure.Auth;
|
||||
|
||||
public record TokenGeneratorOptions(string Issuer, string Audience, TimeSpan ValidTime, SigningCredentials Credentials);
|
||||
Reference in New Issue
Block a user