@aws-sdk/token-providers 3.181.0 → 3.183.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/dist-es/constants.js +2 -2
- package/dist-es/fromSso.js +70 -102
- package/dist-es/fromStatic.js +5 -11
- package/dist-es/getNewSsoOidcToken.js +2 -2
- package/dist-es/getSsoOidcClient.js +3 -3
- package/dist-es/nodeProvider.js +3 -9
- package/dist-es/validateTokenExpiry.js +2 -2
- package/dist-es/validateTokenKey.js +2 -2
- package/dist-es/writeSSOTokenToFile.js +4 -4
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,22 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [3.183.0](https://github.com/aws/aws-sdk-js-v3/compare/v3.182.0...v3.183.0) (2022-10-03)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @aws-sdk/token-providers
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
# [3.182.0](https://github.com/aws/aws-sdk-js-v3/compare/v3.181.0...v3.182.0) (2022-09-30)
|
|
15
|
+
|
|
16
|
+
**Note:** Version bump only for package @aws-sdk/token-providers
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
6
22
|
# [3.181.0](https://github.com/aws/aws-sdk-js-v3/compare/v3.180.0...v3.181.0) (2022-09-29)
|
|
7
23
|
|
|
8
24
|
**Note:** Version bump only for package @aws-sdk/token-providers
|
package/dist-es/constants.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
1
|
+
export const EXPIRE_WINDOW_MS = 5 * 60 * 1000;
|
|
2
|
+
export const REFRESH_MESSAGE = `To refresh this SSO session run 'aws sso login' with the corresponding profile.`;
|
package/dist-es/fromSso.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { __assign, __awaiter, __generator, __values } from "tslib";
|
|
2
1
|
import { TokenProviderError } from "@aws-sdk/property-provider";
|
|
3
2
|
import { getProfileName, getSSOTokenFromFile, loadSsoSessionData, parseKnownFiles, } from "@aws-sdk/shared-ini-file-loader";
|
|
4
3
|
import { EXPIRE_WINDOW_MS, REFRESH_MESSAGE } from "./constants";
|
|
@@ -6,105 +5,74 @@ import { getNewSsoOidcToken } from "./getNewSsoOidcToken";
|
|
|
6
5
|
import { validateTokenExpiry } from "./validateTokenExpiry";
|
|
7
6
|
import { validateTokenKey } from "./validateTokenKey";
|
|
8
7
|
import { writeSSOTokenToFile } from "./writeSSOTokenToFile";
|
|
9
|
-
|
|
10
|
-
export
|
|
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
|
-
case 7:
|
|
80
|
-
_d.trys.push([7, 13, , 14]);
|
|
81
|
-
lastRefreshAttemptTime.setTime(Date.now());
|
|
82
|
-
return [4, getNewSsoOidcToken(ssoToken, ssoRegion)];
|
|
83
|
-
case 8:
|
|
84
|
-
newSsoOidcToken = _d.sent();
|
|
85
|
-
validateTokenKey("accessToken", newSsoOidcToken.accessToken);
|
|
86
|
-
validateTokenKey("expiresIn", newSsoOidcToken.expiresIn);
|
|
87
|
-
newTokenExpiration = new Date(Date.now() + newSsoOidcToken.expiresIn * 1000);
|
|
88
|
-
_d.label = 9;
|
|
89
|
-
case 9:
|
|
90
|
-
_d.trys.push([9, 11, , 12]);
|
|
91
|
-
return [4, writeSSOTokenToFile(ssoSessionName, __assign(__assign({}, ssoToken), { accessToken: newSsoOidcToken.accessToken, expiresAt: newTokenExpiration.toISOString(), refreshToken: newSsoOidcToken.refreshToken }))];
|
|
92
|
-
case 10:
|
|
93
|
-
_d.sent();
|
|
94
|
-
return [3, 12];
|
|
95
|
-
case 11:
|
|
96
|
-
error_1 = _d.sent();
|
|
97
|
-
return [3, 12];
|
|
98
|
-
case 12: return [2, {
|
|
99
|
-
token: newSsoOidcToken.accessToken,
|
|
100
|
-
expiration: newTokenExpiration,
|
|
101
|
-
}];
|
|
102
|
-
case 13:
|
|
103
|
-
error_2 = _d.sent();
|
|
104
|
-
validateTokenExpiry(existingToken);
|
|
105
|
-
return [2, existingToken];
|
|
106
|
-
case 14: return [2];
|
|
107
|
-
}
|
|
108
|
-
});
|
|
109
|
-
}); };
|
|
8
|
+
const lastRefreshAttemptTime = new Date(0);
|
|
9
|
+
export const fromSso = (init = {}) => async () => {
|
|
10
|
+
const profiles = await parseKnownFiles(init);
|
|
11
|
+
const profileName = getProfileName(init);
|
|
12
|
+
const profile = profiles[profileName];
|
|
13
|
+
if (!profile) {
|
|
14
|
+
throw new TokenProviderError(`Profile '${profileName}' could not be found in shared credentials file.`, false);
|
|
15
|
+
}
|
|
16
|
+
else if (!profile["sso_session"]) {
|
|
17
|
+
throw new TokenProviderError(`Profile '${profileName}' is missing required property 'sso_session'.`);
|
|
18
|
+
}
|
|
19
|
+
const ssoSessionName = profile["sso_session"];
|
|
20
|
+
const ssoSessions = await loadSsoSessionData(init);
|
|
21
|
+
const ssoSession = ssoSessions[ssoSessionName];
|
|
22
|
+
if (!ssoSession) {
|
|
23
|
+
throw new TokenProviderError(`Sso session '${ssoSessionName}' could not be found in shared credentials file.`, false);
|
|
24
|
+
}
|
|
25
|
+
for (const ssoSessionRequiredKey of ["sso_start_url", "sso_region"]) {
|
|
26
|
+
if (!ssoSession[ssoSessionRequiredKey]) {
|
|
27
|
+
throw new TokenProviderError(`Sso session '${ssoSessionName}' is missing required property '${ssoSessionRequiredKey}'.`, false);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const ssoStartUrl = ssoSession["sso_start_url"];
|
|
31
|
+
const ssoRegion = ssoSession["sso_region"];
|
|
32
|
+
let ssoToken;
|
|
33
|
+
try {
|
|
34
|
+
ssoToken = await getSSOTokenFromFile(ssoSessionName);
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
throw new TokenProviderError(`The SSO session associated with this profile is invalid. ${REFRESH_MESSAGE}`, false);
|
|
38
|
+
}
|
|
39
|
+
validateTokenKey("accessToken", ssoToken.accessToken);
|
|
40
|
+
validateTokenKey("expiresAt", ssoToken.expiresAt);
|
|
41
|
+
const { accessToken, expiresAt } = ssoToken;
|
|
42
|
+
const existingToken = { token: accessToken, expiration: new Date(expiresAt) };
|
|
43
|
+
if (existingToken.expiration.getTime() - Date.now() > EXPIRE_WINDOW_MS) {
|
|
44
|
+
return existingToken;
|
|
45
|
+
}
|
|
46
|
+
if (Date.now() - lastRefreshAttemptTime.getTime() < 30 * 1000) {
|
|
47
|
+
validateTokenExpiry(existingToken);
|
|
48
|
+
return existingToken;
|
|
49
|
+
}
|
|
50
|
+
validateTokenKey("clientId", ssoToken.clientId);
|
|
51
|
+
validateTokenKey("clientSecret", ssoToken.clientSecret);
|
|
52
|
+
validateTokenKey("refreshToken", ssoToken.refreshToken);
|
|
53
|
+
try {
|
|
54
|
+
lastRefreshAttemptTime.setTime(Date.now());
|
|
55
|
+
const newSsoOidcToken = await getNewSsoOidcToken(ssoToken, ssoRegion);
|
|
56
|
+
validateTokenKey("accessToken", newSsoOidcToken.accessToken);
|
|
57
|
+
validateTokenKey("expiresIn", newSsoOidcToken.expiresIn);
|
|
58
|
+
const newTokenExpiration = new Date(Date.now() + newSsoOidcToken.expiresIn * 1000);
|
|
59
|
+
try {
|
|
60
|
+
await writeSSOTokenToFile(ssoSessionName, {
|
|
61
|
+
...ssoToken,
|
|
62
|
+
accessToken: newSsoOidcToken.accessToken,
|
|
63
|
+
expiresAt: newTokenExpiration.toISOString(),
|
|
64
|
+
refreshToken: newSsoOidcToken.refreshToken,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
token: newSsoOidcToken.accessToken,
|
|
71
|
+
expiration: newTokenExpiration,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
validateTokenExpiry(existingToken);
|
|
76
|
+
return existingToken;
|
|
77
|
+
}
|
|
110
78
|
};
|
package/dist-es/fromStatic.js
CHANGED
|
@@ -1,13 +1,7 @@
|
|
|
1
|
-
import { __awaiter, __generator } from "tslib";
|
|
2
1
|
import { TokenProviderError } from "@aws-sdk/property-provider";
|
|
3
|
-
export
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
throw new TokenProviderError("Please pass a valid token to fromStatic", false);
|
|
9
|
-
}
|
|
10
|
-
return [2, token];
|
|
11
|
-
});
|
|
12
|
-
}); };
|
|
2
|
+
export const fromStatic = ({ token }) => async () => {
|
|
3
|
+
if (!token || !token.token) {
|
|
4
|
+
throw new TokenProviderError(`Please pass a valid token to fromStatic`, false);
|
|
5
|
+
}
|
|
6
|
+
return token;
|
|
13
7
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { CreateTokenCommand } from "@aws-sdk/client-sso-oidc";
|
|
2
2
|
import { getSsoOidcClient } from "./getSsoOidcClient";
|
|
3
|
-
export
|
|
4
|
-
|
|
3
|
+
export const getNewSsoOidcToken = (ssoToken, ssoRegion) => {
|
|
4
|
+
const ssoOidcClient = getSsoOidcClient(ssoRegion);
|
|
5
5
|
return ssoOidcClient.send(new CreateTokenCommand({
|
|
6
6
|
clientId: ssoToken.clientId,
|
|
7
7
|
clientSecret: ssoToken.clientSecret,
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { SSOOIDCClient } from "@aws-sdk/client-sso-oidc";
|
|
2
|
-
|
|
3
|
-
export
|
|
2
|
+
const ssoOidcClientsHash = {};
|
|
3
|
+
export const getSsoOidcClient = (ssoRegion) => {
|
|
4
4
|
if (ssoOidcClientsHash[ssoRegion]) {
|
|
5
5
|
return ssoOidcClientsHash[ssoRegion];
|
|
6
6
|
}
|
|
7
|
-
|
|
7
|
+
const ssoOidcClient = new SSOOIDCClient({ region: ssoRegion });
|
|
8
8
|
ssoOidcClientsHash[ssoRegion] = ssoOidcClient;
|
|
9
9
|
return ssoOidcClient;
|
|
10
10
|
};
|
package/dist-es/nodeProvider.js
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
import { __awaiter, __generator } from "tslib";
|
|
2
1
|
import { chain, memoize, TokenProviderError } from "@aws-sdk/property-provider";
|
|
3
2
|
import { fromSso } from "./fromSso";
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
return __generator(this, function (_a) {
|
|
8
|
-
throw new TokenProviderError("Could not load token from any providers", false);
|
|
9
|
-
});
|
|
10
|
-
}); }), function (token) { return token.expiration !== undefined && token.expiration.getTime() - Date.now() < 300000; }, function (token) { return token.expiration !== undefined; });
|
|
11
|
-
};
|
|
3
|
+
export const nodeProvider = (init = {}) => memoize(chain(fromSso(init), async () => {
|
|
4
|
+
throw new TokenProviderError("Could not load token from any providers", false);
|
|
5
|
+
}), (token) => token.expiration !== undefined && token.expiration.getTime() - Date.now() < 300000, (token) => token.expiration !== undefined);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { TokenProviderError } from "@aws-sdk/property-provider";
|
|
2
2
|
import { REFRESH_MESSAGE } from "./constants";
|
|
3
|
-
export
|
|
3
|
+
export const validateTokenExpiry = (token) => {
|
|
4
4
|
if (token.expiration && token.expiration.getTime() < Date.now()) {
|
|
5
|
-
throw new TokenProviderError(
|
|
5
|
+
throw new TokenProviderError(`Token is expired. ${REFRESH_MESSAGE}`, false);
|
|
6
6
|
}
|
|
7
7
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { TokenProviderError } from "@aws-sdk/property-provider";
|
|
2
2
|
import { REFRESH_MESSAGE } from "./constants";
|
|
3
|
-
export
|
|
3
|
+
export const validateTokenKey = (key, value) => {
|
|
4
4
|
if (typeof value === "undefined") {
|
|
5
|
-
throw new TokenProviderError(
|
|
5
|
+
throw new TokenProviderError(`Value not present for '${key}' in SSO Token'. ${REFRESH_MESSAGE}`, false);
|
|
6
6
|
}
|
|
7
7
|
};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { getSSOTokenFilepath } from "@aws-sdk/shared-ini-file-loader";
|
|
2
2
|
import { promises as fsPromises } from "fs";
|
|
3
|
-
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
const { writeFile } = fsPromises;
|
|
4
|
+
export const writeSSOTokenToFile = (ssoStartUrl, ssoToken) => {
|
|
5
|
+
const tokenFilepath = getSSOTokenFilepath(ssoStartUrl);
|
|
6
|
+
const tokenString = JSON.stringify(ssoToken, null, 2);
|
|
7
7
|
return writeFile(tokenFilepath, tokenString);
|
|
8
8
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aws-sdk/token-providers",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.183.0",
|
|
4
4
|
"description": "A collection of token providers",
|
|
5
5
|
"main": "./dist-cjs/index.js",
|
|
6
6
|
"module": "./dist-es/index.js",
|
|
@@ -25,10 +25,10 @@
|
|
|
25
25
|
},
|
|
26
26
|
"license": "Apache-2.0",
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@aws-sdk/client-sso-oidc": "3.
|
|
29
|
-
"@aws-sdk/property-provider": "3.
|
|
30
|
-
"@aws-sdk/shared-ini-file-loader": "3.
|
|
31
|
-
"@aws-sdk/types": "3.
|
|
28
|
+
"@aws-sdk/client-sso-oidc": "3.183.0",
|
|
29
|
+
"@aws-sdk/property-provider": "3.183.0",
|
|
30
|
+
"@aws-sdk/shared-ini-file-loader": "3.183.0",
|
|
31
|
+
"@aws-sdk/types": "3.183.0",
|
|
32
32
|
"tslib": "^2.3.1"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|