-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Expand file tree
/
Copy pathTokenCredential.cs
More file actions
69 lines (63 loc) · 3.7 KB
/
Copy pathTokenCredential.cs
File metadata and controls
69 lines (63 loc) · 3.7 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Azure.Core
{
/// <summary>
/// Represents a credential capable of providing an OAuth token.
/// </summary>
public abstract class TokenCredential : AuthenticationTokenProvider
{
/// <summary>
/// Gets an <see cref="AccessToken"/> for the specified set of scopes.
/// </summary>
/// <param name="requestContext">The <see cref="TokenRequestContext"/> with authentication information.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
/// <returns>A valid <see cref="AccessToken"/>.</returns>
/// <remarks>Caching and management of the lifespan for the <see cref="AccessToken"/> is considered the responsibility of the caller: each call should request a fresh token being requested.</remarks>
[CallerShouldAudit("https://aka.ms/azsdk/callershouldaudit/identity")]
public abstract ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken);
/// <summary>
/// Gets an <see cref="AccessToken"/> for the specified set of scopes.
/// </summary>
/// <param name="requestContext">The <see cref="TokenRequestContext"/> with authentication information.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
/// <returns>A valid <see cref="AccessToken"/>.</returns>
/// <remarks>Caching and management of the lifespan for the <see cref="AccessToken"/> is considered the responsibility of the caller: each call should request a fresh token being requested.</remarks>
[CallerShouldAudit("https://aka.ms/azsdk/callershouldaudit/identity")]
public abstract AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken);
/// <summary>
/// Gets an <see cref="AuthenticationToken"/> for the provided <paramref name="properties"/>.
/// </summary>
/// <param name="properties"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <exception cref="System.NotImplementedException"></exception>
public override async ValueTask<AuthenticationToken> GetTokenAsync(GetTokenOptions properties, CancellationToken cancellationToken) =>
(await GetTokenAsync(TokenRequestContext.FromGetTokenOptions(properties), cancellationToken).ConfigureAwait(false)).ToAuthenticationToken();
/// <summary>
/// Gets an <see cref="AuthenticationToken"/> for the provided <paramref name="properties"/>.
/// </summary>
/// <param name="properties"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public override AuthenticationToken GetToken(GetTokenOptions properties, CancellationToken cancellationToken) =>
GetToken(TokenRequestContext.FromGetTokenOptions(properties), cancellationToken).ToAuthenticationToken();
/// <inheritdoc />
public override GetTokenOptions? CreateTokenOptions(IReadOnlyDictionary<string, object> properties)
{
// Check if scopes are present
if (properties.TryGetValue(GetTokenOptions.ScopesPropertyName, out var scopesValue))
{
return new GetTokenOptions(properties);
}
// No scopes provided - insufficient information to create TokenRequestContext
return null;
}
}
}