From ee20ec1157f318e6fb925a307b040df904ec1c98 Mon Sep 17 00:00:00 2001 From: Alef Carlos Date: Mon, 13 Jan 2020 22:39:24 -0300 Subject: [PATCH] feat: adicionado cache do token --- src/Handlers/AuthenticationHeaderHandler.cs | 24 +++++++++++++++++---- src/PlusUltra.KeyCloak.ApiClient.csproj | 1 + src/Responses/TokenResponse.cs | 1 + tests/Startup.cs | 4 ++++ tests/appSettings.json | 5 ++++- 5 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/Handlers/AuthenticationHeaderHandler.cs b/src/Handlers/AuthenticationHeaderHandler.cs index 8b42051..b626e2b 100644 --- a/src/Handlers/AuthenticationHeaderHandler.cs +++ b/src/Handlers/AuthenticationHeaderHandler.cs @@ -1,33 +1,49 @@ +using System; using System.Net.Http; using System.Net.Http.Headers; using System.Threading; using System.Threading.Tasks; +using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Options; +using PlusUltra.DistributedCache; using PlusUltra.KeyCloak.ApiClient.Requests; +using PlusUltra.KeyCloak.ApiClient.Responses; namespace PlusUltra.KeyCloak.ApiClient.Handlers { public class AuthenticationHeaderHandler : DelegatingHandler { - public AuthenticationHeaderHandler(IKeyCloakAuthClient authClient, IOptions settings) + public AuthenticationHeaderHandler(IKeyCloakAuthClient authClient, IOptions settings, IDistributedCache cache) { this.authClient = authClient; this.settings = settings.Value; + this.cache = cache; } private readonly IKeyCloakAuthClient authClient; private readonly KeyCloakSettings settings; + private readonly IDistributedCache cache; + + const string CACHE_KEY = "keycloak_key"; protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var form = new AuthRequest { client_id = settings.ClientId, - client_secret= settings.ClientSecret, + client_secret = settings.ClientSecret, }; - //Obter o token - var token = await authClient.LoginAsync(form); + //Tentar obter token do cache, senĂ£o realizar request + var token = await cache.GetObjectAsync(CACHE_KEY); + + if (token is null) + { + //fazer request do token e salvar no cache + token = await authClient.LoginAsync(form); + var ts = token.expires_in <= 60 * 5 ? TimeSpan.FromSeconds(token.expires_in) : TimeSpan.FromSeconds(token.expires_in - 60 * 5); + await cache.SetObjectAsync(CACHE_KEY, token, ts); + } request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token.access_token); diff --git a/src/PlusUltra.KeyCloak.ApiClient.csproj b/src/PlusUltra.KeyCloak.ApiClient.csproj index bb10ca3..566a3cc 100644 --- a/src/PlusUltra.KeyCloak.ApiClient.csproj +++ b/src/PlusUltra.KeyCloak.ApiClient.csproj @@ -6,6 +6,7 @@ + runtime; build; native; contentfiles; analyzers; all diff --git a/src/Responses/TokenResponse.cs b/src/Responses/TokenResponse.cs index 4549ddc..a967f7a 100644 --- a/src/Responses/TokenResponse.cs +++ b/src/Responses/TokenResponse.cs @@ -3,5 +3,6 @@ namespace PlusUltra.KeyCloak.ApiClient.Responses public class TokenResponse { public string access_token { get; set; } + public int expires_in { get; set; } } } \ No newline at end of file diff --git a/tests/Startup.cs b/tests/Startup.cs index 05a42c1..1f482ca 100644 --- a/tests/Startup.cs +++ b/tests/Startup.cs @@ -1,5 +1,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.FeatureManagement; +using PlusUltra.DistributedCache; using PlusUltra.Testing; namespace PlusUltra.KeyCloak.ApiClient.Tests @@ -8,6 +10,8 @@ public class Startup : TestStartup { public override void ConfigureServices(IServiceCollection services, IConfiguration configuration) { + services.AddFeatureManagement(); + services.AddRedisCache(configuration); services.AddKeyCloakApiClients(configuration); } } diff --git a/tests/appSettings.json b/tests/appSettings.json index d497304..180e542 100644 --- a/tests/appSettings.json +++ b/tests/appSettings.json @@ -1,8 +1,11 @@ { "KeyCloakSettings": { - "Uri": "http://localhost:81", + "Uri": "http://localhost:8080", "Realm": "development", "ClientId": "admin-cli", "ClientSecret": "d47aee3b-aad6-4073-be38-fbcc8c525f91" + }, + "ConnectionStrings": { + "Redis": "localhost,name=keycloak.test,defaultDatabase=1" } } \ No newline at end of file