-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathDelegatedTokenCredential.cs
73 lines (65 loc) · 3.83 KB
/
DelegatedTokenCredential.cs
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core.Pipeline;
namespace Azure.Core
{
/// <summary>
/// A factory for creating a delegated <see cref="TokenCredential"/> capable of providing an OAuth token.
/// </summary>
public static class DelegatedTokenCredential
{
/// <summary>
/// Creates a static <see cref="TokenCredential"/> that accepts delegates which will produce an <see cref="AccessToken"/>.
/// </summary>
/// <remarks>
/// Typically, the <see cref="TokenCredential"/> created by this method is for use when you have already obtained an <see cref="AccessToken"/>
/// from some other source and need a <see cref="TokenCredential"/> that will simply return that token. Because the static token can expire,
/// the delegates offer a mechanism to handle <see cref="AccessToken"/> renewal.
/// </remarks>
/// <param name="getToken">A delegate that returns an <see cref="AccessToken"/>.</param>
/// <param name="getTokenAsync">A delegate that returns a <see cref="ValueTask"/> of type <see cref="AccessToken"/>.</param>
/// <returns></returns>
public static TokenCredential Create(
Func<TokenRequestContext, CancellationToken, AccessToken> getToken,
Func<TokenRequestContext, CancellationToken, ValueTask<AccessToken>> getTokenAsync) => new StaticTokenCredential(getToken, getTokenAsync);
/// <summary>
/// Creates a static <see cref="TokenCredential"/> that accepts delegates which will produce an <see cref="AccessToken"/>.
/// </summary>
/// <remarks>
/// Typically, the <see cref="TokenCredential"/> created by this method is for use when you have already obtained an <see cref="AccessToken"/>
/// from some other source and need a <see cref="TokenCredential"/> that will simply return that token. Because the static token can expire,
/// the delegates offer a mechanism to handle <see cref="AccessToken"/> renewal.
/// </remarks>
/// <param name="getToken">A delegate that returns an <see cref="AccessToken"/>.</param>
/// <returns></returns>
public static TokenCredential Create(
Func<TokenRequestContext, CancellationToken, AccessToken> getToken) => new StaticTokenCredential(getToken);
private class StaticTokenCredential : TokenCredential
{
private readonly Func<TokenRequestContext, CancellationToken, AccessToken> _getToken;
private readonly Func<TokenRequestContext, CancellationToken, ValueTask<AccessToken>> _getTokenAsync;
internal StaticTokenCredential(
Func<TokenRequestContext, CancellationToken, AccessToken> getToken,
Func<TokenRequestContext, CancellationToken, ValueTask<AccessToken>> getTokenAsync)
{
_getToken = getToken;
_getTokenAsync = getTokenAsync;
}
internal StaticTokenCredential(
Func<TokenRequestContext, CancellationToken, AccessToken> getToken)
{
_getToken = getToken;
_getTokenAsync = (context, token) => new ValueTask<AccessToken>(_getToken(context, token));
}
/// <inheritdoc cref="GetTokenAsync"/>
public override ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken) =>
_getTokenAsync(requestContext, cancellationToken);
/// <inheritdoc cref="GetToken"/>
public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken) =>
_getToken(requestContext, cancellationToken);
}
}
}