| [5b42c8d] | 1 | using System.IdentityModel.Tokens.Jwt;
|
|---|
| 2 | using System.Security.Claims;
|
|---|
| 3 | using System.Text;
|
|---|
| 4 | using BCrypt.Net;
|
|---|
| 5 | using FinXaccesApi.Controllers;
|
|---|
| 6 | using iknow_api.Core.Models;
|
|---|
| 7 | using FinXaccesApi.DTOs;
|
|---|
| 8 | using FinXaccesApi.Repositories;
|
|---|
| 9 | using Microsoft.IdentityModel.Tokens;
|
|---|
| 10 |
|
|---|
| 11 | namespace FinXaccesApi.Services
|
|---|
| 12 | {
|
|---|
| 13 | public class AuthService : IAuthService
|
|---|
| 14 | {
|
|---|
| 15 | private readonly IUserRepository _userRepository;
|
|---|
| 16 | private readonly IConfiguration _configuration;
|
|---|
| 17 |
|
|---|
| 18 | public AuthService(IUserRepository userRepository, IConfiguration configuration)
|
|---|
| 19 | {
|
|---|
| 20 | _userRepository = userRepository;
|
|---|
| 21 | _configuration = configuration;
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | public async Task<bool> RegisterAsync(UserDto userDto)
|
|---|
| 25 | {
|
|---|
| 26 | if (await _userRepository.UserExistsAsync(userDto.Username))
|
|---|
| 27 | return false;
|
|---|
| 28 |
|
|---|
| 29 | var user = new User
|
|---|
| 30 | {
|
|---|
| 31 | Email = userDto.Username,
|
|---|
| 32 | PasswordHash = BCrypt.Net.BCrypt.HashPassword(userDto.Password)
|
|---|
| 33 | };
|
|---|
| 34 |
|
|---|
| 35 | await _userRepository.AddUserAsync(user);
|
|---|
| 36 | return true;
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | public async Task<string?> LoginAsync(UserDto userDto)
|
|---|
| 40 | {
|
|---|
| 41 | var user = await _userRepository.GetUserByUsernameAsync(userDto.Username);
|
|---|
| 42 | if (user == null || !BCrypt.Net.BCrypt.Verify(userDto.Password, user.PasswordHash))
|
|---|
| 43 | return null;
|
|---|
| 44 |
|
|---|
| 45 | return CreateToken(user);
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | public async Task<int> GetUsersCountAsync()
|
|---|
| 49 | {
|
|---|
| 50 | return await _userRepository.GetUsersCountAsync();
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | private string CreateToken(User user)
|
|---|
| 54 | {
|
|---|
| 55 | var claims = new[]
|
|---|
| 56 | {
|
|---|
| 57 | new Claim("id", user.Id.ToString()),
|
|---|
| 58 | new Claim("username", user.Email)
|
|---|
| 59 | };
|
|---|
| 60 |
|
|---|
| 61 | var keyString = _configuration["Jwt:Key"];
|
|---|
| 62 | if (string.IsNullOrEmpty(keyString))
|
|---|
| 63 | throw new Exception("JWT Key is missing in configuration!");
|
|---|
| 64 |
|
|---|
| 65 | var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(keyString));
|
|---|
| 66 | var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
|
|---|
| 67 |
|
|---|
| 68 | var token = new JwtSecurityToken(
|
|---|
| 69 | issuer: "finxacces-api",
|
|---|
| 70 | audience: "finxacces-api",
|
|---|
| 71 | claims: claims,
|
|---|
| 72 | expires: DateTime.Now.AddHours(1),
|
|---|
| 73 | signingCredentials: creds
|
|---|
| 74 | );
|
|---|
| 75 |
|
|---|
| 76 | return new JwtSecurityTokenHandler().WriteToken(token);
|
|---|
| 77 | }
|
|---|
| 78 | }
|
|---|
| 79 | } |
|---|