@aura-stack/auth 0.1.0 → 0.2.0-rc.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.
Files changed (111) hide show
  1. package/dist/@types/index.d.ts +5 -5
  2. package/dist/@types/router.d.d.ts +5 -5
  3. package/dist/@types/utility.d.ts +1 -5
  4. package/dist/actions/callback/access-token.cjs +95 -74
  5. package/dist/actions/callback/access-token.d.ts +7 -7
  6. package/dist/actions/callback/access-token.js +5 -3
  7. package/dist/actions/callback/callback.cjs +171 -249
  8. package/dist/actions/callback/callback.d.ts +6 -6
  9. package/dist/actions/callback/callback.js +10 -10
  10. package/dist/actions/callback/userinfo.cjs +81 -57
  11. package/dist/actions/callback/userinfo.d.ts +5 -5
  12. package/dist/actions/callback/userinfo.js +7 -5
  13. package/dist/actions/csrfToken/csrfToken.cjs +36 -105
  14. package/dist/actions/csrfToken/csrfToken.js +6 -6
  15. package/dist/actions/index.cjs +309 -444
  16. package/dist/actions/index.d.ts +5 -5
  17. package/dist/actions/index.js +19 -19
  18. package/dist/actions/session/session.cjs +25 -109
  19. package/dist/actions/session/session.js +4 -5
  20. package/dist/actions/signIn/authorization.cjs +96 -87
  21. package/dist/actions/signIn/authorization.d.ts +5 -5
  22. package/dist/actions/signIn/authorization.js +5 -5
  23. package/dist/actions/signIn/signIn.cjs +133 -242
  24. package/dist/actions/signIn/signIn.d.ts +6 -6
  25. package/dist/actions/signIn/signIn.js +8 -9
  26. package/dist/actions/signOut/signOut.cjs +136 -282
  27. package/dist/actions/signOut/signOut.js +8 -9
  28. package/dist/assert.cjs +5 -0
  29. package/dist/assert.d.ts +10 -1
  30. package/dist/assert.js +3 -1
  31. package/dist/chunk-3EUWD5BB.js +63 -0
  32. package/dist/chunk-6R2YZ4AC.js +22 -0
  33. package/dist/chunk-A3N4PVAT.js +70 -0
  34. package/dist/chunk-B737EUJV.js +22 -0
  35. package/dist/{chunk-256KIVJL.js → chunk-CXLATHS5.js} +53 -9
  36. package/dist/{chunk-6SM22VVJ.js → chunk-EIL2FPSS.js} +5 -1
  37. package/dist/{chunk-VFTYH33W.js → chunk-EMKJA2GJ.js} +36 -8
  38. package/dist/{chunk-UJJ7R56J.js → chunk-GA2SMTJO.js} +16 -10
  39. package/dist/chunk-HP34YGGJ.js +22 -0
  40. package/dist/chunk-HT4YLL7N.js +35 -0
  41. package/dist/{chunk-EBPE35JT.js → chunk-IUYZQTJV.js} +0 -1
  42. package/dist/{chunk-RLT4RFKV.js → chunk-IVET23KF.js} +21 -8
  43. package/dist/{chunk-XXJKNKGQ.js → chunk-JVFTCTTE.js} +9 -13
  44. package/dist/chunk-KSWLO5ZU.js +102 -0
  45. package/dist/{chunk-GZU3RBTB.js → chunk-N2APGLXA.js} +19 -10
  46. package/dist/{chunk-CAKJT3KS.js → chunk-N4SX7TZT.js} +21 -17
  47. package/dist/chunk-RRLIF4PQ.js +55 -0
  48. package/dist/chunk-TLE4PXY3.js +39 -0
  49. package/dist/chunk-W6LG7BFW.js +197 -0
  50. package/dist/{chunk-HMRKN75I.js → chunk-YRCB5FLE.js} +14 -9
  51. package/dist/chunk-ZNCZVF6U.js +14 -0
  52. package/dist/cookie.cjs +140 -99
  53. package/dist/cookie.d.ts +35 -44
  54. package/dist/cookie.js +10 -17
  55. package/dist/errors.cjs +85 -0
  56. package/dist/errors.d.ts +49 -0
  57. package/dist/errors.js +18 -0
  58. package/dist/{index-DpfbvTZ_.d.ts → index-DkaLJFn8.d.ts} +192 -61
  59. package/dist/index.cjs +543 -443
  60. package/dist/index.d.ts +6 -6
  61. package/dist/index.js +42 -28
  62. package/dist/jose.cjs +25 -14
  63. package/dist/jose.d.ts +4 -1
  64. package/dist/jose.js +5 -4
  65. package/dist/oauth/bitbucket.d.ts +5 -5
  66. package/dist/oauth/discord.cjs +0 -1
  67. package/dist/oauth/discord.d.ts +5 -5
  68. package/dist/oauth/discord.js +1 -1
  69. package/dist/oauth/figma.d.ts +5 -5
  70. package/dist/oauth/github.d.ts +5 -5
  71. package/dist/oauth/gitlab.d.ts +5 -5
  72. package/dist/oauth/index.cjs +176 -6
  73. package/dist/oauth/index.d.ts +5 -5
  74. package/dist/oauth/index.js +19 -4
  75. package/dist/oauth/mailchimp.cjs +46 -0
  76. package/dist/oauth/mailchimp.d.ts +7 -0
  77. package/dist/oauth/mailchimp.js +6 -0
  78. package/dist/oauth/pinterest.cjs +46 -0
  79. package/dist/oauth/pinterest.d.ts +7 -0
  80. package/dist/oauth/pinterest.js +6 -0
  81. package/dist/oauth/spotify.d.ts +5 -5
  82. package/dist/oauth/strava.cjs +46 -0
  83. package/dist/oauth/strava.d.ts +7 -0
  84. package/dist/oauth/strava.js +6 -0
  85. package/dist/oauth/x.d.ts +5 -5
  86. package/dist/{response.cjs → request.cjs} +14 -10
  87. package/dist/request.d.ts +13 -0
  88. package/dist/request.js +6 -0
  89. package/dist/schemas.cjs +43 -37
  90. package/dist/schemas.d.ts +67 -64
  91. package/dist/schemas.js +3 -1
  92. package/dist/secure.cjs +32 -24
  93. package/dist/secure.d.ts +5 -5
  94. package/dist/secure.js +4 -3
  95. package/dist/utils.cjs +90 -15
  96. package/dist/utils.d.ts +11 -2
  97. package/dist/utils.js +8 -4
  98. package/package.json +8 -7
  99. package/dist/chunk-FJUDBLCP.js +0 -59
  100. package/dist/chunk-HGJ4TXY4.js +0 -137
  101. package/dist/chunk-JAPMIE6S.js +0 -10
  102. package/dist/chunk-LLR722CL.js +0 -96
  103. package/dist/chunk-SJPDVKUS.js +0 -112
  104. package/dist/chunk-SMQO5WD7.js +0 -30
  105. package/dist/chunk-UTDLUEEG.js +0 -31
  106. package/dist/chunk-ZV4BH47P.js +0 -154
  107. package/dist/error.cjs +0 -88
  108. package/dist/error.d.ts +0 -62
  109. package/dist/error.js +0 -16
  110. package/dist/response.d.ts +0 -10
  111. package/dist/response.js +0 -6
