Skip to content

Commit

Permalink
refactor(*): use application security entities
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmet-cetinkaya committed Feb 15, 2024
1 parent e7b9578 commit a9fedc7
Show file tree
Hide file tree
Showing 109 changed files with 596 additions and 562 deletions.
4 changes: 2 additions & 2 deletions src/starterProject/Application/Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
<ProjectReference Include="..\Domain\Domain.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="12.0.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
<PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="MediatR" Version="12.2.0" />
<PackageReference Include="NArchitecture.Core.Application" Version="1.0.0" />
<PackageReference Include="NArchitecture.Core.Mailing" Version="1.0.0" />
Expand All @@ -19,6 +18,7 @@
<PackageReference Include="NArchitecture.Core.Localization.Abstraction" Version="1.0.0" />
<PackageReference Include="NArchitecture.Core.Localization.Resource.Yaml.DependencyInjection" Version="1.0.0" />
<PackageReference Include="NArchitecture.Core.ElasticSearch" Version="1.0.0" />
<PackageReference Include="NArchitecture.Core.Security.DependencyInjection" Version="1.0.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Services\" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using NArchitecture.Core.Localization.Resource.Yaml.DependencyInjection;
using NArchitecture.Core.Mailing;
using NArchitecture.Core.Mailing.MailKit;
using NArchitecture.Core.Security.DependencyInjection;

namespace Application;

Expand Down Expand Up @@ -56,6 +57,8 @@ ElasticSearchConfig elasticSearchConfig

services.AddYamlResourceLocalization();

services.AddSecurityServices<Guid, int>();

return services;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
using Application.Services.AuthenticatorService;
using Application.Services.Repositories;
using Application.Services.UsersService;
using Domain.Entities;
using MediatR;
using MimeKit;
using NArchitecture.Core.Application.Pipelines.Authorization;
using NArchitecture.Core.Mailing;
using NArchitecture.Core.Security.Entities;
using NArchitecture.Core.Security.Enums;

namespace Application.Features.Auth.Commands.EnableEmailAuthenticator;

public class EnableEmailAuthenticatorCommand : IRequest, ISecuredRequest
{
public int UserId { get; set; }
public Guid UserId { get; set; }
public string VerifyEmailUrlPrefix { get; set; }

public string[] Roles => [];
Expand All @@ -24,7 +24,7 @@ public EnableEmailAuthenticatorCommand()
VerifyEmailUrlPrefix = string.Empty;
}

