moved service to separate project (Kontur developer said "WTF WHY ARE U STORING SERVICES IN INFRASTRUCTURE SLAVA D")

This commit is contained in:
Вячеслав 2025-04-18 14:12:00 +05:00
parent 147fb683f7
commit 41ff1555f8
11 changed files with 33 additions and 9 deletions

View file

@ -0,0 +1,32 @@
using System.Text;
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Tokens;
namespace SurveyBackend.Services;
public static class AuthOptions
{
public static string Issuer;
public static string Audience;
public static TimeSpan TokenLifetime;
private static string? SecurityKey { get; set; }
public static SymmetricSecurityKey SymmetricSecurityKey
{
get
{
ArgumentNullException.ThrowIfNull(SecurityKey);
return new SymmetricSecurityKey(Encoding.UTF8.GetBytes(SecurityKey));
}
}
public static void MakeOptions(IConfigurationManager configurationManager, string? securityKey = null)
{
var jwtSettings = configurationManager.GetSection("JwtSettings");
Issuer = jwtSettings["Issuer"] ?? "DefaultIssuer";
Audience = jwtSettings["Audience"] ?? "DefaultAudience";
TokenLifetime = TimeSpan.FromMinutes(int.Parse(jwtSettings["TokenLifetime"] ?? "60"));
SecurityKey = securityKey ?? jwtSettings["SecretKey"];
}
}

View file

@ -0,0 +1,31 @@
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using Microsoft.IdentityModel.Tokens;
using SurveyBackend.Core.Models;
namespace SurveyBackend.Services.Helpers;
public class TokenHelper
{
public static string GetAuthToken(User user)
{
var userId = user.Id.ToString();
var claims = new List<Claim>
{
new(ClaimTypes.NameIdentifier, userId)
};
var jwt = new JwtSecurityToken(
claims: claims,
issuer: AuthOptions.Issuer,
audience: AuthOptions.Audience,
expires: DateTime.UtcNow + AuthOptions.TokenLifetime,
signingCredentials: new SigningCredentials(AuthOptions.SymmetricSecurityKey, SecurityAlgorithms.HmacSha256)
);
var token = new JwtSecurityTokenHandler().WriteToken(jwt);
return token;
}
}

View file

@ -0,0 +1,42 @@
using SurveyBackend.Core.Models;
using SurveyBackend.Core.Services;
using SurveyBackend.Services.Helpers;
namespace SurveyBackend.Services.Services;
public class AuthorizationService
{
private readonly IUserService _userService;
private readonly IPasswordHasher _passwordHasher;
public AuthorizationService(IUserService userService, IPasswordHasher passwordHasher)
{
_userService = userService;
_passwordHasher = passwordHasher;
}
public async Task<string?> LogInUser(string email, string password)
{
var user = await _userService.GetUserByEmail(email);
if (user is null || !_passwordHasher.Verify(password, user.Password))
{
return null;
}
var token = TokenHelper.GetAuthToken(user);
return token;
}
public async Task RegisterUser(User user)
{
var existingUser = await _userService.GetUserByEmail(user.Email);
if (existingUser is not null)
{
throw new Exception("Email already exists");
}
user.Password = _passwordHasher.HashPassword(user.Password);
await _userService.CreateUserAsync(user);
}
}

View file

@ -0,0 +1,62 @@
using System.Security.Cryptography;
using SurveyBackend.Core.Services;
namespace SurveyBackend.Services.Services;
public class Sha256PasswordHasher : IPasswordHasher
{
private const int RehashCount = 10000;
private const int HashSizeBytes = 16;
private const int SaltSizeBytes = 16;
public string HashPassword(string password)
{
var saltBytes = RandomNumberGenerator.GetBytes(SaltSizeBytes);
using var deriveBytes = GetDeriveBytes(password, saltBytes);
var hashBytes = deriveBytes.GetBytes(HashSizeBytes);
var fullHashBytes = new byte[SaltSizeBytes + HashSizeBytes];
Array.Copy(saltBytes, 0, fullHashBytes, 0, SaltSizeBytes);
Array.Copy(hashBytes, 0, fullHashBytes, SaltSizeBytes, HashSizeBytes);
var hashedPassword = Convert.ToBase64String(fullHashBytes);
return hashedPassword;
}
public bool Verify(string password, string hashedPassword)
{
var fullHashBytes = Convert.FromBase64String(hashedPassword);
var saltBytes = new byte[SaltSizeBytes];
Array.Copy(fullHashBytes, 0, saltBytes, 0, SaltSizeBytes);
using var deriveBytes = GetDeriveBytes(password, saltBytes);
var hashBytes = deriveBytes.GetBytes(HashSizeBytes);
var isMatch = CompareHashes(hashBytes, fullHashBytes);
return isMatch;
}
private static bool CompareHashes(byte[] hashBytes, byte[] fullHashBytes)
{
for (var i = 0; i < HashSizeBytes; i++)
if (hashBytes[i] != fullHashBytes[i + SaltSizeBytes])
return false;
return true;
}
private static Rfc2898DeriveBytes GetDeriveBytes(string password, byte[] saltBytes)
{
return new Rfc2898DeriveBytes(password, saltBytes, RehashCount, HashAlgorithmName.SHA256);
}
}

View file

@ -0,0 +1,25 @@
using SurveyBackend.Core.Models;
using SurveyBackend.Core.Repositories;
using SurveyBackend.Core.Services;
namespace SurveyBackend.Services.Services;
public class UserService : IUserService
{
private readonly IUserRepository _userRepository;
public UserService(IUserRepository userRepository)
{
_userRepository = userRepository;
}
public async Task<User?> GetUserByEmail(string email)
{
return await _userRepository.GetUserByEmail(email);
}
public async Task CreateUserAsync(User user)
{
await _userRepository.AddAsync(user);
}
}

View file

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\SurveyBackend.Core\SurveyBackend.Core.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.1.2" />
</ItemGroup>
</Project>