Added login endpoint
Started adding endpoint to login users
This commit is contained in:
@@ -3,20 +3,39 @@ using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
using TextSender_API.Models;
|
||||
using TextSender_API.Repositories;
|
||||
|
||||
namespace TextSender_API.Controllers;
|
||||
|
||||
public class PasswordVerification
|
||||
{
|
||||
#region Fiends
|
||||
private UsersRepository? _userRepo;
|
||||
private SaltRepository? _saltRepo;
|
||||
private int _keySize = 64;
|
||||
private int _iterations = 350000;
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
public PasswordVerification()
|
||||
{
|
||||
}
|
||||
|
||||
public PasswordVerification(UsersRepository userRepo, SaltRepository saltRepo)
|
||||
{
|
||||
this._userRepo = userRepo;
|
||||
this._saltRepo = saltRepo;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
public bool VerifyPassword(User user, string password)
|
||||
{
|
||||
var salt = this._saltRepo.Retrieve(user.Id);
|
||||
var hashedPassword = this.HashPassword(user, salt, password);
|
||||
|
||||
return hashedPassword.Equals(user.Password);
|
||||
}
|
||||
public Salt CreateSalt(User user)
|
||||
{
|
||||
var salt = new Salt();
|
||||
@@ -25,10 +44,15 @@ public class PasswordVerification
|
||||
return salt;
|
||||
}
|
||||
|
||||
public string HashPassword(User user, Salt salt)
|
||||
public string HashPassword(User user, Salt salt, string password = "")
|
||||
{
|
||||
var hashAlgorithm = HashAlgorithmName.SHA512;
|
||||
|
||||
if (!string.IsNullOrEmpty(password))
|
||||
{
|
||||
user.Password = password;
|
||||
}
|
||||
|
||||
var hash = Rfc2898DeriveBytes.Pbkdf2(
|
||||
Encoding.UTF8.GetBytes(user.Password!),
|
||||
salt.Key!,
|
||||
|
||||
Reference in New Issue
Block a user