-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoxerTokenProvider.cs
More file actions
62 lines (58 loc) · 2.81 KB
/
Copy pathBoxerTokenProvider.cs
File metadata and controls
62 lines (58 loc) · 2.81 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
using System.Net.Http.Headers;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Polly;
using SnD.ApiClient.Boxer.Base;
using SnD.ApiClient.Config;
namespace SnD.ApiClient.Boxer;
public class BoxerTokenProvider : IJwtTokenExchangeProvider
{
private readonly Uri authProvider;
private readonly Uri baseUri;
private string? token;
private readonly HttpClient httpClient;
private readonly ILogger logger;
private readonly Func<CancellationToken, Task<string>> getExternalTokenAsync;
/// <summary>
/// Creates new instance
/// </summary>
/// <param name="boxerTokenProviderOptions">Options for boxer connector</param>
/// <param name="httpClient">Http client</param>
/// <param name="logger">Logger for boxer connector</param>
/// <param name="getExternalTokenAsync">Function that returns external authentication token</param>
public BoxerTokenProvider(IOptions<BoxerTokenProviderOptions> boxerTokenProviderOptions, HttpClient httpClient,
ILogger<BoxerTokenProvider> logger, Func<CancellationToken, Task<string>> getExternalTokenAsync)
{
var authorizationProvider = boxerTokenProviderOptions.Value.IdentityProvider
?? throw new ArgumentNullException(nameof(BoxerTokenProviderOptions.IdentityProvider));
this.authProvider = new Uri($"token/{authorizationProvider}", UriKind.Relative);
this.baseUri = new Uri(boxerTokenProviderOptions.Value.BaseUri
?? throw new ArgumentException(nameof(BoxerTokenProviderOptions.BaseUri)));
this.httpClient = httpClient;
this.logger = logger;
this.getExternalTokenAsync = getExternalTokenAsync;
}
/// <inheritdoc/>
public async Task<string> GetTokenAsync(bool refresh, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
if (this.token == null || refresh)
{
var response = await Policy
.Handle<HttpRequestException>()
.OrResult<HttpResponseMessage>(r => !r.IsSuccessStatusCode)
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)))
.ExecuteAsync(async () =>
{
var externalToken = await this.getExternalTokenAsync(cancellationToken);
this.logger.LogInformation("Received external authentication token");
var request = new HttpRequestMessage(HttpMethod.Get, new Uri(baseUri, authProvider));
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", externalToken);
return await httpClient.SendAsync(request, cancellationToken);
});
this.token = await response.Content.ReadAsStringAsync();
this.logger.LogInformation("Received boxer token");
}
return this.token;
}
}