@aura-stack/auth 0.1.0-rc.8 → 0.1.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 (116) hide show
  1. package/dist/@types/index.cjs +15 -15
  2. package/dist/@types/index.d.ts +7 -31
  3. package/dist/@types/index.js +1 -1
  4. package/dist/@types/router.d.cjs +1 -1
  5. package/dist/@types/router.d.d.ts +7 -7
  6. package/dist/@types/utility.cjs +15 -15
  7. package/dist/@types/utility.d.ts +6 -6
  8. package/dist/@types/utility.js +1 -1
  9. package/dist/actions/callback/access-token.cjs +143 -143
  10. package/dist/actions/callback/access-token.d.ts +15 -20
  11. package/dist/actions/callback/access-token.js +8 -4
  12. package/dist/actions/callback/callback.cjs +437 -455
  13. package/dist/actions/callback/callback.d.ts +10 -12
  14. package/dist/actions/callback/callback.js +16 -12
  15. package/dist/actions/callback/userinfo.cjs +131 -134
  16. package/dist/actions/callback/userinfo.d.ts +9 -9
  17. package/dist/actions/callback/userinfo.js +10 -6
  18. package/dist/actions/csrfToken/csrfToken.cjs +162 -171
  19. package/dist/actions/csrfToken/csrfToken.d.ts +3 -3
  20. package/dist/actions/csrfToken/csrfToken.js +12 -8
  21. package/dist/actions/index.cjs +746 -773
  22. package/dist/actions/index.d.ts +13 -13
  23. package/dist/actions/index.js +34 -18
  24. package/dist/actions/session/session.cjs +149 -155
  25. package/dist/actions/session/session.d.ts +3 -3
  26. package/dist/actions/session/session.js +11 -7
  27. package/dist/actions/signIn/authorization.cjs +231 -249
  28. package/dist/actions/signIn/authorization.d.ts +12 -18
  29. package/dist/actions/signIn/authorization.js +16 -6
  30. package/dist/actions/signIn/signIn.cjs +396 -423
  31. package/dist/actions/signIn/signIn.d.ts +10 -10
  32. package/dist/actions/signIn/signIn.js +14 -10
  33. package/dist/actions/signOut/signOut.cjs +417 -440
  34. package/dist/actions/signOut/signOut.d.ts +3 -3
  35. package/dist/actions/signOut/signOut.js +15 -11
  36. package/dist/assert.cjs +35 -36
  37. package/dist/assert.d.ts +4 -4
  38. package/dist/assert.js +10 -2
  39. package/dist/chunk-256KIVJL.js +85 -96
  40. package/dist/chunk-42XB3YCW.js +19 -17
  41. package/dist/chunk-6SM22VVJ.js +13 -10
  42. package/dist/chunk-CAKJT3KS.js +84 -69
  43. package/dist/chunk-E3OXBRYF.js +19 -17
  44. package/dist/chunk-EBPE35JT.js +28 -26
  45. package/dist/chunk-FIPU4MLT.js +18 -16
  46. package/dist/chunk-FJUDBLCP.js +50 -43
  47. package/dist/chunk-FKRDCWBF.js +19 -17
  48. package/dist/chunk-GZU3RBTB.js +51 -40
  49. package/dist/chunk-HGJ4TXY4.js +132 -100
  50. package/dist/chunk-HMRKN75I.js +63 -63
  51. package/dist/chunk-IKHPGFCW.js +11 -9
  52. package/dist/chunk-JAPMIE6S.js +7 -5
  53. package/dist/chunk-KRNOMBXQ.js +19 -17
  54. package/dist/chunk-LLR722CL.js +91 -70
  55. package/dist/chunk-RLT4RFKV.js +39 -30
  56. package/dist/chunk-SJPDVKUS.js +107 -88
  57. package/dist/chunk-SMQO5WD7.js +26 -16
  58. package/dist/chunk-STHEPPUZ.js +8 -6
  59. package/dist/chunk-UJJ7R56J.js +47 -37
  60. package/dist/chunk-UTDLUEEG.js +27 -21
  61. package/dist/chunk-VFTYH33W.js +54 -37
  62. package/dist/chunk-XXJKNKGQ.js +33 -23
  63. package/dist/chunk-ZV4BH47P.js +132 -134
  64. package/dist/cookie.cjs +169 -175
  65. package/dist/cookie.d.ts +23 -51
  66. package/dist/cookie.js +34 -34
  67. package/dist/error.cjs +75 -75
  68. package/dist/error.d.ts +30 -30
  69. package/dist/error.js +15 -8
  70. package/dist/headers.cjs +28 -28
  71. package/dist/headers.d.ts +2 -2
  72. package/dist/headers.js +6 -2
  73. package/dist/index-DpfbvTZ_.d.ts +249 -298
  74. package/dist/index.cjs +936 -969
  75. package/dist/index.d.ts +10 -31
  76. package/dist/index.js +70 -50
  77. package/dist/jose.cjs +61 -64
  78. package/dist/jose.d.ts +8 -8
  79. package/dist/jose.js +9 -5
  80. package/dist/oauth/bitbucket.cjs +38 -38
  81. package/dist/oauth/bitbucket.d.ts +7 -7
  82. package/dist/oauth/bitbucket.js +6 -2
  83. package/dist/oauth/discord.cjs +48 -48
  84. package/dist/oauth/discord.d.ts +7 -7
  85. package/dist/oauth/discord.js +6 -2
  86. package/dist/oauth/figma.cjs +39 -39
  87. package/dist/oauth/figma.d.ts +7 -7
  88. package/dist/oauth/figma.js +6 -2
  89. package/dist/oauth/github.cjs +31 -31
  90. package/dist/oauth/github.d.ts +7 -7
  91. package/dist/oauth/github.js +6 -2
  92. package/dist/oauth/gitlab.cjs +39 -39
  93. package/dist/oauth/gitlab.d.ts +7 -7
  94. package/dist/oauth/gitlab.js +6 -2
  95. package/dist/oauth/index.cjs +180 -180
  96. package/dist/oauth/index.d.ts +7 -26
  97. package/dist/oauth/index.js +36 -9
  98. package/dist/oauth/spotify.cjs +39 -39
  99. package/dist/oauth/spotify.d.ts +7 -7
  100. package/dist/oauth/spotify.js +6 -2
  101. package/dist/oauth/x.cjs +39 -39
  102. package/dist/oauth/x.d.ts +7 -7
  103. package/dist/oauth/x.js +6 -2
  104. package/dist/response.cjs +27 -27
  105. package/dist/response.d.ts +2 -2
  106. package/dist/response.js +6 -2
  107. package/dist/schemas.cjs +91 -91
  108. package/dist/schemas.d.ts +93 -126
  109. package/dist/schemas.js +18 -18
  110. package/dist/secure.cjs +95 -98
  111. package/dist/secure.d.ts +17 -17
  112. package/dist/secure.js +18 -4
  113. package/dist/utils.cjs +119 -132
  114. package/dist/utils.d.ts +11 -26
  115. package/dist/utils.js +21 -21
  116. package/package.json +2 -2
