diff --git a/Program.cs b/Program.cs index b22d831..2c3a88e 100644 --- a/Program.cs +++ b/Program.cs @@ -1,26 +1,64 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; +using System.Text; +using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.AspNetCore.Builder; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; +using Microsoft.IdentityModel.Tokens; +using solidInCsharp; +using solidInCsharp.Repository; +using solidInCsharp.Service; -namespace solidInCsharp +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. +builder.Services.AddControllers(); + +var key = Encoding.ASCII.GetBytes(Settings.Secret); +builder.Services.AddAuthentication(x => { - public class Program + x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; + x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; +}) +.AddJwtBearer(x => +{ + x.RequireHttpsMetadata = false; + x.SaveToken = true; + x.TokenValidationParameters = new TokenValidationParameters { - public static void Main(string[] args) - { - CreateHostBuilder(args).Build().Run(); - } - - public static IHostBuilder CreateHostBuilder(string[] args) => - Host.CreateDefaultBuilder(args) - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder.UseStartup(); - }); - } + ValidateIssuerSigningKey = true, + IssuerSigningKey = new SymmetricSecurityKey(key), + ValidateIssuer = false, + ValidateAudience = false + }; +}); + +builder.Services.AddDbContext(options => options.UseInMemoryDatabase(databaseName: "Test")); +builder.Services.AddDbContext(options => options.UseInMemoryDatabase(databaseName: "Test") ); + +builder.Services.AddScoped(); +builder.Services.AddScoped(); + +builder.Services.AddScoped(); +builder.Services.AddScoped(); + +builder.Services.AddScoped(); +builder.Services.AddScoped(); + + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseDeveloperExceptionPage(); } + +app.UseHttpsRedirection(); + +app.UseAuthentication(); +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); diff --git a/Service/CriptografiaService.cs b/Service/CriptografiaService.cs index 238a490..26df0b2 100644 --- a/Service/CriptografiaService.cs +++ b/Service/CriptografiaService.cs @@ -21,7 +21,7 @@ public bool ValidarSenha(string senhaCripto, string senhaDigitada) { byte[] hashBytes = Convert.FromBase64String(senhaCripto); byte[] salt = new byte[16]; Array.Copy(hashBytes, 0, salt, 0, 16); - var pbkdf2 = new Rfc2898DeriveBytes(senhaDigitada, salt, 100000); + var pbkdf2 = new Rfc2898DeriveBytes(senhaDigitada, salt, 100000, HashAlgorithmName.SHA256); byte[] hash = pbkdf2.GetBytes(20); for (int i=0; i < 20; i++) { if (hashBytes[i+16] != hash[i]) { @@ -32,9 +32,8 @@ public bool ValidarSenha(string senhaCripto, string senhaDigitada) { } public string CriptografarSenha(string senha) { - byte[] salt; - new RNGCryptoServiceProvider().GetBytes(salt = new byte[16]); - var pbkdf2 = new Rfc2898DeriveBytes(senha, salt, 100000); + byte[] salt = RandomNumberGenerator.GetBytes(16); + var pbkdf2 = new Rfc2898DeriveBytes(senha, salt, 100000, HashAlgorithmName.SHA256); byte[] hash = pbkdf2.GetBytes(20); byte[] hashBytes = new byte[36]; Array.Copy(salt, 0, hashBytes, 0, 16); diff --git a/Startup.cs b/Startup.cs deleted file mode 100644 index ab46c5b..0000000 --- a/Startup.cs +++ /dev/null @@ -1,90 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.HttpsPolicy; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using System.Text; -using System.IdentityModel.Tokens.Jwt; -using System.Security.Claims; -using Microsoft.IdentityModel.Tokens; -using Microsoft.AspNetCore.Authentication.JwtBearer; -using solidInCsharp.Repository; -using solidInCsharp.Service; -using Microsoft.EntityFrameworkCore; - -namespace solidInCsharp -{ - public class Startup - { - public Startup(IConfiguration configuration) - { - Configuration = configuration; - } - - public IConfiguration Configuration { get; } - - // This method gets called by the runtime. Use this method to add services to the container. - public void ConfigureServices(IServiceCollection services) - { - services.AddControllers(); - var key = Encoding.ASCII.GetBytes(Settings.Secret); - services.AddAuthentication(x => - { - x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; - x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; - }) - .AddJwtBearer(x => - { - x.RequireHttpsMetadata = false; - x.SaveToken = true; - x.TokenValidationParameters = new TokenValidationParameters - { - ValidateIssuerSigningKey = true, - IssuerSigningKey = new SymmetricSecurityKey(key), - ValidateIssuer = false, - ValidateAudience = false - }; - }); - - services.AddDbContext(options => options.UseInMemoryDatabase(databaseName: "Test")); - services.AddDbContext(options => options.UseInMemoryDatabase(databaseName: "Test") ); - - services.AddScoped(); - services.AddScoped(); - - services.AddScoped(); - services.AddScoped(); - - services.AddScoped(); - services.AddScoped(); - } - - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) - { - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } - - app.UseHttpsRedirection(); - - app.UseRouting(); - - app.UseAuthentication(); - app.UseAuthorization(); - - app.UseEndpoints(endpoints => - { - endpoints.MapControllers(); - }); - } - } -} diff --git a/app.sln b/app.sln new file mode 100644 index 0000000..1e0c325 --- /dev/null +++ b/app.sln @@ -0,0 +1,33 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "solidInCsharp", "solidInCsharp.csproj", "{25E7D50D-20BA-4BBC-8618-3FB9548E6EC4}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{BC70228D-2A11-4108-8ABA-A1B271468DE8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "solidInCsharp.Tests", "tests\solidInCsharp.Tests\solidInCsharp.Tests.csproj", "{5E528372-8332-4961-BF16-04AD3A5D2765}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {25E7D50D-20BA-4BBC-8618-3FB9548E6EC4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {25E7D50D-20BA-4BBC-8618-3FB9548E6EC4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {25E7D50D-20BA-4BBC-8618-3FB9548E6EC4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {25E7D50D-20BA-4BBC-8618-3FB9548E6EC4}.Release|Any CPU.Build.0 = Release|Any CPU + {5E528372-8332-4961-BF16-04AD3A5D2765}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5E528372-8332-4961-BF16-04AD3A5D2765}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5E528372-8332-4961-BF16-04AD3A5D2765}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5E528372-8332-4961-BF16-04AD3A5D2765}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {5E528372-8332-4961-BF16-04AD3A5D2765} = {BC70228D-2A11-4108-8ABA-A1B271468DE8} + EndGlobalSection +EndGlobal diff --git a/solidInCsharp.csproj b/solidInCsharp.csproj index 03c4060..4fed0c9 100644 --- a/solidInCsharp.csproj +++ b/solidInCsharp.csproj @@ -1,15 +1,18 @@ - netcoreapp3.1 + net8.0 - - - - + + + + + + + diff --git a/tests/solidInCsharp.Tests/CriptografiaServiceTests.cs b/tests/solidInCsharp.Tests/CriptografiaServiceTests.cs new file mode 100644 index 0000000..1726908 --- /dev/null +++ b/tests/solidInCsharp.Tests/CriptografiaServiceTests.cs @@ -0,0 +1,58 @@ +using solidInCsharp.Service; +using Xunit; + +namespace solidInCsharp.Tests +{ + public class CriptografiaServiceTests + { + private readonly ICriptografiaService _criptografiaService; + + public CriptografiaServiceTests() + { + _criptografiaService = new CriptografiaService(); + } + + [Fact] + public void CriptografarSenha_DeveRetornarUmHashValido() + { + // Arrange + var senha = "minhaSenhaSuperSecreta"; + + // Act + var hash = _criptografiaService.CriptografarSenha(senha); + + // Assert + Assert.NotNull(hash); + Assert.NotEmpty(hash); + } + + [Fact] + public void ValidarSenha_DeveRetornarVerdadeiroParaSenhaCorreta() + { + // Arrange + var senha = "minhaSenhaSuperSecreta"; + var hash = _criptografiaService.CriptografarSenha(senha); + + // Act + var resultado = _criptografiaService.ValidarSenha(hash, senha); + + // Assert + Assert.True(resultado); + } + + [Fact] + public void ValidarSenha_DeveRetornarFalsoParaSenhaIncorreta() + { + // Arrange + var senhaCorreta = "minhaSenhaSuperSecreta"; + var senhaIncorreta = "senhaIncorreta"; + var hash = _criptografiaService.CriptografarSenha(senhaCorreta); + + // Act + var resultado = _criptografiaService.ValidarSenha(hash, senhaIncorreta); + + // Assert + Assert.False(resultado); + } + } +} diff --git a/tests/solidInCsharp.Tests/UsuarioServiceTests.cs b/tests/solidInCsharp.Tests/UsuarioServiceTests.cs new file mode 100644 index 0000000..5544c39 --- /dev/null +++ b/tests/solidInCsharp.Tests/UsuarioServiceTests.cs @@ -0,0 +1,40 @@ +using Moq; +using solidInCsharp.Model; +using solidInCsharp.Repository; +using solidInCsharp.Service; +using Xunit; + +namespace solidInCsharp.Tests +{ + public class UsuarioServiceTests + { + private readonly Mock _usuarioRepositoryMock; + private readonly Mock _criptografiaServiceMock; + private readonly Mock _jwtServiceMock; + private readonly IUsuarioService _usuarioService; + + public UsuarioServiceTests() + { + _usuarioRepositoryMock = new Mock(); + _criptografiaServiceMock = new Mock(); + _jwtServiceMock = new Mock(); + _usuarioService = new UsuarioService(_usuarioRepositoryMock.Object, _criptografiaServiceMock.Object, _jwtServiceMock.Object); + } + + [Fact] + public void CriarUsuario_DeveChamarRepositorioParaSalvarUsuario() + { + // Arrange + var email = "teste@teste.com"; + var nome = "Teste"; + var senha = "123"; + _criptografiaServiceMock.Setup(x => x.CriptografarSenha(It.IsAny())).Returns("senhaCriptografada"); + + // Act + _usuarioService.CriarUsuario(email, nome, senha); + + // Assert + _usuarioRepositoryMock.Verify(x => x.Add(It.IsAny()), Times.Once); + } + } +} diff --git a/tests/solidInCsharp.Tests/solidInCsharp.Tests.csproj b/tests/solidInCsharp.Tests/solidInCsharp.Tests.csproj new file mode 100644 index 0000000..bf2a5c3 --- /dev/null +++ b/tests/solidInCsharp.Tests/solidInCsharp.Tests.csproj @@ -0,0 +1,28 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + +