| | | 1 | | /** |
| | | 2 | | * Thrown when the AWS SSO session for the active profile could not be loaded |
| | | 3 | | * or has expired, so credentials cannot be resolved. |
| | | 4 | | */ |
| | | 5 | | export class SsoSessionExpiredError extends Error { |
| | | 6 | | readonly profileName?: string; |
| | | 7 | | readonly cause?: unknown; |
| | | 8 | | |
| | | 9 | | constructor(profileName?: string, cause?: unknown) { |
| | 6 | 10 | | super(SsoSessionExpiredError.buildMessage(profileName)); |
| | 6 | 11 | | this.name = 'SsoSessionExpiredError'; |
| | 6 | 12 | | this.profileName = profileName; |
| | 6 | 13 | | this.cause = cause; |
| | | 14 | | } |
| | | 15 | | |
| | | 16 | | private static buildMessage(profileName?: string): string { |
| | 6 | 17 | | if (profileName) { |
| | 4 | 18 | | return ( |
| | | 19 | | `Your AWS SSO session for profile '${profileName}' could not be ` + |
| | | 20 | | `loaded or has expired. Run: aws sso login --profile ${profileName} ` + |
| | | 21 | | 'and then re-run this command.' |
| | | 22 | | ); |
| | | 23 | | } |
| | 2 | 24 | | return ( |
| | | 25 | | 'Your AWS SSO session could not be loaded or has expired. Run: ' + |
| | | 26 | | 'aws sso login and then re-run this command.' |
| | | 27 | | ); |
| | | 28 | | } |
| | | 29 | | } |
| | | 30 | | |
| | 5 | 31 | | const SSO_SESSION_EXPIRED_ERROR_NAMES = new Set(['TokenProviderError']); |
| | | 32 | | |
| | | 33 | | /** |
| | | 34 | | * Detects whether an unknown error represents an expired or unresolved AWS SSO |
| | | 35 | | * session, by inspecting its AWS error name. |
| | | 36 | | */ |
| | | 37 | | export function isSsoSessionExpiredError(error: unknown): boolean { |
| | 8 | 38 | | if (typeof error !== 'object' || error === null || !('name' in error)) { |
| | 1 | 39 | | return false; |
| | | 40 | | } |
| | 7 | 41 | | const name = String((error as { name?: unknown }).name); |
| | 7 | 42 | | return SSO_SESSION_EXPIRED_ERROR_NAMES.has(name); |
| | | 43 | | } |