Вытащил солюшен на уровень выше, чтобы прощё было дотнетить
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2025-10-05 14:32:06 +03:00
parent fa87a56ad1
commit aae4b28089
242 changed files with 159 additions and 159 deletions

View 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;
}
}

View 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) };
}
}

View File

@@ -0,0 +1,5 @@
using Microsoft.IdentityModel.Tokens;
namespace Infrastructure.Auth;
public record TokenGeneratorOptions(string Issuer, string Audience, TimeSpan ValidTime, SigningCredentials Credentials);