Refactoring and password hashing
This commit is contained in:
@@ -0,0 +1,42 @@
|
|||||||
|
using System;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
using TextSender_API.Models;
|
||||||
|
|
||||||
|
namespace TextSender_API.Controllers;
|
||||||
|
|
||||||
|
public class PasswordVerification
|
||||||
|
{
|
||||||
|
#region Fiends
|
||||||
|
private int _keySize = 64;
|
||||||
|
private int _iterations = 350000;
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Constructors
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Methods
|
||||||
|
public Salt CreateSalt(User user)
|
||||||
|
{
|
||||||
|
var salt = new Salt();
|
||||||
|
salt.Key = RandomNumberGenerator.GetBytes(this._keySize);
|
||||||
|
|
||||||
|
return salt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string HashPassword(User user, Salt salt)
|
||||||
|
{
|
||||||
|
var hashAlgorithm = HashAlgorithmName.SHA512;
|
||||||
|
|
||||||
|
var hash = Rfc2898DeriveBytes.Pbkdf2(
|
||||||
|
Encoding.UTF8.GetBytes(user.Password!),
|
||||||
|
salt.Key!,
|
||||||
|
this._iterations,
|
||||||
|
hashAlgorithm,
|
||||||
|
this._keySize);
|
||||||
|
|
||||||
|
return Convert.ToHexString(hash);
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
@@ -9,17 +9,17 @@ namespace TextSender_API.Controllers;
|
|||||||
|
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("api/v1/user")]
|
[Route("api/v1/user")]
|
||||||
public class RegisterController : ControllerBase
|
public class UsersController : ControllerBase
|
||||||
{
|
{
|
||||||
#region Fields
|
#region Fields
|
||||||
private readonly ILogger<RegisterController> _logger;
|
private readonly ILogger<UsersController> _logger;
|
||||||
private IConfiguration _config;
|
private IConfiguration _config;
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
|
|
||||||
public RegisterController(ILogger<RegisterController> logger, IConfiguration config)
|
public UsersController(ILogger<UsersController> logger, IConfiguration config)
|
||||||
{
|
{
|
||||||
this._logger = logger;
|
this._logger = logger;
|
||||||
this._config = config;
|
this._config = config;
|
||||||
@@ -33,15 +33,24 @@ public class RegisterController : ControllerBase
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var connString = this._config.GetConnectionString("MongoDBURI");
|
var connString = this._config.GetConnectionString("MongoDBURI");
|
||||||
var userRepo = new UserRepository(connString);
|
var userRepo = new UsersRepository(connString!);
|
||||||
|
var saltRepo = new SaltRepository(connString!);
|
||||||
var allUsers = userRepo.RetrieveAllUsers();
|
var allUsers = userRepo.RetrieveAllUsers();
|
||||||
var result = allUsers.Exists(ea => ea.Username.Equals(userRequest.Username));
|
var result = allUsers.Exists(ea => ea.Username!.Equals(userRequest.Username));
|
||||||
|
|
||||||
if (!result)
|
if (!result)
|
||||||
{
|
{
|
||||||
|
var pwdVerify = new PasswordVerification();
|
||||||
|
var salt = pwdVerify.CreateSalt(userRequest);
|
||||||
|
var hashedPassword = pwdVerify.HashPassword(userRequest, salt);
|
||||||
|
|
||||||
this._logger.LogInformation("Creating user");
|
this._logger.LogInformation("Creating user");
|
||||||
|
userRequest.Password = hashedPassword;
|
||||||
userRequest.DateCreated = DateTime.Now;
|
userRequest.DateCreated = DateTime.Now;
|
||||||
userRepo.CreateUser(userRequest);
|
userRepo.CreateUser(userRequest);
|
||||||
|
|
||||||
|
salt.UserId = userRequest.Id;
|
||||||
|
saltRepo.Create(salt);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -3,6 +3,7 @@ using System;
|
|||||||
using MongoDB.Bson;
|
using MongoDB.Bson;
|
||||||
using MongoDB.Bson.Serialization.Attributes;
|
using MongoDB.Bson.Serialization.Attributes;
|
||||||
using MongoDB.Bson.Serialization.Conventions;
|
using MongoDB.Bson.Serialization.Conventions;
|
||||||
|
using MongoDB.Bson.Serialization.IdGenerators;
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
|
|
||||||
namespace TextSender_API.Models;
|
namespace TextSender_API.Models;
|
||||||
@@ -27,3 +28,16 @@ public class User
|
|||||||
public DateTime? DateCreated { get; set; }
|
public DateTime? DateCreated { get; set; }
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class Salt
|
||||||
|
{
|
||||||
|
#region Properties
|
||||||
|
[BsonId]
|
||||||
|
[BsonRepresentation(BsonType.ObjectId)]
|
||||||
|
public string? Id { get; set; }
|
||||||
|
[BsonElement("key")]
|
||||||
|
public byte[]? Key { get; set; }
|
||||||
|
[BsonElement("user_id")]
|
||||||
|
public string? UserId { get; set; }
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,61 +0,0 @@
|
|||||||
using System;
|
|
||||||
|
|
||||||
using MongoDB.Bson;
|
|
||||||
using MongoDB.Driver;
|
|
||||||
|
|
||||||
using TextSender_API.Models;
|
|
||||||
|
|
||||||
namespace TextSender_API.Repositories;
|
|
||||||
|
|
||||||
|
|
||||||
public class UserRepository
|
|
||||||
{
|
|
||||||
#region Fields
|
|
||||||
private string _connectionString;
|
|
||||||
private string _databaseName;
|
|
||||||
private string _tableName;
|
|
||||||
private readonly IMongoCollection<User> _bookCollection;
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
|
|
||||||
#region Constructors
|
|
||||||
public UserRepository(string connectionString)
|
|
||||||
{
|
|
||||||
this._connectionString = connectionString;
|
|
||||||
this._databaseName = "TextSender";
|
|
||||||
this._tableName = "Users";
|
|
||||||
var client = this.InitializeClient();
|
|
||||||
this._bookCollection = client.GetDatabase(this._databaseName).GetCollection<User>(this._tableName);
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Metods
|
|
||||||
public void CreateUser(User user)
|
|
||||||
{
|
|
||||||
this._bookCollection.InsertOne(user);
|
|
||||||
var i = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<User> RetrieveAllUsers()
|
|
||||||
{
|
|
||||||
return this._bookCollection.Find(_ => true).ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Update(User user)
|
|
||||||
{
|
|
||||||
var filter = Builders<User>.Filter.Eq(u => u.Id, user.Id);
|
|
||||||
this._bookCollection.ReplaceOne(filter, user);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Delete(User user)
|
|
||||||
{
|
|
||||||
var filter = Builders<User>.Filter.Eq(u => u.Id, user.Id);
|
|
||||||
this._bookCollection.DeleteOne(filter);
|
|
||||||
}
|
|
||||||
|
|
||||||
private MongoClient InitializeClient()
|
|
||||||
{
|
|
||||||
return new MongoClient(this._connectionString);
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
using MongoDB.Bson;
|
||||||
|
using MongoDB.Driver;
|
||||||
|
|
||||||
|
using TextSender_API.Models;
|
||||||
|
|
||||||
|
namespace TextSender_API.Repositories;
|
||||||
|
|
||||||
|
|
||||||
|
public class UsersRepository
|
||||||
|
{
|
||||||
|
#region Fields
|
||||||
|
private string _connectionString;
|
||||||
|
private string _databaseName;
|
||||||
|
private string _tableName;
|
||||||
|
private readonly IMongoCollection<User> _bookCollection;
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region Constructors
|
||||||
|
public UsersRepository(string connectionString)
|
||||||
|
{
|
||||||
|
this._connectionString = connectionString;
|
||||||
|
this._databaseName = "TextSender";
|
||||||
|
this._tableName = "users";
|
||||||
|
var client = this.InitializeClient();
|
||||||
|
this._bookCollection = client.GetDatabase(this._databaseName).GetCollection<User>(this._tableName);
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Metods
|
||||||
|
public void CreateUser(User user)
|
||||||
|
{
|
||||||
|
this._bookCollection.InsertOne(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<User> RetrieveAllUsers()
|
||||||
|
{
|
||||||
|
return this._bookCollection.Find(_ => true).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Update(User user)
|
||||||
|
{
|
||||||
|
var filter = Builders<User>.Filter.Eq(u => u.Id, user.Id);
|
||||||
|
this._bookCollection.ReplaceOne(filter, user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Delete(User user)
|
||||||
|
{
|
||||||
|
var filter = Builders<User>.Filter.Eq(u => u.Id, user.Id);
|
||||||
|
this._bookCollection.DeleteOne(filter);
|
||||||
|
}
|
||||||
|
|
||||||
|
private MongoClient InitializeClient()
|
||||||
|
{
|
||||||
|
return new MongoClient(this._connectionString);
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public class SaltRepository
|
||||||
|
{
|
||||||
|
#region Fields
|
||||||
|
private string _connectionString;
|
||||||
|
private string _databaseName;
|
||||||
|
private string _tableName;
|
||||||
|
private readonly IMongoCollection<Salt> _bookCollection;
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region Constructors
|
||||||
|
public SaltRepository(string connectionString)
|
||||||
|
{
|
||||||
|
this._connectionString = connectionString;
|
||||||
|
this._databaseName = "TextSender";
|
||||||
|
this._tableName = "salt";
|
||||||
|
var client = this.InitializeClient();
|
||||||
|
this._bookCollection = client.GetDatabase(this._databaseName).GetCollection<Salt>(this._tableName);
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Metods
|
||||||
|
public void Create(Salt salt)
|
||||||
|
{
|
||||||
|
this._bookCollection.InsertOne(salt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Salt> RetrieveAll()
|
||||||
|
{
|
||||||
|
return this._bookCollection.Find(_ => true).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Update(Salt salt)
|
||||||
|
{
|
||||||
|
var filter = Builders<Salt>.Filter.Eq(u => u.Id, salt.Id);
|
||||||
|
this._bookCollection.ReplaceOne(filter, salt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Delete(Salt salt)
|
||||||
|
{
|
||||||
|
var filter = Builders<Salt>.Filter.Eq(u => u.Id, salt.Id);
|
||||||
|
this._bookCollection.DeleteOne(filter);
|
||||||
|
}
|
||||||
|
|
||||||
|
private MongoClient InitializeClient()
|
||||||
|
{
|
||||||
|
return new MongoClient(this._connectionString);
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user