|
| 1 | +using Azure.Core; |
| 2 | +using Azure.Identity; |
| 3 | +using Microsoft.VisualStudio.Services.Common; |
| 4 | +using Microsoft.VisualStudio.Services.OAuth; |
| 5 | + |
| 6 | +namespace TfsCmdlets.Services |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Helper class that wraps Azure.Identity's DefaultAzureCredential to obtain and |
| 10 | + /// automatically renew Azure DevOps access tokens. |
| 11 | + /// </summary> |
| 12 | + /// <remarks> |
| 13 | + /// Tokens issued by Azure AD for Azure DevOps have a short lifetime (~1 hour). |
| 14 | + /// This class transparently refreshes the token before it expires by checking expiry |
| 15 | + /// when <see cref="GetValidToken"/> is called. The underlying <see cref="DefaultAzureCredential"/> |
| 16 | + /// chains multiple credential sources (Environment, Managed Identity, Azure CLI, |
| 17 | + /// Visual Studio, etc.) so the caller only needs to be authenticated in one of them. |
| 18 | + /// </remarks> |
| 19 | + public sealed class AzureCredential |
| 20 | + { |
| 21 | + // Azure DevOps resource ID used as the token audience |
| 22 | + private const string AzureDevOpsScope = "499b84ac-1321-427f-aa17-267ca6975798/.default"; |
| 23 | + |
| 24 | + // Refresh the token when it is within this margin of expiry |
| 25 | + private static readonly TimeSpan RefreshMargin = TimeSpan.FromMinutes(5); |
| 26 | + |
| 27 | + private readonly TokenCredential _tokenCredential; |
| 28 | + private AccessToken _cachedToken; |
| 29 | + private readonly object _lock = new object(); |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Initializes a new instance using the default Azure credential chain. |
| 33 | + /// </summary> |
| 34 | + public AzureCredential() |
| 35 | + : this(new DefaultAzureCredential( |
| 36 | + new DefaultAzureCredentialOptions |
| 37 | + { |
| 38 | + // Disable interactive browser auth to avoid hangs in unattended (CI/CD) scenarios. |
| 39 | + ExcludeInteractiveBrowserCredential = true |
| 40 | + })) |
| 41 | + { |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Initializes a new instance using the specified Azure <see cref="TokenCredential"/>. |
| 46 | + /// </summary> |
| 47 | + public AzureCredential(TokenCredential tokenCredential) |
| 48 | + { |
| 49 | + _tokenCredential = tokenCredential ?? throw new ArgumentNullException(nameof(tokenCredential)); |
| 50 | + |
| 51 | + // Eagerly acquire the first token to fail fast on auth errors |
| 52 | + _cachedToken = _tokenCredential.GetToken( |
| 53 | + new TokenRequestContext(new[] { AzureDevOpsScope }), |
| 54 | + default); |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Returns true if the cached token is expired or near expiry. |
| 59 | + /// </summary> |
| 60 | + public bool IsTokenExpired |
| 61 | + { |
| 62 | + get |
| 63 | + { |
| 64 | + lock (_lock) |
| 65 | + { |
| 66 | + return _cachedToken.ExpiresOn <= DateTimeOffset.UtcNow.Add(RefreshMargin); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Gets a current, valid access token, refreshing from Azure if the cached token |
| 73 | + /// is expired or near expiry. |
| 74 | + /// </summary> |
| 75 | + public string GetValidToken() |
| 76 | + { |
| 77 | + lock (_lock) |
| 78 | + { |
| 79 | + if (_cachedToken.ExpiresOn <= DateTimeOffset.UtcNow.Add(RefreshMargin)) |
| 80 | + { |
| 81 | + _cachedToken = _tokenCredential.GetToken( |
| 82 | + new TokenRequestContext(new[] { AzureDevOpsScope }), |
| 83 | + default); |
| 84 | + } |
| 85 | + |
| 86 | + return _cachedToken.Token; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Creates a new <see cref="VssCredentials"/> instance using a valid (potentially refreshed) token. |
| 92 | + /// Each call returns a new instance with a fresh token. |
| 93 | + /// </summary> |
| 94 | + public VssCredentials CreateVssCredentials() |
| 95 | + { |
| 96 | + return new VssCredentials(new VssOAuthAccessTokenCredential(GetValidToken())); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments