-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeycloakTokenService.cs
More file actions
90 lines (73 loc) · 3.05 KB
/
KeycloakTokenService.cs
File metadata and controls
90 lines (73 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using WF.Shared.Contracts.Configuration;
namespace WF.TransactionService.Infrastructure.Authentication;
public class KeycloakTokenService(
IHttpClientFactory httpClientFactory,
IOptions<KeycloakOptions> options,
ILogger<KeycloakTokenService> logger) : IKeycloakTokenService
{
private static readonly JsonSerializerOptions JsonOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
public async Task<string> GetAccessTokenAsync(CancellationToken cancellationToken = default)
{
var result = await GetTokenAsync(cancellationToken);
return result.AccessToken;
}
public async Task<TokenResult> GetTokenAsync(CancellationToken cancellationToken = default)
{
using var httpClient = httpClientFactory.CreateClient("Keycloak");
var tokenEndpoint = $"{options.Value.BaseUrl}/realms/{options.Value.Realm}/protocol/openid-connect/token";
var requestBody = new Dictionary<string, string>
{
{ "grant_type", "client_credentials" },
{ "client_id", options.Value.ClientId },
{ "client_secret", options.Value.ClientSecret }
};
var request = new HttpRequestMessage(HttpMethod.Post, tokenEndpoint)
{
Content = new FormUrlEncodedContent(requestBody)
};
try
{
var response = await httpClient.SendAsync(request, cancellationToken);
response.EnsureSuccessStatusCode();
var tokenResponse = await response.Content.ReadFromJsonAsync<TokenResponse>(
JsonOptions,
cancellationToken: cancellationToken)
?? throw new InvalidOperationException("Failed to deserialize token response from Keycloak.");
logger.LogDebug("Successfully obtained access token from Keycloak. Expires in {ExpiresIn} seconds", tokenResponse.ExpiresIn);
return new TokenResult
{
AccessToken = tokenResponse.AccessToken,
ExpiresIn = tokenResponse.ExpiresIn
};
}
catch (HttpRequestException ex)
{
logger.LogError(ex, "Failed to obtain access token from Keycloak");
throw new InvalidOperationException("Failed to obtain access token from Keycloak.", ex);
}
}
private record TokenResponse
{
[JsonPropertyName("access_token")]
public string AccessToken { get; init; } = string.Empty;
[JsonPropertyName("token_type")]
public string TokenType { get; init; } = string.Empty;
[JsonPropertyName("expires_in")]
public int ExpiresIn { get; init; }
}
}
public record TokenResult
{
public string AccessToken { get; init; } = string.Empty;
public int ExpiresIn { get; init; }
}