@aws-amplify/auth 6.11.6-unstable.cde36a7.0 → 6.11.7-unstable.b188564.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.
Files changed (25) hide show
  1. package/dist/cjs/foundation/factories/serviceClients/cognitoIdentityProvider/shared/serde/createUserPoolDeserializer.js +5 -1
  2. package/dist/cjs/foundation/factories/serviceClients/cognitoIdentityProvider/shared/serde/createUserPoolDeserializer.js.map +1 -1
  3. package/dist/cjs/providers/cognito/credentialsProvider/IdentityIdProvider.js +14 -7
  4. package/dist/cjs/providers/cognito/credentialsProvider/IdentityIdProvider.js.map +1 -1
  5. package/dist/cjs/providers/cognito/credentialsProvider/credentialsProvider.js +26 -13
  6. package/dist/cjs/providers/cognito/credentialsProvider/credentialsProvider.js.map +1 -1
  7. package/dist/esm/foundation/factories/serviceClients/cognitoIdentityProvider/shared/handler/cognitoUserPoolTransferHandler.d.ts +1 -1
  8. package/dist/esm/foundation/factories/serviceClients/cognitoIdentityProvider/shared/serde/createUserPoolDeserializer.mjs +5 -1
  9. package/dist/esm/foundation/factories/serviceClients/cognitoIdentityProvider/shared/serde/createUserPoolDeserializer.mjs.map +1 -1
  10. package/dist/esm/providers/cognito/credentialsProvider/IdentityIdProvider.mjs +14 -7
  11. package/dist/esm/providers/cognito/credentialsProvider/IdentityIdProvider.mjs.map +1 -1
  12. package/dist/esm/providers/cognito/credentialsProvider/credentialsProvider.mjs +26 -13
  13. package/dist/esm/providers/cognito/credentialsProvider/credentialsProvider.mjs.map +1 -1
  14. package/dist/esm/providers/cognito/tokenProvider/types.d.ts +1 -3
  15. package/dist/esm/types/Auth.d.ts +1 -3
  16. package/dist/esm/types/models.d.ts +1 -3
  17. package/dist/esm/types/outputs.d.ts +1 -3
  18. package/package.json +5 -5
  19. package/src/foundation/factories/serviceClients/cognitoIdentityProvider/shared/serde/createUserPoolDeserializer.ts +5 -1
  20. package/src/providers/cognito/credentialsProvider/IdentityIdProvider.ts +10 -4
  21. package/src/providers/cognito/credentialsProvider/credentialsProvider.ts +34 -19
  22. package/src/providers/cognito/tokenProvider/types.ts +1 -3
  23. package/src/types/Auth.ts +1 -1
  24. package/src/types/models.ts +1 -3
  25. package/src/types/outputs.ts +1 -3
@@ -11,7 +11,11 @@ const createUserPoolDeserializer = () => async (response) => {
11
11
  if (response.statusCode >= 300) {
12
12
  const error = await (0, aws_client_utils_1.parseJsonError)(response);
13
13
  (0, assertServiceError_1.assertServiceError)(error);
14
- throw new AuthError_1.AuthError({ name: error.name, message: error.message });
14
+ throw new AuthError_1.AuthError({
15
+ name: error.name,
16
+ message: error.message,
17
+ metadata: error.$metadata,
18
+ });
15
19
  }
16
20
  return (0, aws_client_utils_1.parseJsonBody)(response);
17
21
  };
@@ -1 +1 @@
1
- {"version":3,"file":"createUserPoolDeserializer.js","sources":["../../../../../../../../src/foundation/factories/serviceClients/cognitoIdentityProvider/shared/serde/createUserPoolDeserializer.ts"],"sourcesContent":["\"use strict\";\n// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.createUserPoolDeserializer = void 0;\nconst aws_client_utils_1 = require(\"@aws-amplify/core/internals/aws-client-utils\");\nconst assertServiceError_1 = require(\"../../../../../../errors/utils/assertServiceError\");\nconst AuthError_1 = require(\"../../../../../../errors/AuthError\");\nconst createUserPoolDeserializer = () => async (response) => {\n if (response.statusCode >= 300) {\n const error = await (0, aws_client_utils_1.parseJsonError)(response);\n (0, assertServiceError_1.assertServiceError)(error);\n throw new AuthError_1.AuthError({ name: error.name, message: error.message });\n }\n return (0, aws_client_utils_1.parseJsonBody)(response);\n};\nexports.createUserPoolDeserializer = createUserPoolDeserializer;\n"],"names":[],"mappings":";;AACA;AACA;AACA,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;AAC7D,OAAO,CAAC,0BAA0B,GAAG,MAAM;AAC3C,MAAM,kBAAkB,GAAG,OAAO,CAAC,8CAA8C,CAAC;AAClF,MAAM,oBAAoB,GAAG,OAAO,CAAC,mDAAmD,CAAC;AACzF,MAAM,WAAW,GAAG,OAAO,CAAC,oCAAoC,CAAC;AACjE,MAAM,0BAA0B,GAAG,MAAM,OAAO,QAAQ,KAAK;AAC7D,IAAI,IAAI,QAAQ,CAAC,UAAU,IAAI,GAAG,EAAE;AACpC,QAAQ,MAAM,KAAK,GAAG,MAAM,IAAI,kBAAkB,CAAC,cAAc,EAAE,QAAQ,CAAC;AAC5E,QAAQ,IAAI,oBAAoB,CAAC,kBAAkB,EAAE,KAAK,CAAC;AAC3D,QAAQ,MAAM,IAAI,WAAW,CAAC,SAAS,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,CAAC;AACrF;AACA,IAAI,OAAO,IAAI,kBAAkB,CAAC,aAAa,EAAE,QAAQ,CAAC;AAC1D,CAAC;AACD,OAAO,CAAC,0BAA0B,GAAG,0BAA0B;;"}
1
+ {"version":3,"file":"createUserPoolDeserializer.js","sources":["../../../../../../../../src/foundation/factories/serviceClients/cognitoIdentityProvider/shared/serde/createUserPoolDeserializer.ts"],"sourcesContent":["\"use strict\";\n// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.createUserPoolDeserializer = void 0;\nconst aws_client_utils_1 = require(\"@aws-amplify/core/internals/aws-client-utils\");\nconst assertServiceError_1 = require(\"../../../../../../errors/utils/assertServiceError\");\nconst AuthError_1 = require(\"../../../../../../errors/AuthError\");\nconst createUserPoolDeserializer = () => async (response) => {\n if (response.statusCode >= 300) {\n const error = await (0, aws_client_utils_1.parseJsonError)(response);\n (0, assertServiceError_1.assertServiceError)(error);\n throw new AuthError_1.AuthError({\n name: error.name,\n message: error.message,\n metadata: error.$metadata,\n });\n }\n return (0, aws_client_utils_1.parseJsonBody)(response);\n};\nexports.createUserPoolDeserializer = createUserPoolDeserializer;\n"],"names":[],"mappings":";;AACA;AACA;AACA,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;AAC7D,OAAO,CAAC,0BAA0B,GAAG,MAAM;AAC3C,MAAM,kBAAkB,GAAG,OAAO,CAAC,8CAA8C,CAAC;AAClF,MAAM,oBAAoB,GAAG,OAAO,CAAC,mDAAmD,CAAC;AACzF,MAAM,WAAW,GAAG,OAAO,CAAC,oCAAoC,CAAC;AACjE,MAAM,0BAA0B,GAAG,MAAM,OAAO,QAAQ,KAAK;AAC7D,IAAI,IAAI,QAAQ,CAAC,UAAU,IAAI,GAAG,EAAE;AACpC,QAAQ,MAAM,KAAK,GAAG,MAAM,IAAI,kBAAkB,CAAC,cAAc,EAAE,QAAQ,CAAC;AAC5E,QAAQ,IAAI,oBAAoB,CAAC,kBAAkB,EAAE,KAAK,CAAC;AAC3D,QAAQ,MAAM,IAAI,WAAW,CAAC,SAAS,CAAC;AACxC,YAAY,IAAI,EAAE,KAAK,CAAC,IAAI;AAC5B,YAAY,OAAO,EAAE,KAAK,CAAC,OAAO;AAClC,YAAY,QAAQ,EAAE,KAAK,CAAC,SAAS;AACrC,SAAS,CAAC;AACV;AACA,IAAI,OAAO,IAAI,kBAAkB,CAAC,aAAa,EAAE,QAAQ,CAAC;AAC1D,CAAC;AACD,OAAO,CAAC,0BAA0B,GAAG,0BAA0B;;"}
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.cognitoIdentityIdProvider = void 0;
7
7
  const core_1 = require("@aws-amplify/core");
8
8
  const AuthError_1 = require("../../../errors/AuthError");
9
+ const assertServiceError_1 = require("../../../errors/utils/assertServiceError");
9
10
  const parsers_1 = require("../../../foundation/parsers");
10
11
  const utils_1 = require("./utils");
