Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Certificado Formação Full Stack Developer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
- Application Query Stack e Specification Pattern
- NetDevPack
- Facade
- Elastic Search

# Ferramentas
- Visual Studio 2022 - Version 17.11.4 ++
Expand Down
6 changes: 6 additions & 0 deletions src/services/JSE.Identidade.API/Configuration/ApiConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using JSE.WebAPI.Core.IdentityConfiguration;
using JSE.WebAPI.Core.User;
using NetDevPack.Security.JwtSigningCredentials.AspNetCore;

namespace JSE.Identidade.API.Configuration
{
Expand All @@ -8,6 +10,8 @@ public static IServiceCollection AddApiConfiguration(this IServiceCollection ser
{
services.AddControllers();

services.AddScoped<IAspNetUser, AspNetUser>();

return services;
}

Expand All @@ -29,6 +33,8 @@ public static IApplicationBuilder UseApiConfiguration(this IApplicationBuilder a
endpoints.MapControllers();
});

app.UseJwksDiscovery();

return app;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using JSE.WebAPI.Core.IdentityConfiguration;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using NetDevPack.Security.JwtSigningCredentials;

namespace JSE.Identidade.API.Configuration
{
Expand All @@ -11,6 +12,10 @@ public static class IdentityConfig
public static IServiceCollection AddIdentityConfiguration(this IServiceCollection services,
IConfiguration configuration)
{

services.AddJwksManager(options => options.Algorithm = Algorithm.ES256)
.PersistKeysToDatabaseStore<ApplicationDbContext>();

services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(configuration.GetConnectionString("DefaultConnection")));

Expand Down
22 changes: 16 additions & 6 deletions src/services/JSE.Identidade.API/Controllers/AuthController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using JSE.MessageBus;
using JSE.Core.Messages.Integration;
using JSE.WebAPI.Core.IdentityConfiguration;
using JSE.WebAPI.Core.User;
using NetDevPack.Security.JwtSigningCredentials.Interfaces;

namespace JSE.Identidade.API.Controllers
{
Expand All @@ -19,18 +21,24 @@ public class AuthController : MainController
private readonly SignInManager<IdentityUser> _signInManager;
private readonly UserManager<IdentityUser> _userManager;
private readonly AppSettings _appSettings;
private readonly IAspNetUser _aspNetUser;
private readonly IJsonWebKeySetService _jwksService;

private readonly IMessageBus _bus;

public AuthController(SignInManager<IdentityUser> signInManager,
UserManager<IdentityUser> userManager,
IOptions<AppSettings> appSettings,
IMessageBus bus)
IMessageBus bus,
IAspNetUser aspNetUser,
IJsonWebKeySetService jwksService)
{
_signInManager = signInManager;
_userManager = userManager;
_appSettings = appSettings.Value;
_bus = bus;
_aspNetUser = aspNetUser;
_jwksService = jwksService;
}

[HttpPost("nova-conta")]
Expand Down Expand Up @@ -127,14 +135,16 @@ private async Task<ClaimsIdentity> ObterClaimsUsuario(ICollection<Claim> claims,
private string CodificarToken(ClaimsIdentity identityClaims)
{
var tokenHandler = new JwtSecurityTokenHandler();
var key = Encoding.ASCII.GetBytes(_appSettings.Secret);

var currentIssuer = $"{_aspNetUser.ObterHttpContext().Request.Scheme}://{_aspNetUser.ObterHttpContext().Request.Host}";

var key = _jwksService.GetCurrent();
var token = tokenHandler.CreateToken(new SecurityTokenDescriptor
{
Issuer = _appSettings.Issuer,
Audience = _appSettings.ValidOn,
Issuer = currentIssuer,
Subject = identityClaims,
Expires = DateTime.UtcNow.AddHours(_appSettings.ExpirationHours),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
Expires = DateTime.UtcNow.AddHours(1),
SigningCredentials = key
});

return tokenHandler.WriteToken(token);
Expand Down
6 changes: 5 additions & 1 deletion src/services/JSE.Identidade.API/Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using NetDevPack.Security.JwtSigningCredentials;
using NetDevPack.Security.JwtSigningCredentials.Store.EntityFrameworkCore;

namespace JSE.Identidade.API.Data
{
public class ApplicationDbContext : IdentityDbContext
public class ApplicationDbContext : IdentityDbContext, ISecurityKeyContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }

public DbSet<SecurityKeyWithPrivate> SecurityKeys { get; set; }
}
}
2 changes: 2 additions & 0 deletions src/services/JSE.Identidade.API/JSE.Identidade.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
<PackageReference Include="NetDevPack.Security.JwtSigningCredentials.AspNetCore" Version="1.0.3" />
<PackageReference Include="NetDevPack.Security.JwtSigningCredentials.Store.EntityFrameworkCore" Version="1.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />

Expand Down
Loading
Loading