From 7b1c0687d3df08ab4f01d7506ec654b6f662053e Mon Sep 17 00:00:00 2001 From: Jeferson Almeida Date: Tue, 14 Jan 2025 18:32:29 -0300 Subject: [PATCH] #278 - Implementando o JWK nas demais API's - feature/sp8/#278 --- README.md | 1 + .../appsettings.Development.json | 6 ++---- .../IdentityConfiguration/AppSettings.cs | 5 +---- .../IdentityConfiguration/JwtConfiguration.cs | 14 ++----------- .../JSE.WebAPI.Core/JSE.WebAPI.Core.csproj | 1 + .../appsettings.Development.json | 8 ++++---- .../appsettings.Development.json | 6 ++---- .../appsettings.Development.json | 20 +++++++++---------- .../Configuration/IdentityConfig.cs | 3 --- .../Controllers/AuthController.cs | 2 +- .../appsettings.Development.json | 18 +++++++---------- .../appsettings.Development.json | 5 +---- .../appsettings.Development.json | 5 +---- 13 files changed, 33 insertions(+), 61 deletions(-) diff --git a/README.md b/README.md index a9f9820..4f325ef 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ - NetDevPack - Facade - Elastic Search +- JWK e JWKS # Ferramentas - Visual Studio 2022 - Version 17.11.4 ++ diff --git a/src/api gateways/JSE.Bff.Compras/appsettings.Development.json b/src/api gateways/JSE.Bff.Compras/appsettings.Development.json index 53e6eb7..48f2137 100644 --- a/src/api gateways/JSE.Bff.Compras/appsettings.Development.json +++ b/src/api gateways/JSE.Bff.Compras/appsettings.Development.json @@ -19,9 +19,7 @@ }, "AppSettings": { - "Secret": "F9F52344-59C3-4EAC-90E6-CB47935038BE", - "ExpirationHours": 2, - "Issuer": "MeuSistema", - "ValidOn": "https://localhost" + "AutenticacaoJwksUrl": "https://localhost:44396/jwks" } + } \ No newline at end of file diff --git a/src/building blocks/JSE.WebAPI.Core/IdentityConfiguration/AppSettings.cs b/src/building blocks/JSE.WebAPI.Core/IdentityConfiguration/AppSettings.cs index 22e0511..fd68bd0 100644 --- a/src/building blocks/JSE.WebAPI.Core/IdentityConfiguration/AppSettings.cs +++ b/src/building blocks/JSE.WebAPI.Core/IdentityConfiguration/AppSettings.cs @@ -2,9 +2,6 @@ { public class AppSettings { - public string Secret { get; set; } - public int ExpirationHours { get; set; } - public string Issuer { get; set; } - public string ValidOn { get; set; } + public string AutenticacaoJwksUrl{ get; set; } } } diff --git a/src/building blocks/JSE.WebAPI.Core/IdentityConfiguration/JwtConfiguration.cs b/src/building blocks/JSE.WebAPI.Core/IdentityConfiguration/JwtConfiguration.cs index adaaa47..4e14153 100644 --- a/src/building blocks/JSE.WebAPI.Core/IdentityConfiguration/JwtConfiguration.cs +++ b/src/building blocks/JSE.WebAPI.Core/IdentityConfiguration/JwtConfiguration.cs @@ -2,8 +2,7 @@ using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.IdentityModel.Tokens; -using System.Text; +using NetDevPack.Security.JwtExtensions; namespace JSE.WebAPI.Core.IdentityConfiguration { @@ -16,7 +15,6 @@ public static void AddJwtConfiguration(this IServiceCollection services, services.Configure(appSettingsSection); var appSettings = appSettingsSection.Get(); - var key = Encoding.ASCII.GetBytes(appSettings.Secret); services.AddAuthentication(x => { @@ -26,15 +24,7 @@ public static void AddJwtConfiguration(this IServiceCollection services, { x.RequireHttpsMetadata = true; x.SaveToken = true; - x.TokenValidationParameters = new TokenValidationParameters - { - ValidateIssuerSigningKey = true, - IssuerSigningKey = new SymmetricSecurityKey(key), - ValidateIssuer = true, - ValidateAudience = true, - ValidAudience = appSettings.ValidOn, - ValidIssuer = appSettings.Issuer - }; + x.SetJwksOptions(new JwkOptions(appSettings.AutenticacaoJwksUrl)); }); } diff --git a/src/building blocks/JSE.WebAPI.Core/JSE.WebAPI.Core.csproj b/src/building blocks/JSE.WebAPI.Core/JSE.WebAPI.Core.csproj index deb328c..b0152ea 100644 --- a/src/building blocks/JSE.WebAPI.Core/JSE.WebAPI.Core.csproj +++ b/src/building blocks/JSE.WebAPI.Core/JSE.WebAPI.Core.csproj @@ -12,6 +12,7 @@ + diff --git a/src/services/JSE.Carrinho.API/appsettings.Development.json b/src/services/JSE.Carrinho.API/appsettings.Development.json index fcb10cf..87881dc 100644 --- a/src/services/JSE.Carrinho.API/appsettings.Development.json +++ b/src/services/JSE.Carrinho.API/appsettings.Development.json @@ -2,19 +2,19 @@ "ConnectionStrings": { "DefaultConnection": "Server=.\\SQLEXPRESS;Database=JeffStoreEnterprise;User Id=sa;Password=Asd123!!;Trusted_Connection=true;MultipleActiveResultSets=true;TrustServerCertificate=True;" }, + "MessageQueueConnection": { "MessageBus": "host=localhost:5672;publisherConfirms=true;timeout=10" }, + "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, + "AppSettings": { - "Secret": "F9F52344-59C3-4EAC-90E6-CB47935038BE", - "ExpirationHours": 2, - "Issuer": "MeuSistema", - "ValidOn": "https://localhost" + "AutenticacaoJwksUrl": "https://localhost:44396/jwks" } } \ No newline at end of file diff --git a/src/services/JSE.Catalogo.API/appsettings.Development.json b/src/services/JSE.Catalogo.API/appsettings.Development.json index f0b1f7a..99b9e4c 100644 --- a/src/services/JSE.Catalogo.API/appsettings.Development.json +++ b/src/services/JSE.Catalogo.API/appsettings.Development.json @@ -15,9 +15,7 @@ }, "AppSettings": { - "Secret": "F9F52344-59C3-4EAC-90E6-CB47935038BE", - "ExpirationHours": 2, - "Issuer": "MeuSistema", - "ValidOn": "https://localhost" + "AutenticacaoJwksUrl": "https://localhost:44396/jwks" } + } diff --git a/src/services/JSE.Clientes.API/appsettings.Development.json b/src/services/JSE.Clientes.API/appsettings.Development.json index 3d791c1..b3239b5 100644 --- a/src/services/JSE.Clientes.API/appsettings.Development.json +++ b/src/services/JSE.Clientes.API/appsettings.Development.json @@ -1,20 +1,20 @@ { - "ConnectionStrings": { - "DefaultConnection": "Server=.\\SQLEXPRESS;Database=JeffStoreEnterprise;User Id=sa;Password=Asd123!!;Trusted_Connection=true;MultipleActiveResultSets=true;TrustServerCertificate=True;" - }, - "MessageQueueConnection": { - "MessageBus": "host=localhost:5672;publisherConfirms=true;timeout=10" - }, "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, + + "ConnectionStrings": { + "DefaultConnection": "Server=.\\SQLEXPRESS;Database=JeffStoreEnterprise;User Id=sa;Password=Asd123!!;Trusted_Connection=true;MultipleActiveResultSets=true;TrustServerCertificate=True;" + }, + + "MessageQueueConnection": { + "MessageBus": "host=localhost:5672;publisherConfirms=true;timeout=10" + }, + "AppSettings": { - "Secret": "F9F52344-59C3-4EAC-90E6-CB47935038BE", - "ExpirationHours": 2, - "Issuer": "MeuSistema", - "ValidOn": "https://localhost" + "AutenticacaoJwksUrl": "https://localhost:44396/jwks" } } diff --git a/src/services/JSE.Identidade.API/Configuration/IdentityConfig.cs b/src/services/JSE.Identidade.API/Configuration/IdentityConfig.cs index 92f8b84..6fa7604 100644 --- a/src/services/JSE.Identidade.API/Configuration/IdentityConfig.cs +++ b/src/services/JSE.Identidade.API/Configuration/IdentityConfig.cs @@ -1,6 +1,5 @@ using JSE.Identidade.API.Data; using JSE.Identidade.API.Extensions; -using JSE.WebAPI.Core.IdentityConfiguration; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using NetDevPack.Security.JwtSigningCredentials; @@ -25,8 +24,6 @@ public static IServiceCollection AddIdentityConfiguration(this IServiceCollectio .AddEntityFrameworkStores() .AddDefaultTokenProviders(); - services.AddJwtConfiguration(configuration); - return services; } } diff --git a/src/services/JSE.Identidade.API/Controllers/AuthController.cs b/src/services/JSE.Identidade.API/Controllers/AuthController.cs index ef73d73..728503a 100644 --- a/src/services/JSE.Identidade.API/Controllers/AuthController.cs +++ b/src/services/JSE.Identidade.API/Controllers/AuthController.cs @@ -155,7 +155,7 @@ private UsuarioRespostaLoginViewModel ObterRespostaToken(string encodedToken, Id return new UsuarioRespostaLoginViewModel { AccessToken = encodedToken, - ExpiresIn = TimeSpan.FromHours(_appSettings.ExpirationHours).TotalSeconds, + ExpiresIn = TimeSpan.FromHours(1).TotalSeconds, UsuarioToken = new UsuarioTokenViewModel { Id = user.Id, diff --git a/src/services/JSE.Identidade.API/appsettings.Development.json b/src/services/JSE.Identidade.API/appsettings.Development.json index 3d791c1..b06a22d 100644 --- a/src/services/JSE.Identidade.API/appsettings.Development.json +++ b/src/services/JSE.Identidade.API/appsettings.Development.json @@ -1,20 +1,16 @@ { - "ConnectionStrings": { - "DefaultConnection": "Server=.\\SQLEXPRESS;Database=JeffStoreEnterprise;User Id=sa;Password=Asd123!!;Trusted_Connection=true;MultipleActiveResultSets=true;TrustServerCertificate=True;" - }, - "MessageQueueConnection": { - "MessageBus": "host=localhost:5672;publisherConfirms=true;timeout=10" - }, "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, - "AppSettings": { - "Secret": "F9F52344-59C3-4EAC-90E6-CB47935038BE", - "ExpirationHours": 2, - "Issuer": "MeuSistema", - "ValidOn": "https://localhost" + + "ConnectionStrings": { + "DefaultConnection": "Server=.\\SQLEXPRESS;Database=JeffStoreEnterprise;User Id=sa;Password=Asd123!!;Trusted_Connection=true;MultipleActiveResultSets=true;TrustServerCertificate=True;" + }, + + "MessageQueueConnection": { + "MessageBus": "host=localhost:5672;publisherConfirms=true;timeout=10" } } diff --git a/src/services/JSE.Pagamento.API/appsettings.Development.json b/src/services/JSE.Pagamento.API/appsettings.Development.json index d96fd35..567167d 100644 --- a/src/services/JSE.Pagamento.API/appsettings.Development.json +++ b/src/services/JSE.Pagamento.API/appsettings.Development.json @@ -15,10 +15,7 @@ }, "AppSettings": { - "Secret": "F9F52344-59C3-4EAC-90E6-CB47935038BE", - "ExpirationHours": 2, - "Issuer": "MeuSistema", - "ValidOn": "https://localhost" + "AutenticacaoJwksUrl": "https://localhost:44396/jwks" }, "PagamentoConfig": { diff --git a/src/services/JSE.Pedido.API/appsettings.Development.json b/src/services/JSE.Pedido.API/appsettings.Development.json index 4e14e64..2280b30 100644 --- a/src/services/JSE.Pedido.API/appsettings.Development.json +++ b/src/services/JSE.Pedido.API/appsettings.Development.json @@ -15,9 +15,6 @@ }, "AppSettings": { - "Secret": "F9F52344-59C3-4EAC-90E6-CB47935038BE", - "ExpirationHours": 2, - "Issuer": "MeuSistema", - "ValidOn": "https://localhost" + "AutenticacaoJwksUrl": "https://localhost:44396/jwks" } }