@@ -1,29 +1,31 @@
1
1
  // src/oauth/discord.ts
2
2
  var discord = {
3
- id: "discord",
4
- name: "Discord",
5
- authorizeURL: "https://discord.com/oauth2/authorize",
6
- accessToken: "https://discord.com/api/oauth2/token",
7
- userInfo: "https://discord.com/api/users/@me",
8
- scope: "identify email",
9
- responseType: "code",
10
- profile(profile) {
11
- let image = ""
12
- if (profile.avatar === null) {
13
- const index = profile.discriminator === "0" ? (BigInt(profile.id) >> 22n) % 6n : Number(profile.discriminator) % 5
14
- image = `https://cdn.discordapp.com/embed/avatars/${index}.png`
15
- } else {
16
- const format = profile.avatar.startsWith("a_") ? "gif" : "png"
17
- image = `https://cdn.discordapp.com/avatars/${profile.id}/${profile.avatar}.${format}`
18
- }
19
- return {
20
- sub: profile.id,
21
- // https://discord.com/developers/docs/change-log#display-names
22
- name: profile.global_name ?? profile.username,
23
- email: profile.email ?? "",
24
- image,
25
- }
26
- },
27
- }
3
+ id: "discord",
4
+ name: "Discord",
5
+ authorizeURL: "https://discord.com/oauth2/authorize",
6
+ accessToken: "https://discord.com/api/oauth2/token",
7
+ userInfo: "https://discord.com/api/users/@me",
8
+ scope: "identify email",
9
+ responseType: "code",
10
+ profile(profile) {
11
+ let image = "";
12
+ if (profile.avatar === null) {
13
+ const index = profile.discriminator === "0" ? (BigInt(profile.id) >> 22n) % 6n : Number(profile.discriminator) % 5;
14
+ image = `https://cdn.discordapp.com/embed/avatars/${index}.png`;
15
+ } else {
16
+ const format = profile.avatar.startsWith("a_") ? "gif" : "png";
17
+ image = `https://cdn.discordapp.com/avatars/${profile.id}/${profile.avatar}.${format}`;
18
+ }
19
+ return {
20
+ sub: profile.id,
21
+ // https://discord.com/developers/docs/change-log#display-names
22
+ name: profile.global_name ?? profile.username,
23
+ email: profile.email ?? "",
24
+ image
25
+ };
26
+ }
27
+ };
28
28
 