11
12
  /**
@@ -40,15 +41,21 @@ async function generateIdentityId(logins, authConfig) {
40
41
  const identityPoolId = authConfig?.identityPoolId;
41
42
  const region = (0, parsers_1.getRegionFromIdentityPoolId)(identityPoolId);
42
43
  // IdentityId is absent so get it using IdentityPoolId with Cognito's GetId API
43
- const idResult =
44
+ let idResult;
44
45
  // for a first-time user, this will return a brand new identity
45
46
  // for a returning user, this will retrieve the previous identity assocaited with the logins
46
- (await (0, core_1.getId)({
47
- region,
48
- }, {
49
- IdentityPoolId: identityPoolId,
50
- Logins: logins,
51
- })).IdentityId;
47
+ try {
48
+ idResult = (await (0, core_1.getId)({
49
+ region,
50
+ }, {
51
+ IdentityPoolId: identityPoolId,
52
+ Logins: logins,
53
+ })).IdentityId;
54
+ }
55
+ catch (e) {
56
+ (0, assertServiceError_1.assertServiceError)(e);
57
+ throw new AuthError_1.AuthError(e);
58
+ }
52
59
  if (!idResult) {
53
60
  throw new AuthError_1.AuthError({
54
61
  name: 'GetIdResponseException',
@@ -1 +1 @@
1
- {"version":3,"file":"IdentityIdProvider.js","sources":["../../../../../src/providers/cognito/credentialsProvider/IdentityIdProvider.ts"],"sourcesContent":["\"use strict\";\n// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.cognitoIdentityIdProvider = void 0;\nconst core_1 = require(\"@aws-amplify/core\");\nconst AuthError_1 = require(\"../../../errors/AuthError\");\nconst parsers_1 = require(\"../../../foundation/parsers\");\nconst utils_1 = require(\"./utils\");\n/**\n * Provides a Cognito identityId\n *\n * @param tokens - The AuthTokens received after SignIn\n * @returns string\n * @throws configuration exceptions: `InvalidIdentityPoolIdException`\n * - Auth errors that may arise from misconfiguration.\n * @throws service exceptions: {@link GetIdException }\n */\nasync function cognitoIdentityIdProvider({ tokens, authConfig, identityIdStore, }) {\n identityIdStore.setAuthConfig({ Cognito: authConfig });\n // will return null only if there is no identityId cached or if there is an error retrieving it\n const identityId = await identityIdStore.loadIdentityId();\n if (identityId) {\n return identityId.id;\n }\n const logins = tokens?.idToken\n ? (0, utils_1.formLoginsMap)(tokens.idToken.toString())\n : {};\n const generatedIdentityId = await generateIdentityId(logins, authConfig);\n // Store generated identityId\n identityIdStore.storeIdentityId({\n id: generatedIdentityId,\n type: tokens ? 'primary' : 'guest',\n });\n return generatedIdentityId;\n}\nexports.cognitoIdentityIdProvider = cognitoIdentityIdProvider;\nasync function generateIdentityId(logins, authConfig) {\n const identityPoolId = authConfig?.identityPoolId;\n const region = (0, parsers_1.getRegionFromIdentityPoolId)(identityPoolId);\n // IdentityId is absent so get it using IdentityPoolId with Cognito's GetId API\n const idResult = \n // for a first-time user, this will return a brand new identity\n // for a returning user, this will retrieve the previous identity assocaited with the logins\n (await (0, core_1.getId)({\n region,\n }, {\n IdentityPoolId: identityPoolId,\n Logins: logins,\n })).IdentityId;\n if (!idResult) {\n throw new AuthError_1.AuthError({\n name: 'GetIdResponseException',\n message: 'Received undefined response from getId operation',\n recoverySuggestion: 'Make sure to pass a valid identityPoolId in the configuration.',\n });\n }\n return idResult;\n}\n"],"names":[],"mappings":";;AACA;AACA;AACA,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;AAC7D,OAAO,CAAC,yBAAyB,GAAG,MAAM;AAC1C,MAAM,MAAM,GAAG,OAAO,CAAC,mBAAmB,CAAC;AAC3C,MAAM,WAAW,GAAG,OAAO,CAAC,2BAA2B,CAAC;AACxD,MAAM,SAAS,GAAG,OAAO,CAAC,6BAA6B,CAAC;AACxD,MAAM,OAAO,GAAG,OAAO,CAAC,SAAS,CAAC;AAClC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,yBAAyB,CAAC,EAAE,MAAM,EAAE,UAAU,EAAE,eAAe,GAAG,EAAE;AACnF,IAAI,eAAe,CAAC,aAAa,CAAC,EAAE,OAAO,EAAE,UAAU,EAA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"}
1
+ {"version":3,"file":"IdentityIdProvider.js","sources":["../../../../../src/providers/cognito/credentialsProvider/IdentityIdProvider.ts"],"sourcesContent":["\"use strict\";\n// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.cognitoIdentityIdProvider = void 0;\nconst core_1 = require(\"@aws-amplify/core\");\nconst AuthError_1 = require(\"../../../errors/AuthError\");\nconst assertServiceError_1 = require(\"../../../errors/utils/assertServiceError\");\nconst parsers_1 = require(\"../../../foundation/parsers\");\nconst utils_1 = require(\"./utils\");\n/**\n * Provides a Cognito identityId\n *\n * @param tokens - The AuthTokens received after SignIn\n * @returns string\n * @throws configuration exceptions: `InvalidIdentityPoolIdException`\n * - Auth errors that may arise from misconfiguration.\n * @throws service exceptions: {@link GetIdException }\n */\nasync function cognitoIdentityIdProvider({ tokens, authConfig, identityIdStore, }) {\n identityIdStore.setAuthConfig({ Cognito: authConfig });\n // will return null only if there is no identityId cached or if there is an error retrieving it\n const identityId = await identityIdStore.loadIdentityId();\n if (identityId) {\n return identityId.id;\n }\n const logins = tokens?.idToken\n ? (0, utils_1.formLoginsMap)(tokens.idToken.toString())\n : {};\n const generatedIdentityId = await generateIdentityId(logins, authConfig);\n // Store generated identityId\n identityIdStore.storeIdentityId({\n id: generatedIdentityId,\n type: tokens ? 'primary' : 'guest',\n });\n return generatedIdentityId;\n}\nexports.cognitoIdentityIdProvider = cognitoIdentityIdProvider;\nasync function generateIdentityId(logins, authConfig) {\n const identityPoolId = authConfig?.identityPoolId;\n const region = (0, parsers_1.getRegionFromIdentityPoolId)(identityPoolId);\n // IdentityId is absent so get it using IdentityPoolId with Cognito's GetId API\n let idResult;\n // for a first-time user, this will return a brand new identity\n // for a returning user, this will retrieve the previous identity assocaited with the logins\n try {\n idResult = (await (0, core_1.getId)({\n region,\n }, {\n IdentityPoolId: identityPoolId,\n Logins: logins,\n })).IdentityId;\n }\n catch (e) {\n (0, assertServiceError_1.assertServiceError)(e);\n throw new AuthError_1.AuthError(e);\n }\n if (!idResult) {\n throw new AuthError_1.AuthError({\n name: 'GetIdResponseException',\n message: 'Received undefined response from getId operation',\n recoverySuggestion: 'Make sure to pass a valid identityPoolId in the configuration.',\n });\n }\n return idResult;\n}\n"],"names":[],"mappings":";;AACA;AACA;AACA,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;AAC7D,OAAO,CAAC,yBAAyB,GAAG,MAAM;AAC1C,MAAM,MAAM,GAAG,OAAO,CAAC,mBAAmB,CAAC;AAC3C,MAAM,WAAW,GAAG,OAAO,CAAC,2BAA2B,CAAC;AACxD,MAAM,oBAAoB,GAAG,OAAO,CAAC,0CAA0C,CAAC;AAChF,MAAM,SAAS,GAAG,OAAO,CAAC,6BAA6B,CAAC;AACxD,MAAM,OAAO,GAAG,OAAO,CAAC,SAAS,CAAC;AAClC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,yBAAyB,CAAC,EAAE,MAAM,EAAE,UAAU,EAAE,eAAe,GAAG,EAAE;AACnF,IAAI,eAAe,CAAC,aAAa,CAAC,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC;AAC1D;AACA,IAAI,MAAM,UAAU,GAAG,MAAM,eAAe,CAAC,cAAc,EAAE;AAC7D,IAAI,IAAI,UAAU,EAAE;AACpB,QAAQ,OAAO,UAAU,CAAC,EAAE;AAC5B;AACA,IAAI,MAAM,MAAM,GAAG,MAAM,EAAE;AAC3B,UAAU,IAAI,OAAO,CAAC,aAAa,EAAE,MAAM,CAAC,OAAO,CAAC,QAAQ,EAAE;AAC9D,UAAU,EAAE;AACZ,IAAI,MAAM,mBAAmB,GAAG,MAAM,kBAAkB,CAAC,MAAM,EAAE,UAAU,CAAC;AAC5E;AACA,IAAI,eAAe,CAAC,eAAe,CAAC;AACpC,QAAQ,EAAE,EAAE,mBAAmB;AAC/B,QAAQ,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO;AAC1C,KAAK,CAAC;AACN,IAAI,OAAO,mBAAmB;AAC9B;AACA,OAAO,CAAC,yBAAyB,GAAG,yBAAyB;AAC7D,eAAe,kBAAkB,CAAC,MAAM,EAAE,UAAU,EAAE;AACtD,IAAI,MAAM,cAAc,GAAG,UAAU,EAAE,cAAc;AACrD,IAAI,MAAM,MAAM,GAAG,IAAI,SAAS,CAAC,2BAA2B,EAAE,cAAc,CAAC;AAC7E;AACA,IAAI,IAAI,QAAQ;AAChB;AACA;AACA,IAAI,IAAI;AACR,QAAQ,QAAQ,GAAG,CAAC,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,KAAK,EAAE;AAC5C,YAAY,MAAM;AAClB,SAAS,EAAE;AACX,YAAY,cAAc,EAAE,cAAc;AAC1C,YAAY,MAAM,EAAE,MAAM;AAC1B,SAAS,CAAC,EAAE,UAAU;AACtB;AACA,IAAI,OAAO,CAAC,EAAE;AACd,QAAQ,IAAI,oBAAoB,CAAC,kBAAkB,EAAE,CAAC,CAAC;AACvD,QAAQ,MAAM,IAAI,WAAW,CAAC,SAAS,CAAC,CAAC,CAAC;AAC1C;AACA,IAAI,IAAI,CAAC,QAAQ,EAAE;AACnB,QAAQ,MAAM,IAAI,WAAW,CAAC,SAAS,CAAC;AACxC,YAAY,IAAI,EAAE,wBAAwB;AAC1C,YAAY,OAAO,EAAE,kDAAkD;AACvE,YAAY,kBAAkB,EAAE,gEAAgE;AAChG,SAAS,CAAC;AACV;AACA,IAAI,OAAO,QAAQ;AACnB;;"}
@@ -7,6 +7,7 @@ exports.CognitoAWSCredentialsAndIdentityIdProvider = void 0;
7
7
  const core_1 = require("@aws-amplify/core");
8
8
  const utils_1 = require("@aws-amplify/core/internals/utils");
9
9
  const AuthError_1 = require("../../../errors/AuthError");
10
+ const assertServiceError_1 = require("../../../errors/utils/assertServiceError");
10
11
  const parsers_1 = require("../../../foundation/parsers");
11
12
  const types_1 = require("../utils/types");
12
13
  const IdentityIdProvider_1 = require("./IdentityIdProvider");
@@ -76,12 +77,18 @@ class CognitoAWSCredentialsAndIdentityIdProvider {
76
77
  // save credentials in-memory
77
78
  // No logins params should be passed for guest creds:
78
79
  // https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_GetCredentialsForIdentity.html
79
- const clientResult = await (0, core_1.getCredentialsForIdentity)({ region }, {
80
- IdentityId: identityId,
81
- });
82
- if (clientResult.Credentials &&
83
- clientResult.Credentials.AccessKeyId &&
84
- clientResult.Credentials.SecretKey) {
80
+ let clientResult;
81
+ try {
82
+ clientResult = await (0, core_1.getCredentialsForIdentity)({ region }, {
83
+ IdentityId: identityId,
84
+ });
85
+ }
86
+ catch (e) {
87
+ (0, assertServiceError_1.assertServiceError)(e);
88
+ throw new AuthError_1.AuthError(e);
89
+ }
90
+ if (clientResult?.Credentials?.AccessKeyId &&
91
+ clientResult?.Credentials?.SecretKey) {
85
92
  this._nextCredentialsRefresh = new Date().getTime() + CREDENTIALS_TTL;
86
93
  const res = {
87
94
  credentials: {
@@ -125,13 +132,19 @@ class CognitoAWSCredentialsAndIdentityIdProvider {
125
132
  ? (0, utils_2.formLoginsMap)(authTokens.idToken.toString())
126
133
  : {};
127
134
  const region = (0, parsers_1.getRegionFromIdentityPoolId)(authConfig.identityPoolId);
128
- const clientResult = await (0, core_1.getCredentialsForIdentity)({ region }, {
129
- IdentityId: identityId,
130
- Logins: logins,
131
- });
132
- if (clientResult.Credentials &&
133
- clientResult.Credentials.AccessKeyId &&
134
- clientResult.Credentials.SecretKey) {
135
+ let clientResult;
136
+ try {
137
+ clientResult = await (0, core_1.getCredentialsForIdentity)({ region }, {
138
+ IdentityId: identityId,
139
+ Logins: logins,
140
+ });
141
+ }
142
+ catch (e) {
143
+ (0, assertServiceError_1.assertServiceError)(e);
144
+ throw new AuthError_1.AuthError(e);
145
+ }
146
+ if (clientResult?.Credentials?.AccessKeyId &&
147
+ clientResult?.Credentials?.SecretKey) {
135
148
  this._nextCredentialsRefresh = new Date().getTime() + CREDENTIALS_TTL;
136
149
  const res = {
137
150
  credentials: {
@@ -1 +1 @@
1
- {"version":3,"file":"credentialsProvider.js","sources":["../../../../../src/providers/cognito/credentialsProvider/credentialsProvider.ts"],"sourcesContent":["\"use strict\";\n// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.CognitoAWSCredentialsAndIdentityIdProvider = void 0;\nconst core_1 = require(\"@aws-amplify/core\");\nconst utils_1 = require(\"@aws-amplify/core/internals/utils\");\nconst AuthError_1 = require(\"../../../errors/AuthError\");\nconst parsers_1 = require(\"../../../foundation/parsers\");\nconst types_1 = require(\"../utils/types\");\nconst IdentityIdProvider_1 = require(\"./IdentityIdProvider\");\nconst utils_2 = require(\"./utils\");\nconst logger = new core_1.ConsoleLogger('CognitoCredentialsProvider');\nconst CREDENTIALS_TTL = 50 * 60 * 1000; // 50 min, can be modified on config if required in the future\nclass CognitoAWSCredentialsAndIdentityIdProvider {\n constructor(identityIdStore) {\n this._nextCredentialsRefresh = 0;\n this._identityIdStore = identityIdStore;\n }\n async clearCredentialsAndIdentityId() {\n logger.debug('Clearing out credentials and identityId');\n this._credentialsAndIdentityId = undefined;\n await this._identityIdStore.clearIdentityId();\n }\n async clearCredentials() {\n logger.debug('Clearing out in-memory credentials');\n this._credentialsAndIdentityId = undefined;\n }\n async getCredentialsAndIdentityId(getCredentialsOptions) {\n const isAuthenticated = getCredentialsOptions.authenticated;\n const { tokens } = getCredentialsOptions;\n const { authConfig } = getCredentialsOptions;\n try {\n (0, utils_1.assertIdentityPoolIdConfig)(authConfig?.Cognito);\n }\n catch {\n // No identity pool configured, skipping\n return;\n }\n if (!isAuthenticated && !authConfig.Cognito.allowGuestAccess) {\n // TODO(V6): return partial result like Native platforms\n return;\n }\n const { forceRefresh } = getCredentialsOptions;\n const tokenHasChanged = this.hasTokenChanged(tokens);\n const identityId = await (0, IdentityIdProvider_1.cognitoIdentityIdProvider)({\n tokens,\n authConfig: authConfig.Cognito,\n identityIdStore: this._identityIdStore,\n });\n // Clear cached credentials when forceRefresh is true OR the cache token has changed\n if (forceRefresh || tokenHasChanged) {\n this.clearCredentials();\n }\n if (!isAuthenticated) {\n return this.getGuestCredentials(identityId, authConfig.Cognito);\n }\n else {\n (0, types_1.assertIdTokenInAuthTokens)(tokens);\n return this.credsForOIDCTokens(authConfig.Cognito, tokens, identityId);\n }\n }\n async getGuestCredentials(identityId, authConfig) {\n // Return existing in-memory cached credentials only if it exists, is not past it's lifetime and is unauthenticated credentials\n if (this._credentialsAndIdentityId &&\n !this.isPastTTL() &&\n this._credentialsAndIdentityId.isAuthenticatedCreds === false) {\n logger.info('returning stored credentials as they neither past TTL nor expired.');\n return this._credentialsAndIdentityId;\n }\n // Clear to discard if any authenticated credentials are set and start with a clean slate\n this.clearCredentials();\n const region = (0, parsers_1.getRegionFromIdentityPoolId)(authConfig.identityPoolId);\n // use identityId to obtain guest credentials\n // save credentials in-memory\n // No logins params should be passed for guest creds:\n // https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_GetCredentialsForIdentity.html\n const clientResult = await (0, core_1.getCredentialsForIdentity)({ region }, {\n IdentityId: identityId,\n });\n if (clientResult.Credentials &&\n clientResult.Credentials.AccessKeyId &&\n clientResult.Credentials.SecretKey) {\n this._nextCredentialsRefresh = new Date().getTime() + CREDENTIALS_TTL;\n const res = {\n credentials: {\n accessKeyId: clientResult.Credentials.AccessKeyId,\n secretAccessKey: clientResult.Credentials.SecretKey,\n sessionToken: clientResult.Credentials.SessionToken,\n expiration: clientResult.Credentials.Expiration,\n },\n identityId,\n };\n if (clientResult.IdentityId) {\n res.identityId = clientResult.IdentityId;\n this._identityIdStore.storeIdentityId({\n id: clientResult.IdentityId,\n type: 'guest',\n });\n }\n this._credentialsAndIdentityId = {\n ...res,\n isAuthenticatedCreds: false,\n };\n return res;\n }\n else {\n throw new AuthError_1.AuthError({\n name: 'CredentialsNotFoundException',\n message: `Cognito did not respond with either Credentials, AccessKeyId or SecretKey.`,\n });\n }\n }\n async credsForOIDCTokens(authConfig, authTokens, identityId) {\n if (this._credentialsAndIdentityId &&\n !this.isPastTTL() &&\n this._credentialsAndIdentityId.isAuthenticatedCreds === true) {\n logger.debug('returning stored credentials as they neither past TTL nor expired.');\n return this._credentialsAndIdentityId;\n }\n // Clear to discard if any unauthenticated credentials are set and start with a clean slate\n this.clearCredentials();\n const logins = authTokens.idToken\n ? (0, utils_2.formLoginsMap)(authTokens.idToken.toString())\n : {};\n const region = (0, parsers_1.getRegionFromIdentityPoolId)(authConfig.identityPoolId);\n const clientResult = await (0, core_1.getCredentialsForIdentity)({ region }, {\n IdentityId: identityId,\n Logins: logins,\n });\n if (clientResult.Credentials &&\n clientResult.Credentials.AccessKeyId &&\n clientResult.Credentials.SecretKey) {\n this._nextCredentialsRefresh = new Date().getTime() + CREDENTIALS_TTL;\n const res = {\n credentials: {\n accessKeyId: clientResult.Credentials.AccessKeyId,\n secretAccessKey: clientResult.Credentials.SecretKey,\n sessionToken: clientResult.Credentials.SessionToken,\n expiration: clientResult.Credentials.Expiration,\n },\n identityId,\n };\n if (clientResult.IdentityId) {\n res.identityId = clientResult.IdentityId;\n // note: the following call removes guest identityId from the persistent store (localStorage)\n this._identityIdStore.storeIdentityId({\n id: clientResult.IdentityId,\n type: 'primary',\n });\n }\n // Store the credentials in-memory along with the expiration\n this._credentialsAndIdentityId = {\n ...res,\n isAuthenticatedCreds: true,\n associatedIdToken: authTokens.idToken?.toString(),\n };\n return res;\n }\n else {\n throw new AuthError_1.AuthError({\n name: 'CredentialsException',\n message: `Cognito did not respond with either Credentials, AccessKeyId or SecretKey.`,\n });\n }\n }\n isPastTTL() {\n return this._nextCredentialsRefresh === undefined\n ? true\n : this._nextCredentialsRefresh <= Date.now();\n }\n hasTokenChanged(tokens) {\n return (!!tokens &&\n !!this._credentialsAndIdentityId?.associatedIdToken &&\n tokens.idToken?.toString() !==\n this._credentialsAndIdentityId.associatedIdToken);\n }\n}\nexports.CognitoAWSCredentialsAndIdentityIdProvider = CognitoAWSCredentialsAndIdentityIdProvider;\n"],"names":[],"mappings":";;AACA;AACA;AACA,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;AAC7D,OAAO,CAAC,0CAA0C,GAAG,MAAM;AAC3D,MAAM,MAAM,GAAG,OAAO,CAAC,mBAAmB,CAAC;AAC3C,MAAM,OAAO,GAAG,OAAO,CAAC,mCAAmC,CAAC;AAC5D,MAAM,WAAW,GAAG,OAAO,CAAC,2BAA2B,CAAC;AACxD,MAAM,SAAS,GAAG,OAAO,CAAC,6BAA6B,CAAC;AACxD,MAAM,OAAO,GAAG,OAAO,CAAC,gBAAgB,CAAC;AACzC,MAAM,oBAAoB,GAAG,OAAO,CAAC,sBAAsB,CAAC;AAC5D,MAAM,OAAO,GAAG,OAAO,CAAC,SAAS,CAAC;AAClC,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,aAAa,CAAC,4BAA4B,CAAC;AACrE,MAAM,eAAe,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AACvC,MAAM,0CAA0C,CAAC;AACjD,IAAI,WAAW,CAAC,eAAe,EAAE;AACjC,QAAQ,IAAI,CAAC,uBAAuB,GAAG,CAAC;AACxC,QAAQ,IAAI,CAAC,gBAAgB,GAAG,eAAe;AAC/C;AACA,IAAI,MAAM,6BAA6B,GAAG;AAC1C,QAAQ,MAAM,CAAC,KAAK,CAAC,yCAAyC,CAAC;AAC/D,QAAQ,IAAI,CAAC,yBAAyB,GAAG,SAAS;AAClD,QAAQ,MAAM,IAAI,CAAC,gBAAgB,CAAC,eAAe,EAAE;AACrD;AACA,IAAI,MAAM,gBAAgB,GAAG;AAC7B,QAAQ,MAAM,CAAC,KAAK,CAAC,oCAAoC,CAAC;AAC1D,QAAQ,IAAI,CA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ACd,cAAc,IAAI,CAAC,uBAAuB,IAAI,IAAI,CAAC,GAAG,EAAE;AACxD;AACA,IAAI,eAAe,CAAC,MAAM,EAAE;AAC5B,QAAQ,QAAQ,CAAC,CAAC,MAAM;AACxB,YAAY,CAAC,CAAC,IAAI,CAAC,yBAAyB,EAAE,iBAAiB;AAC/D,YAAY,MAAM,CAAC,OAAO,EAAE,QAAQ,EAAE;AACtC,gBAAgB,IAAI,CAAC,yBAAyB,CAAC,iBAAiB;AAChE;AACA;AACA,OAAO,CAAC,0CAA0C,GAAG,0CAA0C;;"}
1
+ {"version":3,"file":"credentialsProvider.js","sources":["../../../../../src/providers/cognito/credentialsProvider/credentialsProvider.ts"],"sourcesContent":["\"use strict\";\n// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.CognitoAWSCredentialsAndIdentityIdProvider = void 0;\nconst core_1 = require(\"@aws-amplify/core\");\nconst utils_1 = require(\"@aws-amplify/core/internals/utils\");\nconst AuthError_1 = require(\"../../../errors/AuthError\");\nconst assertServiceError_1 = require(\"../../../errors/utils/assertServiceError\");\nconst parsers_1 = require(\"../../../foundation/parsers\");\nconst types_1 = require(\"../utils/types\");\nconst IdentityIdProvider_1 = require(\"./IdentityIdProvider\");\nconst utils_2 = require(\"./utils\");\nconst logger = new core_1.ConsoleLogger('CognitoCredentialsProvider');\nconst CREDENTIALS_TTL = 50 * 60 * 1000; // 50 min, can be modified on config if required in the future\nclass CognitoAWSCredentialsAndIdentityIdProvider {\n constructor(identityIdStore) {\n this._nextCredentialsRefresh = 0;\n this._identityIdStore = identityIdStore;\n }\n async clearCredentialsAndIdentityId() {\n logger.debug('Clearing out credentials and identityId');\n this._credentialsAndIdentityId = undefined;\n await this._identityIdStore.clearIdentityId();\n }\n async clearCredentials() {\n logger.debug('Clearing out in-memory credentials');\n this._credentialsAndIdentityId = undefined;\n }\n async getCredentialsAndIdentityId(getCredentialsOptions) {\n const isAuthenticated = getCredentialsOptions.authenticated;\n const { tokens } = getCredentialsOptions;\n const { authConfig } = getCredentialsOptions;\n try {\n (0, utils_1.assertIdentityPoolIdConfig)(authConfig?.Cognito);\n }\n catch {\n // No identity pool configured, skipping\n return;\n }\n if (!isAuthenticated && !authConfig.Cognito.allowGuestAccess) {\n // TODO(V6): return partial result like Native platforms\n return;\n }\n const { forceRefresh } = getCredentialsOptions;\n const tokenHasChanged = this.hasTokenChanged(tokens);\n const identityId = await (0, IdentityIdProvider_1.cognitoIdentityIdProvider)({\n tokens,\n authConfig: authConfig.Cognito,\n identityIdStore: this._identityIdStore,\n });\n // Clear cached credentials when forceRefresh is true OR the cache token has changed\n if (forceRefresh || tokenHasChanged) {\n this.clearCredentials();\n }\n if (!isAuthenticated) {\n return this.getGuestCredentials(identityId, authConfig.Cognito);\n }\n else {\n (0, types_1.assertIdTokenInAuthTokens)(tokens);\n return this.credsForOIDCTokens(authConfig.Cognito, tokens, identityId);\n }\n }\n async getGuestCredentials(identityId, authConfig) {\n // Return existing in-memory cached credentials only if it exists, is not past it's lifetime and is unauthenticated credentials\n if (this._credentialsAndIdentityId &&\n !this.isPastTTL() &&\n this._credentialsAndIdentityId.isAuthenticatedCreds === false) {\n logger.info('returning stored credentials as they neither past TTL nor expired.');\n return this._credentialsAndIdentityId;\n }\n // Clear to discard if any authenticated credentials are set and start with a clean slate\n this.clearCredentials();\n const region = (0, parsers_1.getRegionFromIdentityPoolId)(authConfig.identityPoolId);\n // use identityId to obtain guest credentials\n // save credentials in-memory\n // No logins params should be passed for guest creds:\n // https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_GetCredentialsForIdentity.html\n let clientResult;\n try {\n clientResult = await (0, core_1.getCredentialsForIdentity)({ region }, {\n IdentityId: identityId,\n });\n }\n catch (e) {\n (0, assertServiceError_1.assertServiceError)(e);\n throw new AuthError_1.AuthError(e);\n }\n if (clientResult?.Credentials?.AccessKeyId &&\n clientResult?.Credentials?.SecretKey) {\n this._nextCredentialsRefresh = new Date().getTime() + CREDENTIALS_TTL;\n const res = {\n credentials: {\n accessKeyId: clientResult.Credentials.AccessKeyId,\n secretAccessKey: clientResult.Credentials.SecretKey,\n sessionToken: clientResult.Credentials.SessionToken,\n expiration: clientResult.Credentials.Expiration,\n },\n identityId,\n };\n if (clientResult.IdentityId) {\n res.identityId = clientResult.IdentityId;\n this._identityIdStore.storeIdentityId({\n id: clientResult.IdentityId,\n type: 'guest',\n });\n }\n this._credentialsAndIdentityId = {\n ...res,\n isAuthenticatedCreds: false,\n };\n return res;\n }\n else {\n throw new AuthError_1.AuthError({\n name: 'CredentialsNotFoundException',\n message: `Cognito did not respond with either Credentials, AccessKeyId or SecretKey.`,\n });\n }\n }\n async credsForOIDCTokens(authConfig, authTokens, identityId) {\n if (this._credentialsAndIdentityId &&\n !this.isPastTTL() &&\n this._credentialsAndIdentityId.isAuthenticatedCreds === true) {\n logger.debug('returning stored credentials as they neither past TTL nor expired.');\n return this._credentialsAndIdentityId;\n }\n // Clear to discard if any unauthenticated credentials are set and start with a clean slate\n this.clearCredentials();\n const logins = authTokens.idToken\n ? (0, utils_2.formLoginsMap)(authTokens.idToken.toString())\n : {};\n const region = (0, parsers_1.getRegionFromIdentityPoolId)(authConfig.identityPoolId);\n let clientResult;\n try {\n clientResult = await (0, core_1.getCredentialsForIdentity)({ region }, {\n IdentityId: identityId,\n Logins: logins,\n });\n }\n catch (e) {\n (0, assertServiceError_1.assertServiceError)(e);\n throw new AuthError_1.AuthError(e);\n }\n if (clientResult?.Credentials?.AccessKeyId &&\n clientResult?.Credentials?.SecretKey) {\n this._nextCredentialsRefresh = new Date().getTime() + CREDENTIALS_TTL;\n const res = {\n credentials: {\n accessKeyId: clientResult.Credentials.AccessKeyId,\n secretAccessKey: clientResult.Credentials.SecretKey,\n sessionToken: clientResult.Credentials.SessionToken,\n expiration: clientResult.Credentials.Expiration,\n },\n identityId,\n };\n if (clientResult.IdentityId) {\n res.identityId = clientResult.IdentityId;\n // note: the following call removes guest identityId from the persistent store (localStorage)\n this._identityIdStore.storeIdentityId({\n id: clientResult.IdentityId,\n type: 'primary',\n });\n }\n // Store the credentials in-memory along with the expiration\n this._credentialsAndIdentityId = {\n ...res,\n isAuthenticatedCreds: true,\n associatedIdToken: authTokens.idToken?.toString(),\n };\n return res;\n }\n else {\n throw new AuthError_1.AuthError({\n name: 'CredentialsException',\n message: `Cognito did not respond with either Credentials, AccessKeyId or SecretKey.`,\n });\n }\n }\n isPastTTL() {\n return this._nextCredentialsRefresh === undefined\n ? true\n : this._nextCredentialsRefresh <= Date.now();\n }\n hasTokenChanged(tokens) {\n return (!!tokens &&\n !!this._credentialsAndIdentityId?.associatedIdToken &&\n tokens.idToken?.toString() !==\n this._credentialsAndIdentityId.associatedIdToken);\n }\n}\nexports.CognitoAWSCredentialsAndIdentityIdProvider = CognitoAWSCredentialsAndIdentityIdProvider;\n"],"names":[],"mappings":";;AACA;AACA;AACA,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;AAC7D,OAAO,CAAC,0CAA0C,GAAG,MAAM;AAC3D,MAAM,MAAM,GAAG,OAAO,CAAC,mBAAmB,CAAC;AAC3C,MAAM,OAAO,GAAG,OAAO,CAAC,mCAAmC,CAAC;AAC5D,MAAM,WAAW,GAAG,OAAO,CAAC,2BAA2B,CAAC;AACxD,MAAM,oBAAoB,GAAG,OAAO,CAAC,0CAA0C,CAAC;AAChF,MAAM,SAAS,GAAG,OAAO,CAAC,6BAA6B,CAAC;AACxD,MAAM,OAAO,GAAG,OAAO,CAAC,gBAAgB,CAAC;AACzC,MAAM,oBAAoB,GAAG,OAAO,CAAC,sBAAsB,CAAC;AAC5D,MAAM,OAAO,GAAG,OAAO,CAAC,SAAS,CAAC;AAClC,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,aAAa,CAAC,4BAA4B,CAAC;AACrE,MAAM,eAAe,GAAG,EAA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6,4 @@ import { HttpResponse } from '@aws-amplify/core/src/clients/types';
6
6
  *
7
7
  * @internal
8
8
  */
9
- export declare const cognitoUserPoolTransferHandler: (request: HttpRequest, options: Record<string, unknown> & import("@aws-amplify/core/internals/aws-client-utils").UserAgentOptions & import("@aws-amplify/core/internals/aws-client-utils").RetryOptions<import("@aws-amplify/core/internals/aws-client-utils").HttpResponse> & import("@aws-amplify/core/internals/aws-client-utils").HttpTransferOptions) => Promise<HttpResponse>;
9
+ export declare const cognitoUserPoolTransferHandler: (request: HttpRequest, options: Record<string, unknown> & import("@aws-amplify/core/internals/aws-client-utils").UserAgentOptions & object & import("@aws-amplify/core/internals/aws-client-utils").RetryOptions<import("@aws-amplify/core/internals/aws-client-utils").HttpResponse> & import("@aws-amplify/core/internals/aws-client-utils").AmzSdkRequestHeaderMiddlewareOptions & import("@aws-amplify/core/internals/aws-client-utils").HttpTransferOptions) => Promise<HttpResponse>;
@@ -8,7 +8,11 @@ const createUserPoolDeserializer = () => async (response) => {
8
8
  if (response.statusCode >= 300) {
9
9
  const error = await parseJsonError(response);
10
10
  assertServiceError(error);
11
- throw new AuthError({ name: error.name, message: error.message });
11
+ throw new AuthError({
12
+ name: error.name,
13
+ message: error.message,
14
+ metadata: error.$metadata,
15
+ });
12
16
  }
13
17
  return parseJsonBody(response);
14
18
  };
@@ -1 +1 @@
1
- {"version":3,"file":"createUserPoolDeserializer.mjs","sources":["../../../../../../../../src/foundation/factories/serviceClients/cognitoIdentityProvider/shared/serde/createUserPoolDeserializer.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { parseJsonBody, parseJsonError, } from '@aws-amplify/core/internals/aws-client-utils';\nimport { assertServiceError } from '../../../../../../errors/utils/assertServiceError';\nimport { AuthError } from '../../../../../../errors/AuthError';\nexport const createUserPoolDeserializer = () => async (response) => {\n if (response.statusCode >= 300) {\n const error = await parseJsonError(response);\n assertServiceError(error);\n throw new AuthError({ name: error.name, message: error.message });\n }\n return parseJsonBody(response);\n};\n"],"names":[],"mappings":";;;;AAAA;AACA;AAIY,MAAC,0BAA0B,GAAG,MAAM,OAAO,QAAQ,KAAK;AACpE,IAAI,IAAI,QAAQ,CAAC,UAAU,IAAI,GAAG,EAAE;AACpC,QAAQ,MAAM,KAAK,GAAG,MAAM,cAAc,CAAC,QAAQ,CAAC;AACpD,QAAQ,kBAAkB,CAAC,KAAK,CAAC;AACjC,QAAQ,MAAM,IAAI,SAAS,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,CAAC;AACzE;AACA,IAAI,OAAO,aAAa,CAAC,QAAQ,CAAC;AAClC;;;;"}
1
+ {"version":3,"file":"createUserPoolDeserializer.mjs","sources":["../../../../../../../../src/foundation/factories/serviceClients/cognitoIdentityProvider/shared/serde/createUserPoolDeserializer.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { parseJsonBody, parseJsonError, } from '@aws-amplify/core/internals/aws-client-utils';\nimport { assertServiceError } from '../../../../../../errors/utils/assertServiceError';\nimport { AuthError } from '../../../../../../errors/AuthError';\nexport const createUserPoolDeserializer = () => async (response) => {\n if (response.statusCode >= 300) {\n const error = await parseJsonError(response);\n assertServiceError(error);\n throw new AuthError({\n name: error.name,\n message: error.message,\n metadata: error.$metadata,\n });\n }\n return parseJsonBody(response);\n};\n"],"names":[],"mappings":";;;;AAAA;AACA;AAIY,MAAC,0BAA0B,GAAG,MAAM,OAAO,QAAQ,KAAK;AACpE,IAAI,IAAI,QAAQ,CAAC,UAAU,IAAI,GAAG,EAAE;AACpC,QAAQ,MAAM,KAAK,GAAG,MAAM,cAAc,CAAC,QAAQ,CAAC;AACpD,QAAQ,kBAAkB,CAAC,KAAK,CAAC;AACjC,QAAQ,MAAM,IAAI,SAAS,CAAC;AAC5B,YAAY,IAAI,EAAE,KAAK,CAAC,IAAI;AAC5B,YAAY,OAAO,EAAE,KAAK,CAAC,OAAO;AAClC,YAAY,QAAQ,EAAE,KAAK,CAAC,SAAS;AACrC,SAAS,CAAC;AACV;AACA,IAAI,OAAO,aAAa,CAAC,QAAQ,CAAC;AAClC;;;;"}
@@ -1,5 +1,6 @@
1
1
  import { getId } from '@aws-amplify/core';
2
2
  import { AuthError } from '../../../errors/AuthError.mjs';
3
+ import { assertServiceError } from '../../../errors/utils/assertServiceError.mjs';
3
4
  import { getRegionFromIdentityPoolId } from '../../../foundation/parsers/regionParsers.mjs';
4
5
  import { formLoginsMap } from './utils.mjs';
5
6
 
@@ -36,15 +37,21 @@ async function generateIdentityId(logins, authConfig) {
36
37
  const identityPoolId = authConfig?.identityPoolId;
37
38
  const region = getRegionFromIdentityPoolId(identityPoolId);
38
39
  // IdentityId is absent so get it using IdentityPoolId with Cognito's GetId API
39
- const idResult =
40
+ let idResult;
40
41
  // for a first-time user, this will return a brand new identity
41
42
  // for a returning user, this will retrieve the previous identity assocaited with the logins
42
- (await getId({
43
- region,
44
- }, {
45
- IdentityPoolId: identityPoolId,
46
- Logins: logins,
47
- })).IdentityId;
43
+ try {
44
+ idResult = (await getId({
45
+ region,
46
+ }, {
47
+ IdentityPoolId: identityPoolId,
48
+ Logins: logins,
49
+ })).IdentityId;
50
+ }
51
+ catch (e) {
52
+ assertServiceError(e);
53
+ throw new AuthError(e);
54
+ }
48
55
  if (!idResult) {
49
56
  throw new AuthError({
50
57
  name: 'GetIdResponseException',
@@ -1 +1 @@
1
- {"version":3,"file":"IdentityIdProvider.mjs","sources":["../../../../../src/providers/cognito/credentialsProvider/IdentityIdProvider.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { getId } from '@aws-amplify/core';\nimport { AuthError } from '../../../errors/AuthError';\nimport { getRegionFromIdentityPoolId } from '../../../foundation/parsers';\nimport { formLoginsMap } from './utils';\n/**\n * Provides a Cognito identityId\n *\n * @param tokens - The AuthTokens received after SignIn\n * @returns string\n * @throws configuration exceptions: `InvalidIdentityPoolIdException`\n * - Auth errors that may arise from misconfiguration.\n * @throws service exceptions: {@link GetIdException }\n */\nexport async function cognitoIdentityIdProvider({ tokens, authConfig, identityIdStore, }) {\n identityIdStore.setAuthConfig({ Cognito: authConfig });\n // will return null only if there is no identityId cached or if there is an error retrieving it\n const identityId = await identityIdStore.loadIdentityId();\n if (identityId) {\n return identityId.id;\n }\n const logins = tokens?.idToken\n ? formLoginsMap(tokens.idToken.toString())\n : {};\n const generatedIdentityId = await generateIdentityId(logins, authConfig);\n // Store generated identityId\n identityIdStore.storeIdentityId({\n id: generatedIdentityId,\n type: tokens ? 'primary' : 'guest',\n });\n return generatedIdentityId;\n}\nasync function generateIdentityId(logins, authConfig) {\n const identityPoolId = authConfig?.identityPoolId;\n const region = getRegionFromIdentityPoolId(identityPoolId);\n // IdentityId is absent so get it using IdentityPoolId with Cognito's GetId API\n const idResult = \n // for a first-time user, this will return a brand new identity\n // for a returning user, this will retrieve the previous identity assocaited with the logins\n (await getId({\n region,\n }, {\n IdentityPoolId: identityPoolId,\n Logins: logins,\n })).IdentityId;\n if (!idResult) {\n throw new AuthError({\n name: 'GetIdResponseException',\n message: 'Received undefined response from getId operation',\n recoverySuggestion: 'Make sure to pass a valid identityPoolId in the configuration.',\n });\n }\n return idResult;\n}\n"],"names":[],"mappings":";;;;;AAAA;AACA;AAKA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,eAAe,yBAAyB,CAAC,EAAE,MAAM,EAAE,UAAU,EAAE,eAAe,GAAG,EAAE;AAC1F,IAAI,eAAe,CAAC,aAAa,CAAC,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC;AAC1D;AACA,IAAI,MAAM,UAAU,GAAG,MAAM,eAAe,CAAC,cAAc,EAAE;AAC7D,IAAI,IAAI,UAAU,EAAE;AACpB,QAAQ,OAAO,UAAU,CAAC,EAAE;AAC5B;AACA,IAAI,MAAM,MAAM,GAAG,MAAM,EAAE;AAC3B,UAAU,aAAa,CAAC,MAAM,CAAC,OAAO,CAAC,QAAQ,EAAE;AACjD,UAAU,EAAE;AACZ,IAAI,MAAM,mBAAmB,GAAG,MAAM,kBAAkB,CAAC,MAAM,EAAE,UAAU,CAAC;AAC5E;AACA,IAAI,eAAe,CAAC,eAAe,CAAC;AACpC,QAAQ,EAAE,EAAE,mBAAmB;AAC/B,QAAQ,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO;AAC1C,KAAK,CAAC;AACN,IAAI,OAAO,mBAAmB;AAC9B;AACA,eAAe,kBAAkB,CAAC,MAAM,EAAE,UAAU,EAAE;AACtD,IAAI,MAAM,cAAc,GAAG,UAAU,EAAE,cAAc;AACrD,IAAI,MAAM,MAAM,GAAG,2BAA2B,CAAC,cAAc,CAAC;AAC9D;AACA,IAAI,MAAM,QAAQ;AAClB;AACA;AACA,IAAI,CAAC,MAAM,KAAK,CAAC;AACjB,QAAQ,MAAM;AACd,KAAK,EAAE;AACP,QAAQ,cAAc,EAAE,cAAc;AACtC,QAAQ,MAAM,EAAE,MAAM;AACtB,KAAK,CAAC,EAAE,UAAU;AAClB,IAAI,IAAI,CAAC,QAAQ,EAAE;AACnB,QAAQ,MAAM,IAAI,SAAS,CAAC;AAC5B,YAAY,IAAI,EAAE,wBAAwB;AAC1C,YAAY,OAAO,EAAE,kDAAkD;AACvE,YAAY,kBAAkB,EAAE,gEAAgE;AAChG,SAAS,CAAC;AACV;AACA,IAAI,OAAO,QAAQ;AACnB;;;;"}
1
+ {"version":3,"file":"IdentityIdProvider.mjs","sources":["../../../../../src/providers/cognito/credentialsProvider/IdentityIdProvider.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { getId } from '@aws-amplify/core';\nimport { AuthError } from '../../../errors/AuthError';\nimport { assertServiceError } from '../../../errors/utils/assertServiceError';\nimport { getRegionFromIdentityPoolId } from '../../../foundation/parsers';\nimport { formLoginsMap } from './utils';\n/**\n * Provides a Cognito identityId\n *\n * @param tokens - The AuthTokens received after SignIn\n * @returns string\n * @throws configuration exceptions: `InvalidIdentityPoolIdException`\n * - Auth errors that may arise from misconfiguration.\n * @throws service exceptions: {@link GetIdException }\n */\nexport async function cognitoIdentityIdProvider({ tokens, authConfig, identityIdStore, }) {\n identityIdStore.setAuthConfig({ Cognito: authConfig });\n // will return null only if there is no identityId cached or if there is an error retrieving it\n const identityId = await identityIdStore.loadIdentityId();\n if (identityId) {\n return identityId.id;\n }\n const logins = tokens?.idToken\n ? formLoginsMap(tokens.idToken.toString())\n : {};\n const generatedIdentityId = await generateIdentityId(logins, authConfig);\n // Store generated identityId\n identityIdStore.storeIdentityId({\n id: generatedIdentityId,\n type: tokens ? 'primary' : 'guest',\n });\n return generatedIdentityId;\n}\nasync function generateIdentityId(logins, authConfig) {\n const identityPoolId = authConfig?.identityPoolId;\n const region = getRegionFromIdentityPoolId(identityPoolId);\n // IdentityId is absent so get it using IdentityPoolId with Cognito's GetId API\n let idResult;\n // for a first-time user, this will return a brand new identity\n // for a returning user, this will retrieve the previous identity assocaited with the logins\n try {\n idResult = (await getId({\n region,\n }, {\n IdentityPoolId: identityPoolId,\n Logins: logins,\n })).IdentityId;\n }\n catch (e) {\n assertServiceError(e);\n throw new AuthError(e);\n }\n if (!idResult) {\n throw new AuthError({\n name: 'GetIdResponseException',\n message: 'Received undefined response from getId operation',\n recoverySuggestion: 'Make sure to pass a valid identityPoolId in the configuration.',\n });\n }\n return idResult;\n}\n"],"names":[],"mappings":";;;;;;AAAA;AACA;AAMA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,eAAe,yBAAyB,CAAC,EAAE,MAAM,EAAE,UAAU,EAAE,eAAe,GAAG,EAAE;AAC1F,IAAI,eAAe,CAAC,aAAa,CAAC,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC;AAC1D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;AACV;AACA,IAAI,OAAO,QAAQ;AACnB;;;;"}
@@ -1,6 +1,7 @@
1
1
  import { ConsoleLogger, getCredentialsForIdentity } from '@aws-amplify/core';
2
2
  import { assertIdentityPoolIdConfig } from '@aws-amplify/core/internals/utils';
3
3
  import { AuthError } from '../../../errors/AuthError.mjs';
4
+ import { assertServiceError } from '../../../errors/utils/assertServiceError.mjs';
4
5
  import { getRegionFromIdentityPoolId } from '../../../foundation/parsers/regionParsers.mjs';
5
6
  import { assertIdTokenInAuthTokens } from '../utils/types.mjs';
6
7
  import { cognitoIdentityIdProvider } from './IdentityIdProvider.mjs';
@@ -73,12 +74,18 @@ class CognitoAWSCredentialsAndIdentityIdProvider {
73
74
  // save credentials in-memory
74
75
  // No logins params should be passed for guest creds:
75
76
  // https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_GetCredentialsForIdentity.html
76
- const clientResult = await getCredentialsForIdentity({ region }, {
77
- IdentityId: identityId,
78
- });
79
- if (clientResult.Credentials &&
80
- clientResult.Credentials.AccessKeyId &&
81
- clientResult.Credentials.SecretKey) {
77
+ let clientResult;
78
+ try {
79
+ clientResult = await getCredentialsForIdentity({ region }, {
80
+ IdentityId: identityId,
81
+ });
82
+ }
83
+ catch (e) {
84
+ assertServiceError(e);
85
+ throw new AuthError(e);
86
+ }
87
+ if (clientResult?.Credentials?.AccessKeyId &&
88
+ clientResult?.Credentials?.SecretKey) {
82
89
  this._nextCredentialsRefresh = new Date().getTime() + CREDENTIALS_TTL;
83
90
  const res = {
84
91
  credentials: {
@@ -122,13 +129,19 @@ class CognitoAWSCredentialsAndIdentityIdProvider {
122
129
  ? formLoginsMap(authTokens.idToken.toString())
123
130
  : {};
124
131
  const region = getRegionFromIdentityPoolId(authConfig.identityPoolId);
125
- const clientResult = await getCredentialsForIdentity({ region }, {
126
- IdentityId: identityId,
127
- Logins: logins,
128
- });
129
- if (clientResult.Credentials &&
130
- clientResult.Credentials.AccessKeyId &&
131
- clientResult.Credentials.SecretKey) {
132
+ let clientResult;
133
+ try {
134
+ clientResult = await getCredentialsForIdentity({ region }, {
135
+ IdentityId: identityId,
136
+ Logins: logins,
137
+ });
138
+ }
139
+ catch (e) {
140
+ assertServiceError(e);
141
+ throw new AuthError(e);
142
+ }
143
+ if (clientResult?.Credentials?.AccessKeyId &&
144
+ clientResult?.Credentials?.SecretKey) {
132
145
  this._nextCredentialsRefresh = new Date().getTime() + CREDENTIALS_TTL;
133
146
  const res = {
134
147
  credentials: {
@@ -1 +1 @@
1
- {"version":3,"file":"credentialsProvider.mjs","sources":["../../../../../src/providers/cognito/credentialsProvider/credentialsProvider.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { ConsoleLogger, getCredentialsForIdentity, } from '@aws-amplify/core';\nimport { assertIdentityPoolIdConfig, } from '@aws-amplify/core/internals/utils';\nimport { AuthError } from '../../../errors/AuthError';\nimport { getRegionFromIdentityPoolId } from '../../../foundation/parsers';\nimport { assertIdTokenInAuthTokens } from '../utils/types';\nimport { cognitoIdentityIdProvider } from './IdentityIdProvider';\nimport { formLoginsMap } from './utils';\nconst logger = new ConsoleLogger('CognitoCredentialsProvider');\nconst CREDENTIALS_TTL = 50 * 60 * 1000; // 50 min, can be modified on config if required in the future\nexport class CognitoAWSCredentialsAndIdentityIdProvider {\n constructor(identityIdStore) {\n this._nextCredentialsRefresh = 0;\n this._identityIdStore = identityIdStore;\n }\n async clearCredentialsAndIdentityId() {\n logger.debug('Clearing out credentials and identityId');\n this._credentialsAndIdentityId = undefined;\n await this._identityIdStore.clearIdentityId();\n }\n async clearCredentials() {\n logger.debug('Clearing out in-memory credentials');\n this._credentialsAndIdentityId = undefined;\n }\n async getCredentialsAndIdentityId(getCredentialsOptions) {\n const isAuthenticated = getCredentialsOptions.authenticated;\n const { tokens } = getCredentialsOptions;\n const { authConfig } = getCredentialsOptions;\n try {\n assertIdentityPoolIdConfig(authConfig?.Cognito);\n }\n catch {\n // No identity pool configured, skipping\n return;\n }\n if (!isAuthenticated && !authConfig.Cognito.allowGuestAccess) {\n // TODO(V6): return partial result like Native platforms\n return;\n }\n const { forceRefresh } = getCredentialsOptions;\n const tokenHasChanged = this.hasTokenChanged(tokens);\n const identityId = await cognitoIdentityIdProvider({\n tokens,\n authConfig: authConfig.Cognito,\n identityIdStore: this._identityIdStore,\n });\n // Clear cached credentials when forceRefresh is true OR the cache token has changed\n if (forceRefresh || tokenHasChanged) {\n this.clearCredentials();\n }\n if (!isAuthenticated) {\n return this.getGuestCredentials(identityId, authConfig.Cognito);\n }\n else {\n assertIdTokenInAuthTokens(tokens);\n return this.credsForOIDCTokens(authConfig.Cognito, tokens, identityId);\n }\n }\n async getGuestCredentials(identityId, authConfig) {\n // Return existing in-memory cached credentials only if it exists, is not past it's lifetime and is unauthenticated credentials\n if (this._credentialsAndIdentityId &&\n !this.isPastTTL() &&\n this._credentialsAndIdentityId.isAuthenticatedCreds === false) {\n logger.info('returning stored credentials as they neither past TTL nor expired.');\n return this._credentialsAndIdentityId;\n }\n // Clear to discard if any authenticated credentials are set and start with a clean slate\n this.clearCredentials();\n const region = getRegionFromIdentityPoolId(authConfig.identityPoolId);\n // use identityId to obtain guest credentials\n // save credentials in-memory\n // No logins params should be passed for guest creds:\n // https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_GetCredentialsForIdentity.html\n const clientResult = await getCredentialsForIdentity({ region }, {\n IdentityId: identityId,\n });\n if (clientResult.Credentials &&\n clientResult.Credentials.AccessKeyId &&\n clientResult.Credentials.SecretKey) {\n this._nextCredentialsRefresh = new Date().getTime() + CREDENTIALS_TTL;\n const res = {\n credentials: {\n accessKeyId: clientResult.Credentials.AccessKeyId,\n secretAccessKey: clientResult.Credentials.SecretKey,\n sessionToken: clientResult.Credentials.SessionToken,\n expiration: clientResult.Credentials.Expiration,\n },\n identityId,\n };\n if (clientResult.IdentityId) {\n res.identityId = clientResult.IdentityId;\n this._identityIdStore.storeIdentityId({\n id: clientResult.IdentityId,\n type: 'guest',\n });\n }\n this._credentialsAndIdentityId = {\n ...res,\n isAuthenticatedCreds: false,\n };\n return res;\n }\n else {\n throw new AuthError({\n name: 'CredentialsNotFoundException',\n message: `Cognito did not respond with either Credentials, AccessKeyId or SecretKey.`,\n });\n }\n }\n async credsForOIDCTokens(authConfig, authTokens, identityId) {\n if (this._credentialsAndIdentityId &&\n !this.isPastTTL() &&\n this._credentialsAndIdentityId.isAuthenticatedCreds === true) {\n logger.debug('returning stored credentials as they neither past TTL nor expired.');\n return this._credentialsAndIdentityId;\n }\n // Clear to discard if any unauthenticated credentials are set and start with a clean slate\n this.clearCredentials();\n const logins = authTokens.idToken\n ? formLoginsMap(authTokens.idToken.toString())\n : {};\n const region = getRegionFromIdentityPoolId(authConfig.identityPoolId);\n const clientResult = await getCredentialsForIdentity({ region }, {\n IdentityId: identityId,\n Logins: logins,\n });\n if (clientResult.Credentials &&\n clientResult.Credentials.AccessKeyId &&\n clientResult.Credentials.SecretKey) {\n this._nextCredentialsRefresh = new Date().getTime() + CREDENTIALS_TTL;\n const res = {\n credentials: {\n accessKeyId: clientResult.Credentials.AccessKeyId,\n secretAccessKey: clientResult.Credentials.SecretKey,\n sessionToken: clientResult.Credentials.SessionToken,\n expiration: clientResult.Credentials.Expiration,\n },\n identityId,\n };\n if (clientResult.IdentityId) {\n res.identityId = clientResult.IdentityId;\n // note: the following call removes guest identityId from the persistent store (localStorage)\n this._identityIdStore.storeIdentityId({\n id: clientResult.IdentityId,\n type: 'primary',\n });\n }\n // Store the credentials in-memory along with the expiration\n this._credentialsAndIdentityId = {\n ...res,\n isAuthenticatedCreds: true,\n associatedIdToken: authTokens.idToken?.toString(),\n };\n return res;\n }\n else {\n throw new AuthError({\n name: 'CredentialsException',\n message: `Cognito did not respond with either Credentials, AccessKeyId or SecretKey.`,\n });\n }\n }\n isPastTTL() {\n return this._nextCredentialsRefresh === undefined\n ? true\n : this._nextCredentialsRefresh <= Date.now();\n }\n hasTokenChanged(tokens) {\n return (!!tokens &&\n !!this._credentialsAndIdentityId?.associatedIdToken &&\n tokens.idToken?.toString() !==\n this._credentialsAndIdentityId.associatedIdToken);\n }\n}\n"],"names":[],"mappings":";;;;;;;;AAAA;AACA;AAQA,MAAM,MAAM,GAAG,IAAI,aAAa,CAAC,4BAA4B,CAAC;AAC9D,MAAM,eAAe,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAChC,MAAM,0CAA0C,CAAC;AACxD,IAAI,WAAW,CAAC,eAAe,EAAE;AACjC,QAAQ,IAAI,CAAC,uBAAuB,GAAG,CAAC;AACxC,QAAQ,IAAI,CAAC,gBAAgB,GAAG,eAAe;AAC/C;AACA,IAAI,MAAM,6BAA6B,GAAG;AAC1C,QAAQ,MAAM,CAAC,KAAK,CAAC,yCAAyC,CAAC;AAC/D,QAAQ,IAAI,CAAC,yBAAyB,GAAG,SAAS;AAClD,QAAQ,MAAM,IAAI,CAAC,gBAAgB,CAAC,eAAe,EAAE;AACrD;AACA,IAAI,MAAM,gBAAgB,GAAG;AAC7B,QAAQ,MAAM,CAAC,KAAK,CAAC,oCAAoC,CAAC;AAC1D,QAAQ,IAAI,CAAC,yBAAyB,GAAG,SAAS;AAClD;AACA,IAAI,MAAM,2BAA2B,CAAC,qBAAqB,EAAE;AAC7D,QAAQ,MAAM,eAAe,GAAG,qBAAqB,CAAC,aAAa;AACnE,QAAQ,MAAM,EAAE,MAAM,EAAE,GAAG,qBAAqB;AAChD,QAAQ,MAAM,EAAE,UAAU,EAAE,GAAG,qBAAqB;AACpD,QAAQ,IAAI;AACZ,YAAY,0BAA0B,CAAC,UAAU,EAAE,OAAO,CAAC;AAC3D;AACA,QAAQ,MAAM;AACd;AACA,YAAY;AACZ;AACA,QAAQ,IAAI,CAAC,eAAe,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,gBAAgB,EAAE;AACtE;AACA,YAAY;AACZ;AACA,QAAQ,MAAM,EAAE,YAAY,EAAE,GAAG,qBA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,oBAAoB,EAAE,EAAE,YAAY,CAAC,UAAU;AAC/C,oBAAoB,IAAI,EAAE,SAAS;AACnC,iBAAiB,CAAC;AAClB;AACA;AACA,YAAY,IAAI,CAAC,yBAAyB,GAAG;AAC7C,gBAAgB,GAAG,GAAG;AACtB,gBAAgB,oBAAoB,EAAE,IAAI;AAC1C,gBAAgB,iBAAiB,EAAE,UAAU,CAAC,OAAO,EAAE,QAAQ,EAAE;AACjE,aAAa;AACb,YAAY,OAAO,GAAG;AACtB;AACA,aAAa;AACb,YAAY,MAAM,IAAI,SAAS,CAAC;AAChC,gBAAgB,IAAI,EAAE,sBAAsB;AAC5C,gBAAgB,OAAO,EAAE,CAAC,0EAA0E,CAAC;AACrG,aAAa,CAAC;AACd;AACA;AACA,IAAI,SAAS,GAAG;AAChB,QAAQ,OAAO,IAAI,CAAC,uBAAuB,KAAK;AAChD,cAAc;AACd,cAAc,IAAI,CAAC,uBAAuB,IAAI,IAAI,CAAC,GAAG,EAAE;AACxD;AACA,IAAI,eAAe,CAAC,MAAM,EAAE;AAC5B,QAAQ,QAAQ,CAAC,CAAC,MAAM;AACxB,YAAY,CAAC,CAAC,IAAI,CAAC,yBAAyB,EAAE,iBAAiB;AAC/D,YAAY,MAAM,CAAC,OAAO,EAAE,QAAQ,EAAE;AACtC,gBAAgB,IAAI,CAAC,yBAAyB,CAAC,iBAAiB;AAChE;AACA;;;;"}
1
+ {"version":3,"file":"credentialsProvider.mjs","sources":["../../../../../src/providers/cognito/credentialsProvider/credentialsProvider.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { ConsoleLogger, getCredentialsForIdentity, } from '@aws-amplify/core';\nimport { assertIdentityPoolIdConfig, } from '@aws-amplify/core/internals/utils';\nimport { AuthError } from '../../../errors/AuthError';\nimport { assertServiceError } from '../../../errors/utils/assertServiceError';\nimport { getRegionFromIdentityPoolId } from '../../../foundation/parsers';\nimport { assertIdTokenInAuthTokens } from '../utils/types';\nimport { cognitoIdentityIdProvider } from './IdentityIdProvider';\nimport { formLoginsMap } from './utils';\nconst logger = new ConsoleLogger('CognitoCredentialsProvider');\nconst CREDENTIALS_TTL = 50 * 60 * 1000; // 50 min, can be modified on config if required in the future\nexport class CognitoAWSCredentialsAndIdentityIdProvider {\n constructor(identityIdStore) {\n this._nextCredentialsRefresh = 0;\n this._identityIdStore = identityIdStore;\n }\n async clearCredentialsAndIdentityId() {\n logger.debug('Clearing out credentials and identityId');\n this._credentialsAndIdentityId = undefined;\n await this._identityIdStore.clearIdentityId();\n }\n async clearCredentials() {\n logger.debug('Clearing out in-memory credentials');\n this._credentialsAndIdentityId = undefined;\n }\n async getCredentialsAndIdentityId(getCredentialsOptions) {\n const isAuthenticated = getCredentialsOptions.authenticated;\n const { tokens } = getCredentialsOptions;\n const { authConfig } = getCredentialsOptions;\n try {\n assertIdentityPoolIdConfig(authConfig?.Cognito);\n }\n catch {\n // No identity pool configured, skipping\n return;\n }\n if (!isAuthenticated && !authConfig.Cognito.allowGuestAccess) {\n // TODO(V6): return partial result like Native platforms\n return;\n }\n const { forceRefresh } = getCredentialsOptions;\n const tokenHasChanged = this.hasTokenChanged(tokens);\n const identityId = await cognitoIdentityIdProvider({\n tokens,\n authConfig: authConfig.Cognito,\n identityIdStore: this._identityIdStore,\n });\n // Clear cached credentials when forceRefresh is true OR the cache token has changed\n if (forceRefresh || tokenHasChanged) {\n this.clearCredentials();\n }\n if (!isAuthenticated) {\n return this.getGuestCredentials(identityId, authConfig.Cognito);\n }\n else {\n assertIdTokenInAuthTokens(tokens);\n return this.credsForOIDCTokens(authConfig.Cognito, tokens, identityId);\n }\n }\n async getGuestCredentials(identityId, authConfig) {\n // Return existing in-memory cached credentials only if it exists, is not past it's lifetime and is unauthenticated credentials\n if (this._credentialsAndIdentityId &&\n !this.isPastTTL() &&\n this._credentialsAndIdentityId.isAuthenticatedCreds === false) {\n logger.info('returning stored credentials as they neither past TTL nor expired.');\n return this._credentialsAndIdentityId;\n }\n // Clear to discard if any authenticated credentials are set and start with a clean slate\n this.clearCredentials();\n const region = getRegionFromIdentityPoolId(authConfig.identityPoolId);\n // use identityId to obtain guest credentials\n // save credentials in-memory\n // No logins params should be passed for guest creds:\n // https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_GetCredentialsForIdentity.html\n let clientResult;\n try {\n clientResult = await getCredentialsForIdentity({ region }, {\n IdentityId: identityId,\n });\n }\n catch (e) {\n assertServiceError(e);\n throw new AuthError(e);\n }\n if (clientResult?.Credentials?.AccessKeyId &&\n clientResult?.Credentials?.SecretKey) {\n this._nextCredentialsRefresh = new Date().getTime() + CREDENTIALS_TTL;\n const res = {\n credentials: {\n accessKeyId: clientResult.Credentials.AccessKeyId,\n secretAccessKey: clientResult.Credentials.SecretKey,\n sessionToken: clientResult.Credentials.SessionToken,\n expiration: clientResult.Credentials.Expiration,\n },\n identityId,\n };\n if (clientResult.IdentityId) {\n res.identityId = clientResult.IdentityId;\n this._identityIdStore.storeIdentityId({\n id: clientResult.IdentityId,\n type: 'guest',\n });\n }\n this._credentialsAndIdentityId = {\n ...res,\n isAuthenticatedCreds: false,\n };\n return res;\n }\n else {\n throw new AuthError({\n name: 'CredentialsNotFoundException',\n message: `Cognito did not respond with either Credentials, AccessKeyId or SecretKey.`,\n });\n }\n }\n async credsForOIDCTokens(authConfig, authTokens, identityId) {\n if (this._credentialsAndIdentityId &&\n !this.isPastTTL() &&\n this._credentialsAndIdentityId.isAuthenticatedCreds === true) {\n logger.debug('returning stored credentials as they neither past TTL nor expired.');\n return this._credentialsAndIdentityId;\n }\n // Clear to discard if any unauthenticated credentials are set and start with a clean slate\n this.clearCredentials();\n const logins = authTokens.idToken\n ? formLoginsMap(authTokens.idToken.toString())\n : {};\n const region = getRegionFromIdentityPoolId(authConfig.identityPoolId);\n let clientResult;\n try {\n clientResult = await getCredentialsForIdentity({ region }, {\n IdentityId: identityId,\n Logins: logins,\n });\n }\n catch (e) {\n assertServiceError(e);\n throw new AuthError(e);\n }\n if (clientResult?.Credentials?.AccessKeyId &&\n clientResult?.Credentials?.SecretKey) {\n this._nextCredentialsRefresh = new Date().getTime() + CREDENTIALS_TTL;\n const res = {\n credentials: {\n accessKeyId: clientResult.Credentials.AccessKeyId,\n secretAccessKey: clientResult.Credentials.SecretKey,\n sessionToken: clientResult.Credentials.SessionToken,\n expiration: clientResult.Credentials.Expiration,\n },\n identityId,\n };\n if (clientResult.IdentityId) {\n res.identityId = clientResult.IdentityId;\n // note: the following call removes guest identityId from the persistent store (localStorage)\n this._identityIdStore.storeIdentityId({\n id: clientResult.IdentityId,\n type: 'primary',\n });\n }\n // Store the credentials in-memory along with the expiration\n this._credentialsAndIdentityId = {\n ...res,\n isAuthenticatedCreds: true,\n associatedIdToken: authTokens.idToken?.toString(),\n };\n return res;\n }\n else {\n throw new AuthError({\n name: 'CredentialsException',\n message: `Cognito did not respond with either Credentials, AccessKeyId or SecretKey.`,\n });\n }\n }\n isPastTTL() {\n return this._nextCredentialsRefresh === undefined\n ? true\n : this._nextCredentialsRefresh <= Date.now();\n }\n hasTokenChanged(tokens) {\n return (!!tokens &&\n !!this._credentialsAndIdentityId?.associatedIdToken &&\n tokens.idToken?.toString() !==\n this._credentialsAndIdentityId.associatedIdToken);\n }\n}\n"],"names":[],"mappings":";;;;;;;;;AAAA;AACA;AASA,MAAM,MAAM,GAAG,IAAI,aAAa,CAAC,4BAA4B,CAAC;AAC9D,MAAM,eAAe,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAChC,MAAM,0CAA0C,CAAC;AACxD,IAAI,WAAW,CAAC,eAAe,EAAE;AACjC,QAAQ,IAAI,CAAC,uBAAuB,GAAG,CAAC;AACxC,QAAQ,IAAI,CAAC,gBAAgB,GAAG,eAAe;AAC/C;AACA,IAAI,MAAM,6BAA6B,GAAG;AAC1C,QAAQ,MAAM,CAAC,KAAK,CAAC,yCAAyC,CAAC;AAC/D,QAAQ,IAAI,CAAC,yB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AE;AACjE,aAAa;AACb,YAAY,OAAO,GAAG;AACtB;AACA,aAAa;AACb,YAAY,MAAM,IAAI,SAAS,CAAC;AAChC,gBAAgB,IAAI,EAAE,sBAAsB;AAC5C,gBAAgB,OAAO,EAAE,CAAC,0EAA0E,CAAC;AACrG,aAAa,CAAC;AACd;AACA;AACA,IAAI,SAAS,GAAG;AAChB,QAAQ,OAAO,IAAI,CAAC,uBAAuB,KAAK;AAChD,cAAc;AACd,cAAc,IAAI,CAAC,uBAAuB,IAAI,IAAI,CAAC,GAAG,EAAE;AACxD;AACA,IAAI,eAAe,CAAC,MAAM,EAAE;AAC5B,QAAQ,QAAQ,CAAC,CAAC,MAAM;AACxB,YAAY,CAAC,CAAC,IAAI,CAAC,yBAAyB,EAAE,iBAAiB;AAC/D,YAAY,MAAM,CAAC,OAAO,EAAE,QAAQ,EAAE;AACtC,gBAAgB,IAAI,CAAC,yBAAyB,CAAC,iBAAiB;AAChE;AACA;;;;"}
@@ -5,9 +5,7 @@ export type TokenRefresher = ({ tokens, authConfig, username, }: {
5
5
  authConfig?: AuthConfig;
6
6
  username: string;
7
7
  }) => Promise<CognitoAuthTokens>;
8
- export type AuthKeys<AuthKey extends string> = {
9
- [Key in AuthKey]: string;
10
- };
8
+ export type AuthKeys<AuthKey extends string> = Record<AuthKey, string>;
11
9
  export declare const AuthTokenStorageKeys: {
12
10
  accessToken: string;
13
11
  idToken: string;
@@ -16,9 +16,7 @@ export declare enum AuthErrorTypes {
16
16
  AutoSignInError = "autoSignInError",
17
17
  OAuthSignInError = "oauthSignInError"
18
18
  }
19
- export type AuthErrorMessages = {
20
- [key in AuthErrorTypes]: AuthErrorMessage;
21
- };
19
+ export type AuthErrorMessages = Record<AuthErrorTypes, AuthErrorMessage>;
22
20
  export interface AuthErrorMessage {
23
21
  message: string;
24
22
  log?: string;
@@ -201,9 +201,7 @@ export type AuthNextSignInStep<UserAttributeKey extends AuthUserAttributeKey = A
201
201
  /**
202
202
  * Key/value pairs describing a user attributes.
203
203
  */
204
- export type AuthUserAttributes<UserAttributeKey extends AuthUserAttributeKey = AuthUserAttributeKey> = {
205
- [Attribute in UserAttributeKey]?: string;
206
- };
204
+ export type AuthUserAttributes<UserAttributeKey extends AuthUserAttributeKey = AuthUserAttributeKey> = Partial<Record<UserAttributeKey, string>>;
207
205
  /**
208
206
  * The interface of a user attribute.
209
207
  */
@@ -16,6 +16,4 @@ export interface AuthUpdateUserAttributeOutput<UserAttributeKey extends AuthUser
16
16
  isUpdated: boolean;
17
17
  nextStep: AuthNextUpdateAttributeStep<UserAttributeKey>;
18
18
  }
19
- export type AuthUpdateUserAttributesOutput<UserAttributeKey extends AuthUserAttributeKey = AuthUserAttributeKey> = {
20
- [authKey in UserAttributeKey]: AuthUpdateUserAttributeOutput<UserAttributeKey>;
21
- };
19
+ export type AuthUpdateUserAttributesOutput<UserAttributeKey extends AuthUserAttributeKey = AuthUserAttributeKey> = Record<UserAttributeKey, AuthUpdateUserAttributeOutput<UserAttributeKey>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-amplify/auth",
3
- "version": "6.11.6-unstable.cde36a7.0+cde36a7",
3
+ "version": "6.11.7-unstable.b188564.0+b188564",
4
4
  "description": "Auth category of aws-amplify",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.mjs",
@@ -96,13 +96,13 @@
96
96
  "tslib": "^2.5.0"
97
97
  },
98
98
  "peerDependencies": {
99
- "@aws-amplify/core": "6.10.6-unstable.cde36a7.0+cde36a7"
99
+ "@aws-amplify/core": "6.10.7-unstable.b188564.0+b188564"
100
100
  },
101
101
  "devDependencies": {
102
- "@aws-amplify/core": "6.10.6-unstable.cde36a7.0+cde36a7",
103
- "@aws-amplify/react-native": "1.1.8-unstable.cde36a7.0+cde36a7",
102
+ "@aws-amplify/core": "6.10.7-unstable.b188564.0+b188564",
103
+ "@aws-amplify/react-native": "1.1.8-unstable.b188564.0+b188564",
104
104
  "@jest/test-sequencer": "^29.7.0",
105
105
  "typescript": "5.0.2"
106
106
  },
107
- "gitHead": "cde36a7b5ca5254a6821db46832a80c9e0ea54fd"
107
+ "gitHead": "b1885648589e0549c0e7c257e8be5fb4936d5bde"
108
108
  }
@@ -16,7 +16,11 @@ export const createUserPoolDeserializer =
16
16
  if (response.statusCode >= 300) {
17
17
  const error = await parseJsonError(response);
18
18
  assertServiceError(error);
19
- throw new AuthError({ name: error.name, message: error.message });
19
+ throw new AuthError({
20
+ name: error.name,
21
+ message: error.message,
22
+ metadata: error.$metadata,
23
+ });
20
24
  }
21
25
 
22
26
  return parseJsonBody(response);
@@ -5,6 +5,7 @@ import { AuthTokens, Identity, getId } from '@aws-amplify/core';
5
5
  import { CognitoIdentityPoolConfig } from '@aws-amplify/core/internals/utils';
6
6
 
7
7
  import { AuthError } from '../../../errors/AuthError';
8
+ import { assertServiceError } from '../../../errors/utils/assertServiceError';
8
9
  import { getRegionFromIdentityPoolId } from '../../../foundation/parsers';
9
10
  import { GetIdException } from '../types/errors';
10
11
 
@@ -58,10 +59,11 @@ async function generateIdentityId(
58
59
  const region = getRegionFromIdentityPoolId(identityPoolId);
59
60
 
60
61
  // IdentityId is absent so get it using IdentityPoolId with Cognito's GetId API
61
- const idResult =
62
- // for a first-time user, this will return a brand new identity
63
- // for a returning user, this will retrieve the previous identity assocaited with the logins
64
- (
62
+ let idResult: string | undefined;
63
+ // for a first-time user, this will return a brand new identity
64
+ // for a returning user, this will retrieve the previous identity assocaited with the logins
65
+ try {
66
+ idResult = (
65
67
  await getId(
66
68
  {
67
69
  region,
@@ -72,6 +74,10 @@ async function generateIdentityId(
72
74
  },
73
75
  )
74
76
  ).IdentityId;
77
+ } catch (e) {
78
+ assertServiceError(e);
79
+ throw new AuthError(e);
80
+ }
75
81
  if (!idResult) {
76
82
  throw new AuthError({
77
83
  name: 'GetIdResponseException',
@@ -15,6 +15,7 @@ import {
15
15
  } from '@aws-amplify/core/internals/utils';
16
16
 
17
17
  import { AuthError } from '../../../errors/AuthError';
18
+ import { assertServiceError } from '../../../errors/utils/assertServiceError';
18
19
  import { getRegionFromIdentityPoolId } from '../../../foundation/parsers';
19
20
  import { assertIdTokenInAuthTokens } from '../utils/types';
20
21
 
@@ -116,17 +117,24 @@ export class CognitoAWSCredentialsAndIdentityIdProvider
116
117
  // save credentials in-memory
117
118
  // No logins params should be passed for guest creds:
118
119
  // https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_GetCredentialsForIdentity.html
119
- const clientResult = await getCredentialsForIdentity(
120
- { region },
121
- {
122
- IdentityId: identityId,
123
- },
124
- );
120
+ let clientResult:
121
+ | Awaited<ReturnType<typeof getCredentialsForIdentity>>
122
+ | undefined;
123
+ try {
124
+ clientResult = await getCredentialsForIdentity(
125
+ { region },
126
+ {
127
+ IdentityId: identityId,
128
+ },
129
+ );
130
+ } catch (e) {
131
+ assertServiceError(e);
132
+ throw new AuthError(e);
133
+ }
125
134
 
126
135
  if (
127
- clientResult.Credentials &&
128
- clientResult.Credentials.AccessKeyId &&
129
- clientResult.Credentials.SecretKey
136
+ clientResult?.Credentials?.AccessKeyId &&
137
+ clientResult?.Credentials?.SecretKey
130
138
  ) {
131
139
  this._nextCredentialsRefresh = new Date().getTime() + CREDENTIALS_TTL;
132
140
 
@@ -186,18 +194,25 @@ export class CognitoAWSCredentialsAndIdentityIdProvider
186
194
 
187
195
  const region = getRegionFromIdentityPoolId(authConfig.identityPoolId);
188
196
 
189
- const clientResult = await getCredentialsForIdentity(
190
- { region },
191
- {
192
- IdentityId: identityId,
193
- Logins: logins,
194
- },
195
- );
197
+ let clientResult:
198
+ | Awaited<ReturnType<typeof getCredentialsForIdentity>>
199
+ | undefined;
200
+ try {
201
+ clientResult = await getCredentialsForIdentity(
202
+ { region },
203
+ {
204
+ IdentityId: identityId,
205
+ Logins: logins,
206
+ },
207
+ );
208
+ } catch (e) {
209
+ assertServiceError(e);
210
+ throw new AuthError(e);
211
+ }
196
212
 
197
213
  if (
198
- clientResult.Credentials &&
199
- clientResult.Credentials.AccessKeyId &&
200
- clientResult.Credentials.SecretKey
214
+ clientResult?.Credentials?.AccessKeyId &&
215
+ clientResult?.Credentials?.SecretKey
201
216
  ) {
202
217
  this._nextCredentialsRefresh = new Date().getTime() + CREDENTIALS_TTL;
203
218
 
@@ -20,9 +20,7 @@ export type TokenRefresher = ({
20
20
  username: string;
21
21
  }) => Promise<CognitoAuthTokens>;
22
22
 
23
- export type AuthKeys<AuthKey extends string> = {
24
- [Key in AuthKey]: string;
25
- };
23
+ export type AuthKeys<AuthKey extends string> = Record<AuthKey, string>;
26
24
 
27
25
  export const AuthTokenStorageKeys = {
28
26
  accessToken: 'accessToken',
package/src/types/Auth.ts CHANGED
@@ -20,7 +20,7 @@ export enum AuthErrorTypes {
20
20
  OAuthSignInError = 'oauthSignInError',
21
21
  }
22
22
 
23
- export type AuthErrorMessages = { [key in AuthErrorTypes]: AuthErrorMessage };
23
+ export type AuthErrorMessages = Record<AuthErrorTypes, AuthErrorMessage>;
24
24
 
25
25
  export interface AuthErrorMessage {
26
26
  message: string;
@@ -251,9 +251,7 @@ export type AuthNextSignInStep<
251
251
  */
252
252
  export type AuthUserAttributes<
253
253
  UserAttributeKey extends AuthUserAttributeKey = AuthUserAttributeKey,
254
- > = {
255
- [Attribute in UserAttributeKey]?: string;
256
- };
254
+ > = Partial<Record<UserAttributeKey, string>>;
257
255
 
258
256
  /**
259
257
  * The interface of a user attribute.
@@ -40,6 +40,4 @@ export interface AuthUpdateUserAttributeOutput<
40
40
 
41
41
  export type AuthUpdateUserAttributesOutput<
42
42
  UserAttributeKey extends AuthUserAttributeKey = AuthUserAttributeKey,
43
- > = {
44
- [authKey in UserAttributeKey]: AuthUpdateUserAttributeOutput<UserAttributeKey>;
45
- };
43
+ > = Record<UserAttributeKey, AuthUpdateUserAttributeOutput<UserAttributeKey>>;