diff --git a/src/MyRecipes.Application/DependencyInjectionExtension.cs b/src/MyRecipes.Application/DependencyInjectionExtension.cs index 5dc01b6..e199401 100644 --- a/src/MyRecipes.Application/DependencyInjectionExtension.cs +++ b/src/MyRecipes.Application/DependencyInjectionExtension.cs @@ -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; @@ -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) @@ -33,11 +31,4 @@ private static void AddUseCases(IServiceCollection services) services.AddScoped(); services.AddScoped(); } - - private static void AddPasswordEncripter(IServiceCollection services, IConfiguration configuration) - { - var additionalKey = configuration.GetValue("Settings:Passwords:AdditionalKey"); - - services.AddScoped(options => new PasswordEncripter(additionalKey!)); - } } diff --git a/src/MyRecipes.Application/UseCases/Login/DoLogin/DoLoginUseCase.cs b/src/MyRecipes.Application/UseCases/Login/DoLogin/DoLoginUseCase.cs index e756ef5..77d2322 100644 --- a/src/MyRecipes.Application/UseCases/Login/DoLogin/DoLoginUseCase.cs +++ b/src/MyRecipes.Application/UseCases/Login/DoLogin/DoLoginUseCase.cs @@ -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; @@ -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; diff --git a/src/MyRecipes.Application/UseCases/User/Register/RegisterUserUseCase.cs b/src/MyRecipes.Application/UseCases/User/Register/RegisterUserUseCase.cs index ca690b4..98e6288 100644 --- a/src/MyRecipes.Application/UseCases/User/Register/RegisterUserUseCase.cs +++ b/src/MyRecipes.Application/UseCases/User/Register/RegisterUserUseCase.cs @@ -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; @@ -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) { diff --git a/src/MyRecipes.Domain/Security/Cryptography/IPasswordEncripter.cs b/src/MyRecipes.Domain/Security/Cryptography/IPasswordEncripter.cs new file mode 100644 index 0000000..8f8952d --- /dev/null +++ b/src/MyRecipes.Domain/Security/Cryptography/IPasswordEncripter.cs @@ -0,0 +1,5 @@ +namespace MyRecipes.Domain.Security.Cryptography; +public interface IPasswordEncripter +{ + public string Encrypt(string password); +} diff --git a/src/MyRecipes.Infrastructure/DependencyInjectionExtension.cs b/src/MyRecipes.Infrastructure/DependencyInjectionExtension.cs index 79cafc8..977c6b5 100644 --- a/src/MyRecipes.Infrastructure/DependencyInjectionExtension.cs +++ b/src/MyRecipes.Infrastructure/DependencyInjectionExtension.cs @@ -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; @@ -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; @@ -76,4 +79,11 @@ private static void AddLoggedUser(IServiceCollection services) { services.AddScoped(); } + + private static void AddPasswordEncripter(IServiceCollection services, IConfiguration configuration) + { + var additionalKey = configuration.GetValue("Settings:Passwords:AdditionalKey"); + + services.AddScoped(options => new Sha512Encripter(additionalKey!)); + } } diff --git a/src/MyRecipes.Infrastructure/MyRecipes.Infrastructure.csproj b/src/MyRecipes.Infrastructure/MyRecipes.Infrastructure.csproj index 00b3df8..d89bbc9 100644 --- a/src/MyRecipes.Infrastructure/MyRecipes.Infrastructure.csproj +++ b/src/MyRecipes.Infrastructure/MyRecipes.Infrastructure.csproj @@ -6,6 +6,10 @@ enable + + + + diff --git a/src/MyRecipes.Application/Services/Cryptography/PasswordEncripter.cs b/src/MyRecipes.Infrastructure/Security/Cryptography/Sha512Encripter.cs similarity index 72% rename from src/MyRecipes.Application/Services/Cryptography/PasswordEncripter.cs rename to src/MyRecipes.Infrastructure/Security/Cryptography/Sha512Encripter.cs index 9beb9fd..64699af 100644 --- a/src/MyRecipes.Application/Services/Cryptography/PasswordEncripter.cs +++ b/src/MyRecipes.Infrastructure/Security/Cryptography/Sha512Encripter.cs @@ -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; } diff --git a/tests/CommonTestsUtilities/Cryptography/PasswordEncripterBuilder.cs b/tests/CommonTestsUtilities/Cryptography/PasswordEncripterBuilder.cs index bad6264..d6c0250 100644 --- a/tests/CommonTestsUtilities/Cryptography/PasswordEncripterBuilder.cs +++ b/tests/CommonTestsUtilities/Cryptography/PasswordEncripterBuilder.cs @@ -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"); }