-
Notifications
You must be signed in to change notification settings - Fork 219
/
Copy pathAzureFederatedTokenProvider.cs
87 lines (76 loc) · 2.98 KB
/
AzureFederatedTokenProvider.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Identity;
namespace Microsoft.Identity.Web
{
/// <summary>
/// See https://aka.ms/ms-id-web/identity-federation.
/// </summary>
public class AzureFederatedTokenProvider
{
/// <summary>
/// See https://aka.ms/ms-id-web/identity-federation.
/// </summary>
public AzureFederatedTokenProvider(): this(null)
{
}
/// <summary>
/// See https://aka.ms/ms-id-web/identity-federation.
/// </summary>
/// <param name="federatedClientId"></param>
public AzureFederatedTokenProvider(string? federatedClientId)
{
_federatedClientId = federatedClientId;
ClientAssertionProvider = GetSignedAssertionFromFederatedTokenProvider;
}
private readonly string? _federatedClientId;
/// <summary>
/// Prototype of certificate-less authentication using a signed assertion
/// acquired with Managed Identity (federated identity).
/// </summary>
/// <returns>The signed assertion.</returns>
private async Task<ClientAssertion> GetSignedAssertionFromFederatedTokenProvider(CancellationToken cancellationToken)
{
var credential = new DefaultAzureCredential(new DefaultAzureCredentialOptions { ManagedIdentityClientId = _federatedClientId });
var result = await credential.GetTokenAsync(
new TokenRequestContext(new[] { "api://AzureADTokenExchange/.default" }, null),
cancellationToken).ConfigureAwait(false);
return new ClientAssertion(result.Token, result.ExpiresOn);
}
/// <summary>
/// Delegate to get the client assertion.
/// </summary>
private Func<CancellationToken, Task<ClientAssertion>> ClientAssertionProvider { get; set; }
/// <summary>
/// Client assertion.
/// </summary>
private ClientAssertion? _clientAssertion;
/// <summary>
/// Get the signed assertion (and refreshes it if needed).
/// </summary>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>The signed assertion.</returns>
public async Task<string> GetSignedAssertion(CancellationToken cancellationToken)
{
if (_clientAssertion == null || (Expiry != null && DateTimeOffset.Now > Expiry))
{
_clientAssertion = await ClientAssertionProvider(cancellationToken).ConfigureAwait(false);
}
return _clientAssertion.SignedAssertion;
}
/// <summary>
/// Expiry of the client assertion.
/// </summary>
private DateTimeOffset? Expiry
{
get
{
return _clientAssertion?.Expiry;
}
}
}
}