Skip to content

Commit

Permalink
feat: adicionado cache do token
Browse files Browse the repository at this point in the history
  • Loading branch information
alefcarlos committed Jan 14, 2020
1 parent 47323c5 commit ee20ec1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
24 changes: 20 additions & 4 deletions src/Handlers/AuthenticationHeaderHandler.cs
Original file line number Diff line number Diff line change
@@ -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<KeyCloakSettings> settings)
public AuthenticationHeaderHandler(IKeyCloakAuthClient authClient, IOptions<KeyCloakSettings> 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<HttpResponseMessage> 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<TokenResponse>(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);

Expand Down
1 change: 1 addition & 0 deletions src/PlusUltra.KeyCloak.ApiClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<ItemGroup>
<PackageReference Include="PlusUltra.ApiClient" Version="0.2.4" />
<PackageReference Include="PlusUltra.DistributedCache" Version="1.0.0" />
<PackageReference Include="GitVersionTask" Version="5.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers;</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
1 change: 1 addition & 0 deletions src/Responses/TokenResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ namespace PlusUltra.KeyCloak.ApiClient.Responses
public class TokenResponse
{
public string access_token { get; set; }
public int expires_in { get; set; }
}
}
4 changes: 4 additions & 0 deletions tests/Startup.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -8,6 +10,8 @@ public class Startup : TestStartup
{
public override void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
services.AddFeatureManagement();
services.AddRedisCache(configuration);
services.AddKeyCloakApiClients(configuration);
}
}
Expand Down
5 changes: 4 additions & 1 deletion tests/appSettings.json
Original file line number Diff line number Diff line change
@@ -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"
}
}

0 comments on commit ee20ec1

Please sign in to comment.