Adding structure to responses
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
using System;
|
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
@@ -10,17 +9,13 @@ namespace TextSender_API.Controllers;
|
|||||||
public class PasswordVerification
|
public class PasswordVerification
|
||||||
{
|
{
|
||||||
#region Fiends
|
#region Fiends
|
||||||
private UsersRepository? _userRepo;
|
private UsersRepository _userRepo;
|
||||||
private SaltRepository? _saltRepo;
|
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)
|
public PasswordVerification(UsersRepository userRepo, SaltRepository saltRepo)
|
||||||
{
|
{
|
||||||
this._userRepo = userRepo;
|
this._userRepo = userRepo;
|
||||||
@@ -31,7 +26,7 @@ public class PasswordVerification
|
|||||||
#region Methods
|
#region Methods
|
||||||
public bool VerifyPassword(User user, string password)
|
public bool VerifyPassword(User user, string password)
|
||||||
{
|
{
|
||||||
var salt = this._saltRepo.Retrieve(user.Id);
|
var salt = this._saltRepo.Retrieve(user.Id!);
|
||||||
var hashedPassword = this.HashPassword(user, salt, password);
|
var hashedPassword = this.HashPassword(user, salt, password);
|
||||||
|
|
||||||
return hashedPassword.Equals(user.Password);
|
return hashedPassword.Equals(user.Password);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
using TextSender_API.Models;
|
using TextSender_API.Models;
|
||||||
using TextSender_API.Repositories;
|
using TextSender_API.Repositories;
|
||||||
@@ -30,6 +31,8 @@ public class UsersController : ControllerBase
|
|||||||
[HttpPost("login")]
|
[HttpPost("login")]
|
||||||
public IActionResult Login([FromBody] User userRequest)
|
public IActionResult Login([FromBody] User userRequest)
|
||||||
{
|
{
|
||||||
|
var response = new LoginResponse();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var connString = this._config.GetConnectionString("MongoDBURI");
|
var connString = this._config.GetConnectionString("MongoDBURI");
|
||||||
@@ -40,11 +43,12 @@ public class UsersController : ControllerBase
|
|||||||
|
|
||||||
if (result.Item1 && pwdVerify.VerifyPassword(result.Item2, userRequest.Password!))
|
if (result.Item1 && pwdVerify.VerifyPassword(result.Item2, userRequest.Password!))
|
||||||
{
|
{
|
||||||
Ok(result.Item2);
|
response.Data.Add(result.Item2);
|
||||||
|
Ok(response);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return NotFound();
|
return NotFound(response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -52,12 +56,14 @@ public class UsersController : ControllerBase
|
|||||||
this._logger.LogError($"An error occurred: {ex.Message}");
|
this._logger.LogError($"An error occurred: {ex.Message}");
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok();
|
return Ok(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("register"), DisableRequestSizeLimit]
|
[HttpPost("register"), DisableRequestSizeLimit]
|
||||||
public IActionResult Upload([FromBody] User userRequest)
|
public IActionResult Upload([FromBody] User userRequest)
|
||||||
{
|
{
|
||||||
|
var response = new RegisterUserResponse();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var connString = this._config.GetConnectionString("MongoDBURI");
|
var connString = this._config.GetConnectionString("MongoDBURI");
|
||||||
@@ -68,7 +74,7 @@ public class UsersController : ControllerBase
|
|||||||
|
|
||||||
if (!result)
|
if (!result)
|
||||||
{
|
{
|
||||||
var pwdVerify = new PasswordVerification();
|
var pwdVerify = new PasswordVerification(userRepo, saltRepo);
|
||||||
var salt = pwdVerify.CreateSalt(userRequest);
|
var salt = pwdVerify.CreateSalt(userRequest);
|
||||||
var hashedPassword = pwdVerify.HashPassword(userRequest, salt);
|
var hashedPassword = pwdVerify.HashPassword(userRequest, salt);
|
||||||
|
|
||||||
@@ -77,6 +83,8 @@ public class UsersController : ControllerBase
|
|||||||
userRequest.DateCreated = DateTime.Now;
|
userRequest.DateCreated = DateTime.Now;
|
||||||
userRepo.CreateUser(userRequest);
|
userRepo.CreateUser(userRequest);
|
||||||
|
|
||||||
|
response.Data.Add(userRequest);
|
||||||
|
|
||||||
salt.UserId = userRequest.Id;
|
salt.UserId = userRequest.Id;
|
||||||
saltRepo.Create(salt);
|
saltRepo.Create(salt);
|
||||||
}
|
}
|
||||||
@@ -90,7 +98,45 @@ public class UsersController : ControllerBase
|
|||||||
this._logger.LogError($"An error occurred: {ex.Message}");
|
this._logger.LogError($"An error occurred: {ex.Message}");
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok();
|
return Ok(response);
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Responses
|
||||||
|
public class LoginResponse
|
||||||
|
{
|
||||||
|
#region Properties
|
||||||
|
[JsonProperty("data")]
|
||||||
|
public List<User> Data { get; set; }
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Constructors
|
||||||
|
public LoginResponse()
|
||||||
|
{
|
||||||
|
this.Data = new List<User>();
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Methods
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
|
||||||
|
public class RegisterUserResponse
|
||||||
|
{
|
||||||
|
#region Properties
|
||||||
|
[JsonProperty("data")]
|
||||||
|
public List<User> Data { get; set; }
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Constructors
|
||||||
|
public RegisterUserResponse()
|
||||||
|
{
|
||||||
|
this.Data = new List<User>();
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Methods
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-5
@@ -1,10 +1,6 @@
|
|||||||
using System;
|
|
||||||
|
|
||||||
using MongoDB.Bson;
|
using MongoDB.Bson;
|
||||||
using MongoDB.Bson.Serialization.Attributes;
|
using MongoDB.Bson.Serialization.Attributes;
|
||||||
using MongoDB.Bson.Serialization.Conventions;
|
using Newtonsoft.Json;
|
||||||
using MongoDB.Bson.Serialization.IdGenerators;
|
|
||||||
using MongoDB.Driver;
|
|
||||||
|
|
||||||
namespace TextSender_API.Models;
|
namespace TextSender_API.Models;
|
||||||
|
|
||||||
@@ -13,18 +9,25 @@ public class User
|
|||||||
#region Properties
|
#region Properties
|
||||||
[BsonId]
|
[BsonId]
|
||||||
[BsonRepresentation(BsonType.ObjectId)]
|
[BsonRepresentation(BsonType.ObjectId)]
|
||||||
|
[JsonProperty("id")]
|
||||||
public string? Id { get; set; }
|
public string? Id { get; set; }
|
||||||
[BsonElement("firstname")]
|
[BsonElement("firstname")]
|
||||||
|
[JsonProperty("firstname")]
|
||||||
public string? Firstname { get; set; }
|
public string? Firstname { get; set; }
|
||||||
[BsonElement("lastname")]
|
[BsonElement("lastname")]
|
||||||
|
[JsonProperty("lastname")]
|
||||||
public string? Lastname { get; set; }
|
public string? Lastname { get; set; }
|
||||||
[BsonElement("phonenumber")]
|
[BsonElement("phonenumber")]
|
||||||
|
[JsonProperty("phone_number")]
|
||||||
public string? PhoneNumber { get; set; }
|
public string? PhoneNumber { get; set; }
|
||||||
[BsonElement("username")]
|
[BsonElement("username")]
|
||||||
|
[JsonProperty("username")]
|
||||||
public string? Username { get; set; }
|
public string? Username { get; set; }
|
||||||
[BsonElement("password")]
|
[BsonElement("password")]
|
||||||
|
[JsonProperty("password")]
|
||||||
public string? Password { get; set; }
|
public string? Password { get; set; }
|
||||||
[BsonElement("datecreated")]
|
[BsonElement("datecreated")]
|
||||||
|
[JsonProperty("date_created")]
|
||||||
public DateTime? DateCreated { get; set; }
|
public DateTime? DateCreated { get; set; }
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
using Newtonsoft.Json;
|
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
|
|||||||
Reference in New Issue
Block a user