|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 2 | +// See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +import { DeviceCodeCredential, type DeviceCodeInfo, AzureAuthorityHosts } from '@azure/identity'; |
| 5 | +import type { ITerminal } from '@rushstack/node-core-library'; |
| 6 | +import { CredentialCache, type ICredentialCacheEntry } from '@rushstack/rush-sdk'; |
| 7 | +import { PrintUtilities } from '@rushstack/terminal'; |
| 8 | + |
| 9 | +/** |
| 10 | + * @public |
| 11 | + */ |
| 12 | +export type AzureEnvironmentName = keyof typeof AzureAuthorityHosts; |
| 13 | + |
| 14 | +/** |
| 15 | + * @public |
| 16 | + */ |
| 17 | +export interface IAzureAuthenticationBaseOptions { |
| 18 | + azureEnvironment?: AzureEnvironmentName; |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * @public |
| 23 | + */ export interface ICredentialResult { |
| 24 | + credentialString: string; |
| 25 | + expiresOn: Date | undefined; |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * @public |
| 30 | + */ |
| 31 | +export abstract class AzureAuthenticationBase { |
| 32 | + protected abstract readonly _credentialNameForCache: string; |
| 33 | + protected abstract readonly _credentialKindForLogging: string; |
| 34 | + protected abstract readonly _credentialUpdateCommandForLogging: string | undefined; |
| 35 | + |
| 36 | + protected readonly _azureEnvironment: AzureEnvironmentName; |
| 37 | + |
| 38 | + private __credentialCacheId: string | undefined; |
| 39 | + private get _credentialCacheId(): string { |
| 40 | + if (!this.__credentialCacheId) { |
| 41 | + const cacheIdParts: string[] = [ |
| 42 | + this._credentialNameForCache, |
| 43 | + this._azureEnvironment, |
| 44 | + ...this._getCacheIdParts() |
| 45 | + ]; |
| 46 | + |
| 47 | + this.__credentialCacheId = cacheIdParts.join('|'); |
| 48 | + } |
| 49 | + |
| 50 | + return this.__credentialCacheId; |
| 51 | + } |
| 52 | + |
| 53 | + public constructor(options: IAzureAuthenticationBaseOptions) { |
| 54 | + this._azureEnvironment = options.azureEnvironment || 'AzurePublicCloud'; |
| 55 | + } |
| 56 | + |
| 57 | + public async updateCachedCredentialAsync(terminal: ITerminal, credential: string): Promise<void> { |
| 58 | + await CredentialCache.usingAsync( |
| 59 | + { |
| 60 | + supportEditing: true |
| 61 | + }, |
| 62 | + async (credentialsCache: CredentialCache) => { |
| 63 | + credentialsCache.setCacheEntry(this._credentialCacheId, credential); |
| 64 | + await credentialsCache.saveIfModifiedAsync(); |
| 65 | + } |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Launches an interactive flow to renew a cached credential. |
| 71 | + * |
| 72 | + * @param terminal - The terminal to log output to |
| 73 | + * @param onlyIfExistingCredentialExpiresAfter - If specified, and a cached credential exists that is still valid |
| 74 | + * after the date specified, no action will be taken. |
| 75 | + */ |
| 76 | + public async updateCachedCredentialInteractiveAsync( |
| 77 | + terminal: ITerminal, |
| 78 | + onlyIfExistingCredentialExpiresAfter?: Date |
| 79 | + ): Promise<void> { |
| 80 | + await CredentialCache.usingAsync( |
| 81 | + { |
| 82 | + supportEditing: true |
| 83 | + }, |
| 84 | + async (credentialsCache: CredentialCache) => { |
| 85 | + if (onlyIfExistingCredentialExpiresAfter) { |
| 86 | + const existingCredentialExpiration: Date | undefined = credentialsCache.tryGetCacheEntry( |
| 87 | + this._credentialCacheId |
| 88 | + )?.expires; |
| 89 | + if ( |
| 90 | + existingCredentialExpiration && |
| 91 | + existingCredentialExpiration > onlyIfExistingCredentialExpiresAfter |
| 92 | + ) { |
| 93 | + return; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + const credential: ICredentialResult = await this._getCredentialAsync(terminal); |
| 98 | + credentialsCache.setCacheEntry( |
| 99 | + this._credentialCacheId, |
| 100 | + credential.credentialString, |
| 101 | + credential.expiresOn |
| 102 | + ); |
| 103 | + await credentialsCache.saveIfModifiedAsync(); |
| 104 | + } |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + public async deleteCachedCredentialsAsync(terminal: ITerminal): Promise<void> { |
| 109 | + await CredentialCache.usingAsync( |
| 110 | + { |
| 111 | + supportEditing: true |
| 112 | + }, |
| 113 | + async (credentialsCache: CredentialCache) => { |
| 114 | + credentialsCache.deleteCacheEntry(this._credentialCacheId); |
| 115 | + await credentialsCache.saveIfModifiedAsync(); |
| 116 | + } |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + public async tryGetCachedCredentialAsync(doNotThrowIfExpired?: boolean): Promise<string | undefined> { |
| 121 | + let cacheEntry: ICredentialCacheEntry | undefined; |
| 122 | + await CredentialCache.usingAsync( |
| 123 | + { |
| 124 | + supportEditing: false |
| 125 | + }, |
| 126 | + (credentialsCache: CredentialCache) => { |
| 127 | + cacheEntry = credentialsCache.tryGetCacheEntry(this._credentialCacheId); |
| 128 | + } |
| 129 | + ); |
| 130 | + |
| 131 | + const expirationTime: number | undefined = cacheEntry?.expires?.getTime(); |
| 132 | + if (expirationTime && expirationTime < Date.now()) { |
| 133 | + if (!doNotThrowIfExpired) { |
| 134 | + let errorMessage: string = `Cached Azure ${this._credentialKindForLogging} credentials have expired.`; |
| 135 | + if (this._credentialUpdateCommandForLogging) { |
| 136 | + errorMessage += ` Update the credentials by running "${this._credentialUpdateCommandForLogging}".`; |
| 137 | + } |
| 138 | + |
| 139 | + throw new Error(errorMessage); |
| 140 | + } else { |
| 141 | + return undefined; |
| 142 | + } |
| 143 | + } else { |
| 144 | + return cacheEntry?.credential; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Get parts of the cache ID that are specific to the credential type. Note that this should |
| 150 | + * not contain the Azure environment or the {@link AzureAuthenticationBase._credentialNameForCache} |
| 151 | + * value, as those are added automatically. |
| 152 | + */ |
| 153 | + protected abstract _getCacheIdParts(): string[]; |
| 154 | + |
| 155 | + protected abstract _getCredentialFromDeviceCodeAsync( |
| 156 | + terminal: ITerminal, |
| 157 | + deviceCodeCredential: DeviceCodeCredential |
| 158 | + ): Promise<ICredentialResult>; |
| 159 | + |
| 160 | + private async _getCredentialAsync(terminal: ITerminal): Promise<ICredentialResult> { |
| 161 | + const authorityHost: string | undefined = AzureAuthorityHosts[this._azureEnvironment]; |
| 162 | + if (!authorityHost) { |
| 163 | + throw new Error(`Unexpected Azure environment: ${this._azureEnvironment}`); |
| 164 | + } |
| 165 | + |
| 166 | + const deviceCodeCredential: DeviceCodeCredential = new DeviceCodeCredential({ |
| 167 | + authorityHost: authorityHost, |
| 168 | + userPromptCallback: (deviceCodeInfo: DeviceCodeInfo) => { |
| 169 | + PrintUtilities.printMessageInBox(deviceCodeInfo.message, terminal); |
| 170 | + } |
| 171 | + }); |
| 172 | + |
| 173 | + return await this._getCredentialFromDeviceCodeAsync(terminal, deviceCodeCredential); |
| 174 | + } |
| 175 | +} |
0 commit comments