@aws-amplify/adapter-nextjs 1.2.1-unstable.1a1a6ce.0 → 1.2.1
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/dist/cjs/utils/createRunWithAmplifyServerContext.js +5 -1
- package/dist/cjs/utils/createRunWithAmplifyServerContext.js.map +1 -1
- package/dist/cjs/utils/createTokenValidator.js +34 -0
- package/dist/cjs/utils/createTokenValidator.js.map +1 -0
- package/dist/cjs/utils/index.js +3 -1
- package/dist/cjs/utils/index.js.map +1 -1
- package/dist/cjs/utils/isValidCognitoToken.js +35 -0
- package/dist/cjs/utils/isValidCognitoToken.js.map +1 -0
- package/dist/esm/api/createServerRunnerForAPI.mjs +1 -0
- package/dist/esm/api/createServerRunnerForAPI.mjs.map +1 -1
- package/dist/esm/createServerRunner.mjs +1 -0
- package/dist/esm/createServerRunner.mjs.map +1 -1
- package/dist/esm/utils/createRunWithAmplifyServerContext.mjs +5 -1
- package/dist/esm/utils/createRunWithAmplifyServerContext.mjs.map +1 -1
- package/dist/esm/utils/createTokenValidator.d.ts +10 -0
- package/dist/esm/utils/createTokenValidator.mjs +32 -0
- package/dist/esm/utils/createTokenValidator.mjs.map +1 -0
- package/dist/esm/utils/index.d.ts +1 -0
- package/dist/esm/utils/index.mjs +1 -0
- package/dist/esm/utils/index.mjs.map +1 -1
- package/dist/esm/utils/isValidCognitoToken.d.ts +15 -0
- package/dist/esm/utils/isValidCognitoToken.mjs +33 -0
- package/dist/esm/utils/isValidCognitoToken.mjs.map +1 -0
- package/package.json +73 -72
- package/src/utils/createRunWithAmplifyServerContext.ts +6 -0
- package/src/utils/createTokenValidator.ts +39 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/isValidCognitoToken.ts +37 -0
|
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.createRunWithAmplifyServerContext = void 0;
|
|
7
7
|
const core_1 = require("@aws-amplify/core");
|
|
8
8
|
const adapter_core_1 = require("aws-amplify/adapter-core");
|
|
9
|
+
const createTokenValidator_1 = require("./createTokenValidator");
|
|
9
10
|
const createCookieStorageAdapterFromNextServerContext_1 = require("./createCookieStorageAdapterFromNextServerContext");
|
|
10
11
|
const createRunWithAmplifyServerContext = ({ config: resourcesConfig, }) => {
|
|
11
12
|
const runWithAmplifyServerContext = async ({ nextServerContext, operation }) => {
|
|
@@ -19,7 +20,10 @@ const createRunWithAmplifyServerContext = ({ config: resourcesConfig, }) => {
|
|
|
19
20
|
// static rendering uses the same unauthenticated role cross-sever.
|
|
20
21
|
nextServerContext === null
|
|
21
22
|
? core_1.sharedInMemoryStorage
|
|
22
|
-
: (0, adapter_core_1.createKeyValueStorageFromCookieStorageAdapter)((0, createCookieStorageAdapterFromNextServerContext_1.createCookieStorageAdapterFromNextServerContext)(nextServerContext))
|
|
23
|
+
: (0, adapter_core_1.createKeyValueStorageFromCookieStorageAdapter)((0, createCookieStorageAdapterFromNextServerContext_1.createCookieStorageAdapterFromNextServerContext)(nextServerContext), (0, createTokenValidator_1.createTokenValidator)({
|
|
24
|
+
userPoolId: resourcesConfig?.Auth.Cognito?.userPoolId,
|
|
25
|
+
userPoolClientId: resourcesConfig?.Auth.Cognito?.userPoolClientId,
|
|
26
|
+
}));
|
|
23
27
|
const credentialsProvider = (0, adapter_core_1.createAWSCredentialsAndIdentityIdProvider)(resourcesConfig.Auth, keyValueStorage);
|
|
24
28
|
const tokenProvider = (0, adapter_core_1.createUserPoolsTokenProvider)(resourcesConfig.Auth, keyValueStorage);
|
|
25
29
|
return (0, adapter_core_1.runWithAmplifyServerContext)(resourcesConfig, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createRunWithAmplifyServerContext.js","sources":["../../../src/utils/createRunWithAmplifyServerContext.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.createRunWithAmplifyServerContext = void 0;\nconst core_1 = require(\"@aws-amplify/core\");\nconst adapter_core_1 = require(\"aws-amplify/adapter-core\");\nconst createCookieStorageAdapterFromNextServerContext_1 = require(\"./createCookieStorageAdapterFromNextServerContext\");\nconst createRunWithAmplifyServerContext = ({ config: resourcesConfig, }) => {\n const runWithAmplifyServerContext = async ({ nextServerContext, operation }) => {\n // When the Auth config is presented, attempt to create a Amplify server\n // context with token and credentials provider.\n if (resourcesConfig.Auth) {\n const keyValueStorage = \n // When `null` is passed as the value of `nextServerContext`, opt-in\n // unauthenticated role (primarily for static rendering). It's\n // safe to use the singleton `MemoryKeyValueStorage` here, as the\n // static rendering uses the same unauthenticated role cross-sever.\n nextServerContext === null\n ? core_1.sharedInMemoryStorage\n : (0, adapter_core_1.createKeyValueStorageFromCookieStorageAdapter)((0, createCookieStorageAdapterFromNextServerContext_1.createCookieStorageAdapterFromNextServerContext)(nextServerContext));\n const credentialsProvider = (0, adapter_core_1.createAWSCredentialsAndIdentityIdProvider)(resourcesConfig.Auth, keyValueStorage);\n const tokenProvider = (0, adapter_core_1.createUserPoolsTokenProvider)(resourcesConfig.Auth, keyValueStorage);\n return (0, adapter_core_1.runWithAmplifyServerContext)(resourcesConfig, {\n Auth: { credentialsProvider, tokenProvider },\n }, operation);\n }\n // Otherwise it may be the case that auth is not used, e.g. API key.\n // Omitting the `Auth` in the second parameter.\n return (0, adapter_core_1.runWithAmplifyServerContext)(resourcesConfig, {}, operation);\n };\n return runWithAmplifyServerContext;\n};\nexports.createRunWithAmplifyServerContext = createRunWithAmplifyServerContext;\n"],"names":[],"mappings":";;AACA;AACA;AACA,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;AAC9D,OAAO,CAAC,iCAAiC,GAAG,KAAK,CAAC,CAAC;AACnD,MAAM,MAAM,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAAC;AAC5C,MAAM,cAAc,GAAG,OAAO,CAAC,0BAA0B,CAAC,CAAC;AAC3D,MAAM,iDAAiD,GAAG,OAAO,CAAC,mDAAmD,CAAC,CAAC;AACvH,MAAM,iCAAiC,GAAG,CAAC,EAAE,MAAM,EAAE,eAAe,GAAG,KAAK;AAC5E,IAAI,MAAM,2BAA2B,GAAG,OAAO,EAAE,iBAAiB,EAAE,SAAS,EAAE,KAAK;AACpF;AACA;AACA,QAAQ,IAAI,eAAe,CAAC,IAAI,EAAE;AAClC,YAAY,MAAM,eAAe;AACjC;AACA;AACA;AACA;AACA,YAAY,iBAAiB,KAAK,IAAI;AACtC,kBAAkB,MAAM,CAAC,qBAAqB;AAC9C,kBAAkB,IAAI,cAAc,CAAC,6CAA6C,EAAE,IAAI,iDAAiD,CAAC,+CAA+C,EAAE,iBAAiB,CAAC,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"createRunWithAmplifyServerContext.js","sources":["../../../src/utils/createRunWithAmplifyServerContext.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.createRunWithAmplifyServerContext = void 0;\nconst core_1 = require(\"@aws-amplify/core\");\nconst adapter_core_1 = require(\"aws-amplify/adapter-core\");\nconst createTokenValidator_1 = require(\"./createTokenValidator\");\nconst createCookieStorageAdapterFromNextServerContext_1 = require(\"./createCookieStorageAdapterFromNextServerContext\");\nconst createRunWithAmplifyServerContext = ({ config: resourcesConfig, }) => {\n const runWithAmplifyServerContext = async ({ nextServerContext, operation }) => {\n // When the Auth config is presented, attempt to create a Amplify server\n // context with token and credentials provider.\n if (resourcesConfig.Auth) {\n const keyValueStorage = \n // When `null` is passed as the value of `nextServerContext`, opt-in\n // unauthenticated role (primarily for static rendering). It's\n // safe to use the singleton `MemoryKeyValueStorage` here, as the\n // static rendering uses the same unauthenticated role cross-sever.\n nextServerContext === null\n ? core_1.sharedInMemoryStorage\n : (0, adapter_core_1.createKeyValueStorageFromCookieStorageAdapter)((0, createCookieStorageAdapterFromNextServerContext_1.createCookieStorageAdapterFromNextServerContext)(nextServerContext), (0, createTokenValidator_1.createTokenValidator)({\n userPoolId: resourcesConfig?.Auth.Cognito?.userPoolId,\n userPoolClientId: resourcesConfig?.Auth.Cognito?.userPoolClientId,\n }));\n const credentialsProvider = (0, adapter_core_1.createAWSCredentialsAndIdentityIdProvider)(resourcesConfig.Auth, keyValueStorage);\n const tokenProvider = (0, adapter_core_1.createUserPoolsTokenProvider)(resourcesConfig.Auth, keyValueStorage);\n return (0, adapter_core_1.runWithAmplifyServerContext)(resourcesConfig, {\n Auth: { credentialsProvider, tokenProvider },\n }, operation);\n }\n // Otherwise it may be the case that auth is not used, e.g. API key.\n // Omitting the `Auth` in the second parameter.\n return (0, adapter_core_1.runWithAmplifyServerContext)(resourcesConfig, {}, operation);\n };\n return runWithAmplifyServerContext;\n};\nexports.createRunWithAmplifyServerContext = createRunWithAmplifyServerContext;\n"],"names":[],"mappings":";;AACA;AACA;AACA,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;AAC9D,OAAO,CAAC,iCAAiC,GAAG,KAAK,CAAC,CAAC;AACnD,MAAM,MAAM,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAAC;AAC5C,MAAM,cAAc,GAAG,OAAO,CAAC,0BAA0B,CAAC,CAAC;AAC3D,MAAM,sBAAsB,GAAG,OAAO,CAAC,wBAAwB,CAAC,CAAC;AACjE,MAAM,iDAAiD,GAAG,OAAO,CAAC,mDAAmD,CAAC,CAAC;AACvH,MAAM,iCAAiC,GAAG,CAAC,EAAE,MAAM,EAAE,eAAe,GAAG,KAAK;AAC5E,IAAI,MAAM,2BAA2B,GAAG,OAAO,EAAE,iBAAiB,EAAE,SAAS,EAAE,KAAK;AACpF;AACA;AACA,QAAQ,IAAI,eAAe,CAAC,IAAI,EAAE;AAClC,YAAY,MAAM,eAAe;AACjC;AACA;AACA;AACA;AACA,YAAY,iBAAiB,KAAK,IAAI;AACtC,kBAAkB,MAAM,CAAC,qBAAqB;AAC9C,kBAAkB,IAAI,cAAc,CAAC,6CAA6C,EAAE,IAAI,iDAAiD,CAAC,+CAA+C,EAAE,iBAAiB,CAAC,EAAE,IAAI,sBAAsB,CAAC,oBAAoB,EAAE;AAChQ,oBAAoB,UAAU,EAAE,eAAe,EAAE,IAAI,CAAC,OAAO,EAAE,UAAU;AACzE,oBAAoB,gBAAgB,EAAE,eAAe,EAAE,IAAI,CAAC,OAAO,EAAE,gBAAgB;AACrF,iBAAiB,CAAC,CAAC,CAAC;AACpB,YAAY,MAAM,mBAAmB,GAAG,IAAI,cAAc,CAAC,yCAAyC,EAAE,eAAe,CAAC,IAAI,EAAE,eAAe,CAAC,CAAC;AAC7I,YAAY,MAAM,aAAa,GAAG,IAAI,cAAc,CAAC,4BAA4B,EAAE,eAAe,CAAC,IAAI,EAAE,eAAe,CAAC,CAAC;AAC1H,YAAY,OAAO,IAAI,cAAc,CAAC,2BAA2B,EAAE,eAAe,EAAE;AACpF,gBAAgB,IAAI,EAAE,EAAE,mBAAmB,EAAE,aAAa,EAAE;AAC5D,aAAa,EAAE,SAAS,CAAC,CAAC;AAC1B,SAAS;AACT;AACA;AACA,QAAQ,OAAO,IAAI,cAAc,CAAC,2BAA2B,EAAE,eAAe,EAAE,EAAE,EAAE,SAAS,CAAC,CAAC;AAC/F,KAAK,CAAC;AACN,IAAI,OAAO,2BAA2B,CAAC;AACvC,CAAC,CAAC;AACF,OAAO,CAAC,iCAAiC,GAAG,iCAAiC;;"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
4
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createTokenValidator = void 0;
|
|
7
|
+
const isValidCognitoToken_1 = require("./isValidCognitoToken");
|
|
8
|
+
/**
|
|
9
|
+
* Creates a validator object for validating methods in a KeyValueStorage.
|
|
10
|
+
*/
|
|
11
|
+
const createTokenValidator = ({ userPoolId, userPoolClientId: clientId, }) => {
|
|
12
|
+
return {
|
|
13
|
+
// validate access, id tokens
|
|
14
|
+
getItem: async (key, value) => {
|
|
15
|
+
const tokenType = key.includes('.accessToken')
|
|
16
|
+
? 'access'
|
|
17
|
+
: key.includes('.idToken')
|
|
18
|
+
? 'id'
|
|
19
|
+
: null;
|
|
20
|
+
if (!tokenType)
|
|
21
|
+
return true;
|
|
22
|
+
if (!userPoolId || !clientId)
|
|
23
|
+
return false;
|
|
24
|
+
return (0, isValidCognitoToken_1.isValidCognitoToken)({
|
|
25
|
+
clientId,
|
|
26
|
+
userPoolId,
|
|
27
|
+
tokenType,
|
|
28
|
+
token: value,
|
|
29
|
+
});
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
exports.createTokenValidator = createTokenValidator;
|
|
34
|
+
//# sourceMappingURL=createTokenValidator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createTokenValidator.js","sources":["../../../src/utils/createTokenValidator.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.createTokenValidator = void 0;\nconst isValidCognitoToken_1 = require(\"./isValidCognitoToken\");\n/**\n * Creates a validator object for validating methods in a KeyValueStorage.\n */\nconst createTokenValidator = ({ userPoolId, userPoolClientId: clientId, }) => {\n return {\n // validate access, id tokens\n getItem: async (key, value) => {\n const tokenType = key.includes('.accessToken')\n ? 'access'\n : key.includes('.idToken')\n ? 'id'\n : null;\n if (!tokenType)\n return true;\n if (!userPoolId || !clientId)\n return false;\n return (0, isValidCognitoToken_1.isValidCognitoToken)({\n clientId,\n userPoolId,\n tokenType,\n token: value,\n });\n },\n };\n};\nexports.createTokenValidator = createTokenValidator;\n"],"names":[],"mappings":";;AACA;AACA;AACA,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;AAC9D,OAAO,CAAC,oBAAoB,GAAG,KAAK,CAAC,CAAC;AACtC,MAAM,qBAAqB,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAAC;AAC/D;AACA;AACA;AACA,MAAM,oBAAoB,GAAG,CAAC,EAAE,UAAU,EAAE,gBAAgB,EAAE,QAAQ,GAAG,KAAK;AAC9E,IAAI,OAAO;AACX;AACA,QAAQ,OAAO,EAAE,OAAO,GAAG,EAAE,KAAK,KAAK;AACvC,YAAY,MAAM,SAAS,GAAG,GAAG,CAAC,QAAQ,CAAC,cAAc,CAAC;AAC1D,kBAAkB,QAAQ;AAC1B,kBAAkB,GAAG,CAAC,QAAQ,CAAC,UAAU,CAAC;AAC1C,sBAAsB,IAAI;AAC1B,sBAAsB,IAAI,CAAC;AAC3B,YAAY,IAAI,CAAC,SAAS;AAC1B,gBAAgB,OAAO,IAAI,CAAC;AAC5B,YAAY,IAAI,CAAC,UAAU,IAAI,CAAC,QAAQ;AACxC,gBAAgB,OAAO,KAAK,CAAC;AAC7B,YAAY,OAAO,IAAI,qBAAqB,CAAC,mBAAmB,EAAE;AAClE,gBAAgB,QAAQ;AACxB,gBAAgB,UAAU;AAC1B,gBAAgB,SAAS;AACzB,gBAAgB,KAAK,EAAE,KAAK;AAC5B,aAAa,CAAC,CAAC;AACf,SAAS;AACT,KAAK,CAAC;AACN,CAAC,CAAC;AACF,OAAO,CAAC,oBAAoB,GAAG,oBAAoB;;"}
|
package/dist/cjs/utils/index.js
CHANGED
|
@@ -3,7 +3,9 @@
|
|
|
3
3
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
4
4
|
// SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.createRunWithAmplifyServerContext = void 0;
|
|
6
|
+
exports.isValidCognitoToken = exports.createRunWithAmplifyServerContext = void 0;
|
|
7
7
|
var createRunWithAmplifyServerContext_1 = require("./createRunWithAmplifyServerContext");
|
|
8
8
|
Object.defineProperty(exports, "createRunWithAmplifyServerContext", { enumerable: true, get: function () { return createRunWithAmplifyServerContext_1.createRunWithAmplifyServerContext; } });
|
|
9
|
+
var isValidCognitoToken_1 = require("./isValidCognitoToken");
|
|
10
|
+
Object.defineProperty(exports, "isValidCognitoToken", { enumerable: true, get: function () { return isValidCognitoToken_1.isValidCognitoToken; } });
|
|
9
11
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../../src/utils/index.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.createRunWithAmplifyServerContext = void 0;\nvar createRunWithAmplifyServerContext_1 = require(\"./createRunWithAmplifyServerContext\");\nObject.defineProperty(exports, \"createRunWithAmplifyServerContext\", { enumerable: true, get: function () { return createRunWithAmplifyServerContext_1.createRunWithAmplifyServerContext; } });\n"],"names":[],"mappings":";;AACA;AACA;AACA,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;AAC9D,OAAO,CAAC,iCAAiC,GAAG,KAAK,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../src/utils/index.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.isValidCognitoToken = exports.createRunWithAmplifyServerContext = void 0;\nvar createRunWithAmplifyServerContext_1 = require(\"./createRunWithAmplifyServerContext\");\nObject.defineProperty(exports, \"createRunWithAmplifyServerContext\", { enumerable: true, get: function () { return createRunWithAmplifyServerContext_1.createRunWithAmplifyServerContext; } });\nvar isValidCognitoToken_1 = require(\"./isValidCognitoToken\");\nObject.defineProperty(exports, \"isValidCognitoToken\", { enumerable: true, get: function () { return isValidCognitoToken_1.isValidCognitoToken; } });\n"],"names":[],"mappings":";;AACA;AACA;AACA,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;AAC9D,OAAO,CAAC,mBAAmB,GAAG,OAAO,CAAC,iCAAiC,GAAG,KAAK,CAAC,CAAC;AACjF,IAAI,mCAAmC,GAAG,OAAO,CAAC,qCAAqC,CAAC,CAAC;AACzF,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,mCAAmC,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,GAAG,EAAE,YAAY,EAAE,OAAO,mCAAmC,CAAC,iCAAiC,CAAC,EAAE,EAAE,CAAC,CAAC;AAC9L,IAAI,qBAAqB,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAAC;AAC7D,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,qBAAqB,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,GAAG,EAAE,YAAY,EAAE,OAAO,qBAAqB,CAAC,mBAAmB,CAAC,EAAE,EAAE,CAAC;;"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
4
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.isValidCognitoToken = void 0;
|
|
7
|
+
const aws_jwt_verify_1 = require("aws-jwt-verify");
|
|
8
|
+
/**
|
|
9
|
+
* Verifies a Cognito JWT token for its validity.
|
|
10
|
+
*
|
|
11
|
+
* @param input - An object containing:
|
|
12
|
+
* - token: The JWT token as a string that needs to be verified.
|
|
13
|
+
* - userPoolId: The ID of the AWS Cognito User Pool to which the token belongs.
|
|
14
|
+
* - clientId: The Client ID associated with the Cognito User Pool.
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
const isValidCognitoToken = async (input) => {
|
|
18
|
+
const { userPoolId, clientId, tokenType, token } = input;
|
|
19
|
+
try {
|
|
20
|
+
const verifier = aws_jwt_verify_1.CognitoJwtVerifier.create({
|
|
21
|
+
userPoolId,
|
|
22
|
+
tokenUse: tokenType,
|
|
23
|
+
clientId,
|
|
24
|
+
});
|
|
25
|
+
await verifier.verify(token);
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
// TODO (ashwinkumar6): surface invalid cognito token error to customer
|
|
30
|
+
// TODO: clear invalid tokens from Storage
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
exports.isValidCognitoToken = isValidCognitoToken;
|
|
35
|
+
//# sourceMappingURL=isValidCognitoToken.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isValidCognitoToken.js","sources":["../../../src/utils/isValidCognitoToken.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.isValidCognitoToken = void 0;\nconst aws_jwt_verify_1 = require(\"aws-jwt-verify\");\n/**\n * Verifies a Cognito JWT token for its validity.\n *\n * @param input - An object containing:\n * - token: The JWT token as a string that needs to be verified.\n * - userPoolId: The ID of the AWS Cognito User Pool to which the token belongs.\n * - clientId: The Client ID associated with the Cognito User Pool.\n * @internal\n */\nconst isValidCognitoToken = async (input) => {\n const { userPoolId, clientId, tokenType, token } = input;\n try {\n const verifier = aws_jwt_verify_1.CognitoJwtVerifier.create({\n userPoolId,\n tokenUse: tokenType,\n clientId,\n });\n await verifier.verify(token);\n return true;\n }\n catch (error) {\n // TODO (ashwinkumar6): surface invalid cognito token error to customer\n // TODO: clear invalid tokens from Storage\n return false;\n }\n};\nexports.isValidCognitoToken = isValidCognitoToken;\n"],"names":[],"mappings":";;AACA;AACA;AACA,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;AAC9D,OAAO,CAAC,mBAAmB,GAAG,KAAK,CAAC,CAAC;AACrC,MAAM,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAC;AACnD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAM,mBAAmB,GAAG,OAAO,KAAK,KAAK;AAC7C,IAAI,MAAM,EAAE,UAAU,EAAE,QAAQ,EAAE,SAAS,EAAE,KAAK,EAAE,GAAG,KAAK,CAAC;AAC7D,IAAI,IAAI;AACR,QAAQ,MAAM,QAAQ,GAAG,gBAAgB,CAAC,kBAAkB,CAAC,MAAM,CAAC;AACpE,YAAY,UAAU;AACtB,YAAY,QAAQ,EAAE,SAAS;AAC/B,YAAY,QAAQ;AACpB,SAAS,CAAC,CAAC;AACX,QAAQ,MAAM,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AACrC,QAAQ,OAAO,IAAI,CAAC;AACpB,KAAK;AACL,IAAI,OAAO,KAAK,EAAE;AAClB;AACA;AACA,QAAQ,OAAO,KAAK,CAAC;AACrB,KAAK;AACL,CAAC,CAAC;AACF,OAAO,CAAC,mBAAmB,GAAG,mBAAmB;;"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { parseAmplifyConfig } from '@aws-amplify/core/internals/utils';
|
|
2
2
|
import { createRunWithAmplifyServerContext } from '../utils/createRunWithAmplifyServerContext.mjs';
|
|
3
|
+
import 'aws-jwt-verify';
|
|
3
4
|
|
|
4
5
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
5
6
|
// SPDX-License-Identifier: Apache-2.0
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createServerRunnerForAPI.mjs","sources":["../../../src/api/createServerRunnerForAPI.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { parseAmplifyConfig } from '@aws-amplify/core/internals/utils';\nimport { createRunWithAmplifyServerContext } from '../utils';\nexport const createServerRunnerForAPI = ({ config, }) => {\n const amplifyConfig = parseAmplifyConfig(config);\n return {\n runWithAmplifyServerContext: createRunWithAmplifyServerContext({\n config: amplifyConfig,\n }),\n resourcesConfig: amplifyConfig,\n };\n};\n"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"createServerRunnerForAPI.mjs","sources":["../../../src/api/createServerRunnerForAPI.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { parseAmplifyConfig } from '@aws-amplify/core/internals/utils';\nimport { createRunWithAmplifyServerContext } from '../utils';\nexport const createServerRunnerForAPI = ({ config, }) => {\n const amplifyConfig = parseAmplifyConfig(config);\n return {\n runWithAmplifyServerContext: createRunWithAmplifyServerContext({\n config: amplifyConfig,\n }),\n resourcesConfig: amplifyConfig,\n };\n};\n"],"names":[],"mappings":";;;;AAAA;AACA;AAGY,MAAC,wBAAwB,GAAG,CAAC,EAAE,MAAM,GAAG,KAAK;AACzD,IAAI,MAAM,aAAa,GAAG,kBAAkB,CAAC,MAAM,CAAC,CAAC;AACrD,IAAI,OAAO;AACX,QAAQ,2BAA2B,EAAE,iCAAiC,CAAC;AACvE,YAAY,MAAM,EAAE,aAAa;AACjC,SAAS,CAAC;AACV,QAAQ,eAAe,EAAE,aAAa;AACtC,KAAK,CAAC;AACN;;;;"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { parseAmplifyConfig } from '@aws-amplify/core/internals/utils';
|
|
2
2
|
import { createRunWithAmplifyServerContext } from './utils/createRunWithAmplifyServerContext.mjs';
|
|
3
|
+
import 'aws-jwt-verify';
|
|
3
4
|
|
|
4
5
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
5
6
|
// SPDX-License-Identifier: Apache-2.0
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createServerRunner.mjs","sources":["../../src/createServerRunner.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { parseAmplifyConfig } from '@aws-amplify/core/internals/utils';\nimport { createRunWithAmplifyServerContext } from './utils';\n/**\n * Creates the `runWithAmplifyServerContext` function to run Amplify server side APIs in an isolated request context.\n *\n * @remarks\n * This function should be called only once; you can use the returned `runWithAmplifyServerContext` across\n * your codebase.\n *\n * @param input The input used to create the `runWithAmplifyServerContext` function.\n * @param input.config The {@link ResourcesConfig} imported from the `amplifyconfiguration.json` file or manually\n * created.\n * @returns An object that contains the `runWithAmplifyServerContext` function.\n *\n * @example\n * import { createServerRunner } from '@aws-amplify/adapter-nextjs';\n * import config from './amplifyconfiguration.json';\n *\n * export const { runWithAmplifyServerContext } = createServerRunner({ config })\n */\nexport const createServerRunner = ({ config, }) => {\n const amplifyConfig = parseAmplifyConfig(config);\n return {\n runWithAmplifyServerContext: createRunWithAmplifyServerContext({\n config: amplifyConfig,\n }),\n };\n};\n"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"createServerRunner.mjs","sources":["../../src/createServerRunner.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { parseAmplifyConfig } from '@aws-amplify/core/internals/utils';\nimport { createRunWithAmplifyServerContext } from './utils';\n/**\n * Creates the `runWithAmplifyServerContext` function to run Amplify server side APIs in an isolated request context.\n *\n * @remarks\n * This function should be called only once; you can use the returned `runWithAmplifyServerContext` across\n * your codebase.\n *\n * @param input The input used to create the `runWithAmplifyServerContext` function.\n * @param input.config The {@link ResourcesConfig} imported from the `amplifyconfiguration.json` file or manually\n * created.\n * @returns An object that contains the `runWithAmplifyServerContext` function.\n *\n * @example\n * import { createServerRunner } from '@aws-amplify/adapter-nextjs';\n * import config from './amplifyconfiguration.json';\n *\n * export const { runWithAmplifyServerContext } = createServerRunner({ config })\n */\nexport const createServerRunner = ({ config, }) => {\n const amplifyConfig = parseAmplifyConfig(config);\n return {\n runWithAmplifyServerContext: createRunWithAmplifyServerContext({\n config: amplifyConfig,\n }),\n };\n};\n"],"names":[],"mappings":";;;;AAAA;AACA;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACY,MAAC,kBAAkB,GAAG,CAAC,EAAE,MAAM,GAAG,KAAK;AACnD,IAAI,MAAM,aAAa,GAAG,kBAAkB,CAAC,MAAM,CAAC,CAAC;AACrD,IAAI,OAAO;AACX,QAAQ,2BAA2B,EAAE,iCAAiC,CAAC;AACvE,YAAY,MAAM,EAAE,aAAa;AACjC,SAAS,CAAC;AACV,KAAK,CAAC;AACN;;;;"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { sharedInMemoryStorage } from '@aws-amplify/core';
|
|
2
2
|
import { createKeyValueStorageFromCookieStorageAdapter, createAWSCredentialsAndIdentityIdProvider, createUserPoolsTokenProvider, runWithAmplifyServerContext } from 'aws-amplify/adapter-core';
|
|
3
|
+
import { createTokenValidator } from './createTokenValidator.mjs';
|
|
3
4
|
import { createCookieStorageAdapterFromNextServerContext } from './createCookieStorageAdapterFromNextServerContext.mjs';
|
|
4
5
|
|
|
5
6
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
@@ -16,7 +17,10 @@ const createRunWithAmplifyServerContext = ({ config: resourcesConfig, }) => {
|
|
|
16
17
|
// static rendering uses the same unauthenticated role cross-sever.
|
|
17
18
|
nextServerContext === null
|
|
18
19
|
? sharedInMemoryStorage
|
|
19
|
-
: createKeyValueStorageFromCookieStorageAdapter(createCookieStorageAdapterFromNextServerContext(nextServerContext)
|
|
20
|
+
: createKeyValueStorageFromCookieStorageAdapter(createCookieStorageAdapterFromNextServerContext(nextServerContext), createTokenValidator({
|
|
21
|
+
userPoolId: resourcesConfig?.Auth.Cognito?.userPoolId,
|
|
22
|
+
userPoolClientId: resourcesConfig?.Auth.Cognito?.userPoolClientId,
|
|
23
|
+
}));
|
|
20
24
|
const credentialsProvider = createAWSCredentialsAndIdentityIdProvider(resourcesConfig.Auth, keyValueStorage);
|
|
21
25
|
const tokenProvider = createUserPoolsTokenProvider(resourcesConfig.Auth, keyValueStorage);
|
|
22
26
|
return runWithAmplifyServerContext(resourcesConfig, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createRunWithAmplifyServerContext.mjs","sources":["../../../src/utils/createRunWithAmplifyServerContext.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { sharedInMemoryStorage } from '@aws-amplify/core';\nimport { createAWSCredentialsAndIdentityIdProvider, createKeyValueStorageFromCookieStorageAdapter, createUserPoolsTokenProvider, runWithAmplifyServerContext as runWithAmplifyServerContextCore, } from 'aws-amplify/adapter-core';\nimport { createCookieStorageAdapterFromNextServerContext } from './createCookieStorageAdapterFromNextServerContext';\nexport const createRunWithAmplifyServerContext = ({ config: resourcesConfig, }) => {\n const runWithAmplifyServerContext = async ({ nextServerContext, operation }) => {\n // When the Auth config is presented, attempt to create a Amplify server\n // context with token and credentials provider.\n if (resourcesConfig.Auth) {\n const keyValueStorage = \n // When `null` is passed as the value of `nextServerContext`, opt-in\n // unauthenticated role (primarily for static rendering). It's\n // safe to use the singleton `MemoryKeyValueStorage` here, as the\n // static rendering uses the same unauthenticated role cross-sever.\n nextServerContext === null\n ? sharedInMemoryStorage\n : createKeyValueStorageFromCookieStorageAdapter(createCookieStorageAdapterFromNextServerContext(nextServerContext));\n const credentialsProvider = createAWSCredentialsAndIdentityIdProvider(resourcesConfig.Auth, keyValueStorage);\n const tokenProvider = createUserPoolsTokenProvider(resourcesConfig.Auth, keyValueStorage);\n return runWithAmplifyServerContextCore(resourcesConfig, {\n Auth: { credentialsProvider, tokenProvider },\n }, operation);\n }\n // Otherwise it may be the case that auth is not used, e.g. API key.\n // Omitting the `Auth` in the second parameter.\n return runWithAmplifyServerContextCore(resourcesConfig, {}, operation);\n };\n return runWithAmplifyServerContext;\n};\n"],"names":["runWithAmplifyServerContext","runWithAmplifyServerContextCore"],"mappings":"
|
|
1
|
+
{"version":3,"file":"createRunWithAmplifyServerContext.mjs","sources":["../../../src/utils/createRunWithAmplifyServerContext.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { sharedInMemoryStorage } from '@aws-amplify/core';\nimport { createAWSCredentialsAndIdentityIdProvider, createKeyValueStorageFromCookieStorageAdapter, createUserPoolsTokenProvider, runWithAmplifyServerContext as runWithAmplifyServerContextCore, } from 'aws-amplify/adapter-core';\nimport { createTokenValidator } from './createTokenValidator';\nimport { createCookieStorageAdapterFromNextServerContext } from './createCookieStorageAdapterFromNextServerContext';\nexport const createRunWithAmplifyServerContext = ({ config: resourcesConfig, }) => {\n const runWithAmplifyServerContext = async ({ nextServerContext, operation }) => {\n // When the Auth config is presented, attempt to create a Amplify server\n // context with token and credentials provider.\n if (resourcesConfig.Auth) {\n const keyValueStorage = \n // When `null` is passed as the value of `nextServerContext`, opt-in\n // unauthenticated role (primarily for static rendering). It's\n // safe to use the singleton `MemoryKeyValueStorage` here, as the\n // static rendering uses the same unauthenticated role cross-sever.\n nextServerContext === null\n ? sharedInMemoryStorage\n : createKeyValueStorageFromCookieStorageAdapter(createCookieStorageAdapterFromNextServerContext(nextServerContext), createTokenValidator({\n userPoolId: resourcesConfig?.Auth.Cognito?.userPoolId,\n userPoolClientId: resourcesConfig?.Auth.Cognito?.userPoolClientId,\n }));\n const credentialsProvider = createAWSCredentialsAndIdentityIdProvider(resourcesConfig.Auth, keyValueStorage);\n const tokenProvider = createUserPoolsTokenProvider(resourcesConfig.Auth, keyValueStorage);\n return runWithAmplifyServerContextCore(resourcesConfig, {\n Auth: { credentialsProvider, tokenProvider },\n }, operation);\n }\n // Otherwise it may be the case that auth is not used, e.g. API key.\n // Omitting the `Auth` in the second parameter.\n return runWithAmplifyServerContextCore(resourcesConfig, {}, operation);\n };\n return runWithAmplifyServerContext;\n};\n"],"names":["runWithAmplifyServerContext","runWithAmplifyServerContextCore"],"mappings":";;;;;AAAA;AACA;AAKY,MAAC,iCAAiC,GAAG,CAAC,EAAE,MAAM,EAAE,eAAe,GAAG,KAAK;AACnF,IAAI,MAAMA,6BAA2B,GAAG,OAAO,EAAE,iBAAiB,EAAE,SAAS,EAAE,KAAK;AACpF;AACA;AACA,QAAQ,IAAI,eAAe,CAAC,IAAI,EAAE;AAClC,YAAY,MAAM,eAAe;AACjC;AACA;AACA;AACA;AACA,YAAY,iBAAiB,KAAK,IAAI;AACtC,kBAAkB,qBAAqB;AACvC,kBAAkB,6CAA6C,CAAC,+CAA+C,CAAC,iBAAiB,CAAC,EAAE,oBAAoB,CAAC;AACzJ,oBAAoB,UAAU,EAAE,eAAe,EAAE,IAAI,CAAC,OAAO,EAAE,UAAU;AACzE,oBAAoB,gBAAgB,EAAE,eAAe,EAAE,IAAI,CAAC,OAAO,EAAE,gBAAgB;AACrF,iBAAiB,CAAC,CAAC,CAAC;AACpB,YAAY,MAAM,mBAAmB,GAAG,yCAAyC,CAAC,eAAe,CAAC,IAAI,EAAE,eAAe,CAAC,CAAC;AACzH,YAAY,MAAM,aAAa,GAAG,4BAA4B,CAAC,eAAe,CAAC,IAAI,EAAE,eAAe,CAAC,CAAC;AACtG,YAAY,OAAOC,2BAA+B,CAAC,eAAe,EAAE;AACpE,gBAAgB,IAAI,EAAE,EAAE,mBAAmB,EAAE,aAAa,EAAE;AAC5D,aAAa,EAAE,SAAS,CAAC,CAAC;AAC1B,SAAS;AACT;AACA;AACA,QAAQ,OAAOA,2BAA+B,CAAC,eAAe,EAAE,EAAE,EAAE,SAAS,CAAC,CAAC;AAC/E,KAAK,CAAC;AACN,IAAI,OAAOD,6BAA2B,CAAC;AACvC;;;;"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { KeyValueStorageMethodValidator } from '@aws-amplify/core/internals/adapter-core';
|
|
2
|
+
interface CreateTokenValidatorInput {
|
|
3
|
+
userPoolId?: string;
|
|
4
|
+
userPoolClientId?: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Creates a validator object for validating methods in a KeyValueStorage.
|
|
8
|
+
*/
|
|
9
|
+
export declare const createTokenValidator: ({ userPoolId, userPoolClientId: clientId, }: CreateTokenValidatorInput) => KeyValueStorageMethodValidator;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { isValidCognitoToken } from './isValidCognitoToken.mjs';
|
|
2
|
+
|
|
3
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
4
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
/**
|
|
6
|
+
* Creates a validator object for validating methods in a KeyValueStorage.
|
|
7
|
+
*/
|
|
8
|
+
const createTokenValidator = ({ userPoolId, userPoolClientId: clientId, }) => {
|
|
9
|
+
return {
|
|
10
|
+
// validate access, id tokens
|
|
11
|
+
getItem: async (key, value) => {
|
|
12
|
+
const tokenType = key.includes('.accessToken')
|
|
13
|
+
? 'access'
|
|
14
|
+
: key.includes('.idToken')
|
|
15
|
+
? 'id'
|
|
16
|
+
: null;
|
|
17
|
+
if (!tokenType)
|
|
18
|
+
return true;
|
|
19
|
+
if (!userPoolId || !clientId)
|
|
20
|
+
return false;
|
|
21
|
+
return isValidCognitoToken({
|
|
22
|
+
clientId,
|
|
23
|
+
userPoolId,
|
|
24
|
+
tokenType,
|
|
25
|
+
token: value,
|
|
26
|
+
});
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export { createTokenValidator };
|
|
32
|
+
//# sourceMappingURL=createTokenValidator.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createTokenValidator.mjs","sources":["../../../src/utils/createTokenValidator.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { isValidCognitoToken } from './isValidCognitoToken';\n/**\n * Creates a validator object for validating methods in a KeyValueStorage.\n */\nexport const createTokenValidator = ({ userPoolId, userPoolClientId: clientId, }) => {\n return {\n // validate access, id tokens\n getItem: async (key, value) => {\n const tokenType = key.includes('.accessToken')\n ? 'access'\n : key.includes('.idToken')\n ? 'id'\n : null;\n if (!tokenType)\n return true;\n if (!userPoolId || !clientId)\n return false;\n return isValidCognitoToken({\n clientId,\n userPoolId,\n tokenType,\n token: value,\n });\n },\n };\n};\n"],"names":[],"mappings":";;AAAA;AACA;AAEA;AACA;AACA;AACY,MAAC,oBAAoB,GAAG,CAAC,EAAE,UAAU,EAAE,gBAAgB,EAAE,QAAQ,GAAG,KAAK;AACrF,IAAI,OAAO;AACX;AACA,QAAQ,OAAO,EAAE,OAAO,GAAG,EAAE,KAAK,KAAK;AACvC,YAAY,MAAM,SAAS,GAAG,GAAG,CAAC,QAAQ,CAAC,cAAc,CAAC;AAC1D,kBAAkB,QAAQ;AAC1B,kBAAkB,GAAG,CAAC,QAAQ,CAAC,UAAU,CAAC;AAC1C,sBAAsB,IAAI;AAC1B,sBAAsB,IAAI,CAAC;AAC3B,YAAY,IAAI,CAAC,SAAS;AAC1B,gBAAgB,OAAO,IAAI,CAAC;AAC5B,YAAY,IAAI,CAAC,UAAU,IAAI,CAAC,QAAQ;AACxC,gBAAgB,OAAO,KAAK,CAAC;AAC7B,YAAY,OAAO,mBAAmB,CAAC;AACvC,gBAAgB,QAAQ;AACxB,gBAAgB,UAAU;AAC1B,gBAAgB,SAAS;AACzB,gBAAgB,KAAK,EAAE,KAAK;AAC5B,aAAa,CAAC,CAAC;AACf,SAAS;AACT,KAAK,CAAC;AACN;;;;"}
|
package/dist/esm/utils/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Verifies a Cognito JWT token for its validity.
|
|
3
|
+
*
|
|
4
|
+
* @param input - An object containing:
|
|
5
|
+
* - token: The JWT token as a string that needs to be verified.
|
|
6
|
+
* - userPoolId: The ID of the AWS Cognito User Pool to which the token belongs.
|
|
7
|
+
* - clientId: The Client ID associated with the Cognito User Pool.
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export declare const isValidCognitoToken: (input: {
|
|
11
|
+
token: string;
|
|
12
|
+
userPoolId: string;
|
|
13
|
+
clientId: string;
|
|
14
|
+
tokenType: 'id' | 'access';
|
|
15
|
+
}) => Promise<boolean>;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { CognitoJwtVerifier } from 'aws-jwt-verify';
|
|
2
|
+
|
|
3
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
4
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
/**
|
|
6
|
+
* Verifies a Cognito JWT token for its validity.
|
|
7
|
+
*
|
|
8
|
+
* @param input - An object containing:
|
|
9
|
+
* - token: The JWT token as a string that needs to be verified.
|
|
10
|
+
* - userPoolId: The ID of the AWS Cognito User Pool to which the token belongs.
|
|
11
|
+
* - clientId: The Client ID associated with the Cognito User Pool.
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
const isValidCognitoToken = async (input) => {
|
|
15
|
+
const { userPoolId, clientId, tokenType, token } = input;
|
|
16
|
+
try {
|
|
17
|
+
const verifier = CognitoJwtVerifier.create({
|
|
18
|
+
userPoolId,
|
|
19
|
+
tokenUse: tokenType,
|
|
20
|
+
clientId,
|
|
21
|
+
});
|
|
22
|
+
await verifier.verify(token);
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
// TODO (ashwinkumar6): surface invalid cognito token error to customer
|
|
27
|
+
// TODO: clear invalid tokens from Storage
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export { isValidCognitoToken };
|
|
33
|
+
//# sourceMappingURL=isValidCognitoToken.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isValidCognitoToken.mjs","sources":["../../../src/utils/isValidCognitoToken.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { CognitoJwtVerifier } from 'aws-jwt-verify';\n/**\n * Verifies a Cognito JWT token for its validity.\n *\n * @param input - An object containing:\n * - token: The JWT token as a string that needs to be verified.\n * - userPoolId: The ID of the AWS Cognito User Pool to which the token belongs.\n * - clientId: The Client ID associated with the Cognito User Pool.\n * @internal\n */\nexport const isValidCognitoToken = async (input) => {\n const { userPoolId, clientId, tokenType, token } = input;\n try {\n const verifier = CognitoJwtVerifier.create({\n userPoolId,\n tokenUse: tokenType,\n clientId,\n });\n await verifier.verify(token);\n return true;\n }\n catch (error) {\n // TODO (ashwinkumar6): surface invalid cognito token error to customer\n // TODO: clear invalid tokens from Storage\n return false;\n }\n};\n"],"names":[],"mappings":";;AAAA;AACA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACY,MAAC,mBAAmB,GAAG,OAAO,KAAK,KAAK;AACpD,IAAI,MAAM,EAAE,UAAU,EAAE,QAAQ,EAAE,SAAS,EAAE,KAAK,EAAE,GAAG,KAAK,CAAC;AAC7D,IAAI,IAAI;AACR,QAAQ,MAAM,QAAQ,GAAG,kBAAkB,CAAC,MAAM,CAAC;AACnD,YAAY,UAAU;AACtB,YAAY,QAAQ,EAAE,SAAS;AAC/B,YAAY,QAAQ;AACpB,SAAS,CAAC,CAAC;AACX,QAAQ,MAAM,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AACrC,QAAQ,OAAO,IAAI,CAAC;AACpB,KAAK;AACL,IAAI,OAAO,KAAK,EAAE;AAClB;AACA;AACA,QAAQ,OAAO,KAAK,CAAC;AACrB,KAAK;AACL;;;;"}
|
package/package.json
CHANGED
|
@@ -1,74 +1,75 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
2
|
+
"author": "Amazon Web Services",
|
|
3
|
+
"name": "@aws-amplify/adapter-nextjs",
|
|
4
|
+
"version": "1.2.1",
|
|
5
|
+
"description": "The adapter for the supporting of using Amplify APIs in Next.js.",
|
|
6
|
+
"peerDependencies": {
|
|
7
|
+
"aws-amplify": "^6.0.7",
|
|
8
|
+
"next": ">=13.5.0 <15.0.0"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"aws-jwt-verify": "^4.0.1",
|
|
12
|
+
"cookie": "0.5.0"
|
|
13
|
+
},
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@types/cookie": "0.5.1",
|
|
16
|
+
"@types/node": "^20.3.1",
|
|
17
|
+
"@types/react": "^18.2.13",
|
|
18
|
+
"@types/react-dom": "^18.2.6",
|
|
19
|
+
"aws-amplify": "6.3.4",
|
|
20
|
+
"jest-fetch-mock": "3.0.3",
|
|
21
|
+
"next": ">= 13.5.0 < 15.0.0",
|
|
22
|
+
"typescript": "5.0.2"
|
|
23
|
+
},
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/aws/aws-amplify/issues"
|
|
29
|
+
},
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"types": "./dist/esm/index.d.ts",
|
|
33
|
+
"import": "./dist/esm/index.mjs",
|
|
34
|
+
"require": "./dist/cjs/index.js"
|
|
35
|
+
},
|
|
36
|
+
"./api": {
|
|
37
|
+
"types": "./dist/esm/api/index.d.ts",
|
|
38
|
+
"import": "./dist/esm/api/index.mjs",
|
|
39
|
+
"require": "./dist/cjs/api/index.js"
|
|
40
|
+
},
|
|
41
|
+
"./data": {
|
|
42
|
+
"types": "./dist/esm/api/index.d.ts",
|
|
43
|
+
"import": "./dist/esm/api/index.mjs",
|
|
44
|
+
"require": "./dist/cjs/api/index.js"
|
|
45
|
+
},
|
|
46
|
+
"./package.json": "./package.json"
|
|
47
|
+
},
|
|
48
|
+
"files": [
|
|
49
|
+
"dist/cjs",
|
|
50
|
+
"dist/esm",
|
|
51
|
+
"src",
|
|
52
|
+
"api",
|
|
53
|
+
"data"
|
|
54
|
+
],
|
|
55
|
+
"homepage": "https://aws-amplify.github.io/",
|
|
56
|
+
"license": "Apache-2.0",
|
|
57
|
+
"main": "./dist/cjs/index.js",
|
|
58
|
+
"module": "./dist/esm/index.mjs",
|
|
59
|
+
"typings": "./dist/esm/index.d.ts",
|
|
60
|
+
"sideEffects": false,
|
|
61
|
+
"scripts": {
|
|
62
|
+
"build": "npm run clean && npm run build:esm-cjs",
|
|
63
|
+
"build-with-test": "npm test && npm run build",
|
|
64
|
+
"build:esm-cjs": "rollup --forceExit -c rollup.config.mjs",
|
|
65
|
+
"build:watch": "npm run build:esm-cjs -- --watch",
|
|
66
|
+
"clean": "npm run clean:size && rimraf dist",
|
|
67
|
+
"clean:size": "rimraf dual-publish-tmp tmp*",
|
|
68
|
+
"format": "echo \"Not implemented\"",
|
|
69
|
+
"lint": "eslint '**/*.{ts,tsx}' && npm run ts-coverage",
|
|
70
|
+
"lint:fix": "eslint '**/*.{ts,tsx}' --fix",
|
|
71
|
+
"test": "npm run lint && jest -w 1 --coverage --logHeapUsage",
|
|
72
|
+
"ts-coverage": "typescript-coverage-report -p ./tsconfig.build.json -t 90.31"
|
|
73
|
+
},
|
|
74
|
+
"gitHead": "181642fe6e2362316cbbdf944d770d98dbbe6e89"
|
|
74
75
|
}
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
|
|
12
12
|
import { NextServer } from '../types';
|
|
13
13
|
|
|
14
|
+
import { createTokenValidator } from './createTokenValidator';
|
|
14
15
|
import { createCookieStorageAdapterFromNextServerContext } from './createCookieStorageAdapterFromNextServerContext';
|
|
15
16
|
|
|
16
17
|
export const createRunWithAmplifyServerContext = ({
|
|
@@ -34,6 +35,11 @@ export const createRunWithAmplifyServerContext = ({
|
|
|
34
35
|
createCookieStorageAdapterFromNextServerContext(
|
|
35
36
|
nextServerContext,
|
|
36
37
|
),
|
|
38
|
+
createTokenValidator({
|
|
39
|
+
userPoolId: resourcesConfig?.Auth.Cognito?.userPoolId,
|
|
40
|
+
userPoolClientId:
|
|
41
|
+
resourcesConfig?.Auth.Cognito?.userPoolClientId,
|
|
42
|
+
}),
|
|
37
43
|
);
|
|
38
44
|
const credentialsProvider = createAWSCredentialsAndIdentityIdProvider(
|
|
39
45
|
resourcesConfig.Auth,
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { KeyValueStorageMethodValidator } from '@aws-amplify/core/internals/adapter-core';
|
|
5
|
+
|
|
6
|
+
import { isValidCognitoToken } from './isValidCognitoToken';
|
|
7
|
+
|
|
8
|
+
interface CreateTokenValidatorInput {
|
|
9
|
+
userPoolId?: string;
|
|
10
|
+
userPoolClientId?: string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Creates a validator object for validating methods in a KeyValueStorage.
|
|
14
|
+
*/
|
|
15
|
+
export const createTokenValidator = ({
|
|
16
|
+
userPoolId,
|
|
17
|
+
userPoolClientId: clientId,
|
|
18
|
+
}: CreateTokenValidatorInput): KeyValueStorageMethodValidator => {
|
|
19
|
+
return {
|
|
20
|
+
// validate access, id tokens
|
|
21
|
+
getItem: async (key: string, value: string): Promise<boolean> => {
|
|
22
|
+
const tokenType = key.includes('.accessToken')
|
|
23
|
+
? 'access'
|
|
24
|
+
: key.includes('.idToken')
|
|
25
|
+
? 'id'
|
|
26
|
+
: null;
|
|
27
|
+
if (!tokenType) return true;
|
|
28
|
+
|
|
29
|
+
if (!userPoolId || !clientId) return false;
|
|
30
|
+
|
|
31
|
+
return isValidCognitoToken({
|
|
32
|
+
clientId,
|
|
33
|
+
userPoolId,
|
|
34
|
+
tokenType,
|
|
35
|
+
token: value,
|
|
36
|
+
});
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
};
|
package/src/utils/index.ts
CHANGED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { CognitoJwtVerifier } from 'aws-jwt-verify';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Verifies a Cognito JWT token for its validity.
|
|
8
|
+
*
|
|
9
|
+
* @param input - An object containing:
|
|
10
|
+
* - token: The JWT token as a string that needs to be verified.
|
|
11
|
+
* - userPoolId: The ID of the AWS Cognito User Pool to which the token belongs.
|
|
12
|
+
* - clientId: The Client ID associated with the Cognito User Pool.
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
export const isValidCognitoToken = async (input: {
|
|
16
|
+
token: string;
|
|
17
|
+
userPoolId: string;
|
|
18
|
+
clientId: string;
|
|
19
|
+
tokenType: 'id' | 'access';
|
|
20
|
+
}): Promise<boolean> => {
|
|
21
|
+
const { userPoolId, clientId, tokenType, token } = input;
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
const verifier = CognitoJwtVerifier.create({
|
|
25
|
+
userPoolId,
|
|
26
|
+
tokenUse: tokenType,
|
|
27
|
+
clientId,
|
|
28
|
+
});
|
|
29
|
+
await verifier.verify(token);
|
|
30
|
+
|
|
31
|
+
return true;
|
|
32
|
+
} catch (error) {
|
|
33
|
+
// TODO (ashwinkumar6): surface invalid cognito token error to customer
|
|
34
|
+
// TODO: clear invalid tokens from Storage
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
};
|