@aws-amplify/adapter-nextjs 1.1.6 → 1.1.7-s-auth.30d0cd2.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 (88) hide show
  1. package/dist/cjs/auth/createTokenExchangeRouteHandlerFactory.js +52 -0
  2. package/dist/cjs/auth/createTokenExchangeRouteHandlerFactory.js.map +1 -0
  3. package/dist/cjs/auth/httpOnlyCookieBasedAuthProviders/createHttpOnlyCookieBasedAuthProviders.js +39 -0
  4. package/dist/cjs/auth/httpOnlyCookieBasedAuthProviders/createHttpOnlyCookieBasedAuthProviders.js.map +1 -0
  5. package/dist/cjs/auth/httpOnlyCookieBasedAuthProviders/index.js +8 -0
  6. package/dist/cjs/auth/httpOnlyCookieBasedAuthProviders/index.js.map +1 -0
  7. package/dist/cjs/auth/types.js +4 -0
  8. package/dist/cjs/auth/types.js.map +1 -0
  9. package/dist/cjs/client/index.js +7 -0
  10. package/dist/cjs/client/index.js.map +1 -0
  11. package/dist/cjs/createServerRunner.js +18 -1
  12. package/dist/cjs/createServerRunner.js.map +1 -1
  13. package/dist/cjs/oauth/createGetOAuthInitiationRouteFactory.js +22 -0
  14. package/dist/cjs/oauth/createGetOAuthInitiationRouteFactory.js.map +1 -0
  15. package/dist/cjs/oauth/createOAuthRouteHandlerFactory.js +55 -0
  16. package/dist/cjs/oauth/createOAuthRouteHandlerFactory.js.map +1 -0
  17. package/dist/cjs/oauth/index.js +9 -0
  18. package/dist/cjs/oauth/index.js.map +1 -0
  19. package/dist/cjs/oauth/types.js +6 -0
  20. package/dist/cjs/oauth/types.js.map +1 -0
  21. package/dist/cjs/oauth/utils/completeOAuthFlow.js +101 -0
  22. package/dist/cjs/oauth/utils/completeOAuthFlow.js.map +1 -0
  23. package/dist/cjs/oauth/utils/getRedirectUrl.js +20 -0
  24. package/dist/cjs/oauth/utils/getRedirectUrl.js.map +1 -0
  25. package/dist/cjs/oauth/utils/initOAuthFlow.js +70 -0
  26. package/dist/cjs/oauth/utils/initOAuthFlow.js.map +1 -0
  27. package/dist/cjs/utils/createRunWithAmplifyServerContext.js +2 -2
  28. package/dist/cjs/utils/createRunWithAmplifyServerContext.js.map +1 -1
  29. package/dist/esm/api/createServerRunnerForAPI.d.ts +1 -1
  30. package/dist/esm/auth/createTokenExchangeRouteHandlerFactory.d.ts +2 -0
  31. package/dist/esm/auth/createTokenExchangeRouteHandlerFactory.mjs +50 -0
  32. package/dist/esm/auth/createTokenExchangeRouteHandlerFactory.mjs.map +1 -0
  33. package/dist/esm/auth/httpOnlyCookieBasedAuthProviders/createHttpOnlyCookieBasedAuthProviders.d.ts +4 -0
  34. package/dist/esm/auth/httpOnlyCookieBasedAuthProviders/createHttpOnlyCookieBasedAuthProviders.mjs +37 -0
  35. package/dist/esm/auth/httpOnlyCookieBasedAuthProviders/createHttpOnlyCookieBasedAuthProviders.mjs.map +1 -0
  36. package/dist/esm/auth/httpOnlyCookieBasedAuthProviders/index.d.ts +2 -0
  37. package/dist/esm/auth/httpOnlyCookieBasedAuthProviders/index.mjs +3 -0
  38. package/dist/esm/auth/httpOnlyCookieBasedAuthProviders/index.mjs.map +1 -0
  39. package/dist/esm/auth/types.d.ts +17 -0
  40. package/dist/esm/auth/types.mjs +2 -0
  41. package/dist/esm/auth/types.mjs.map +1 -0
  42. package/dist/esm/client/index.d.ts +1 -0
  43. package/dist/esm/client/index.mjs +3 -0
  44. package/dist/esm/client/index.mjs.map +1 -0
  45. package/dist/esm/createServerRunner.mjs +18 -1
  46. package/dist/esm/createServerRunner.mjs.map +1 -1
  47. package/dist/esm/oauth/createGetOAuthInitiationRouteFactory.d.ts +2 -0
  48. package/dist/esm/oauth/createGetOAuthInitiationRouteFactory.mjs +20 -0
  49. package/dist/esm/oauth/createGetOAuthInitiationRouteFactory.mjs.map +1 -0
  50. package/dist/esm/oauth/createOAuthRouteHandlerFactory.d.ts +2 -0
  51. package/dist/esm/oauth/createOAuthRouteHandlerFactory.mjs +53 -0
  52. package/dist/esm/oauth/createOAuthRouteHandlerFactory.mjs.map +1 -0
  53. package/dist/esm/oauth/index.d.ts +1 -0
  54. package/dist/esm/oauth/index.mjs +2 -0
  55. package/dist/esm/oauth/index.mjs.map +1 -0
  56. package/dist/esm/oauth/types.d.ts +39 -0
  57. package/dist/esm/oauth/types.mjs +2 -0
  58. package/dist/esm/oauth/types.mjs.map +1 -0
  59. package/dist/esm/oauth/utils/completeOAuthFlow.d.ts +12 -0
  60. package/dist/esm/oauth/utils/completeOAuthFlow.mjs +99 -0
  61. package/dist/esm/oauth/utils/completeOAuthFlow.mjs.map +1 -0
  62. package/dist/esm/oauth/utils/getRedirectUrl.d.ts +2 -0
  63. package/dist/esm/oauth/utils/getRedirectUrl.mjs +18 -0
  64. package/dist/esm/oauth/utils/getRedirectUrl.mjs.map +1 -0
  65. package/dist/esm/oauth/utils/initOAuthFlow.d.ts +11 -0
  66. package/dist/esm/oauth/utils/initOAuthFlow.mjs +68 -0
  67. package/dist/esm/oauth/utils/initOAuthFlow.mjs.map +1 -0
  68. package/dist/esm/types/NextServer.d.ts +19 -1
  69. package/dist/esm/utils/createRunWithAmplifyServerContext.d.ts +2 -1
  70. package/dist/esm/utils/createRunWithAmplifyServerContext.mjs +2 -2
  71. package/dist/esm/utils/createRunWithAmplifyServerContext.mjs.map +1 -1
  72. package/package.json +78 -72
  73. package/src/api/createServerRunnerForAPI.ts +7 -1
  74. package/src/auth/createTokenExchangeRouteHandlerFactory.ts +70 -0
  75. package/src/auth/httpOnlyCookieBasedAuthProviders/createHttpOnlyCookieBasedAuthProviders.ts +57 -0
  76. package/src/auth/httpOnlyCookieBasedAuthProviders/index.ts +3 -0
  77. package/src/auth/types.ts +26 -0
  78. package/src/client/index.ts +1 -0
  79. package/src/createServerRunner.ts +19 -0
  80. package/src/oauth/createGetOAuthInitiationRouteFactory.ts +35 -0
  81. package/src/oauth/createOAuthRouteHandlerFactory.ts +77 -0
  82. package/src/oauth/index.ts +4 -0
  83. package/src/oauth/types.ts +60 -0
  84. package/src/oauth/utils/completeOAuthFlow.ts +176 -0
  85. package/src/oauth/utils/getRedirectUrl.ts +23 -0
  86. package/src/oauth/utils/initOAuthFlow.ts +109 -0
  87. package/src/types/NextServer.ts +27 -1
  88. package/src/utils/createRunWithAmplifyServerContext.ts +3 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"initOAuthFlow.mjs","sources":["../../../../src/oauth/utils/initOAuthFlow.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { cognitoHostedUIIdentityProviderMap, createKeyValueStorageFromCookieStorageAdapter, generateCodeVerifier, generateState, } from 'aws-amplify/adapter-core';\nimport { NextResponse } from 'next/server.js';\nimport { urlSafeEncode } from '@aws-amplify/core/internals/utils';\nimport { DefaultOAuthStore } from '@aws-amplify/auth/cognito';\nimport { createCookieStorageAdapterFromNextServerContext } from '../../utils/createCookieStorageAdapterFromNextServerContext';\nimport { getRedirectUrl } from './getRedirectUrl';\nexport const initOAuthFlow = async ({ request, customState, cognitoUserPoolConfig, oAuthConfig, setAuthCookieOptions, }) => {\n const { searchParams } = request.nextUrl;\n const specifiedProvider = searchParams.get('provider');\n const provider = getProvider(specifiedProvider);\n const randomState = generateState();\n const state = customState\n ? `${randomState}-${urlSafeEncode(customState)}`\n : randomState;\n const scope = oAuthConfig.scopes.join(' ');\n const redirectUrlSearchParams = new URLSearchParams({\n redirect_uri: getRedirectUrl(origin, oAuthConfig),\n response_type: oAuthConfig.responseType,\n client_id: cognitoUserPoolConfig.userPoolClientId,\n identity_provider: provider,\n scope,\n state,\n });\n let peckKey;\n if (oAuthConfig.responseType === 'code') {\n const { value, method, toCodeChallenge } = generateCodeVerifier(128);\n peckKey = value;\n redirectUrlSearchParams.append('code_challenge', toCodeChallenge());\n redirectUrlSearchParams.append('code_challenge_method', method);\n }\n const redirectUrl = new URL(`https://${oAuthConfig.domain}/oauth2/authorize?${redirectUrlSearchParams.toString()}`);\n const response = NextResponse.redirect(redirectUrl);\n const keyValueStorage = createKeyValueStorageFromCookieStorageAdapter(createCookieStorageAdapterFromNextServerContext({\n request,\n response,\n }), setAuthCookieOptions);\n const oauthStore = new DefaultOAuthStore(keyValueStorage);\n oauthStore.setAuthConfig(cognitoUserPoolConfig);\n oauthStore.storeOAuthState(state);\n peckKey && oauthStore.storePKCE(peckKey);\n return response;\n};\nconst getProvider = (provider) => {\n if (typeof provider === 'string') {\n return resolveProvider(provider);\n }\n return 'COGNITO';\n};\nconst resolveProvider = (provider) => {\n try {\n assertAuthProvider(provider);\n return cognitoHostedUIIdentityProviderMap[provider];\n }\n catch (_) {\n return provider;\n }\n};\nfunction assertAuthProvider(provider) {\n if (!['Amazon', 'Apple', 'Facebook', 'Google'].includes(provider)) {\n throw new Error('No valid provider specified.');\n }\n}\n"],"names":[],"mappings":"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"}
