Skip to content

Commit fae7e70

Browse files
committed
feat(credential-provider-sso): use SSOTokenProvider when new config format is detected
1 parent c850b15 commit fae7e70

File tree

8 files changed

+32
-18
lines changed

8 files changed

+32
-18
lines changed

packages/credential-provider-sso/src/resolveSSOCredentials.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { FromSSOInit, SsoCredentialsParameters } from "./fromSSO";
1010
* The time window (15 mins) that SDK will treat the SSO token expires in before the defined expiration date in token.
1111
* This is needed because server side may have invalidated the token before the defined expiration date.
1212
*
13-
* @internal
13+
* @private
1414
*/
1515
const EXPIRE_WINDOW_MS = 15 * 60 * 1000;
1616

packages/shared-ini-file-loader/src/getSSOTokenFilepath.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { getHomeDir } from "./getHomeDir";
66
/**
77
* Returns the filepath of the file where SSO token is stored.
88
*/
9-
export const getSSOTokenFilepath = (ssoStartUrl: string) => {
9+
export const getSSOTokenFilepath = (id: string) => {
1010
const hasher = createHash("sha1");
11-
const cacheName = hasher.update(ssoStartUrl).digest("hex");
11+
const cacheName = hasher.update(id).digest("hex");
1212
return join(getHomeDir(), ".aws", "sso", "cache", `${cacheName}.json`);
1313
};

packages/shared-ini-file-loader/src/getSSOTokenFromFile.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@ export interface SSOToken {
5353
}
5454

5555
/**
56+
* @param id - can be either a start URL or the SSO session name.
5657
* Returns the SSO token from the file system.
5758
*/
58-
export const getSSOTokenFromFile = async (ssoStartUrl: string) => {
59-
const ssoTokenFilepath = getSSOTokenFilepath(ssoStartUrl);
59+
export const getSSOTokenFromFile = async (id: string) => {
60+
const ssoTokenFilepath = getSSOTokenFilepath(id);
6061
const ssoTokenText = await readFile(ssoTokenFilepath, "utf8");
6162
return JSON.parse(ssoTokenText) as SSOToken;
6263
};

packages/token-providers/src/fromSso.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ describe(fromSso.name, () => {
123123
const mockError = new Error("mockError");
124124
(getSSOTokenFromFile as jest.Mock).mockRejectedValue(mockError);
125125
const expectedError = new TokenProviderError(
126-
`The SSO session associated with this profile is invalid. ${REFRESH_MESSAGE}`,
126+
`The SSO session associated with this profile is invalid. ${REFRESH_MESSAGE}\n${mockError}`,
127127
false
128128
);
129129
await expect(fromSso(mockInit)()).rejects.toStrictEqual(expectedError);
@@ -214,7 +214,8 @@ describe(fromSso.name, () => {
214214
expect(validateTokenKey).toHaveBeenNthCalledWith(
215215
(validateTokenKey as jest.Mock).mock.calls.length,
216216
key,
217-
mockSsoToken[key]
217+
mockSsoToken[key],
218+
true
218219
);
219220
}
220221
);

packages/token-providers/src/fromSso.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ export const fromSso =
6868

6969
let ssoToken: SSOToken;
7070
try {
71-
ssoToken = await getSSOTokenFromFile(ssoStartUrl);
71+
ssoToken = await getSSOTokenFromFile(ssoSessionName);
7272
} catch (e) {
7373
throw new TokenProviderError(
74-
`The SSO session associated with this profile is invalid. ${REFRESH_MESSAGE}`,
74+
`The SSO session associated with this profile is invalid. ${REFRESH_MESSAGE}\n${e}`,
7575
false
7676
);
7777
}
@@ -93,9 +93,9 @@ export const fromSso =
9393
return existingToken;
9494
}
9595

96-
validateTokenKey("clientId", ssoToken.clientId);
97-
validateTokenKey("clientSecret", ssoToken.clientSecret);
98-
validateTokenKey("refreshToken", ssoToken.refreshToken);
96+
validateTokenKey("clientId", ssoToken.clientId, true);
97+
validateTokenKey("clientSecret", ssoToken.clientSecret, true);
98+
validateTokenKey("refreshToken", ssoToken.refreshToken, true);
9999

100100
try {
101101
lastRefreshAttemptTime.setTime(Date.now());

packages/token-providers/src/validateTokenKey.spec.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@ describe(validateTokenKey.name, () => {
99
const value = undefined;
1010

1111
expect(() => validateTokenKey(key, value)).toThrow(
12-
new TokenProviderError(`Value not present for '${key}' in SSO Token'. ${REFRESH_MESSAGE}`, false)
12+
new TokenProviderError(`Value not present for '${key}' in SSO Token. ${REFRESH_MESSAGE}`, false)
13+
);
14+
});
15+
16+
it("specifies whether validation was for refresh", () => {
17+
const key = "key";
18+
const value = undefined;
19+
20+
expect(() => validateTokenKey(key, value, true)).toThrow(
21+
new TokenProviderError(`Value not present for '${key}' in SSO Token. Cannot refresh. ${REFRESH_MESSAGE}`, false)
1322
);
1423
});
1524

packages/token-providers/src/validateTokenKey.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ import { REFRESH_MESSAGE } from "./constants";
55
/**
66
* Throws TokenProviderError if value is undefined for key.
77
*/
8-
export const validateTokenKey = (key: string, value: unknown) => {
8+
export const validateTokenKey = (key: string, value: unknown, forRefresh = false) => {
99
if (typeof value === "undefined") {
10-
throw new TokenProviderError(`Value not present for '${key}' in SSO Token'. ${REFRESH_MESSAGE}`, false);
10+
throw new TokenProviderError(
11+
`Value not present for '${key}' in SSO Token${forRefresh ? ". Cannot refresh" : ""}. ${REFRESH_MESSAGE}`,
12+
false
13+
);
1114
}
1215
};

packages/token-providers/src/writeSSOTokenToFile.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import { promises as fsPromises } from "fs";
55
const { writeFile } = fsPromises;
66

77
/**
8-
* Writes SSO token to file based on filepath computed from ssoStartUrl.
8+
* Writes SSO token to file based on filepath computed from ssoStartUrl or session name.
99
*/
10-
export const writeSSOTokenToFile = (ssoStartUrl: string, ssoToken: SSOToken) => {
11-
const tokenFilepath = getSSOTokenFilepath(ssoStartUrl);
10+
export const writeSSOTokenToFile = (id: string, ssoToken: SSOToken) => {
11+
const tokenFilepath = getSSOTokenFilepath(id);
1212
const tokenString = JSON.stringify(ssoToken, null, 2);
1313
return writeFile(tokenFilepath, tokenString);
1414
};

0 commit comments

Comments
 (0)