mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 15:11:10 +00:00
Implements Authorization system
This commit is contained in:
1 parent
5bfb2ca212
commit
2c9c210e43
68 files changed
+2734
-336
No files matched your search
+13
-4
@@ -6,6 +6,7 @@ using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Errors;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Security;
|
||||
using Lumina.Application.Common.Infrastructure.Time;
|
||||
using Lumina.Contracts.Responses.Authentication;
|
||||
using Mediator;
|
||||
using System;
|
||||
@@ -27,6 +28,7 @@ public class SetupApplicationCommandHandler : IRequestHandler<SetupApplicationCo
|
||||
private readonly ICryptographyService _cryptographyService;
|
||||
private readonly ITotpTokenGenerator _totpTokenGenerator;
|
||||
private readonly IQRCodeGenerator _qRCodeGenerator;
|
||||
private readonly IDateTimeProvider _dateTimeProvider;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SetupApplicationCommandHandler"/> class.
|
||||
@@ -36,18 +38,21 @@ public class SetupApplicationCommandHandler : IRequestHandler<SetupApplicationCo
|
||||
/// <param name="cryptographyService">Injected service for cryptographic functionality.</param>
|
||||
/// <param name="totpTokenGenerator">Injected service for generating and validating TOTP tokens.</param>
|
||||
/// <param name="qRCodeGenerator">Injected service for generating QR codes.</param>
|
||||
/// <param name="dateTimeProvider">Injected service for time related concerns.</param>
|
||||
public SetupApplicationCommandHandler(
|
||||
IUnitOfWork unitOfWork,
|
||||
IHashService hashService,
|
||||
ICryptographyService cryptographyService,
|
||||
ITotpTokenGenerator totpTokenGenerator,
|
||||
IQRCodeGenerator qRCodeGenerator)
|
||||
IQRCodeGenerator qRCodeGenerator,
|
||||
IDateTimeProvider dateTimeProvider)
|
||||
{
|
||||
_unitOfWork = unitOfWork;
|
||||
_hashService = hashService;
|
||||
_cryptographyService = cryptographyService;
|
||||
_totpTokenGenerator = totpTokenGenerator;
|
||||
_qRCodeGenerator = qRCodeGenerator;
|
||||
_dateTimeProvider = dateTimeProvider;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -69,13 +74,17 @@ public class SetupApplicationCommandHandler : IRequestHandler<SetupApplicationCo
|
||||
return Errors.Authorization.AdminAccountAlreadyCreated;
|
||||
// no users are present, register the admin one
|
||||
string? totpSecret = null;
|
||||
Guid id = Guid.NewGuid();
|
||||
UserEntity user = new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Id = id,
|
||||
Username = request.Username!,
|
||||
Password = Uri.EscapeDataString(_hashService.HashString(request.Password!)),
|
||||
Created = DateTime.UtcNow,
|
||||
Libraries = []
|
||||
CreatedOnUtc = _dateTimeProvider.UtcNow,
|
||||
Libraries = [],
|
||||
UserPermissions = [], // TODO: implement default roles and permissions
|
||||
UserRoles = [],
|
||||
CreatedBy = id
|
||||
};
|
||||
// if the user enabled two factor auth, include a QR with the totp secret
|
||||
if (request.Use2fa)
|
||||
|
||||
+13
-4
@@ -6,6 +6,7 @@ using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Errors;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Security;
|
||||
using Lumina.Application.Common.Infrastructure.Time;
|
||||
using Lumina.Contracts.Responses.Authentication;
|
||||
using Mediator;
|
||||
using System;
|
||||
@@ -25,6 +26,7 @@ public class RegisterUserCommandHandler : IRequestHandler<RegisterUserCommand, E
|
||||
private readonly ICryptographyService _cryptographyService;
|
||||
private readonly ITotpTokenGenerator _totpTokenGenerator;
|
||||
private readonly IQRCodeGenerator _qRCodeGenerator;
|
||||
private readonly IDateTimeProvider _dateTimeProvider;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RegisterUserCommandHandler"/> class.
|
||||
@@ -34,18 +36,21 @@ public class RegisterUserCommandHandler : IRequestHandler<RegisterUserCommand, E
|
||||
/// <param name="cryptographyService">Injected service for cryptographic functionality.</param>
|
||||
/// <param name="totpTokenGenerator">Injected service for generating and validating TOTP tokens.</param>
|
||||
/// <param name="qRCodeGenerator">Injected service for generating QR codes.</param>
|
||||
/// <param name="dateTimeProvider">Injected service for time related concerns.</param>
|
||||
public RegisterUserCommandHandler(
|
||||
IUnitOfWork unitOfWork,
|
||||
IHashService hashService,
|
||||
ICryptographyService cryptographyService,
|
||||
ITotpTokenGenerator totpTokenGenerator,
|
||||
IQRCodeGenerator qRCodeGenerator)
|
||||
IQRCodeGenerator qRCodeGenerator,
|
||||
IDateTimeProvider dateTimeProvider)
|
||||
{
|
||||
_unitOfWork = unitOfWork;
|
||||
_hashService = hashService;
|
||||
_cryptographyService = cryptographyService;
|
||||
_totpTokenGenerator = totpTokenGenerator;
|
||||
_qRCodeGenerator = qRCodeGenerator;
|
||||
_dateTimeProvider = dateTimeProvider;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -66,13 +71,17 @@ public class RegisterUserCommandHandler : IRequestHandler<RegisterUserCommand, E
|
||||
else if (resultSelectUser.Value is not null)
|
||||
return Errors.Authentication.UsernameAlreadyExists;
|
||||
string? totpSecret = null;
|
||||
Guid id = Guid.NewGuid();
|
||||
UserEntity user = new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Id = id,
|
||||
Username = request.Username!,
|
||||
Password = Uri.EscapeDataString(_hashService.HashString(request.Password!)),
|
||||
Created = DateTime.UtcNow,
|
||||
Libraries = []
|
||||
CreatedOnUtc = _dateTimeProvider.UtcNow,
|
||||
Libraries = [],
|
||||
UserPermissions = [],
|
||||
UserRoles = [],
|
||||
CreatedBy = id
|
||||
};
|
||||
// if the user enabled two factor auth, include a QR with the totp secret
|
||||
if (request.Use2fa)
|
||||
|
||||
+7
-2
@@ -6,6 +6,7 @@ using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Errors;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Security;
|
||||
using Lumina.Application.Common.Infrastructure.Time;
|
||||
using Lumina.Contracts.Responses.Authentication;
|
||||
using Mediator;
|
||||
using System;
|
||||
@@ -25,6 +26,7 @@ public class LoginUserQueryHandler : IRequestHandler<LoginUserQuery, ErrorOr<Log
|
||||
private readonly IJwtTokenGenerator _jwtTokenGenerator;
|
||||
private readonly ITotpTokenGenerator _totpTokenGenerator;
|
||||
private readonly ICryptographyService _cryptographyService;
|
||||
private readonly IDateTimeProvider _dateTimeProvider;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LoginUserQueryHandler"/> class.
|
||||
@@ -34,18 +36,21 @@ public class LoginUserQueryHandler : IRequestHandler<LoginUserQuery, ErrorOr<Log
|
||||
/// <param name="jwtTokenGenerator">Injected service for generating JWT tokens.</param>
|
||||
/// <param name="totpTokenGenerator">Injected service for generating and validating TOTP tokens.</param>
|
||||
/// <param name="cryptographyService">Injected service for cryptographic functionality.</param>
|
||||
/// <param name="dateTimeProvider">Injected service for time related concerns.</param>
|
||||
public LoginUserQueryHandler(
|
||||
IUnitOfWork unitOfWork,
|
||||
IHashService hashService,
|
||||
IJwtTokenGenerator jwtTokenGenerator,
|
||||
ITotpTokenGenerator totpTokenGenerator,
|
||||
ICryptographyService cryptographyService)
|
||||
ICryptographyService cryptographyService,
|
||||
IDateTimeProvider dateTimeProvider)
|
||||
{
|
||||
_unitOfWork = unitOfWork;
|
||||
_hashService = hashService;
|
||||
_jwtTokenGenerator = jwtTokenGenerator;
|
||||
_totpTokenGenerator = totpTokenGenerator;
|
||||
_cryptographyService = cryptographyService;
|
||||
_dateTimeProvider = dateTimeProvider;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -72,7 +77,7 @@ public class LoginUserQueryHandler : IRequestHandler<LoginUserQuery, ErrorOr<Log
|
||||
if (resultSelectUser.Value.TempPassword is not null)
|
||||
{
|
||||
// the temporary password should only be valid for 15 minutes, if its obsolete, remove it and return error
|
||||
if (resultSelectUser.Value.TempPasswordCreated!.Value.AddMinutes(15) < DateTime.UtcNow)
|
||||
if (resultSelectUser.Value.TempPasswordCreated!.Value.AddMinutes(15) < _dateTimeProvider.UtcNow)
|
||||
{
|
||||
resultSelectUser.Value.TempPassword = null;
|
||||
resultSelectUser.Value.TempPasswordCreated = null;
|
||||
|
||||
Reference in new issue
Block a user