-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathClientCredentialsTokenManagementService.cs
More file actions
47 lines (40 loc) · 1.72 KB
/
ClientCredentialsTokenManagementService.cs
File metadata and controls
47 lines (40 loc) · 1.72 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
// Copyright (c) Duende Software. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
using Microsoft.Extensions.Logging;
using System.Threading;
namespace Duende.AccessTokenManagement;
/// <summary>
/// Implements token management logic
/// </summary>
public class ClientCredentialsTokenManagementService(
IClientCredentialsTokenEndpointService clientCredentialsTokenEndpointService,
IClientCredentialsTokenCache tokenCache
) : IClientCredentialsTokenManagementService
{
/// <inheritdoc/>
public async Task<ClientCredentialsToken> GetAccessTokenAsync(
string clientName,
TokenRequestParameters? parameters = null,
CancellationToken cancellationToken = default)
{
parameters ??= new TokenRequestParameters();
return await tokenCache.GetOrCreateAsync(
clientName: clientName,
requestParameters: parameters,
factory: InvokeGetAccessToken,
cancellationToken: cancellationToken).ConfigureAwait(false);
}
private async Task<ClientCredentialsToken> InvokeGetAccessToken(string clientName, TokenRequestParameters parameters, CancellationToken cancellationToken)
{
return await clientCredentialsTokenEndpointService.RequestToken(clientName, parameters, cancellationToken).ConfigureAwait(false);
}
/// <inheritdoc/>
public async Task DeleteAccessTokenAsync(
string clientName,
TokenRequestParameters? parameters = null,
CancellationToken cancellationToken = default)
{
parameters ??= new TokenRequestParameters();
await tokenCache.DeleteAsync(clientName, parameters, cancellationToken);
}
}