@@ -0,0 +1,63 @@
1
+ import {
2
+ createAuthorizationURL,
3
+ createRedirectTo,
4
+ createRedirectURI
5
+ } from "./chunk-N4SX7TZT.js";
6
+ import {
7
+ cacheControl
8
+ } from "./chunk-STHEPPUZ.js";
9
+ import {
10
+ createPKCE,
11
+ generateSecure
12
+ } from "./chunk-N2APGLXA.js";
13
+
14
+ // src/actions/signIn/signIn.ts
15
+ import { z } from "zod";
16
+ import { createEndpoint, createEndpointConfig, HeadersBuilder } from "@aura-stack/router";
17
+ var signInConfig = (oauth) => {
18
+ return createEndpointConfig("/signIn/:oauth", {
19
+ schemas: {
20
+ params: z.object({
21
+ oauth: z.enum(
22
+ Object.keys(oauth),
23
+ "The OAuth provider is not supported or invalid."
24
+ )
25
+ }),
26
+ searchParams: z.object({
27
+ redirectTo: z.string().optional()
28
+ })
29
+ }
30
+ });
31
+ };
32
+ var signInAction = (oauth) => {
33
+ return createEndpoint(
34
+ "GET",
35
+ "/signIn/:oauth",
36
+ async (ctx) => {
37
+ const {
38
+ request,
39
+ params: { oauth: oauth2 },
40
+ searchParams: { redirectTo },
41
+ context: { oauth: providers, cookies, trustedProxyHeaders, basePath }
42
+ } = ctx;
43
+ const state = generateSecure();
44
+ const redirectURI = createRedirectURI(request, oauth2, basePath, trustedProxyHeaders);
45
+ const redirectToValue = createRedirectTo(request, redirectTo, trustedProxyHeaders);
46
+ const { codeVerifier, codeChallenge, method } = await createPKCE();
47
+ const authorization = createAuthorizationURL(providers[oauth2], redirectURI, state, codeChallenge, method);
48
+ const headers = new HeadersBuilder(cacheControl).setHeader("Location", authorization).setCookie(cookies.state.name, state, cookies.state.attributes).setCookie(cookies.redirectURI.name, redirectURI, cookies.redirectURI.attributes).setCookie(cookies.redirectTo.name, redirectToValue, cookies.redirectTo.attributes).setCookie(cookies.codeVerifier.name, codeVerifier, cookies.codeVerifier.attributes).toHeaders();
49
+ return Response.json(
50
+ { oauth: oauth2 },
51
+ {
52
+ status: 302,
53
+ headers
54
+ }
55
+ );
56
+ },
57
+ signInConfig(oauth)
58
+ );
59
+ };
60
+
61
+ export {
62
+ signInAction
63
+ };
@@ -0,0 +1,22 @@
1
+ // src/oauth/strava.ts
2
+ var strava = {
3
+ id: "strava",
4
+ name: "Strava",
5
+ authorizeURL: "https://www.strava.com/oauth/authorize",
6
+ accessToken: "https://www.strava.com/oauth/token",
7
+ userInfo: "https://www.strava.com/api/v3/athlete",
8
+ scope: "read",
9
+ responseType: "code",
10
+ profile(profile) {
11
+ return {
12
+ sub: profile.id.toString(),
13
+ name: `${profile.firstname} ${profile.lastname}`,
14
+ image: profile.profile,
15
+ email: ""
16
+ };
17
+ }
18
+ };
19
+
20
+ export {
21
+ strava
22
+ };
@@ -0,0 +1,70 @@
1
+ import {
2
+ createRedirectTo
3
+ } from "./chunk-N4SX7TZT.js";
4
+ import {
5
+ expiredCookieAttributes
6
+ } from "./chunk-W6LG7BFW.js";
7
+ import {
8
+ cacheControl
9
+ } from "./chunk-STHEPPUZ.js";
10
+ import {
11
+ verifyCSRF
12
+ } from "./chunk-N2APGLXA.js";
13
+ import {
14
+ getNormalizedOriginPath
15
+ } from "./chunk-CXLATHS5.js";
16
+ import {
17
+ AuthSecurityError
18
+ } from "./chunk-RRLIF4PQ.js";
19
+
20
+ // src/actions/signOut/signOut.ts
21
+ import { z } from "zod";
22
+ import { createEndpoint, createEndpointConfig, HeadersBuilder, statusCode } from "@aura-stack/router";
23
+ var config = createEndpointConfig({
24
+ schemas: {
25
+ searchParams: z.object({
26
+ token_type_hint: z.literal("session_token"),
27
+ redirectTo: z.string().optional()
28
+ })
29
+ }
30
+ });
31
+ var signOutAction = createEndpoint(
32
+ "POST",
33
+ "/signOut",
34
+ async (ctx) => {
35
+ const {
36
+ request,
37
+ headers,
38
+ searchParams: { redirectTo },
39
+ context: { jose, cookies }
40
+ } = ctx;
41
+ const session = headers.getCookie(cookies.sessionToken.name);
42
+ const csrfToken = headers.getCookie(cookies.csrfToken.name);
43
+ const header = headers.getHeader("X-CSRF-Token");
44
+ if (!session) {
45
+ throw new AuthSecurityError("SESSION_TOKEN_MISSING", "The sessionToken is missing.");
46
+ }
47
+ if (!csrfToken) {
48
+ throw new AuthSecurityError("CSRF_TOKEN_MISSING", "The CSRF token is missing.");
49
+ }
50
+ if (!header) {
51
+ throw new AuthSecurityError("CSRF_TOKEN_MISSING", "The CSRF header is missing.");
52
+ }
53
+ await verifyCSRF(jose, csrfToken, header);
54
+ await jose.decodeJWT(session);
55
+ const normalizedOriginPath = getNormalizedOriginPath(request.url);
56
+ const location = createRedirectTo(
57
+ new Request(normalizedOriginPath, {
58
+ headers: headers.toHeaders()
59
+ }),
60
+ redirectTo
61
+ );
62
+ const headersList = new HeadersBuilder(cacheControl).setHeader("Location", location).setCookie(cookies.csrfToken.name, "", expiredCookieAttributes).setCookie(cookies.sessionToken.name, "", expiredCookieAttributes).toHeaders();
63
+ return Response.json({ message: "Signed out successfully" }, { status: statusCode.ACCEPTED, headers: headersList });
64
+ },
65
+ config
66
+ );
67
+
68
+ export {
69
+ signOutAction
70
+ };
@@ -0,0 +1,22 @@
1
+ // src/oauth/mailchimp.ts
2
+ var mailchimp = {
3
+ id: "mailchimp",
4
+ name: "Mailchimp",
5
+ authorizeURL: "https://login.mailchimp.com/oauth2/authorize",
6
+ accessToken: "https://login.mailchimp.com/oauth2/token",
7
+ userInfo: "https://login.mailchimp.com/oauth2/metadata",
8
+ scope: "",
9
+ responseType: "code",
10
+ profile(profile) {
11
+ return {
12
+ sub: profile.user_id,
13
+ name: profile.accountname,
14
+ email: profile.login.login_email,
15
+ image: null
16
+ };
17
+ }
18
+ };
19
+
20
+ export {
21
+ mailchimp
22
+ };
@@ -1,9 +1,11 @@
1
1
  import {
2
- isAuthError
3
- } from "./chunk-FJUDBLCP.js";
2
+ isAuthInternalError,
3
+ isAuthSecurityError,
4
+ isOAuthProtocolError
5
+ } from "./chunk-RRLIF4PQ.js";
4
6
 