29
- export { discord }
29
+ export {
30
+ discord
31
+ };
@@ -1,19 +1,21 @@
1
1
  // src/oauth/bitbucket.ts
2
2
  var bitbucket = {
3
- id: "bitbucket",
4
- name: "Bitbucket",
5
- authorizeURL: "https://bitbucket.org/site/oauth2/authorize",
6
- accessToken: "https://bitbucket.org/site/oauth2/access_token",
7
- userInfo: "https://api.bitbucket.org/2.0/user",
8
- scope: "account email",
9
- responseType: "code",
10
- profile(profile) {
11
- return {
12
- sub: profile.uuid ?? profile.account_id,
13
- name: profile.display_name ?? profile.nickname,
14
- image: profile.links.avatar.href,
15
- }
16
- },
17
- }
3
+ id: "bitbucket",
4
+ name: "Bitbucket",
5
+ authorizeURL: "https://bitbucket.org/site/oauth2/authorize",
6
+ accessToken: "https://bitbucket.org/site/oauth2/access_token",
7
+ userInfo: "https://api.bitbucket.org/2.0/user",
8
+ scope: "account email",
9
+ responseType: "code",
10
+ profile(profile) {
11
+ return {
12
+ sub: profile.uuid ?? profile.account_id,
13
+ name: profile.display_name ?? profile.nickname,
14
+ image: profile.links.avatar.href
15
+ };
16
+ }
17
+ };
18
18
 
19
- export { bitbucket }
19
+ export {
20
+ bitbucket
21
+ };
@@ -1,52 +1,59 @@
1
1
  // src/error.ts
