2022-09-26 04:40:18 +03:00
|
|
|
using System.Security.Claims;
|
|
|
|
using BlueWest.Cryptography;
|
2022-11-18 03:15:53 +03:00
|
|
|
using CodeLiturgy.Data.Application.Users;
|
2022-10-27 20:13:02 +03:00
|
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
2022-09-26 04:40:18 +03:00
|
|
|
using Microsoft.AspNetCore.Identity;
|
2022-11-18 03:15:53 +03:00
|
|
|
using static CodeLiturgy.Data.Auth.Context.Users.AuthConsts;
|
2022-09-26 04:40:18 +03:00
|
|
|
|
2022-11-18 03:15:53 +03:00
|
|
|
namespace CodeLiturgy.Data.Auth.Context.Users
|
2022-09-26 04:40:18 +03:00
|
|
|
{
|
2022-10-27 20:13:02 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Authentication Manager for the Application Users
|
|
|
|
/// </summary>
|
2022-09-27 20:12:13 +03:00
|
|
|
public class AuthManager : IAuthManager
|
2022-09-26 04:40:18 +03:00
|
|
|
{
|
|
|
|
private readonly ApplicationUserManager _userManager;
|
|
|
|
private readonly IHasher _hasher;
|
|
|
|
private readonly IJwtFactory _jwtFactory;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Auth manager constructor
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="userManager"></param>
|
|
|
|
/// <param name="hasher"></param>
|
|
|
|
/// <param name="jwtFactory"></param>
|
|
|
|
/// <param name="sessionCache"></param>
|
|
|
|
public AuthManager(
|
|
|
|
ApplicationUserManager userManager,
|
|
|
|
IHasher hasher,
|
2022-11-13 14:27:48 +03:00
|
|
|
IJwtFactory jwtFactory)
|
2022-09-26 04:40:18 +03:00
|
|
|
{
|
|
|
|
_userManager = userManager;
|
|
|
|
_hasher = hasher;
|
|
|
|
_jwtFactory = jwtFactory;
|
|
|
|
}
|
|
|
|
|
2022-11-13 14:27:48 +03:00
|
|
|
|
2022-09-26 04:40:18 +03:00
|
|
|
|
|
|
|
private string GetHashFromUuid(string uuid)
|
|
|
|
{
|
|
|
|
return _hasher.CreateHash(uuid, BaseCryptoItem.HashAlgorithm.SHA2_512);
|
|
|
|
}
|
2022-10-27 20:13:02 +03:00
|
|
|
|
|
|
|
|
2022-11-13 14:27:48 +03:00
|
|
|
|
2022-09-26 04:40:18 +03:00
|
|
|
|
|
|
|
private async Task<(bool, string)> GenerateBearerToken(ClaimsIdentity identity, ApplicationUser user)
|
|
|
|
{
|
|
|
|
var jwtToken = await _jwtFactory.GenerateEncodedToken(user.Id, user.UserName);
|
|
|
|
var completed = await _userManager.SetAuthenticationTokenAsync(user, SessionConstants.ApiNamePolicy,
|
|
|
|
SessionConstants.ApiNamePolicy, jwtToken.Token);
|
|
|
|
return (completed == IdentityResult.Success, jwtToken.Token);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-27 20:13:02 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Verify Password
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="email"></param>
|
|
|
|
/// <param name="password"></param>
|
|
|
|
/// <returns></returns>
|
2022-09-26 04:40:18 +03:00
|
|
|
public async Task<bool> VerifyLoginByEmailAsync(string email, string password)
|
|
|
|
{
|
|
|
|
var user = await _userManager.FindByEmailAsync(email);
|
|
|
|
return user != null && await _userManager.CheckPasswordAsync(user, password);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-27 20:13:02 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Create user
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="userSignupDto"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public async Task<IdentityResult> CreateUserAsync(RegisterRequest userSignupDto)
|
2022-09-26 04:40:18 +03:00
|
|
|
{
|
|
|
|
userSignupDto.Password = _hasher.CreateHash(userSignupDto.Password, BaseCryptoItem.HashAlgorithm.SHA3_512);;
|
|
|
|
var newUser = userSignupDto.ToUser();
|
|
|
|
return await _userManager.CreateAsync(newUser);
|
|
|
|
}
|
2022-11-13 14:27:48 +03:00
|
|
|
|
|
|
|
public async Task<(ClaimsIdentity, bool)> DoLogin(LoginRequest loginRequest)
|
|
|
|
{
|
|
|
|
var user = await _userManager.FindByEmailAsync(loginRequest.Email);
|
|
|
|
|
|
|
|
if (user == null) return NegativeToken;
|
|
|
|
|
|
|
|
if (!await _userManager.CheckPasswordAsync(user, loginRequest.Password)) return NegativeToken;
|
|
|
|
|
|
|
|
var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
|
|
|
|
identity.AddClaim(new Claim(ClaimTypes.Email, user.Email));
|
|
|
|
identity.AddClaim(new Claim(ClaimTypes.MobilePhone, user.PhoneNumber));
|
|
|
|
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id));
|
|
|
|
|
|
|
|
|
|
|
|
return (identity, true);
|
|
|
|
}
|
2022-09-26 04:40:18 +03:00
|
|
|
}
|
|
|
|
}
|