Skip to content

Commit

Permalink
refactor: password encryption implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
raffreitas committed Dec 19, 2024
1 parent 2b92ddd commit 56d59da
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 23 deletions.
9 changes: 0 additions & 9 deletions src/MyRecipes.Application/DependencyInjectionExtension.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MyRecipes.Application.Services.AutoMapper;
using MyRecipes.Application.Services.Cryptography;
using MyRecipes.Application.UseCases.Login.DoLogin;
using MyRecipes.Application.UseCases.User.Profile;
using MyRecipes.Application.UseCases.User.Register;
Expand All @@ -15,7 +14,6 @@ public static void AddApplication(this IServiceCollection services, IConfigurati
{
AddAutoMapper(services);
AddUseCases(services);
AddPasswordEncripter(services, configuration);
}

private static void AddAutoMapper(IServiceCollection services)
Expand All @@ -33,11 +31,4 @@ private static void AddUseCases(IServiceCollection services)
services.AddScoped<IGetUserProfileUseCase, GetUserProfileUseCase>();
services.AddScoped<IUpdateUserUseCase, UpdateUserUseCase>();
}

private static void AddPasswordEncripter(IServiceCollection services, IConfiguration configuration)
{
var additionalKey = configuration.GetValue<string>("Settings:Passwords:AdditionalKey");

services.AddScoped(options => new PasswordEncripter(additionalKey!));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using MyRecipes.Application.Services.Cryptography;
using MyRecipes.Communication.Requests;
using MyRecipes.Communication.Requests;
using MyRecipes.Communication.Responses;
using MyRecipes.Domain.Repositories.User;
using MyRecipes.Domain.Security.Cryptography;
using MyRecipes.Domain.Security.Tokens;
using MyRecipes.Exceptions.ExceptionsBase;

Expand All @@ -10,11 +10,11 @@ namespace MyRecipes.Application.UseCases.Login.DoLogin;
internal class DoLoginUseCase : IDoLoginUseCase
{
private readonly IUserReadOnlyRepository _userReadOnlyRepository;
private readonly PasswordEncripter _passwordEncripter;
private readonly IPasswordEncripter _passwordEncripter;
private readonly IAccessTokenGenerator _accessTokenGenerator;

public DoLoginUseCase(IUserReadOnlyRepository userReadOnlyRepository,
PasswordEncripter passwordEncripter,
IPasswordEncripter passwordEncripter,
IAccessTokenGenerator accessTokenGenerator)
{
_userReadOnlyRepository = userReadOnlyRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using AutoMapper;
using MyRecipes.Application.Services.Cryptography;
using MyRecipes.Communication.Requests;
using MyRecipes.Communication.Responses;
using MyRecipes.Domain.Repositories;
using MyRecipes.Domain.Repositories.User;
using MyRecipes.Domain.Security.Cryptography;
using MyRecipes.Domain.Security.Tokens;
using MyRecipes.Exceptions;
using MyRecipes.Exceptions.ExceptionsBase;
Expand All @@ -15,14 +15,14 @@ internal class RegisterUserUseCase : IRegisterUserUseCase
private readonly IUserWriteOnlyRepository _writeOnlyRepository;
private readonly IUserReadOnlyRepository _readOnlyRepository;
private readonly IMapper _mapper;
private readonly PasswordEncripter _passwordEncripter;
private readonly IPasswordEncripter _passwordEncripter;
private readonly IUnitOfWork _unitOfWork;
private readonly IAccessTokenGenerator _accessTokenGenerator;

public RegisterUserUseCase(IUserWriteOnlyRepository writeOnlyRepository,
IUserReadOnlyRepository readOnlyRepository,
IMapper mapper,
PasswordEncripter passwordEncripter,
IPasswordEncripter passwordEncripter,
IUnitOfWork unitOfWork,
IAccessTokenGenerator accessTokenGenerator)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace MyRecipes.Domain.Security.Cryptography;
public interface IPasswordEncripter
{
public string Encrypt(string password);
}
10 changes: 10 additions & 0 deletions src/MyRecipes.Infrastructure/DependencyInjectionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
using Microsoft.Extensions.DependencyInjection;
using MyRecipes.Domain.Repositories;
using MyRecipes.Domain.Repositories.User;
using MyRecipes.Domain.Security.Cryptography;
using MyRecipes.Domain.Security.Tokens;
using MyRecipes.Domain.Services.LoggedUser;
using MyRecipes.Infrastructure.DataAccess;
using MyRecipes.Infrastructure.DataAccess.Repositories;
using MyRecipes.Infrastructure.Extensions;
using MyRecipes.Infrastructure.Security.Cryptography;
using MyRecipes.Infrastructure.Security.Tokens.Access.Generator;
using MyRecipes.Infrastructure.Security.Tokens.Access.Validator;
using MyRecipes.Infrastructure.Services.LoggedUser;
Expand All @@ -23,6 +25,7 @@ public static void AddInfrastructure(this IServiceCollection services, IConfigur
AddRepositories(services);
AddLoggedUser(services);
AddTokens(services, configuration);
AddPasswordEncripter(services, configuration);

if (configuration.IsUnitTestEnvironment())
return;
Expand Down Expand Up @@ -76,4 +79,11 @@ private static void AddLoggedUser(IServiceCollection services)
{
services.AddScoped<ILoggedUser, LoggedUser>();
}

private static void AddPasswordEncripter(IServiceCollection services, IConfiguration configuration)
{
var additionalKey = configuration.GetValue<string>("Settings:Passwords:AdditionalKey");

services.AddScoped<IPasswordEncripter>(options => new Sha512Encripter(additionalKey!));
}
}
4 changes: 4 additions & 0 deletions src/MyRecipes.Infrastructure/MyRecipes.Infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<InternalsVisibleTo Include="CommonTestsUtilities" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Dapper" Version="2.1.35" />
<PackageReference Include="FluentMigrator" Version="6.2.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System.Security.Cryptography;
using MyRecipes.Domain.Security.Cryptography;
using System.Security.Cryptography;
using System.Text;

namespace MyRecipes.Application.Services.Cryptography;

public class PasswordEncripter
namespace MyRecipes.Infrastructure.Security.Cryptography;
internal class Sha512Encripter : IPasswordEncripter
{
private readonly string _additionalKey;
public PasswordEncripter(string additionalKey)
public Sha512Encripter(string additionalKey)
{
_additionalKey = additionalKey;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using MyRecipes.Application.Services.Cryptography;
using MyRecipes.Domain.Security.Cryptography;
using MyRecipes.Infrastructure.Security.Cryptography;

namespace CommonTestsUtilities.Cryptography;

public class PasswordEncripterBuilder
{
public static PasswordEncripter Build() => new("ABC1234");
public static IPasswordEncripter Build() => new Sha512Encripter("ABC1234");
}

0 comments on commit 56d59da

Please sign in to comment.