2
2
  var AuthError = class extends Error {
3
- constructor(type, message) {
4
- super(message)
5
- this.type = type
6
- this.name = "AuthError"
7
- }
8
- }
3
+ constructor(type, message) {
4
+ super(message);
5
+ this.type = type;
6
+ this.name = "AuthError";
7
+ }
8
+ };
9
9
  var InvalidCsrfTokenError = class extends AuthError {
10
- constructor(message = "The provided CSRF token is invalid or has expired") {
11
- super("invalid_csrf_token", message)
12
- this.name = "InvalidCsrfTokenError"
13
- }
14
- }
10
+ constructor(message = "The provided CSRF token is invalid or has expired") {
11
+ super("invalid_csrf_token", message);
12
+ this.name = "InvalidCsrfTokenError";
13
+ }
14
+ };
15
15
  var InvalidRedirectToError = class extends AuthError {
16
- constructor(message = "The redirectTo parameter does not match the hosted origin.") {
17
- super("invalid_redirect_to", message)
18
- this.name = "InvalidRedirectToError"
19
- }
20
- }
16
+ constructor(message = "The redirectTo parameter does not match the hosted origin.") {
17
+ super("invalid_redirect_to", message);
18
+ this.name = "InvalidRedirectToError";
19
+ }
20
+ };
21
21
  var isAuthError = (error) => {
22
- return error instanceof AuthError
23
- }
22
+ return error instanceof AuthError;
23
+ };
24
24
  var throwAuthError = (error, message) => {
25
- if (error instanceof Error) {
26
- if (isAuthError(error)) {
27
- throw error
28
- }
29
- throw new AuthError("invalid_request", error.message ?? message)
25
+ if (error instanceof Error) {
26
+ if (isAuthError(error)) {
27
+ throw error;
30
28
  }
31
- }
29
+ throw new AuthError("invalid_request", error.message ?? message);
30
+ }
31
+ };
32
32
  var ERROR_RESPONSE = {
33
- AUTHORIZATION: {
34
- INVALID_REQUEST: "invalid_request",
35
- UNAUTHORIZED_CLIENT: "unauthorized_client",
36
- ACCESS_DENIED: "access_denied",
37
- UNSUPPORTED_RESPONSE_TYPE: "unsupported_response_type",
38
- INVALID_SCOPE: "invalid_scope",
39
- SERVER_ERROR: "server_error",
40
- TEMPORARILY_UNAVAILABLE: "temporarily_unavailable",
41
- },
42
- ACCESS_TOKEN: {
43
- INVALID_REQUEST: "invalid_request",
44
- INVALID_CLIENT: "invalid_client",
45
- INVALID_GRANT: "invalid_grant",
46
- UNAUTHORIZED_CLIENT: "unauthorized_client",
47
- UNSUPPORTED_GRANT_TYPE: "unsupported_grant_type",
48
- INVALID_SCOPE: "invalid_scope",
49
- },
50
- }
33
+ AUTHORIZATION: {
34
+ INVALID_REQUEST: "invalid_request",
35
+ UNAUTHORIZED_CLIENT: "unauthorized_client",
36
+ ACCESS_DENIED: "access_denied",
37
+ UNSUPPORTED_RESPONSE_TYPE: "unsupported_response_type",
38
+ INVALID_SCOPE: "invalid_scope",
39
+ SERVER_ERROR: "server_error",
40
+ TEMPORARILY_UNAVAILABLE: "temporarily_unavailable"
41
+ },
42
+ ACCESS_TOKEN: {
43
+ INVALID_REQUEST: "invalid_request",
44
+ INVALID_CLIENT: "invalid_client",
45
+ INVALID_GRANT: "invalid_grant",
46
+ UNAUTHORIZED_CLIENT: "unauthorized_client",
47
+ UNSUPPORTED_GRANT_TYPE: "unsupported_grant_type",
48
+ INVALID_SCOPE: "invalid_scope"
49
+ }
50
+ };
51
51
 
52
- export { AuthError, InvalidCsrfTokenError, InvalidRedirectToError, isAuthError, throwAuthError, ERROR_RESPONSE }
52
+ export {
53
+ AuthError,
54
+ InvalidCsrfTokenError,
55
+ InvalidRedirectToError,
56
+ isAuthError,
57
+ throwAuthError,
58
+ ERROR_RESPONSE
59
+ };
@@ -1,20 +1,22 @@
1
1
  // src/oauth/figma.ts