@@ -1,10 +1,13 @@
1
1
  import { GetServerSidePropsContext as NextGetServerSidePropsContext } from 'next';
2
2
  import { NextRequest, NextResponse } from 'next/server.js';
3
3
  import { cookies } from 'next/headers.js';
4
- import { AmplifyOutputs, LegacyConfig } from 'aws-amplify/adapter-core';
4
+ import { AmplifyOutputs, CookieStorage, LegacyConfig } from 'aws-amplify/adapter-core';
5
5
  import { AmplifyServer } from '@aws-amplify/core/internals/adapter-core';
6
6
  import { ResourcesConfig } from '@aws-amplify/core';
7
+ import { CreateOAuthRouteHandler, GetOAuthInitiationRoute } from '../oauth/types';
8
+ import { CreateTokenExchangeRouteHandler } from '../auth/types';
7
9
  export declare namespace NextServer {
10
+ type SetCookieOptions = CookieStorage.SetCookieOptions;
8
11
  /**
9
12
  * This context is normally available in the following:
10
13
  * - Next App Router [middleware](https://nextjs.org/docs/app/building-your-application/routing/middleware)
@@ -54,10 +57,25 @@ export declare namespace NextServer {
54
57
  }
55
58
  type RunOperationWithContext = <OperationResult>(input: RunWithContextInput<OperationResult>) => Promise<OperationResult>;
56
59
  interface CreateServerRunnerInput {
60
+ /**
61
+ * The Amplify resources config. Typically imported from `amplify_outputs.json` (Gen2)
62
+ * or `amplifyconfiguration.json` (Gen1).
63
+ */
57
64
  config: ResourcesConfig | LegacyConfig | AmplifyOutputs;
65
+ /**
66
+ * The origin of your Next app.
67
+ */
68
+ origin?: string;
69
+ /**
70
+ * Configures attributes of Set-Cookie.
71
+ */
72
+ setAuthCookieOptions?: SetCookieOptions;
58
73
  }
