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!,
|
||||
|
||||
@@ -27,6 +27,34 @@ public class UsersController : ControllerBase
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
[HttpPost("login")]
|
||||
public IActionResult Login([FromBody] User userRequest)
|
||||
{
|
||||
try
|
||||
{
|
||||
var connString = this._config.GetConnectionString("MongoDBURI");
|
||||
var userRepo = new UsersRepository(connString!);
|
||||
var saltRepo = new SaltRepository(connString!);
|
||||
var pwdVerify = new PasswordVerification(userRepo, saltRepo);
|
||||
var result = userRepo.Exists(userRequest);
|
||||
|
||||
if (result.Item1 && pwdVerify.VerifyPassword(result.Item2, userRequest.Password!))
|
||||
{
|
||||
Ok(result.Item2);
|
||||
}
|
||||
else
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this._logger.LogError($"An error occurred: {ex.Message}");
|
||||
}
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpPost("register"), DisableRequestSizeLimit]
|
||||
public IActionResult Upload([FromBody] User userRequest)
|
||||
{
|
||||
|
||||
@@ -30,6 +30,20 @@ public class UsersRepository
|
||||
#endregion
|
||||
|
||||
#region Metods
|
||||
public Tuple<bool, User> Exists(User user)
|
||||
{
|
||||
var allUsers = this.RetrieveAllUsers();
|
||||
var result = allUsers.FirstOrDefault(ea => ea.Username!.Equals(user.Username));
|
||||
|
||||
if (result != null)
|
||||
{
|
||||
return new Tuple<bool, User>(true, result!);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Tuple<bool, User>(false, new User());
|
||||
}
|
||||
}
|
||||
public void CreateUser(User user)
|
||||
{
|
||||
this._bookCollection.InsertOne(user);
|
||||
@@ -92,6 +106,11 @@ public class SaltRepository
|
||||
return this._bookCollection.Find(_ => true).ToList();
|
||||
}
|
||||
|
||||
public Salt Retrieve(string userId)
|
||||
{
|
||||
return this._bookCollection.Find(s => s.UserId.Equals(userId)).FirstOrDefault();
|
||||
}
|
||||
|
||||
public void Update(Salt salt)
|
||||
{
|
||||
var filter = Builders<Salt>.Filter.Eq(u => u.Id, salt.Id);
|
||||
|
||||
Reference in New Issue
Block a user