2
2
  var figma = {
3
- id: "figma",
4
- name: "Figma",
5
- authorizeURL: "https://www.figma.com/oauth",
6
- accessToken: "https://api.figma.com/v1/oauth/token",
7
- userInfo: "https://api.figma.com/v1/me",
8
- scope: "current_user:read",
9
- responseType: "code",
10
- profile(profile) {
11
- return {
12
- sub: profile.id,
13
- name: profile.handle,
14
- email: profile.email,
15
- image: profile.img_url,
16
- }
17
- },
18
- }
3
+ id: "figma",
4
+ name: "Figma",
5
+ authorizeURL: "https://www.figma.com/oauth",
6
+ accessToken: "https://api.figma.com/v1/oauth/token",
7
+ userInfo: "https://api.figma.com/v1/me",
8
+ scope: "current_user:read",
9
+ responseType: "code",
10
+ profile(profile) {
11
+ return {
12
+ sub: profile.id,
13
+ name: profile.handle,
14
+ email: profile.email,
15
+ image: profile.img_url
16
+ };
17
+ }
18
+ };
19
19
 
20
- export { figma }
20
+ export {
21
+ figma
22
+ };
@@ -1,51 +1,62 @@
1
- import { equals } from "./chunk-256KIVJL.js"
2
- import { InvalidCsrfTokenError } from "./chunk-FJUDBLCP.js"
1
+ import {
2
+ equals
3
+ } from "./chunk-256KIVJL.js";
4
+ import {
5
+ InvalidCsrfTokenError
6
+ } from "./chunk-FJUDBLCP.js";
3
7
 
4
8
  // src/secure.ts
5
- import crypto from "crypto"
9
+ import crypto from "crypto";
6
10
  var generateSecure = (length = 32) => {
7
- return crypto.randomBytes(length).toString("base64url")
8
- }
11
+ return crypto.randomBytes(length).toString("base64url");
12
+ };
9
13
  var createHash = (data, base = "hex") => {
10
- return crypto.createHash("sha256").update(data).digest().toString(base)
11
- }
14
+ return crypto.createHash("sha256").update(data).digest().toString(base);
15
+ };
12
16
  var createPKCE = async (verifier) => {
13
- const codeVerifier = verifier ?? generateSecure(86)
14
- const codeChallenge = createHash(codeVerifier, "base64url")
15
- return { codeVerifier, codeChallenge, method: "S256" }
16
- }
17
+ const codeVerifier = verifier ?? generateSecure(86);
18
+ const codeChallenge = createHash(codeVerifier, "base64url");
19
+ return { codeVerifier, codeChallenge, method: "S256" };
20
+ };
17
21
  var createCSRF = async (jose, csrfCookie) => {
18
- try {
19
- const token = generateSecure(32)
20
- if (csrfCookie) {
21
- await jose.verifyJWS(csrfCookie)
22
- return csrfCookie
23
- }
24
- return jose.signJWS({ token })
25
- } catch {
26
- const token = generateSecure(32)
27
- return jose.signJWS({ token })
22
+ try {
23
+ const token = generateSecure(32);
24
+ if (csrfCookie) {
25
+ await jose.verifyJWS(csrfCookie);
26
+ return csrfCookie;
28
27
  }
29
- }
28
+ return jose.signJWS({ token });
29
+ } catch {
30
+ const token = generateSecure(32);
31
+ return jose.signJWS({ token });
32
+ }
33
+ };
30
34
  var verifyCSRF = async (jose, cookie, header) => {
31
- try {
32
- const { token: cookieToken } = await jose.verifyJWS(cookie)
33
- const { token: headerToken } = await jose.verifyJWS(header)
34
- const cookieBuffer = Buffer.from(cookieToken)
35
- const headerBuffer = Buffer.from(headerToken)
36
- if (!equals(headerBuffer.length, cookieBuffer.length)) {
37
- throw new InvalidCsrfTokenError()
38
- }
39
- if (!crypto.timingSafeEqual(cookieBuffer, headerBuffer)) {
40
- throw new InvalidCsrfTokenError()
41
- }
42
- return true
43
- } catch {
44
- throw new InvalidCsrfTokenError()
35
+ try {
36
+ const { token: cookieToken } = await jose.verifyJWS(cookie);
37
+ const { token: headerToken } = await jose.verifyJWS(header);
38
+ const cookieBuffer = Buffer.from(cookieToken);
39
+ const headerBuffer = Buffer.from(headerToken);
40
+ if (!equals(headerBuffer.length, cookieBuffer.length)) {
41
+ throw new InvalidCsrfTokenError();
45
42
  }
46
- }
43
+ if (!crypto.timingSafeEqual(cookieBuffer, headerBuffer)) {
44
+ throw new InvalidCsrfTokenError();
45
+ }
46
+ return true;
47
+ } catch {
48
+ throw new InvalidCsrfTokenError();
49
+ }
50
+ };
47
51
  var createDerivedSalt = (secret) => {
48
- return crypto.createHash("sha256").update(secret).update("aura-auth-salt").digest("hex")
49
- }
52
+ return crypto.createHash("sha256").update(secret).update("aura-auth-salt").digest("hex");
53
+ };
50
54
 
