2021-04-02 17:28:07 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudDb.Model;
|
2022-04-11 18:00:34 +05:00
|
|
|
|
using Mapster;
|
2021-08-24 10:59:10 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2021-04-02 17:28:07 +05:00
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2022-04-11 18:00:34 +05:00
|
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
2021-04-02 17:28:07 +05:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Text;
|
2021-08-11 16:54:42 +05:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-04-02 17:28:07 +05:00
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services
|
|
|
|
|
{
|
|
|
|
|
public class AuthService : IAuthService
|
|
|
|
|
{
|
|
|
|
|
private readonly IAsbCloudDbContext db;
|
2021-12-11 16:46:04 +05:00
|
|
|
|
private readonly IUserService userService;
|
2021-04-02 17:28:07 +05:00
|
|
|
|
|
|
|
|
|
public const string issuer = "a";
|
|
|
|
|
public const string audience = "a";
|
2022-04-11 18:00:34 +05:00
|
|
|
|
public static readonly SymmetricSecurityKey securityKey = new(Encoding.ASCII.GetBytes("супер секретный ключ для шифрования"));
|
2021-12-07 18:27:52 +05:00
|
|
|
|
private const string algorithms = SecurityAlgorithms.HmacSha256;
|
2021-04-02 17:28:07 +05:00
|
|
|
|
|
|
|
|
|
private static readonly TimeSpan expiresTimespan = TimeSpan.FromDays(365.25);
|
|
|
|
|
private static readonly Encoding encoding = Encoding.UTF8;
|
|
|
|
|
private const int PasswordSaltLength = 5;
|
2021-08-17 13:03:17 +05:00
|
|
|
|
private const string claimIdUser = "id";
|
2021-12-07 18:27:52 +05:00
|
|
|
|
private const string claimNameIdCompany = "idCompany";
|
|
|
|
|
private readonly HashAlgorithm hashAlgorithm;
|
2021-04-02 17:28:07 +05:00
|
|
|
|
private readonly Random rnd;
|
|
|
|
|
|
2021-12-20 15:17:09 +05:00
|
|
|
|
public AuthService(IAsbCloudDbContext db, IUserService userService)
|
2021-04-02 17:28:07 +05:00
|
|
|
|
{
|
|
|
|
|
this.db = db;
|
2021-12-11 16:46:04 +05:00
|
|
|
|
this.userService = userService;
|
2021-12-07 18:27:52 +05:00
|
|
|
|
hashAlgorithm = SHA384.Create();
|
2021-04-02 17:28:07 +05:00
|
|
|
|
rnd = new Random((int)(DateTime.Now.Ticks % 2147480161));
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-11 16:54:42 +05:00
|
|
|
|
public async Task<UserTokenDto> LoginAsync(string login, string password,
|
2021-11-24 11:30:29 +05:00
|
|
|
|
CancellationToken token)
|
2021-04-02 17:28:07 +05:00
|
|
|
|
{
|
2021-10-27 17:48:19 +05:00
|
|
|
|
var (identity, user) = await GetClaimsUserAsync(login, password, token)
|
2021-08-11 17:26:02 +05:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
2021-12-22 16:55:39 +05:00
|
|
|
|
if (identity == default || user.IdState == 0)
|
2021-04-02 17:28:07 +05:00
|
|
|
|
return null;
|
|
|
|
|
|
2022-06-16 12:33:05 +05:00
|
|
|
|
var userDto = await userService.GetOrDefaultAsync(user.Id, token)
|
2021-12-11 16:46:04 +05:00
|
|
|
|
.ConfigureAwait(false);
|
2021-11-29 17:34:53 +05:00
|
|
|
|
|
2021-12-11 16:46:04 +05:00
|
|
|
|
var userTokenDto = userDto.Adapt<UserTokenDto>();
|
|
|
|
|
userTokenDto.Permissions = userService.GetNestedPermissions(userDto.Id);
|
|
|
|
|
userTokenDto.Token = MakeToken(identity.Claims);
|
|
|
|
|
return userTokenDto;
|
2021-04-02 17:28:07 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Refresh(ClaimsPrincipal user)
|
|
|
|
|
{
|
|
|
|
|
return MakeToken(user.Claims);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-11 16:46:04 +05:00
|
|
|
|
public int Register(UserRegistrationDto userDto)
|
2021-04-02 17:28:07 +05:00
|
|
|
|
{
|
2021-10-22 17:41:00 +05:00
|
|
|
|
if (userDto.Login is null || userDto.Login.Length is < 3 or > 50)
|
2021-04-02 17:28:07 +05:00
|
|
|
|
return -1;
|
|
|
|
|
|
2021-10-22 17:41:00 +05:00
|
|
|
|
if (userDto.Password is null || userDto.Password.Length is < 3 or > 50)
|
2021-04-02 17:28:07 +05:00
|
|
|
|
return -2;
|
2022-04-11 18:00:34 +05:00
|
|
|
|
|
2021-10-22 17:41:00 +05:00
|
|
|
|
if (userDto.Email?.Length > 255)
|
2021-10-19 15:22:18 +05:00
|
|
|
|
return -3;
|
2022-04-11 18:00:34 +05:00
|
|
|
|
|
2021-10-22 17:41:00 +05:00
|
|
|
|
if (userDto.Phone?.Length > 50)
|
2021-10-19 15:22:18 +05:00
|
|
|
|
return -4;
|
2022-04-11 18:00:34 +05:00
|
|
|
|
|
2021-10-22 17:41:00 +05:00
|
|
|
|
if (userDto.Position?.Length > 255)
|
2021-10-19 15:22:18 +05:00
|
|
|
|
return -5;
|
2021-04-02 17:28:07 +05:00
|
|
|
|
|
2021-10-25 12:04:34 +05:00
|
|
|
|
var user = db.Users.FirstOrDefault(u => u.Login == userDto.Login);
|
2022-04-11 18:00:34 +05:00
|
|
|
|
|
|
|
|
|
if (user is not null)
|
2021-10-25 12:04:34 +05:00
|
|
|
|
return -6;
|
|
|
|
|
|
2021-04-02 17:28:07 +05:00
|
|
|
|
var salt = GenerateSalt();
|
|
|
|
|
|
2021-10-25 12:04:34 +05:00
|
|
|
|
var newUser = new User
|
2021-04-02 17:28:07 +05:00
|
|
|
|
{
|
2021-07-21 12:30:51 +05:00
|
|
|
|
IdCompany = userDto.IdCompany,
|
2021-12-22 16:55:39 +05:00
|
|
|
|
IdState = 0,
|
2021-04-02 17:28:07 +05:00
|
|
|
|
Name = userDto.Name,
|
|
|
|
|
Surname = userDto.Surname,
|
|
|
|
|
Patronymic = userDto.Patronymic,
|
2021-10-19 15:22:18 +05:00
|
|
|
|
Email = userDto.Email,
|
|
|
|
|
Phone = userDto.Phone,
|
|
|
|
|
Position = userDto.Position,
|
2021-04-02 17:28:07 +05:00
|
|
|
|
Login = userDto.Login,
|
2021-10-21 17:24:25 +05:00
|
|
|
|
PasswordHash = salt + ComputeHash(salt, userDto.Password),
|
2021-04-02 17:28:07 +05:00
|
|
|
|
};
|
|
|
|
|
|
2021-10-25 12:04:34 +05:00
|
|
|
|
db.Users.Add(newUser);
|
2022-04-11 18:00:34 +05:00
|
|
|
|
|
2021-04-02 17:28:07 +05:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
db.SaveChanges();
|
2021-12-07 18:27:52 +05:00
|
|
|
|
db.RelationUserUserRoles.Add(new RelationUserUserRole
|
2021-11-24 11:30:29 +05:00
|
|
|
|
{
|
|
|
|
|
IdUser = newUser.Id,
|
|
|
|
|
IdUserRole = 2
|
|
|
|
|
});
|
|
|
|
|
db.SaveChanges();
|
2021-04-02 17:28:07 +05:00
|
|
|
|
}
|
|
|
|
|
catch //(Exception ex)
|
|
|
|
|
{
|
2021-10-25 12:04:34 +05:00
|
|
|
|
return -7;
|
2021-04-02 17:28:07 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int ChangePassword(string userLogin, string newPassword)
|
|
|
|
|
{
|
2021-08-11 10:16:01 +05:00
|
|
|
|
var user = db.Users.AsNoTracking().FirstOrDefault(u => u.Login == userLogin);
|
2021-04-02 17:28:07 +05:00
|
|
|
|
if (user == null)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
var salt = GenerateSalt();
|
|
|
|
|
user.PasswordHash = salt + ComputeHash(salt, newPassword);
|
|
|
|
|
db.SaveChanges();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int ChangePassword(int idUser, string newPassword)
|
|
|
|
|
{
|
|
|
|
|
var user = db.Users.FirstOrDefault(u => u.Id == idUser);
|
|
|
|
|
if (user == null)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
var salt = GenerateSalt();
|
|
|
|
|
user.PasswordHash = salt + ComputeHash(salt, newPassword);
|
|
|
|
|
db.SaveChanges();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-19 15:21:19 +05:00
|
|
|
|
private static string MakeToken(IEnumerable<Claim> claims)
|
2021-04-02 17:28:07 +05:00
|
|
|
|
{
|
|
|
|
|
var now = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
var jwt = new JwtSecurityToken(
|
2021-12-07 18:27:52 +05:00
|
|
|
|
issuer,
|
|
|
|
|
audience,
|
2021-04-02 17:28:07 +05:00
|
|
|
|
notBefore: now,
|
|
|
|
|
claims: claims,
|
|
|
|
|
expires: now.Add(expiresTimespan),
|
|
|
|
|
signingCredentials: new SigningCredentials(securityKey, algorithms));
|
|
|
|
|
|
|
|
|
|
return new JwtSecurityTokenHandler().WriteToken(jwt);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-24 10:59:10 +05:00
|
|
|
|
private async Task<(ClaimsIdentity Identity, User User)> GetClaimsUserAsync(string login,
|
2021-08-11 16:54:42 +05:00
|
|
|
|
string password, CancellationToken token = default)
|
2021-04-02 17:28:07 +05:00
|
|
|
|
{
|
2021-12-11 16:46:04 +05:00
|
|
|
|
var user = await db.Users
|
|
|
|
|
.Include(e => e.Company)
|
|
|
|
|
.Where(e => e.Login == login)
|
2021-08-11 10:16:01 +05:00
|
|
|
|
.AsNoTracking()
|
2021-08-11 17:26:02 +05:00
|
|
|
|
.FirstOrDefaultAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
2021-04-02 17:28:07 +05:00
|
|
|
|
|
|
|
|
|
if (user is null)
|
|
|
|
|
return default;
|
|
|
|
|
|
|
|
|
|
if (!CheckPassword(user.PasswordHash, password))
|
|
|
|
|
return default;
|
|
|
|
|
|
|
|
|
|
var claims = new List<Claim>
|
|
|
|
|
{
|
2021-12-07 18:27:52 +05:00
|
|
|
|
new (claimIdUser, user.Id.ToString()),
|
|
|
|
|
new (ClaimsIdentity.DefaultNameClaimType, user.Login),
|
|
|
|
|
new (claimNameIdCompany, user.IdCompany.ToString()),
|
2021-04-02 17:28:07 +05:00
|
|
|
|
};
|
2021-12-11 16:46:04 +05:00
|
|
|
|
var roles = userService.GetRolesByIdUser(user.Id);
|
|
|
|
|
if (roles is not null)
|
|
|
|
|
foreach (var role in roles)
|
|
|
|
|
claims.Add(new Claim(ClaimsIdentity.DefaultRoleClaimType, role.Caption));
|
2021-11-24 11:30:29 +05:00
|
|
|
|
|
2021-11-29 12:39:28 +05:00
|
|
|
|
var claimsIdentity = new ClaimsIdentity(claims, "Token", ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType);
|
2021-04-02 17:28:07 +05:00
|
|
|
|
return (claimsIdentity, user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool CheckPassword(string passwordHash, string password)
|
|
|
|
|
{
|
|
|
|
|
if (passwordHash.Length == 0 && password.Length == 0)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
if (passwordHash.Length < PasswordSaltLength)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var salt = passwordHash[0..PasswordSaltLength];
|
|
|
|
|
var hashDb = passwordHash[PasswordSaltLength..];
|
|
|
|
|
|
|
|
|
|
return hashDb == ComputeHash(salt, password);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string ComputeHash(string salt, string password)
|
|
|
|
|
{
|
2021-12-07 18:27:52 +05:00
|
|
|
|
var hashBytes = hashAlgorithm.ComputeHash(encoding.GetBytes(salt + password));
|
2021-04-02 17:28:07 +05:00
|
|
|
|
var hashString = BitConverter.ToString(hashBytes)
|
|
|
|
|
.Replace("-", "")
|
|
|
|
|
.ToLower();
|
|
|
|
|
return hashString;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-07 18:27:52 +05:00
|
|
|
|
private string GenerateSalt()
|
2021-04-02 17:28:07 +05:00
|
|
|
|
{
|
|
|
|
|
const string saltChars = "sHwiaX7kZT1QRp0cPILGUuK2Sz=9q8lmejDNfoYCE3B_WtgyVv6M5OxAJ4Frbhnd";
|
2021-12-07 18:27:52 +05:00
|
|
|
|
var salt = "";
|
|
|
|
|
for (var i = 0; i < PasswordSaltLength - 1; i++)
|
2021-04-02 17:28:07 +05:00
|
|
|
|
salt += saltChars[rnd.Next(0, saltChars.Length)];
|
|
|
|
|
salt += "|";
|
|
|
|
|
return salt;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|