file-scoped namespaces

This commit is contained in:
2024-08-26 11:33:31 +03:00
parent bfce112a59
commit 0d8e30004d
56 changed files with 650 additions and 708 deletions

View File

@@ -3,12 +3,12 @@ using ApplicationLayer.InfrastructureServicesInterfaces;
using ApplicationLayer.Services.AuthServices.NeededServices;
using Microsoft.Extensions.DependencyInjection;
namespace Infrastructure.Auth
namespace Infrastructure.Auth;
public static class ServiceCollectionsExtensions
{
public static class ServiceCollectionsExtensions
public static IServiceCollection AddTokenGenerator(this IServiceCollection services, TokenGeneratorOptions options)
{
public static IServiceCollection AddTokenGenerator(this IServiceCollection services, TokenGeneratorOptions options)
{
services.AddSingleton<JwtSecurityTokenHandler>();
services.AddSingleton<ITokenGenerator, TokenGenerator>(provider =>
{
@@ -20,5 +20,4 @@ namespace Infrastructure.Auth
return services;
}
}
}
}

View File

@@ -4,27 +4,26 @@ using ApplicationLayer.InfrastructureServicesInterfaces;
using ApplicationLayer.Services.AuthServices.NeededServices;
using Domains.Users;
namespace Infrastructure.Auth
namespace Infrastructure.Auth;
public class TokenGenerator(TokenGeneratorOptions options, JwtSecurityTokenHandler tokenHandler, IDateTimeProvider dateTimeProvider)
: ITokenGenerator
{
public class TokenGenerator(TokenGeneratorOptions options, JwtSecurityTokenHandler tokenHandler, IDateTimeProvider dateTimeProvider)
: ITokenGenerator
public string CreateToken(User user)
{
public string CreateToken(User user)
var claims = new List<Claim>
{
var claims = new List<Claim>
{
new(ClaimTypes.Role, user.Role.ToString()),
new(ClaimTypes.NameIdentifier, user.Id.ToString())
};
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);
var token = new JwtSecurityToken(
issuer: options.Issuer,
audience: options.Audience,
expires: dateTimeProvider.Now().Add(options.ValidTime),
signingCredentials: options.Credentials,
claims: claims);
return tokenHandler.WriteToken(token);
}
return tokenHandler.WriteToken(token);
}
}
}

View File

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