2024-03-15 21:17:05 -05:00
|
|
|
namespace AAIntegration.SimmonsBank.API.Services;
|
|
|
|
|
2024-03-15 21:35:24 -05:00
|
|
|
using AutoMapper;
|
|
|
|
using BCrypt.Net;
|
|
|
|
using AAIntegration.SimmonsBank.API.Entities;
|
|
|
|
using AAIntegration.SimmonsBank.API.Config;
|
|
|
|
using AAIntegration.SimmonsBank.API.Models.Users;
|
2024-03-15 21:17:05 -05:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using Microsoft.EntityFrameworkCore.Internal;
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
using System.Text;
|
2024-03-15 21:35:24 -05:00
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
2024-03-15 21:17:05 -05:00
|
|
|
using System.Security.Claims;
|
|
|
|
using Microsoft.Extensions.Options;
|
2024-03-19 21:21:27 -05:00
|
|
|
using System.Security.Cryptography;
|
2024-03-15 21:17:05 -05:00
|
|
|
|
|
|
|
public interface IUserService
|
|
|
|
{
|
2024-03-19 21:21:27 -05:00
|
|
|
// New Based way
|
|
|
|
string Create(UserCreateRequest model);
|
|
|
|
void Update(string apiKey, UserUpdateRequest model);
|
|
|
|
void Delete(string apiKey);
|
|
|
|
Dictionary<string, int> GetAllApiKeys();
|
|
|
|
|
|
|
|
/* Other cringe way
|
2024-03-15 21:35:24 -05:00
|
|
|
AuthenticateResponse Authenticate(AuthenticateRequest model);
|
|
|
|
void Register(RegisterRequest model);
|
|
|
|
IEnumerable<User> GetAll();
|
|
|
|
User GetById(int id);
|
|
|
|
void Update(int id, UserUpdateRequest model);
|
|
|
|
void Delete(int id);
|
2024-03-15 21:17:05 -05:00
|
|
|
Dictionary<string, int> GetAllApiKeys();
|
2024-03-15 21:35:24 -05:00
|
|
|
string GetUserApiKey(int id);
|
|
|
|
void InvalidateApiKey(string apiKey);
|
|
|
|
string CreateUserApiKey(int id);
|
2024-03-19 21:21:27 -05:00
|
|
|
*/
|
2024-03-15 21:17:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public class UserService : IUserService
|
|
|
|
{
|
|
|
|
private DataContext _context;
|
2024-03-15 21:35:24 -05:00
|
|
|
private readonly IMapper _mapper;
|
|
|
|
private readonly IOptions<AppSettings> _appSettings;
|
2024-03-15 21:17:05 -05:00
|
|
|
|
|
|
|
public UserService(
|
|
|
|
DataContext context,
|
2024-03-15 21:35:24 -05:00
|
|
|
IMapper mapper,
|
2024-03-15 21:17:05 -05:00
|
|
|
IOptions<AppSettings> appSettings)
|
|
|
|
{
|
|
|
|
_context = context;
|
2024-03-15 21:35:24 -05:00
|
|
|
_mapper = mapper;
|
|
|
|
_appSettings = appSettings;
|
|
|
|
}
|
|
|
|
|
2024-03-19 21:21:27 -05:00
|
|
|
public string Create(UserCreateRequest model)
|
2024-03-15 21:17:05 -05:00
|
|
|
{
|
2024-03-19 21:21:27 -05:00
|
|
|
User user = new User();
|
|
|
|
user.SimmonsBankUsername = model.Username;
|
|
|
|
user.SimmonsBankPassword = model.Password;
|
|
|
|
user.MFAKey = model.MFAKey;
|
2024-03-15 21:35:24 -05:00
|
|
|
|
2024-03-19 21:21:27 -05:00
|
|
|
// Generate API Key
|
|
|
|
user.ApiKey = generateApiKey();
|
2024-03-15 21:17:05 -05:00
|
|
|
|
|
|
|
// save user
|
|
|
|
_context.Users.Add(user);
|
|
|
|
_context.SaveChanges();
|
|
|
|
|
2024-03-19 21:21:27 -05:00
|
|
|
// Return API Key
|
|
|
|
return user.ApiKey;
|
2024-03-15 21:35:24 -05:00
|
|
|
}
|
2024-03-15 21:17:05 -05:00
|
|
|
|
2024-03-19 21:21:27 -05:00
|
|
|
public void Update(string apiKey, UserUpdateRequest model)
|
2024-03-15 21:17:05 -05:00
|
|
|
{
|
2024-03-19 21:21:27 -05:00
|
|
|
var user = getUser(apiKey);
|
2024-03-15 21:17:05 -05:00
|
|
|
|
2024-03-19 21:21:27 -05:00
|
|
|
// User.Username
|
|
|
|
if (model.Username != null)
|
|
|
|
user.SimmonsBankUsername = model.Username;
|
2024-03-15 21:17:05 -05:00
|
|
|
|
2024-03-19 21:21:27 -05:00
|
|
|
// User.Password
|
|
|
|
if (model.Password != null)
|
|
|
|
user.SimmonsBankPassword = model.Password;
|
2024-03-15 21:17:05 -05:00
|
|
|
|
2024-03-19 21:21:27 -05:00
|
|
|
// User.MFAKey
|
|
|
|
if (model.MFAKey != null)
|
|
|
|
user.MFAKey = model.MFAKey;
|
2024-03-15 21:17:05 -05:00
|
|
|
|
|
|
|
_context.Users.Update(user);
|
|
|
|
_context.SaveChanges();
|
|
|
|
}
|
|
|
|
|
2024-03-19 21:21:27 -05:00
|
|
|
public void Delete(string apiKey)
|
2024-03-15 21:17:05 -05:00
|
|
|
{
|
2024-03-19 21:21:27 -05:00
|
|
|
var user = getUser(apiKey);
|
2024-03-15 21:17:05 -05:00
|
|
|
_context.Users.Remove(user);
|
|
|
|
_context.SaveChanges();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Dictionary<string, int> GetAllApiKeys()
|
|
|
|
{
|
|
|
|
return _context.Users
|
|
|
|
.Where(u => u.ApiKey != null)
|
|
|
|
.ToDictionary(u => u.ApiKey, u => u.Id);
|
|
|
|
}
|
|
|
|
|
2024-03-19 21:21:27 -05:00
|
|
|
|
|
|
|
// helper methods
|
|
|
|
|
|
|
|
private User getUser(int id)
|
2024-03-15 21:17:05 -05:00
|
|
|
{
|
2024-03-19 21:21:27 -05:00
|
|
|
var user = _context.Users.Find(id);
|
|
|
|
if (user == null) throw new KeyNotFoundException("User not found");
|
|
|
|
return user;
|
2024-03-15 21:35:24 -05:00
|
|
|
}
|
2024-03-15 21:17:05 -05:00
|
|
|
|
2024-03-19 21:21:27 -05:00
|
|
|
private User getUser(string ApiKey)
|
2024-03-15 21:17:05 -05:00
|
|
|
{
|
2024-03-19 21:21:27 -05:00
|
|
|
var user = _context.Users
|
|
|
|
.Where(u => u.ApiKey == ApiKey)
|
|
|
|
.FirstOrDefault();
|
2024-03-15 21:17:05 -05:00
|
|
|
|
2024-03-19 21:21:27 -05:00
|
|
|
if (user == null) throw new KeyNotFoundException("User not found");
|
|
|
|
return user;
|
2024-03-15 21:35:24 -05:00
|
|
|
}
|
2024-03-15 21:17:05 -05:00
|
|
|
|
2024-03-19 21:21:27 -05:00
|
|
|
private const string _prefix = "CT-";
|
|
|
|
private const int _numberOfSecureBytesToGenerate = 32;
|
|
|
|
private const int _lengthOfKey = 32;
|
2024-03-15 21:17:05 -05:00
|
|
|
|
2024-03-19 21:21:27 -05:00
|
|
|
private string generateApiKey()
|
2024-03-15 21:17:05 -05:00
|
|
|
{
|
2024-03-19 21:21:27 -05:00
|
|
|
var bytes = RandomNumberGenerator.GetBytes(_numberOfSecureBytesToGenerate);
|
|
|
|
|
|
|
|
string base64String = Convert.ToBase64String(bytes)
|
|
|
|
.Replace("+", "-")
|
|
|
|
.Replace("/", "_");
|
|
|
|
|
|
|
|
var keyLength = _lengthOfKey - _prefix.Length;
|
|
|
|
|
|
|
|
return _prefix + base64String[..keyLength];
|
2024-03-15 21:17:05 -05:00
|
|
|
}
|
2024-03-19 21:21:27 -05:00
|
|
|
|
2024-03-15 21:17:05 -05:00
|
|
|
}
|