51
- export { generateSecure, createHash, createPKCE, createCSRF, verifyCSRF, createDerivedSalt }
55
+ export {
56
+ generateSecure,
57
+ createHash,
58
+ createPKCE,
59
+ createCSRF,
60
+ verifyCSRF,
61
+ createDerivedSalt
62
+ };
@@ -1,105 +1,137 @@
1
- import { getUserInfo } from "./chunk-RLT4RFKV.js"
2
- import { createAccessToken } from "./chunk-UJJ7R56J.js"
3
- import { createSessionCookie, expireCookie, getCookie, secureCookieOptions, setCookie } from "./chunk-ZV4BH47P.js"
4
- import { cacheControl } from "./chunk-STHEPPUZ.js"
5
- import { createCSRF } from "./chunk-GZU3RBTB.js"
6
- import { equals, isValidRelativePath, sanitizeURL } from "./chunk-256KIVJL.js"
7
- import { AuthError, ERROR_RESPONSE, isAuthError } from "./chunk-FJUDBLCP.js"
8
- import { AuraResponse } from "./chunk-JAPMIE6S.js"
9
- import { OAuthAuthorizationErrorResponse, OAuthAuthorizationResponse } from "./chunk-HMRKN75I.js"
1
+ import {
2
+ getUserInfo
3
+ } from "./chunk-RLT4RFKV.js";
4
+ import {
5
+ createAccessToken
6
+ } from "./chunk-UJJ7R56J.js";
7
+ import {
8
+ createSessionCookie,
9
+ expireCookie,
10
+ getCookie,
11
+ secureCookieOptions,
12
+ setCookie
13
+ } from "./chunk-ZV4BH47P.js";
14
+ import {
15
+ cacheControl
16
+ } from "./chunk-STHEPPUZ.js";
17
+ import {
18
+ createCSRF
19
+ } from "./chunk-GZU3RBTB.js";
20
+ import {
21
+ equals,
22
+ isValidRelativePath,
23
+ sanitizeURL
24
+ } from "./chunk-256KIVJL.js";
25
+ import {
26
+ AuthError,
27
+ ERROR_RESPONSE,
28
+ isAuthError
29
+ } from "./chunk-FJUDBLCP.js";
30
+ import {
31
+ AuraResponse
32
+ } from "./chunk-JAPMIE6S.js";
33
+ import {
34
+ OAuthAuthorizationErrorResponse,
35
+ OAuthAuthorizationResponse
36
+ } from "./chunk-HMRKN75I.js";
10
37
 
11
38
  // src/actions/callback/callback.ts