59
74
  interface CreateServerRunnerOutput {
60
75
  runWithAmplifyServerContext: RunOperationWithContext;
76
+ createOAuthRouteHandler: CreateOAuthRouteHandler;
77
+ getOAuthInitiationRoute: GetOAuthInitiationRoute;
78
+ createTokenExchangeRouteHandler: CreateTokenExchangeRouteHandler;
61
79
  }
62
80
  type CreateServerRunner = (input: CreateServerRunnerInput) => CreateServerRunnerOutput;
63
81
  }
@@ -1,5 +1,6 @@
1
1
  import { ResourcesConfig } from '@aws-amplify/core';
2
2
  import { NextServer } from '../types';
3
- export declare const createRunWithAmplifyServerContext: ({ config: resourcesConfig, }: {
3
+ export declare const createRunWithAmplifyServerContext: ({ config: resourcesConfig, setAuthCookieOptions, }: {
4
4
  config: ResourcesConfig;
5
+ setAuthCookieOptions?: Partial<Pick<import("cookie").CookieSerializeOptions, "domain" | "expires" | "httpOnly" | "maxAge" | "sameSite" | "secure">> | undefined;
5
6
  }) => NextServer.RunOperationWithContext;
@@ -4,7 +4,7 @@ import { createCookieStorageAdapterFromNextServerContext } from './createCookieS
4
4
 
5
5
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6
6
  // SPDX-License-Identifier: Apache-2.0
7
- const createRunWithAmplifyServerContext = ({ config: resourcesConfig, }) => {
7
+ const createRunWithAmplifyServerContext = ({ config: resourcesConfig, setAuthCookieOptions, }) => {
8
8
  const runWithAmplifyServerContext$1 = async ({ nextServerContext, operation }) => {
9
9
  // When the Auth config is presented, attempt to create a Amplify server
10
10
  // context with token and credentials provider.
@@ -16,7 +16,7 @@ const createRunWithAmplifyServerContext = ({ config: resourcesConfig, }) => {
16
16
  // static rendering uses the same unauthenticated role cross-sever.
17
17
  nextServerContext === null
18
18
  ? sharedInMemoryStorage
19
- : createKeyValueStorageFromCookieStorageAdapter(createCookieStorageAdapterFromNextServerContext(nextServerContext));
19
+ : createKeyValueStorageFromCookieStorageAdapter(createCookieStorageAdapterFromNextServerContext(nextServerContext), setAuthCookieOptions);
20
20
  const credentialsProvider = createAWSCredentialsAndIdentityIdProvider(resourcesConfig.Auth, keyValueStorage);
21
21
  const tokenProvider = createUserPoolsTokenProvider(resourcesConfig.Auth, keyValueStorage);
22
22
  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":";;;;AAAA;AACA;AAIY,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,CAAC,CAAC;AACpI,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;;;;"}
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, setAuthCookieOptions, }) => {\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), setAuthCookieOptions);\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;AAIY,MAAC,iCAAiC,GAAG,CAAC,EAAE,MAAM,EAAE,eAAe,EAAE,oBAAoB,GAAG,KAAK;AACzG,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,CAAC;AAC1J,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;;;;"}
package/package.json CHANGED
@@ -1,74 +1,80 @@
1
1
  {
2
- "author": "Amazon Web Services",
3
- "name": "@aws-amplify/adapter-nextjs",
4
- "version": "1.1.6",
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
- "cookie": "0.5.0"
12
- },
13
- "devDependencies": {
14
- "@types/cookie": "0.5.1",
15
- "@types/node": "^20.3.1",
16
- "@types/react": "^18.2.13",
17
- "@types/react-dom": "^18.2.6",
18
- "aws-amplify": "6.3.0",
19
- "jest-fetch-mock": "3.0.3",
20
- "next": ">= 13.5.0 < 15.0.0",
21
- "typescript": "5.0.2"
22
- },
23
- "publishConfig": {
24
- "access": "public"
25
- },
26
- "bugs": {
27
- "url": "https://github.com/aws/aws-amplify/issues"
28
- },
29
- "exports": {
30
- ".": {
31
- "types": "./dist/esm/index.d.ts",
32
- "import": "./dist/esm/index.mjs",
33
- "require": "./dist/cjs/index.js"
34
- },
35
- "./api": {
36
- "types": "./dist/esm/api/index.d.ts",
37
- "import": "./dist/esm/api/index.mjs",
38
- "require": "./dist/cjs/api/index.js"
39
- },
40
- "./data": {
41
- "types": "./dist/esm/api/index.d.ts",
42
- "import": "./dist/esm/api/index.mjs",
43
- "require": "./dist/cjs/api/index.js"
44
- },
45
- "./package.json": "./package.json"
46
- },
47
- "files": [
48
- "dist/cjs",
49
- "dist/esm",
50
- "src",
51
- "api",
52
- "data"
53
- ],
54
- "homepage": "https://aws-amplify.github.io/",
55
- "license": "Apache-2.0",
56
- "main": "./dist/cjs/index.js",
57
- "module": "./dist/esm/index.mjs",
58
- "typings": "./dist/esm/index.d.ts",
59
- "sideEffects": false,
60
- "scripts": {
61
- "build": "npm run clean && npm run build:esm-cjs",
62
- "build-with-test": "npm test && npm run build",
63
- "build:esm-cjs": "rollup --forceExit -c rollup.config.mjs",
64
- "build:watch": "npm run build:esm-cjs -- --watch",
65
- "clean": "npm run clean:size && rimraf dist",
66
- "clean:size": "rimraf dual-publish-tmp tmp*",
67
- "format": "echo \"Not implemented\"",
68
- "lint": "eslint '**/*.{ts,tsx}' && npm run ts-coverage",
69
- "lint:fix": "eslint '**/*.{ts,tsx}' --fix",
70
- "test": "npm run lint && jest -w 1 --coverage --logHeapUsage",
71
- "ts-coverage": "typescript-coverage-report -p ./tsconfig.build.json -t 90.31"
72
- },
73
- "gitHead": "3283f520db9a28b317b261f08ad0ad33b5957441"
2
+ "author": "Amazon Web Services",
3
+ "name": "@aws-amplify/adapter-nextjs",
4
+ "version": "1.1.7-s-auth.30d0cd2.0+30d0cd2",
5
+ "description": "The adapter for the supporting of using Amplify APIs in Next.js.",
6
+ "peerDependencies": {
7
+ "aws-amplify": "6.3.1-s-auth.30d0cd2.0+30d0cd2",
8
+ "next": ">=14.2.3 <15.0.0"
9
+ },
10
+ "dependencies": {
11
+ "client-only": "0.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.1-s-auth.30d0cd2.0+30d0cd2",
20
+ "jest-fetch-mock": "3.0.3",
21
+ "next": ">=14.2.3 <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
+ "./client": {
47
+ "types": "./dist/esm/client/index.d.ts",
48
+ "import": "./dist/esm/client/index.mjs",
49
+ "require": "./dist/cjs/client/index.js"
50
+ },
51
+ "./package.json": "./package.json"
52
+ },
53
+ "files": [
54
+ "dist/cjs",
55
+ "dist/esm",
56
+ "src",
57
+ "api",
58
+ "data"
59
+ ],
60
+ "homepage": "https://aws-amplify.github.io/",
61
+ "license": "Apache-2.0",
62
+ "main": "./dist/cjs/index.js",
63
+ "module": "./dist/esm/index.mjs",
64
+ "typings": "./dist/esm/index.d.ts",
65
+ "sideEffects": false,
66
+ "scripts": {
67
+ "build": "npm run clean && npm run build:esm-cjs",
68
+ "build-with-test": "npm test && npm run build",
69
+ "build:esm-cjs": "rollup --forceExit -c rollup.config.mjs",
70
+ "build:watch": "npm run build:esm-cjs -- --watch",
71
+ "clean": "npm run clean:size && rimraf dist",
72
+ "clean:size": "rimraf dual-publish-tmp tmp*",
73
+ "format": "echo \"Not implemented\"",
74
+ "lint": "eslint '**/*.{ts,tsx}' && npm run ts-coverage",
75
+ "lint:fix": "eslint '**/*.{ts,tsx}' --fix",
76
+ "test": "echo 'no-op'",
77
+ "ts-coverage": "typescript-coverage-report -p ./tsconfig.build.json -t 90.31"
78
+ },
79
+ "gitHead": "30d0cd2b2221a9b825760000a7dbc2bce6b9ff64"
74
80
  }
@@ -9,7 +9,13 @@ import { NextServer } from '../types';
9
9
 
10
10
  export const createServerRunnerForAPI = ({
11
11
  config,
12
- }: NextServer.CreateServerRunnerInput): NextServer.CreateServerRunnerOutput & {
12
+ }: Omit<NextServer.CreateServerRunnerInput, 'origin'>): Omit<
13
+ NextServer.CreateServerRunnerOutput,
14
+ | 'createOAuthRouteHandler'
15
+ | 'getOAuthInitiationRoute'
16
+ | 'createTokenExchangeRouteHandler'
17
+ | 'origin'
18
+ > & {
13
19
  resourcesConfig: ResourcesConfig;
14
20
  } => {
15
21
  const amplifyConfig = parseAmplifyConfig(config);
@@ -0,0 +1,70 @@
1
+ import { NextRequest } from 'next/server.js';
2
+ import { cookies } from 'next/headers.js';
3
+ import { fetchAuthSession } from 'aws-amplify/auth/server';
4
+
5
+ import { createRunWithAmplifyServerContext } from '../utils';
6
+
7
+ import {
8
+ CreateTokenExchangeRouteHandlerFactory,
9
+ CreateTokenExchangeRouteHandlerInput,
10
+ } from './types';
11
+
12
+ export const createTokenExchangeRouteHandlerFactory: CreateTokenExchangeRouteHandlerFactory =
13
+ input => {
14
+ const runWithAmplifyServerContext =
15
+ createRunWithAmplifyServerContext(input);
16
+
17
+ const handleRequest = async (
18
+ _: NextRequest,
19
+ __: CreateTokenExchangeRouteHandlerInput,
20
+ ) => {
21
+ const { origin } = input;
22
+
23
+ if (!origin) {
24
+ throw new Error(
25
+ '`origin` parameter is required when using `getOAuthInitiationRoute`.',
26
+ );
27
+ }
28
+
29
+ const userSession = await runWithAmplifyServerContext({
30
+ nextServerContext: { cookies },
31
+ operation: contextSpec => fetchAuthSession(contextSpec),
32
+ });
33
+
34
+ const clockDrift = cookies()
35
+ .getAll()
36
+ .find(cookie => cookie.name.endsWith('.clockDrift'))?.value;
37
+
38
+ return new Response(
39
+ JSON.stringify({
40
+ ...userSession,
41
+ tokens: {
42
+ accessToken: userSession.tokens?.accessToken.toString(),
43
+ idToken: userSession.tokens?.idToken?.toString(),
44
+ },
45
+ username: userSession.tokens?.accessToken.payload.username,
46
+ clockDrift,
47
+ userSession,
48
+ }),
49
+ {
50
+ headers: {
51
+ 'content-type': 'application/json',
52
+ 'Access-Control-Allow-Origin': origin,
53
+ 'Access-Control-Allow-Methods': 'POST',
54
+ },
55
+ },
56
+ );
57
+ };
58
+
59
+ return handlerInput => ({
60
+ async POST(request) {
61
+ try {
62
+ return await handleRequest(request, handlerInput);
63
+ } catch (error) {
64
+ const { onError } = handlerInput;
65
+
66
+ onError(error as Error);
67
+ }
68
+ },
69
+ });
70
+ };
@@ -0,0 +1,57 @@
1
+ import { LibraryOptions, sharedInMemoryStorage } from '@aws-amplify/core';
2
+ import { runInBrowserContext } from '@aws-amplify/core/internals/utils';
3
+ import {
4
+ cognitoCredentialsProvider,
5
+ cognitoUserPoolsTokenProvider,
6
+ } from 'aws-amplify/auth/cognito';
7
+
8
+ export const createHttpOnlyCookieBasedAuthProviders = ({
9
+ authTokenExchangeRoute,
10
+ }: {
11
+ authTokenExchangeRoute: string;
12
+ }): LibraryOptions['Auth'] => {
13
+ cognitoUserPoolsTokenProvider.setKeyValueStorage(sharedInMemoryStorage);
14
+
15
+ runInBrowserContext(() => {
16
+ refreshSession({
17
+ authTokenExchangeRoute,
18
+ tokenProvider: cognitoUserPoolsTokenProvider,
19
+ credentialsProvider: cognitoCredentialsProvider,
20
+ });
21
+ });
22
+
23
+ return {
24
+ tokenProvider: cognitoUserPoolsTokenProvider,
25
+ credentialsProvider: cognitoCredentialsProvider,
26
+ };
27
+ };
28
+
29
+ const refreshSession = async ({
30
+ authTokenExchangeRoute,
31
+ tokenProvider,
32
+ credentialsProvider,
33
+ }: {
34
+ authTokenExchangeRoute: string;
35
+ tokenProvider: typeof cognitoUserPoolsTokenProvider;
36
+ credentialsProvider: typeof cognitoCredentialsProvider;
37
+ }) => {
38
+ const response = await fetch(authTokenExchangeRoute, { method: 'POST' });
39
+ const session = await response.json();
40
+
41
+ tokenProvider.tokenOrchestrator.setTokens({
42
+ tokens: {
43
+ accessToken: session.tokens.accessToken,
44
+ idToken: session.tokens.idToken,
45
+ clockDrift: session.clockDrift,
46
+ username: session.username,
47
+ },
48
+ });
49
+
50
+ credentialsProvider.setIdentityIdCredentials(
51
+ {
52
+ credentials: session.credentials,
53
+ identityId: session.identityId,
54
+ },
55
+ session.tokens.idToken,
56
+ );
57
+ };
@@ -0,0 +1,3 @@
1
+ import 'client-only';
2
+
3
+ export { createHttpOnlyCookieBasedAuthProviders } from './createHttpOnlyCookieBasedAuthProviders';
@@ -0,0 +1,26 @@
1
+ import { ResourcesConfig } from 'aws-amplify';
2
+ import { NextRequest } from 'next/server';
3
+
4
+ import { NextServer } from '../types';
5
+
6
+ interface CreateTokenExchangeRouteHandlerFactoryInput {
7
+ config: ResourcesConfig;
8
+ origin?: string;
9
+ setAuthCookieOptions?: NextServer.SetCookieOptions;
10
+ }
11
+
12
+ interface CreateOAuthRouteHandlerOutput {
13
+ POST(request: NextRequest): Promise<Response | void>;
14
+ }
15
+
16
+ export interface CreateTokenExchangeRouteHandlerInput {
17
+ onError(error: Error): void;
18
+ }
19
+
20
+ export type CreateTokenExchangeRouteHandler = (
21
+ input: CreateTokenExchangeRouteHandlerInput,
22
+ ) => CreateOAuthRouteHandlerOutput;
23
+
24
+ export type CreateTokenExchangeRouteHandlerFactory = (
25
+ input: CreateTokenExchangeRouteHandlerFactoryInput,
26
+ ) => CreateTokenExchangeRouteHandler;
@@ -0,0 +1 @@
1
+ export { createHttpOnlyCookieBasedAuthProviders } from '../auth/httpOnlyCookieBasedAuthProviders';
@@ -6,6 +6,9 @@ import { parseAmplifyConfig } from '@aws-amplify/core/internals/utils';
6
6
 
7
7
  import { createRunWithAmplifyServerContext } from './utils';
8
8
  import { NextServer } from './types';
9
+ import { createOAuthRouteHandlerFactory } from './oauth';
10
+ import { createTokenExchangeRouteHandlerFactory } from './auth/createTokenExchangeRouteHandlerFactory';
11
+ import { createGetOAuthInitiationRouteFactory } from './oauth/createGetOAuthInitiationRouteFactory';
9
12
 
10
13
  /**
11
14
  * Creates the `runWithAmplifyServerContext` function to run Amplify server side APIs in an isolated request context.
@@ -27,12 +30,28 @@ import { NextServer } from './types';
27
30
  */
28
31
  export const createServerRunner: NextServer.CreateServerRunner = ({
29
32
  config,
33
+ origin,
34
+ setAuthCookieOptions,
30
35
  }) => {
31
36
  const amplifyConfig = parseAmplifyConfig(config);
32
37
 
33
38
  return {
34
39
  runWithAmplifyServerContext: createRunWithAmplifyServerContext({
35
40
  config: amplifyConfig,
41
+ setAuthCookieOptions,
42
+ }),
43
+ createOAuthRouteHandler: createOAuthRouteHandlerFactory({
44
+ config: amplifyConfig,
45
+ setAuthCookieOptions,
46
+ }),
47
+ getOAuthInitiationRoute: createGetOAuthInitiationRouteFactory({
48
+ config: amplifyConfig,
49
+ origin,
50
+ }),
51
+ createTokenExchangeRouteHandler: createTokenExchangeRouteHandlerFactory({
52
+ config: amplifyConfig,
53
+ origin,
54
+ setAuthCookieOptions,
36
55
  }),
37
56
  };
38
57
  };
@@ -0,0 +1,35 @@
1
+ import {
2
+ assertOAuthConfig,
3
+ assertTokenProviderConfig,
4
+ } from '@aws-amplify/core/internals/utils';
5
+
6
+ import {
7
+ CreateGetOAuthInitiationRouteFactory,
8
+ GetOAuthInitiationRoute,
9
+ } from './types';
10
+ import { getRedirectUrl } from './utils/getRedirectUrl';
11
+
12
+ export const createGetOAuthInitiationRouteFactory: CreateGetOAuthInitiationRouteFactory =
13
+ ({ config: resourcesConfig, origin }) => {
14
+ const getOAuthInitiationRoute: GetOAuthInitiationRoute = input => {
15
+ assertTokenProviderConfig(resourcesConfig.Auth?.Cognito);
16
+ assertOAuthConfig(resourcesConfig.Auth.Cognito);
17
+
18
+ const { Cognito: cognitoUserPoolConfig } = resourcesConfig.Auth;
19
+ if (!origin) {
20
+ throw new Error(
21
+ '`origin` parameter is required when using `getOAuthInitiationRoute`.',
22
+ );
23
+ }
24
+
25
+ const redirectUrl = getRedirectUrl(
26
+ origin,
27
+ cognitoUserPoolConfig.loginWith.oauth,
28
+ );
29
+ const { provider } = input;
30
+
31
+ return `${redirectUrl}?init=true&provider=${provider}`;
32
+ };
33
+
34
+ return getOAuthInitiationRoute;
35
+ };
@@ -0,0 +1,77 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import {
5
+ assertOAuthConfig,
6
+ assertTokenProviderConfig,
7
+ } from '@aws-amplify/core/internals/utils';
8
+ import { NextRequest } from 'next/server';
9
+ import { AuthError } from '@aws-amplify/auth';
10
+
11
+ import {
12
+ CreateOAuthRouteHandler,
13
+ CreateOAuthRouteHandlerFactory,
14
+ CreateOAuthRouteHandlerInput,
15
+ } from './types';
16
+ import { initOAuthFlow } from './utils/initOAuthFlow';
17
+ import { completeOAuthFlow } from './utils/completeOAuthFlow';
18
+
19
+ export const createOAuthRouteHandlerFactory: CreateOAuthRouteHandlerFactory = ({
20
+ config: resourcesConfig,
21
+ origin,
22
+ setAuthCookieOptions,
23
+ }): CreateOAuthRouteHandler => {
24
+ const handleRequest = async (
25
+ request: NextRequest,
26
+ {
27
+ customState,
28
+ redirectOnAuthComplete,
29
+ onError,
30
+ }: CreateOAuthRouteHandlerInput,
31
+ ): Promise<Response | void> => {
32
+ if (!origin) throw new Error('Origin is not provided');
33
+ assertTokenProviderConfig(resourcesConfig.Auth?.Cognito);
34
+ assertOAuthConfig(resourcesConfig.Auth.Cognito);
35
+
36
+ const { Cognito: cognitoUserPoolConfig } = resourcesConfig.Auth;
37
+ const { searchParams } = request.nextUrl;
38
+
39
+ // when request url has `init` query param - initiate oauth flow
40
+ if (searchParams.has('init')) {
41
+ return initOAuthFlow({
42
+ origin,
43
+ setAuthCookieOptions,
44
+ request,
45
+ customState,
46
+ cognitoUserPoolConfig,
47
+ oAuthConfig: cognitoUserPoolConfig.loginWith.oauth,
48
+ });
49
+ }
50
+
51
+ if (searchParams.has('code') && searchParams.has('state')) {
52
+ return completeOAuthFlow({
53
+ origin,
54
+ request,
55
+ redirectOnComplete: redirectOnAuthComplete,
56
+ setAuthCookieOptions,
57
+ customState,
58
+ cognitoUserPoolConfig,
59
+ oAuthConfig: cognitoUserPoolConfig.loginWith.oauth,
60
+ });
61
+ }
62
+
63
+ onError(new Error('Invalid point (update me)'));
64
+ };
65
+
66
+ return handlerInput => ({
67
+ async GET(request) {
68
+ try {
69
+ return await handleRequest(request, handlerInput);
70
+ } catch (error) {
71
+ const { onError } = handlerInput;
72
+
73
+ onError(error as AuthError);
74
+ }
75
+ },
76
+ });
77
+ };
@@ -0,0 +1,4 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export { createOAuthRouteHandlerFactory } from './createOAuthRouteHandlerFactory';
@@ -0,0 +1,60 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { AuthError, AuthProvider } from '@aws-amplify/auth';
5
+ import { ResourcesConfig } from 'aws-amplify';
6
+ import { NextRequest } from 'next/server';
7
+
8
+ import { NextServer } from '../types';
9
+
10
+ export interface CreateOAuthRouteHandlerInput {
11
+ /** A custom state identifying an OAuth flow. */
12
+ customState?: string;
13
+
14
+ /** The path to redirect to when an OAuth flow completes. */
15
+ redirectOnAuthComplete: string;
16
+
17
+ /**
18
+ * A callback function to be called with a {@link AuthError} object that thrown
19
+ * from an inflight OAuth flow when error occurs. You need to return a
20
+ * {@link Response} object to redirect end user away from the API route
21
+ * you set up, for example, redirect back to the sign in page by
22
+ * `return NextResponse.redirect('/sign-in')`.
23
+ */
24
+ onError(error: AuthError): void;
25
+ }
26
+
27
+ interface CreateOAuthRouteHandlerOutput {
28
+ GET(request: NextRequest): Promise<Response | void>;
29
+ }
30
+
31
+ export type CreateOAuthRouteHandler = (
32
+ input: CreateOAuthRouteHandlerInput,
33
+ ) => CreateOAuthRouteHandlerOutput;
34
+
35
+ interface CreateOAuthRouteHandlerFactoryInput {
36
+ config: ResourcesConfig;
37
+ origin?: string;
38
+ setAuthCookieOptions?: NextServer.SetCookieOptions;
39
+ }
40
+
41
+ export type CreateOAuthRouteHandlerFactory = (
42
+ input: CreateOAuthRouteHandlerFactoryInput,
43
+ ) => CreateOAuthRouteHandler;
44
+
45
+ export type GetOAuthInitiationRoute = (input: {
46
+ provider:
47
+ | AuthProvider
48
+ | {
49
+ custom: string;
50
+ };
51
+ }) => string;
52
+
53
+ interface CreateGetOAuthInitiationRouteFactoryInput {
54
+ config: ResourcesConfig;
55
+ origin?: string;
56
+ }
57
+
58
+ export type CreateGetOAuthInitiationRouteFactory = (
59
+ input: CreateGetOAuthInitiationRouteFactoryInput,
60
+ ) => GetOAuthInitiationRoute;