5
7
  // src/utils.ts
6
- import { isRouterError } from "@aura-stack/router";
8
+ import { isInvalidZodSchemaError, isRouterError } from "@aura-stack/router";
7
9
  var toSnakeCase = (str) => {
8
10
  return str.replace(/([a-z0-9])([A-Z])/g, "$1_$2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1_$2").toLowerCase().replace(/^_+/, "");
9
11
  };
@@ -64,13 +66,35 @@ var isValidRelativePath = (path) => {
64
66
  var onErrorHandler = (error) => {
65
67
  if (isRouterError(error)) {
66
68
  const { message, status, statusText } = error;
67
- return Response.json({ error: "invalid_request", error_description: message }, { status, statusText });
69
+ return Response.json({ type: "ROUTER_ERROR", code: "ROUTER_INTERNAL_ERROR", message }, { status, statusText });
68
70
  }
69
- if (isAuthError(error)) {
70
- const { type, message } = error;
71
- return Response.json({ error: type, error_description: message }, { status: 400 });
71
+ if (isInvalidZodSchemaError(error)) {
72
+ return Response.json({ type: "ROUTER_ERROR", code: "INVALID_REQUEST", message: error.errors }, { status: 422 });
72
73
  }
73
- return Response.json({ error: "server_error", error_description: "An unexpected error occurred" }, { status: 500 });
74
+ if (isOAuthProtocolError(error)) {
75
+ const { error: errorCode, message, type, errorURI } = error;
76
+ return Response.json(
77
+ {
78
+ type,
79
+ error: errorCode,
80
+ error_description: message,
81
+ error_uri: errorURI
82
+ },
83
+ { status: 400 }
84
+ );
85
+ }
86
+ if (isAuthInternalError(error) || isAuthSecurityError(error)) {
87
+ const { type, code, message } = error;
88
+ return Response.json(
89
+ {
90
+ type,
91
+ code,
92
+ message
93
+ },
94
+ { status: 400 }
95
+ );
96
+ }
97
+ return Response.json({ type: "SERVER_ERROR", code: "server_error", message: "An unexpected error occurred" }, { status: 500 });
74
98
  };
75
99
  var getNormalizedOriginPath = (path) => {
76
100
  try {
@@ -85,6 +109,24 @@ var getNormalizedOriginPath = (path) => {
85
109
  var toISOString = (date) => {
86
110
  return new Date(date).toISOString();
87
111
  };
112
+ var useSecureCookies = (request, trustedProxyHeaders) => {
113
+ return trustedProxyHeaders ? request.url.startsWith("https://") || request.headers.get("X-Forwarded-Proto") === "https" || (request.headers.get("Forwarded")?.includes("proto=https") ?? false) : request.url.startsWith("https://");
114
+ };
115
+ var formatZodError = (error) => {
116
+ if (!error.issues || error.issues.length === 0) {
117
+ return {};
118
+ }
119
+ return error.issues.reduce((previous, issue) => {
120
+ const key = issue.path.join(".");
121
+ return {
122
+ ...previous,
123
+ [key]: {
124
+ code: issue.code,
125
+ message: issue.message
126
+ }
127
+ };
128
+ }, {});
129
+ };
88
130
 
89
131
  export {
90
132
  toSnakeCase,
@@ -95,5 +137,7 @@ export {
95
137
  isValidRelativePath,
96
138
  onErrorHandler,
97
139
  getNormalizedOriginPath,
98
- toISOString
140
+ toISOString,
141
+ useSecureCookies,
142
+ formatZodError
99
143
  };
@@ -10,9 +10,13 @@ var isValidURL = (value) => {
10
10
  const regex = /^https?:\/\/(?:[a-zA-Z0-9._-]+|localhost|\[[0-9a-fA-F:]+\])(?::\d{1,5})?(?:\/[a-zA-Z0-9._~!$&'()*+,;=:@-]*)*\/?$/;
11
11
  return regex.test(value);
12
12
  };
13
+ var isJWTPayloadWithToken = (payload) => {
14
+ return typeof payload === "object" && payload !== null && "token" in payload && typeof payload?.token === "string";
15
+ };
13
16
 
14
17
  export {
15
18
  isFalsy,
16
19
  isRequest,
17
- isValidURL
20
+ isValidURL,
21
+ isJWTPayloadWithToken
18
22
  };
@@ -1,3 +1,6 @@
1
+ import {
2
+ x
3
+ } from "./chunk-42XB3YCW.js";
1
4
  import {
2
5
  figma
3
6
  } from "./chunk-FKRDCWBF.js";
@@ -7,18 +10,33 @@ import {
7
10
  import {
8
11
  gitlab
9
12
  } from "./chunk-KRNOMBXQ.js";
13
+ import {
14
+ mailchimp
15
+ } from "./chunk-B737EUJV.js";
16
+ import {
17
+ pinterest
18
+ } from "./chunk-HP34YGGJ.js";
10
19
  import {
11
20
  spotify
12
21
  } from "./chunk-E3OXBRYF.js";
13
22
  import {
14
- x
15
- } from "./chunk-42XB3YCW.js";
23
+ strava
24
+ } from "./chunk-6R2YZ4AC.js";
16
25
  import {
17
26
  bitbucket
18
27
  } from "./chunk-FIPU4MLT.js";
19
28
  import {
20
29
  discord
21
- } from "./chunk-EBPE35JT.js";
30
+ } from "./chunk-IUYZQTJV.js";
31
+ import {
32
+ formatZodError
33
+ } from "./chunk-CXLATHS5.js";
34
+ import {
35
+ AuthInternalError
36
+ } from "./chunk-RRLIF4PQ.js";
37
+ import {
38
+ OAuthEnvSchema
39
+ } from "./chunk-YRCB5FLE.js";
22
40
 
23
41
  // src/oauth/index.ts
24
42
  var builtInOAuthProviders = {
@@ -28,14 +46,24 @@ var builtInOAuthProviders = {
28
46
  discord,
29
47
  gitlab,
30
48
  spotify,
31
- x
49
+ x,
50
+ strava,
51
+ mailchimp,
52
+ pinterest
32
53
  };
33
54
  var defineOAuthEnvironment = (oauth) => {
34
55
  const env = process.env;
35
- return {
36
- clientId: env[`AURA_AUTH_${oauth.toUpperCase()}_CLIENT_ID`],
37
- clientSecret: env[`AURA_AUTH_${oauth.toUpperCase()}_CLIENT_SECRET`]
38
- };
56
+ const clientIdSuffix = `${oauth.toUpperCase()}_CLIENT_ID`;
57
+ const clientSecretSuffix = `${oauth.toUpperCase()}_CLIENT_SECRET`;
58
+ const loadEnvs = OAuthEnvSchema.safeParse({
59
+ clientId: env[`AURA_AUTH_${clientIdSuffix}`] ?? env[`AUTH_${clientIdSuffix}`] ?? env[`${clientIdSuffix}`],
60
+ clientSecret: env[`AURA_AUTH_${clientSecretSuffix}`] ?? env[`AUTH_${clientSecretSuffix}`] ?? env[`${clientSecretSuffix}`]
61
+ });
62
+ if (!loadEnvs.success) {
63
+ const msg = JSON.stringify(formatZodError(loadEnvs.error), null, 2);
64
+ throw new AuthInternalError("INVALID_ENVIRONMENT_CONFIGURATION", msg);
65
+ }
66
+ return loadEnvs.data;
39
67
  };
40
68
  var defineOAuthProviderConfig = (config) => {
41
69
  if (typeof config === "string") {
@@ -1,23 +1,29 @@
1
1
  import {
2
- AuthError,
3
- ERROR_RESPONSE,
4
- throwAuthError
5
- } from "./chunk-FJUDBLCP.js";
2
+ formatZodError
3
+ } from "./chunk-CXLATHS5.js";
4
+ import {
5
+ AuthInternalError,
6
+ OAuthProtocolError
7
+ } from "./chunk-RRLIF4PQ.js";
8
+ import {
9
+ fetchAsync
10
+ } from "./chunk-ZNCZVF6U.js";
6
11
  import {
7
12
  OAuthAccessToken,
8
13
  OAuthAccessTokenErrorResponse,
9
14
  OAuthAccessTokenResponse
10
- } from "./chunk-HMRKN75I.js";
15
+ } from "./chunk-YRCB5FLE.js";
11
16
 
12
17
  // src/actions/callback/access-token.ts
13
18
  var createAccessToken = async (oauthConfig, redirectURI, code, codeVerifier) => {
14
19
  const parsed = OAuthAccessToken.safeParse({ ...oauthConfig, redirectURI, code, codeVerifier });
15
20
  if (!parsed.success) {
16
- throw new AuthError(ERROR_RESPONSE.ACCESS_TOKEN.INVALID_REQUEST, "Invalid OAuth configuration");
21
+ const msg = JSON.stringify(formatZodError(parsed.error), null, 2);
22
+ throw new AuthInternalError("INVALID_OAUTH_CONFIGURATION", msg);
17
23
  }
18
24
  const { accessToken, clientId, clientSecret, code: codeParsed, redirectURI: redirectParsed } = parsed.data;
19
25
  try {
20
- const response = await fetch(accessToken, {
26
+ const response = await fetchAsync(accessToken, {
21
27
  method: "POST",
22
28
  headers: {
23
29
  Accept: "application/json",
@@ -37,13 +43,13 @@ var createAccessToken = async (oauthConfig, redirectURI, code, codeVerifier) =>
37
43
  if (!token.success) {
38
44
  const { success, data } = OAuthAccessTokenErrorResponse.safeParse(json);
39
45
  if (!success) {
40
- throw new AuthError(ERROR_RESPONSE.ACCESS_TOKEN.INVALID_GRANT, "Invalid access token response format");
46
+ throw new OAuthProtocolError("INVALID_REQUEST", "Invalid access token response format");
41
47
  }
42
- throw new AuthError(data.error, data?.error_description ?? "Failed to retrieve access token");
48
+ throw new OAuthProtocolError(data.error, data?.error_description ?? "Failed to retrieve access token");
43
49
  }
44
50
  return token.data;
45
51
  } catch (error) {
46
- throw throwAuthError(error, "Failed to create access token");
52
+ throw error;
47
53
  }
48
54
  };
49
55
 
@@ -0,0 +1,22 @@
1
+ // src/oauth/pinterest.ts
2
+ var pinterest = {
3
+ id: "pinterest",
4
+ name: "Pinterest",
5
+ authorizeURL: "https://api.pinterest.com/oauth/",
6
+ accessToken: "https://api.pinterest.com/v5/oauth/token",
7
+ userInfo: "https://api.pinterest.com/v5/user_account",
8
+ scope: "user_accounts:read",
9
+ responseType: "code",
10
+ profile(profile) {
11
+ return {
12
+ sub: profile.id,
13
+ name: profile.username,
14
+ email: null,
15
+ image: profile.profile_image
16
+ };
17
+ }
18
+ };
19
+
20
+ export {
21
+ pinterest
22
+ };
@@ -0,0 +1,35 @@
1
+ import {
2
+ getCookie,
3
+ setCookie
4
+ } from "./chunk-W6LG7BFW.js";
5
+ import {
6
+ cacheControl
7
+ } from "./chunk-STHEPPUZ.js";
8
+ import {
9
+ createCSRF
10
+ } from "./chunk-N2APGLXA.js";
11
+
12
+ // src/actions/csrfToken/csrfToken.ts
13
+ import { createEndpoint } from "@aura-stack/router";
14
+ var getCSRFToken = (request, cookieName) => {
15
+ try {
16
+ return getCookie(request, cookieName);
17
+ } catch {
18
+ return void 0;
19
+ }
20
+ };
21
+ var csrfTokenAction = createEndpoint("GET", "/csrfToken", async (ctx) => {
22
+ const {
23
+ request,
24
+ context: { jose, cookies }
25
+ } = ctx;
26
+ const token = getCSRFToken(request, cookies.csrfToken.name);
27
+ const csrfToken = await createCSRF(jose, token);
28
+ const headers = new Headers(cacheControl);
29
+ headers.append("Set-Cookie", setCookie(cookies.csrfToken.name, csrfToken, cookies.csrfToken.attributes));
30
+ return Response.json({ csrfToken }, { headers });
31
+ });
32
+
33
+ export {
34
+ csrfTokenAction
35
+ };
@@ -18,7 +18,6 @@ var discord = {
18
18
  }
19
19
  return {
20
20
  sub: profile.id,
21
- // https://discord.com/developers/docs/change-log#display-names
22
21
  name: profile.global_name ?? profile.username,
23
22
  email: profile.email ?? "",
24
23
  image
@@ -1,13 +1,17 @@
1
1
  import {
2
2
  generateSecure
3
- } from "./chunk-GZU3RBTB.js";
3
+ } from "./chunk-N2APGLXA.js";
4
4
  import {
5
- AuthError,
6
- throwAuthError
7
- } from "./chunk-FJUDBLCP.js";
5
+ OAuthProtocolError,
6
+ isNativeError,
7
+ isOAuthProtocolError
8
+ } from "./chunk-RRLIF4PQ.js";
9
+ import {
10
+ fetchAsync
11
+ } from "./chunk-ZNCZVF6U.js";
8
12
  import {
9
13
  OAuthErrorResponse
10
- } from "./chunk-HMRKN75I.js";
14
+ } from "./chunk-YRCB5FLE.js";
11
15
 
12
16
  // src/actions/callback/userinfo.ts
13
17
  var getDefaultUserInfo = (profile) => {
@@ -22,7 +26,7 @@ var getDefaultUserInfo = (profile) => {
22
26
  var getUserInfo = async (oauthConfig, accessToken) => {
23
27
  const userinfoEndpoint = oauthConfig.userInfo;
24
28
  try {
25
- const response = await fetch(userinfoEndpoint, {
29
+ const response = await fetchAsync(userinfoEndpoint, {
26
30
  method: "GET",
27
31
  headers: {
28
32
  Accept: "application/json",
@@ -32,11 +36,20 @@ var getUserInfo = async (oauthConfig, accessToken) => {
32
36
  const json = await response.json();
33
37
  const { success, data } = OAuthErrorResponse.safeParse(json);
34
38
  if (success) {
35
- throw new AuthError(data.error, data?.error_description ?? "An error occurred while fetching user information.");
39
+ throw new OAuthProtocolError(
40
+ data.error,
41
+ data?.error_description ?? "An error occurred while fetching user information."
42
+ );
36
43
  }
37
44
  return oauthConfig?.profile ? oauthConfig.profile(json) : getDefaultUserInfo(json);
38
45
  } catch (error) {
39
- throw throwAuthError(error, "Failed to retrieve userinfo");
46
+ if (isOAuthProtocolError(error)) {
47
+ throw error;
48
+ }
49
+ if (isNativeError(error)) {
50
+ throw new OAuthProtocolError("invalid_request", error.message, "", { cause: error });
51
+ }
52
+ throw new OAuthProtocolError("invalid_request", "Failed to fetch user information.", "", { cause: error });
40
53
  }
41
54
  };
42
55
 
@@ -1,33 +1,29 @@
1
1
  import {
2
- expireCookie,
3
- getCookie,
4
- secureCookieOptions
5
- } from "./chunk-ZV4BH47P.js";
2
+ expiredCookieAttributes,
3
+ getCookie
4
+ } from "./chunk-W6LG7BFW.js";
6
5
  import {
7
6
  cacheControl
8
7
  } from "./chunk-STHEPPUZ.js";
9
8
  import {
10
9
  toISOString
11
- } from "./chunk-256KIVJL.js";
10
+ } from "./chunk-CXLATHS5.js";
12
11
 
13
12
  // src/actions/session/session.ts
14
- import { createEndpoint } from "@aura-stack/router";
13
+ import { createEndpoint, HeadersBuilder } from "@aura-stack/router";
15
14
  var sessionAction = createEndpoint("GET", "/session", async (ctx) => {
16
15
  const {
17
16
  request,
18
- context: { cookies, jose, trustedProxyHeaders }
17
+ context: { jose, cookies }
19
18
  } = ctx;
20
- const cookieOptions = secureCookieOptions(request, cookies, trustedProxyHeaders);
21
19
  try {
22
- const session = getCookie(request, "sessionToken", cookieOptions);
20
+ const session = getCookie(request, cookies.sessionToken.name);
23
21
  const decoded = await jose.decodeJWT(session);
24
22
  const { exp, iat, jti, nbf, ...user } = decoded;
25
23
  const headers = new Headers(cacheControl);
26
24
  return Response.json({ user, expires: toISOString(exp * 1e3) }, { headers });
27
- } catch {
28
- const headers = new Headers(cacheControl);
29
- const sessionCookie = expireCookie("sessionToken", cookieOptions);
30
- headers.set("Set-Cookie", sessionCookie);
25
+ } catch (error) {
26
+ const headers = new HeadersBuilder(cacheControl).setCookie(cookies.sessionToken.name, "", expiredCookieAttributes).toHeaders();
31
27
  return Response.json({ authenticated: false, message: "Unauthorized" }, { status: 401, headers });
32
28
  }
33
29
  });
@@ -0,0 +1,102 @@
1
+ import {
2
+ createAccessToken
3
+ } from "./chunk-GA2SMTJO.js";
4
+ import {
5
+ getUserInfo
6
+ } from "./chunk-IVET23KF.js";
7
+ import {
8
+ createSessionCookie,
9
+ expiredCookieAttributes,
10
+ getCookie
11
+ } from "./chunk-W6LG7BFW.js";
12
+ import {
13
+ cacheControl
14
+ } from "./chunk-STHEPPUZ.js";
15
+ import {
16
+ createCSRF
17
+ } from "./chunk-N2APGLXA.js";
18
+ import {
19
+ equals,
20
+ isValidRelativePath,
21
+ sanitizeURL
22
+ } from "./chunk-CXLATHS5.js";
23
+ import {
24
+ AuthSecurityError,
25
+ OAuthProtocolError
26
+ } from "./chunk-RRLIF4PQ.js";
27
+ import {
28
+ OAuthAuthorizationErrorResponse
29
+ } from "./chunk-YRCB5FLE.js";
30
+
31
+ // src/actions/callback/callback.ts
32
+ import { z } from "zod";
33
+ import { createEndpoint, createEndpointConfig, HeadersBuilder } from "@aura-stack/router";
34
+ var callbackConfig = (oauth) => {
35
+ return createEndpointConfig("/callback/:oauth", {
36
+ schemas: {
37
+ params: z.object({
38
+ oauth: z.enum(
39
+ Object.keys(oauth),
40
+ "The OAuth provider is not supported or invalid."
41
+ )
42
+ }),
43
+ searchParams: z.object({
44
+ code: z.string("Missing code parameter in the OAuth authorization response."),
45
+ state: z.string("Missing state parameter in the OAuth authorization response.")
46
+ })
47
+ },
48
+ middlewares: [
49
+ (ctx) => {
50
+ const response = OAuthAuthorizationErrorResponse.safeParse(ctx.searchParams);
51
+ if (response.success) {
52
+ const { error, error_description } = response.data;
53
+ throw new OAuthProtocolError(error, error_description ?? "OAuth Authorization Error");
54
+ }
55
+ return ctx;
56
+ }
57
+ ]
58
+ });
59
+ };
60
+ var callbackAction = (oauth) => {
61
+ return createEndpoint(
62
+ "GET",
63
+ "/callback/:oauth",
64
+ async (ctx) => {
65
+ const {
66
+ request,
67
+ params: { oauth: oauth2 },
68
+ searchParams: { code, state },
69
+ context: { oauth: providers, cookies, jose }
70
+ } = ctx;
71
+ const oauthConfig = providers[oauth2];
72
+ const cookieState = getCookie(request, cookies.state.name);
73
+ const cookieRedirectTo = getCookie(request, cookies.redirectTo.name);
74
+ const cookieRedirectURI = getCookie(request, cookies.redirectURI.name);
75
+ const codeVerifier = getCookie(request, cookies.codeVerifier.name);
76
+ if (!equals(cookieState, state)) {
77
+ throw new AuthSecurityError(
78
+ "MISMATCHING_STATE",
79
+ "The provided state passed in the OAuth response does not match the stored state."
80
+ );
81
+ }
82
+ const accessToken = await createAccessToken(oauthConfig, cookieRedirectURI, code, codeVerifier);
83
+ const sanitized = sanitizeURL(cookieRedirectTo);
84
+ if (!isValidRelativePath(sanitized)) {
85
+ throw new AuthSecurityError(
86
+ "POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED",
87
+ "Invalid redirect path. Potential open redirect attack detected."
88
+ );
89
+ }
90
+ const userInfo = await getUserInfo(oauthConfig, accessToken.access_token);
91
+ const sessionCookie = await createSessionCookie(jose, userInfo);
92
+ const csrfToken = await createCSRF(jose);
93
+ const headers = new HeadersBuilder(cacheControl).setHeader("Location", sanitized).setCookie(cookies.sessionToken.name, sessionCookie, cookies.sessionToken.attributes).setCookie(cookies.csrfToken.name, csrfToken, cookies.csrfToken.attributes).setCookie(cookies.state.name, "", expiredCookieAttributes).setCookie(cookies.redirectURI.name, "", expiredCookieAttributes).setCookie(cookies.redirectTo.name, "", expiredCookieAttributes).setCookie(cookies.codeVerifier.name, "", expiredCookieAttributes).toHeaders();
94
+ return Response.json({ oauth: oauth2 }, { status: 302, headers });
95
+ },
96
+ callbackConfig(oauth)
97
+ );
98
+ };
99
+
100
+ export {
101
+ callbackAction
102
+ };