12
- import z from "zod"
13
- import { createEndpoint, createEndpointConfig, statusCode } from "@aura-stack/router"
39
+ import z from "zod";
40
+ import { createEndpoint, createEndpointConfig, statusCode } from "@aura-stack/router";
14
41
  var callbackConfig = (oauth) => {
15
- return createEndpointConfig("/callback/:oauth", {
16
- schemas: {
17
- searchParams: OAuthAuthorizationResponse,
18
- params: z.object({
19
- oauth: z.enum(Object.keys(oauth)),
20
- }),
21
- },
22
- middlewares: [
23
- (ctx) => {
24
- const response = OAuthAuthorizationErrorResponse.safeParse(ctx.searchParams)
25
- if (response.success) {
26
- const { error, error_description } = response.data
27
- throw new AuthError(error, error_description ?? "OAuth Authorization Error")
28
- }
29
- return ctx
30
- },
31
- ],
32
- })
33
- }
42
+ return createEndpointConfig("/callback/:oauth", {
43
+ schemas: {
44
+ searchParams: OAuthAuthorizationResponse,
45
+ params: z.object({
46
+ oauth: z.enum(Object.keys(oauth))
47
+ })
48
+ },
49
+ middlewares: [
50
+ (ctx) => {
51
+ const response = OAuthAuthorizationErrorResponse.safeParse(ctx.searchParams);
52
+ if (response.success) {
53
+ const { error, error_description } = response.data;
54
+ throw new AuthError(error, error_description ?? "OAuth Authorization Error");
55
+ }
56
+ return ctx;
57
+ }
58
+ ]
59
+ });
60
+ };
34
61
  var callbackAction = (oauth) => {
35
- return createEndpoint(
36
- "GET",
37
- "/callback/:oauth",
38
- async (ctx) => {
39
- const {
40
- request,
41
- params: { oauth: oauth2 },
42
- searchParams: { code, state },
43
- context: { oauth: providers, cookies, jose, trustedProxyHeaders },
44
- } = ctx
45
- try {
46
- const oauthConfig = providers[oauth2]
47
- const cookieOptions = secureCookieOptions(request, cookies, trustedProxyHeaders)
48
- const cookieState = getCookie(request, "state", cookieOptions)
49
- const cookieRedirectTo = getCookie(request, "redirect_to", cookieOptions)
50
- const cookieRedirectURI = getCookie(request, "redirect_uri", cookieOptions)
51
- const codeVerifier = getCookie(request, "code_verifier", cookieOptions)
52
- if (!equals(cookieState, state)) {
53
- throw new AuthError(ERROR_RESPONSE.ACCESS_TOKEN.INVALID_REQUEST, "Mismatching state")
54
- }
55
- const accessToken = await createAccessToken(oauthConfig, cookieRedirectURI, code, codeVerifier)
56
- const sanitized = sanitizeURL(cookieRedirectTo)
57
- if (!isValidRelativePath(sanitized)) {
58
- throw new AuthError(
59
- ERROR_RESPONSE.ACCESS_TOKEN.INVALID_REQUEST,
60
- "Invalid redirect path. Potential open redirect attack detected."
61
- )
62
- }
63
- const headers = new Headers(cacheControl)
64
- headers.set("Location", sanitized)
65
- const userInfo = await getUserInfo(oauthConfig, accessToken.access_token)
66
- const sessionCookie = await createSessionCookie(userInfo, cookieOptions, jose)
67
- const csrfToken = await createCSRF(jose)
68
- const csrfCookie = setCookie(
69
- "csrfToken",
70
- csrfToken,
71
- secureCookieOptions(
72
- request,
73
- {
74
- ...cookies,
75
- strategy: "host",
76
- },
77
- trustedProxyHeaders
78
- )
79
- )
80
- headers.set("Set-Cookie", sessionCookie)
81
- headers.append("Set-Cookie", expireCookie("state", cookieOptions))
82
- headers.append("Set-Cookie", expireCookie("redirect_uri", cookieOptions))
83
- headers.append("Set-Cookie", expireCookie("redirect_to", cookieOptions))
84
- headers.append("Set-Cookie", expireCookie("code_verifier", cookieOptions))
85
- headers.append("Set-Cookie", csrfCookie)
86
- return Response.json({ oauth: oauth2 }, { status: 302, headers })
87
- } catch (error) {
88
- if (isAuthError(error)) {
89
- const { type, message } = error
90
- return AuraResponse.json({ error: type, error_description: message }, { status: statusCode.BAD_REQUEST })
91
- }
92
- return AuraResponse.json(
93
- {
94
- error: ERROR_RESPONSE.ACCESS_TOKEN.INVALID_CLIENT,
95
- error_description: "An unexpected error occurred",
96
- },
97
- { status: statusCode.INTERNAL_SERVER_ERROR }
98
- )
99
- }
100
- },
101
- callbackConfig(oauth)
102
- )
103
- }
62
+ return createEndpoint(
63
+ "GET",
64
+ "/callback/:oauth",
65
+ async (ctx) => {
66
+ const {
67
+ request,
68
+ params: { oauth: oauth2 },
69
+ searchParams: { code, state },
70
+ context: { oauth: providers, cookies, jose, trustedProxyHeaders }
71
+ } = ctx;
72
+ try {
73
+ const oauthConfig = providers[oauth2];
74
+ const cookieOptions = secureCookieOptions(request, cookies, trustedProxyHeaders);
75
+ const cookieState = getCookie(request, "state", cookieOptions);
76
+ const cookieRedirectTo = getCookie(request, "redirect_to", cookieOptions);
77
+ const cookieRedirectURI = getCookie(request, "redirect_uri", cookieOptions);
78
+ const codeVerifier = getCookie(request, "code_verifier", cookieOptions);
79
+ if (!equals(cookieState, state)) {
80
+ throw new AuthError(ERROR_RESPONSE.ACCESS_TOKEN.INVALID_REQUEST, "Mismatching state");
81
+ }
82
+ const accessToken = await createAccessToken(oauthConfig, cookieRedirectURI, code, codeVerifier);
83
+ const sanitized = sanitizeURL(cookieRedirectTo);
84
+ if (!isValidRelativePath(sanitized)) {
85
+ throw new AuthError(
86
+ ERROR_RESPONSE.ACCESS_TOKEN.INVALID_REQUEST,
87
+ "Invalid redirect path. Potential open redirect attack detected."
88
+ );
89
+ }
90
+ const headers = new Headers(cacheControl);
91
+ headers.set("Location", sanitized);
92
+ const userInfo = await getUserInfo(oauthConfig, accessToken.access_token);
93
+ const sessionCookie = await createSessionCookie(userInfo, cookieOptions, jose);
94
+ const csrfToken = await createCSRF(jose);
95
+ const csrfCookie = setCookie(
96
+ "csrfToken",
97
+ csrfToken,
98
+ secureCookieOptions(
99
+ request,
100
+ {
101
+ ...cookies,
102
+ strategy: "host"
103
+ },
104
+ trustedProxyHeaders
105
+ )
106
+ );
107
+ headers.set("Set-Cookie", sessionCookie);
108
+ headers.append("Set-Cookie", expireCookie("state", cookieOptions));
109
+ headers.append("Set-Cookie", expireCookie("redirect_uri", cookieOptions));
110
+ headers.append("Set-Cookie", expireCookie("redirect_to", cookieOptions));
111
+ headers.append("Set-Cookie", expireCookie("code_verifier", cookieOptions));
112
+ headers.append("Set-Cookie", csrfCookie);
113
+ return Response.json({ oauth: oauth2 }, { status: 302, headers });
114
+ } catch (error) {
115
+ if (isAuthError(error)) {
116
+ const { type, message } = error;
117
+ return AuraResponse.json(
118
+ { error: type, error_description: message },
119
+ { status: statusCode.BAD_REQUEST }
120
+ );
121
+ }
122
+ return AuraResponse.json(
123
+ {
124
+ error: ERROR_RESPONSE.ACCESS_TOKEN.INVALID_CLIENT,
125
+ error_description: "An unexpected error occurred"
126
+ },
127
+ { status: statusCode.INTERNAL_SERVER_ERROR }
128
+ );
129
+ }
130
+ },
131
+ callbackConfig(oauth)
132
+ );
133
+ };
104
134
 
105
- export { callbackAction }
135
+ export {
136
+ callbackAction
137
+ };