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 System.Text;
|
||||||
|
|
||||||
using TextSender_API.Models;
|
using TextSender_API.Models;
|
||||||
|
using TextSender_API.Repositories;
|
||||||
|
|
||||||
namespace TextSender_API.Controllers;
|
namespace TextSender_API.Controllers;
|
||||||
|
|
||||||
public class PasswordVerification
|
public class PasswordVerification
|
||||||
{
|
{
|
||||||
#region Fiends
|
#region Fiends
|
||||||
|
private UsersRepository? _userRepo;
|
||||||
|
private SaltRepository? _saltRepo;
|
||||||
private int _keySize = 64;
|
private int _keySize = 64;
|
||||||
private int _iterations = 350000;
|
private int _iterations = 350000;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
|
public PasswordVerification()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public PasswordVerification(UsersRepository userRepo, SaltRepository saltRepo)
|
||||||
|
{
|
||||||
|
this._userRepo = userRepo;
|
||||||
|
this._saltRepo = saltRepo;
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Methods
|
#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)
|
public Salt CreateSalt(User user)
|
||||||
{
|
{
|
||||||
var salt = new Salt();
|
var salt = new Salt();
|
||||||
@@ -25,10 +44,15 @@ public class PasswordVerification
|
|||||||
return salt;
|
return salt;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string HashPassword(User user, Salt salt)
|
public string HashPassword(User user, Salt salt, string password = "")
|
||||||
{
|
{
|
||||||
var hashAlgorithm = HashAlgorithmName.SHA512;
|
var hashAlgorithm = HashAlgorithmName.SHA512;
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(password))
|
||||||
|
{
|
||||||
|
user.Password = password;
|
||||||
|
}
|
||||||
|
|
||||||
var hash = Rfc2898DeriveBytes.Pbkdf2(
|
var hash = Rfc2898DeriveBytes.Pbkdf2(
|
||||||
Encoding.UTF8.GetBytes(user.Password!),
|
Encoding.UTF8.GetBytes(user.Password!),
|
||||||
salt.Key!,
|
salt.Key!,
|
||||||
|
|||||||
@@ -27,6 +27,34 @@ public class UsersController : ControllerBase
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Methods
|
#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]
|
[HttpPost("register"), DisableRequestSizeLimit]
|
||||||
public IActionResult Upload([FromBody] User userRequest)
|
public IActionResult Upload([FromBody] User userRequest)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -30,6 +30,20 @@ public class UsersRepository
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Metods
|
#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)
|
public void CreateUser(User user)
|
||||||
{
|
{
|
||||||
this._bookCollection.InsertOne(user);
|
this._bookCollection.InsertOne(user);
|
||||||
@@ -92,6 +106,11 @@ public class SaltRepository
|
|||||||
return this._bookCollection.Find(_ => true).ToList();
|
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)
|
public void Update(Salt salt)
|
||||||
{
|
{
|
||||||
var filter = Builders<Salt>.Filter.Eq(u => u.Id, salt.Id);
|
var filter = Builders<Salt>.Filter.Eq(u => u.Id, salt.Id);
|
||||||
|
|||||||
Reference in New Issue
Block a user