public EnableEmailAuthenticatorCommand(int userId, string verifyEmailUrlPrefix)
public EnableEmailAuthenticatorCommand(Guid userId, string verifyEmailUrlPrefix)
{
UserId = userId;
VerifyEmailUrlPrefix = verifyEmailUrlPrefix;
Expand Down Expand Up @@ -55,18 +55,15 @@ IAuthenticatorService authenticatorService

public async Task Handle(EnableEmailAuthenticatorCommand request, CancellationToken cancellationToken)
{
User<int, int>? user = await _userService.GetAsync(
predicate: u => u.Id == request.UserId,
cancellationToken: cancellationToken
);
User? user = await _userService.GetAsync(predicate: u => u.Id == request.UserId, cancellationToken: cancellationToken);
await _authBusinessRules.UserShouldBeExistsWhenSelected(user);
await _authBusinessRules.UserShouldNotBeHaveAuthenticator(user!);

user!.AuthenticatorType = AuthenticatorType.Email;
await _userService.UpdateAsync(user);

EmailAuthenticator<int, int> emailAuthenticator = await _authenticatorService.CreateEmailAuthenticator(user);
EmailAuthenticator<int, int> addedEmailAuthenticator = await _emailAuthenticatorRepository.AddAsync(emailAuthenticator);
EmailAuthenticator emailAuthenticator = await _authenticatorService.CreateEmailAuthenticator(user);
EmailAuthenticator addedEmailAuthenticator = await _emailAuthenticatorRepository.AddAsync(emailAuthenticator);

var toEmailList = new List<MailboxAddress> { new(name: user.Email, user.Email) };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
using Application.Services.AuthenticatorService;
using Application.Services.Repositories;
using Application.Services.UsersService;
using Domain.Entities;
using MediatR;
using NArchitecture.Core.Application.Pipelines.Authorization;
using NArchitecture.Core.Security.Entities;

namespace Application.Features.Auth.Commands.EnableOtpAuthenticator;

public class EnableOtpAuthenticatorCommand : IRequest<EnabledOtpAuthenticatorResponse>, ISecuredRequest
{
public int UserId { get; set; }
public Guid UserId { get; set; }

public string[] Roles => [];

Expand Down Expand Up @@ -39,23 +39,20 @@ public async Task<EnabledOtpAuthenticatorResponse> Handle(
CancellationToken cancellationToken
)
{
User<int, int>? user = await _userService.GetAsync(
predicate: u => u.Id == request.UserId,
cancellationToken: cancellationToken
);
User? user = await _userService.GetAsync(predicate: u => u.Id == request.UserId, cancellationToken: cancellationToken);
await _authBusinessRules.UserShouldBeExistsWhenSelected(user);
await _authBusinessRules.UserShouldNotBeHaveAuthenticator(user!);

OtpAuthenticator<int, int>? doesExistOtpAuthenticator = await _otpAuthenticatorRepository.GetAsync(
OtpAuthenticator? doesExistOtpAuthenticator = await _otpAuthenticatorRepository.GetAsync(
predicate: o => o.UserId == request.UserId,
cancellationToken: cancellationToken
);
await _authBusinessRules.OtpAuthenticatorThatVerifiedShouldNotBeExists(doesExistOtpAuthenticator);
if (doesExistOtpAuthenticator is not null)
await _otpAuthenticatorRepository.DeleteAsync(doesExistOtpAuthenticator);

OtpAuthenticator<int, int> newOtpAuthenticator = await _authenticatorService.CreateOtpAuthenticator(user!);
OtpAuthenticator<int, int> addedOtpAuthenticator = await _otpAuthenticatorRepository.AddAsync(newOtpAuthenticator);
OtpAuthenticator newOtpAuthenticator = await _authenticatorService.CreateOtpAuthenticator(user!);
OtpAuthenticator addedOtpAuthenticator = await _otpAuthenticatorRepository.AddAsync(newOtpAuthenticator);

EnabledOtpAuthenticatorResponse enabledOtpAuthenticatorDto =
new() { SecretKey = await _authenticatorService.ConvertSecretKeyToString(addedOtpAuthenticator.SecretKey) };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Application.Features.Auth.Commands.Login;
public class LoggedResponse : IResponse
{
public AccessToken? AccessToken { get; set; }
public NArchitecture.Core.Security.Entities.RefreshToken<int, int>? RefreshToken { get; set; }
public Domain.Entities.RefreshToken? RefreshToken { get; set; }
public AuthenticatorType? RequiredAuthenticatorType { get; set; }

public LoggedHttpResponse ToHttpResponse()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using Application.Services.AuthenticatorService;
using Application.Services.AuthService;
using Application.Services.UsersService;
using Domain.Entities;
using MediatR;
using NArchitecture.Core.Application.Dtos;
using NArchitecture.Core.Security.Entities;
using NArchitecture.Core.Security.Enums;
using NArchitecture.Core.Security.JWT;

Expand Down Expand Up @@ -49,16 +49,16 @@ IAuthenticatorService authenticatorService

public async Task<LoggedResponse> Handle(LoginCommand request, CancellationToken cancellationToken)
{
User<int, int>? user = await _userService.GetAsync(
User? user = await _userService.GetAsync(
predicate: u => u.Email == request.UserForLoginDto.Email,
cancellationToken: cancellationToken
);
await _authBusinessRules.UserShouldBeExistsWhenSelected(user);
await _authBusinessRules.UserPasswordShouldBeMatch(user!.Id, request.UserForLoginDto.Password);
await _authBusinessRules.UserPasswordShouldBeMatch(user!, request.UserForLoginDto.Password);

LoggedResponse loggedResponse = new();

if (user.AuthenticatorType is not AuthenticatorType.None)
if (user!.AuthenticatorType is not AuthenticatorType.None)
{
if (request.UserForLoginDto.AuthenticatorCode is null)
{
Expand All @@ -72,13 +72,8 @@ public async Task<LoggedResponse> Handle(LoginCommand request, CancellationToken

AccessToken createdAccessToken = await _authService.CreateAccessToken(user);

NArchitecture.Core.Security.Entities.RefreshToken<int, int> createdRefreshToken = await _authService.CreateRefreshToken(
user,
request.IpAddress
);
NArchitecture.Core.Security.Entities.RefreshToken<int, int> addedRefreshToken = await _authService.AddRefreshToken(
createdRefreshToken
);
Domain.Entities.RefreshToken createdRefreshToken = await _authService.CreateRefreshToken(user, request.IpAddress);
Domain.Entities.RefreshToken addedRefreshToken = await _authService.AddRefreshToken(createdRefreshToken);
await _authService.DeleteOldRefreshTokens(user.Id);

loggedResponse.AccessToken = createdAccessToken;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Application.Features.Auth.Rules;
using Application.Services.AuthService;
using Application.Services.UsersService;
using Domain.Entities;
using MediatR;
using NArchitecture.Core.Security.Entities;
using NArchitecture.Core.Security.JWT;

namespace Application.Features.Auth.Commands.RefreshToken;
Expand Down Expand Up @@ -39,9 +39,7 @@ public RefreshTokenCommandHandler(IAuthService authService, IUserService userSer

public async Task<RefreshedTokensResponse> Handle(RefreshTokenCommand request, CancellationToken cancellationToken)
{
NArchitecture.Core.Security.Entities.RefreshToken<int, int>? refreshToken = await _authService.GetRefreshTokenByToken(
request.RefreshToken
);
Domain.Entities.RefreshToken? refreshToken = await _authService.GetRefreshTokenByToken(request.RefreshToken);
await _authBusinessRules.RefreshTokenShouldBeExists(refreshToken);

if (refreshToken!.RevokedDate != null)
Expand All @@ -52,20 +50,15 @@ await _authService.RevokeDescendantRefreshTokens(
);
await _authBusinessRules.RefreshTokenShouldBeActive(refreshToken);

User<int, int>? user = await _userService.GetAsync(
predicate: u => u.Id == refreshToken.UserId,
cancellationToken: cancellationToken
);
User? user = await _userService.GetAsync(predicate: u => u.Id == refreshToken.UserId, cancellationToken: cancellationToken);
await _authBusinessRules.UserShouldBeExistsWhenSelected(user);

NArchitecture.Core.Security.Entities.RefreshToken<int, int> newRefreshToken = await _authService.RotateRefreshToken(
Domain.Entities.RefreshToken newRefreshToken = await _authService.RotateRefreshToken(
user: user!,
refreshToken,
request.IpAddress
);
NArchitecture.Core.Security.Entities.RefreshToken<int, int> addedRefreshToken = await _authService.AddRefreshToken(
newRefreshToken
);
Domain.Entities.RefreshToken addedRefreshToken = await _authService.AddRefreshToken(newRefreshToken);
await _authService.DeleteOldRefreshTokens(refreshToken.UserId);

AccessToken createdAccessToken = await _authService.CreateAccessToken(user!);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ namespace Application.Features.Auth.Commands.RefreshToken;
public class RefreshedTokensResponse : IResponse
{
public AccessToken AccessToken { get; set; }
public NArchitecture.Core.Security.Entities.RefreshToken<int, int> RefreshToken { get; set; }
public Domain.Entities.RefreshToken RefreshToken { get; set; }

public RefreshedTokensResponse()
{
AccessToken = null!;
RefreshToken = null!;
}

public RefreshedTokensResponse(AccessToken accessToken, NArchitecture.Core.Security.Entities.RefreshToken<int, int> refreshToken)
public RefreshedTokensResponse(AccessToken accessToken, Domain.Entities.RefreshToken refreshToken)
{
AccessToken = accessToken;
RefreshToken = refreshToken;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Application.Features.Auth.Rules;
using Application.Services.AuthService;
using Application.Services.Repositories;
using Domain.Entities;
using MediatR;
using NArchitecture.Core.Application.Dtos;
using NArchitecture.Core.Security.Entities;
using NArchitecture.Core.Security.Hashing;
using NArchitecture.Core.Security.JWT;

Expand Down Expand Up @@ -48,24 +48,19 @@ public async Task<RegisteredResponse> Handle(RegisterCommand request, Cancellati
passwordHash: out byte[] passwordHash,
passwordSalt: out byte[] passwordSalt
);
User<int, int> newUser =
User newUser =
new()
{
Email = request.UserForRegisterDto.Email,
PasswordHash = passwordHash,
PasswordSalt = passwordSalt,
};
User<int, int> createdUser = await _userRepository.AddAsync(newUser);
User createdUser = await _userRepository.AddAsync(newUser);

AccessToken createdAccessToken = await _authService.CreateAccessToken(createdUser);

NArchitecture.Core.Security.Entities.RefreshToken<int, int> createdRefreshToken = await _authService.CreateRefreshToken(
createdUser,
request.IpAddress
);
NArchitecture.Core.Security.Entities.RefreshToken<int, int> addedRefreshToken = await _authService.AddRefreshToken(
createdRefreshToken
);
Domain.Entities.RefreshToken createdRefreshToken = await _authService.CreateRefreshToken(createdUser, request.IpAddress);
Domain.Entities.RefreshToken addedRefreshToken = await _authService.AddRefreshToken(createdRefreshToken);

RegisteredResponse registeredResponse = new() { AccessToken = createdAccessToken, RefreshToken = addedRefreshToken };
return registeredResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ namespace Application.Features.Auth.Commands.Register;
public class RegisteredResponse : IResponse
{
public AccessToken AccessToken { get; set; }
public NArchitecture.Core.Security.Entities.RefreshToken<int, int> RefreshToken { get; set; }
public Domain.Entities.RefreshToken RefreshToken { get; set; }

public RegisteredResponse()
{
AccessToken = null!;
RefreshToken = null!;
}

public RegisteredResponse(AccessToken accessToken, NArchitecture.Core.Security.Entities.RefreshToken<int, int> refreshToken)
public RegisteredResponse(AccessToken accessToken, Domain.Entities.RefreshToken refreshToken)
{
AccessToken = accessToken;
RefreshToken = refreshToken;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ public RevokeTokenCommandHandler(IAuthService authService, AuthBusinessRules aut

public async Task<RevokedTokenResponse> Handle(RevokeTokenCommand request, CancellationToken cancellationToken)
{
NArchitecture.Core.Security.Entities.RefreshToken<int, int>? refreshToken = await _authService.GetRefreshTokenByToken(
request.Token
);
Domain.Entities.RefreshToken? refreshToken = await _authService.GetRefreshTokenByToken(request.Token);
await _authBusinessRules.RefreshTokenShouldBeExists(refreshToken);
await _authBusinessRules.RefreshTokenShouldBeActive(refreshToken!);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ namespace Application.Features.Auth.Commands.RevokeToken;

public class RevokedTokenResponse : IResponse
{
public int Id { get; set; }
public Guid Id { get; set; }
public string Token { get; set; }

public RevokedTokenResponse()
{
Token = string.Empty;
}

public RevokedTokenResponse(int id, string token)
public RevokedTokenResponse(Guid id, string token)
{
Id = id;
Token = token;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Application.Features.Auth.Rules;
using Application.Services.Repositories;
using Domain.Entities;
using MediatR;
using NArchitecture.Core.Security.Entities;

namespace Application.Features.Auth.Commands.VerifyEmailAuthenticator;

Expand Down Expand Up @@ -35,7 +35,7 @@ AuthBusinessRules authBusinessRules

public async Task Handle(VerifyEmailAuthenticatorCommand request, CancellationToken cancellationToken)
{
EmailAuthenticator<int, int>? emailAuthenticator = await _emailAuthenticatorRepository.GetAsync(
EmailAuthenticator? emailAuthenticator = await _emailAuthenticatorRepository.GetAsync(
predicate: e => e.ActivationKey == request.ActivationKey,
cancellationToken: cancellationToken
);
Expand Down
Loading

0 comments on commit a9fedc7

Please sign in to comment.