Doxy, -Wall -Werror, other cleanup and organization first round

This commit is contained in:
2023-08-19 20:46:31 -04:00
parent 1fedd4d016
commit 49de8aa8d7
43 changed files with 728 additions and 354 deletions

View File

@ -1,23 +1,31 @@
using IO.Swagger.Models.db;
using System.Security.Cryptography;
using System;
using IO.Swagger.Models.RequestDto;
using IO.Swagger.Services;
using IO.Swagger.Models.dto;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System;
using System.Security.Cryptography;
using System.Threading.Tasks;
namespace IO.Swagger.Repositories
{
/// <summary>
/// The EF implementation of this interface
/// </summary>
/// <seealso cref="IO.Swagger.Repositories.IUserRepository" />
public class UserRepository : IUserRepository
{
private readonly BankDbContext bankDbContext;
/// <summary>
/// Initializes a new instance of the <see cref="UserRepository"/> class.
/// </summary>
/// <param name="bankDbContext">The bank database context.</param>
public UserRepository(BankDbContext bankDbContext)
{
this.bankDbContext = bankDbContext;
}
/// <inheritdoc/>
public async Task<User> RegisterUser(AuthRegisterBody request)
{
request.Email = request.Email.ToLower();
@ -25,15 +33,14 @@ namespace IO.Swagger.Repositories
return null;
// Generate a random salt
byte[] saltBytes = new byte[16];
new RNGCryptoServiceProvider().GetBytes(saltBytes);
byte[] saltBytes = RandomNumberGenerator.GetBytes(16);
string salt = Convert.ToBase64String(saltBytes);
// Hash the password along with the salt
string password = request.Password;
string saltedPassword = password + salt;
byte[] passwordBytes = System.Text.Encoding.UTF8.GetBytes(saltedPassword);
byte[] hashedBytes = new SHA256Managed().ComputeHash(passwordBytes);
byte[] hashedBytes = SHA256.HashData(passwordBytes);
string hashedPassword = Convert.ToBase64String(hashedBytes);
// Create and insert the user
@ -51,24 +58,26 @@ namespace IO.Swagger.Repositories
return newUser;
}
/// <inheritdoc/>
public async Task<User> LoginUser(AuthLoginBody request)
{
request.Email = request.Email.ToLower();
var user = await bankDbContext.Users.FirstOrDefaultAsync(u => u.Email.Equals(request.Email));
if (user == null)
if (user == null)
return null;
// Hash the supplied password with the retrieved salt
string password = request.Password;
string saltedPassword = password + user.Salt;
byte[] passwordBytes = System.Text.Encoding.UTF8.GetBytes(saltedPassword);
byte[] hashedBytes = new SHA256Managed().ComputeHash(passwordBytes);
byte[] hashedBytes = SHA256.HashData(passwordBytes);
string hashedPassword = Convert.ToBase64String(hashedBytes);
if (hashedPassword != user.PasswordHash)
return null;
return user;
}
/// <inheritdoc/>
public async Task<User> RetrieveUser(int userId)
{
return await bankDbContext.Users.FirstOrDefaultAsync(u => u.Id == userId);