Skip to content

Commit dcfb1a6

Browse files
committed
#277 - JWK e JWKS na API de identidade - feature/sp8/#277
1 parent 0250bf5 commit dcfb1a6

File tree

8 files changed

+407
-7
lines changed

8 files changed

+407
-7
lines changed

src/services/JSE.Identidade.API/Configuration/ApiConfig.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using JSE.WebAPI.Core.IdentityConfiguration;
2+
using JSE.WebAPI.Core.User;
3+
using NetDevPack.Security.JwtSigningCredentials.AspNetCore;
24

35
namespace JSE.Identidade.API.Configuration
46
{
@@ -8,6 +10,8 @@ public static IServiceCollection AddApiConfiguration(this IServiceCollection ser
810
{
911
services.AddControllers();
1012

13+
services.AddScoped<IAspNetUser, AspNetUser>();
14+
1115
return services;
1216
}
1317

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

36+
app.UseJwksDiscovery();
37+
3238
return app;
3339
}
3440
}

src/services/JSE.Identidade.API/Configuration/IdentityConfig.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using JSE.WebAPI.Core.IdentityConfiguration;
44
using Microsoft.AspNetCore.Identity;
55
using Microsoft.EntityFrameworkCore;
6+
using NetDevPack.Security.JwtSigningCredentials;
67

78
namespace JSE.Identidade.API.Configuration
89
{
@@ -11,6 +12,10 @@ public static class IdentityConfig
1112
public static IServiceCollection AddIdentityConfiguration(this IServiceCollection services,
1213
IConfiguration configuration)
1314
{
15+
16+
services.AddJwksManager(options => options.Algorithm = Algorithm.ES256)
17+
.PersistKeysToDatabaseStore<ApplicationDbContext>();
18+
1419
services.AddDbContext<ApplicationDbContext>(options =>
1520
options.UseSqlServer(configuration.GetConnectionString("DefaultConnection")));
1621

src/services/JSE.Identidade.API/Controllers/AuthController.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
using JSE.MessageBus;
1111
using JSE.Core.Messages.Integration;
1212
using JSE.WebAPI.Core.IdentityConfiguration;
13+
using JSE.WebAPI.Core.User;
14+
using NetDevPack.Security.JwtSigningCredentials.Interfaces;
1315

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

2327
private readonly IMessageBus _bus;
2428

2529
public AuthController(SignInManager<IdentityUser> signInManager,
2630
UserManager<IdentityUser> userManager,
2731
IOptions<AppSettings> appSettings,
28-
IMessageBus bus)
32+
IMessageBus bus,
33+
IAspNetUser aspNetUser,
34+
IJsonWebKeySetService jwksService)
2935
{
3036
_signInManager = signInManager;
3137
_userManager = userManager;
3238
_appSettings = appSettings.Value;
3339
_bus = bus;
40+
_aspNetUser = aspNetUser;
41+
_jwksService = jwksService;
3442
}
3543

3644
[HttpPost("nova-conta")]
@@ -127,14 +135,16 @@ private async Task<ClaimsIdentity> ObterClaimsUsuario(ICollection<Claim> claims,
127135
private string CodificarToken(ClaimsIdentity identityClaims)
128136
{
129137
var tokenHandler = new JwtSecurityTokenHandler();
130-
var key = Encoding.ASCII.GetBytes(_appSettings.Secret);
138+
139+
var currentIssuer = $"{_aspNetUser.ObterHttpContext().Request.Scheme}://{_aspNetUser.ObterHttpContext().Request.Host}";
140+
141+
var key = _jwksService.GetCurrent();
131142
var token = tokenHandler.CreateToken(new SecurityTokenDescriptor
132143
{
133-
Issuer = _appSettings.Issuer,
134-
Audience = _appSettings.ValidOn,
144+
Issuer = currentIssuer,
135145
Subject = identityClaims,
136-
Expires = DateTime.UtcNow.AddHours(_appSettings.ExpirationHours),
137-
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
146+
Expires = DateTime.UtcNow.AddHours(1),
147+
SigningCredentials = key
138148
});
139149

140150
return tokenHandler.WriteToken(token);
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
22
using Microsoft.EntityFrameworkCore;
3+
using NetDevPack.Security.JwtSigningCredentials;
4+
using NetDevPack.Security.JwtSigningCredentials.Store.EntityFrameworkCore;
35

46
namespace JSE.Identidade.API.Data
57
{
6-
public class ApplicationDbContext : IdentityDbContext
8+
public class ApplicationDbContext : IdentityDbContext, ISecurityKeyContext
79
{
810
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
11+
12+
public DbSet<SecurityKeyWithPrivate> SecurityKeys { get; set; }
913
}
1014
}

src/services/JSE.Identidade.API/JSE.Identidade.API.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1717
</PackageReference>
1818
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
19+
<PackageReference Include="NetDevPack.Security.JwtSigningCredentials.AspNetCore" Version="1.0.3" />
20+
<PackageReference Include="NetDevPack.Security.JwtSigningCredentials.Store.EntityFrameworkCore" Version="1.0.3" />
1921
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
2022
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
2123

0 commit comments

Comments
 (0)