@aura-stack/auth 0.4.0 → 0.5.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 (149) hide show
  1. package/dist/@types/index.d.ts +6 -2
  2. package/dist/@types/router.d.d.ts +6 -2
  3. package/dist/actions/callback/access-token.cjs +103 -59
  4. package/dist/actions/callback/access-token.d.ts +7 -3
  5. package/dist/actions/callback/access-token.js +3 -3
  6. package/dist/actions/callback/callback.cjs +200 -134
  7. package/dist/actions/callback/callback.d.ts +32 -3
  8. package/dist/actions/callback/callback.js +11 -12
  9. package/dist/actions/callback/userinfo.cjs +103 -70
  10. package/dist/actions/callback/userinfo.d.ts +6 -2
  11. package/dist/actions/callback/userinfo.js +7 -8
  12. package/dist/actions/csrfToken/csrfToken.cjs +7 -15
  13. package/dist/actions/csrfToken/csrfToken.d.ts +3 -1
  14. package/dist/actions/csrfToken/csrfToken.js +7 -8
  15. package/dist/actions/index.cjs +502 -295
  16. package/dist/actions/index.d.ts +5 -2
  17. package/dist/actions/index.js +23 -20
  18. package/dist/actions/session/session.cjs +76 -24
  19. package/dist/actions/session/session.d.ts +3 -1
  20. package/dist/actions/session/session.js +6 -4
  21. package/dist/actions/signIn/authorization-url.cjs +288 -0
  22. package/dist/actions/signIn/authorization-url.d.ts +31 -0
  23. package/dist/actions/signIn/authorization-url.js +16 -0
  24. package/dist/actions/signIn/authorization.cjs +91 -132
  25. package/dist/actions/signIn/authorization.d.ts +17 -16
  26. package/dist/actions/signIn/authorization.js +8 -7
  27. package/dist/actions/signIn/signIn.cjs +319 -191
  28. package/dist/actions/signIn/signIn.d.ts +32 -3
  29. package/dist/actions/signIn/signIn.js +10 -9
  30. package/dist/actions/signOut/signOut.cjs +211 -212
  31. package/dist/actions/signOut/signOut.d.ts +9 -1
  32. package/dist/actions/signOut/signOut.js +9 -10
  33. package/dist/api/createApi.cjs +750 -0
  34. package/dist/api/createApi.d.ts +12 -0
  35. package/dist/api/createApi.js +19 -0
  36. package/dist/api/getSession.cjs +141 -0
  37. package/dist/api/getSession.d.ts +16 -0
  38. package/dist/api/getSession.js +10 -0
  39. package/dist/api/signIn.cjs +549 -0
  40. package/dist/api/signIn.d.ts +26 -0
  41. package/dist/api/signIn.js +15 -0
  42. package/dist/api/signOut.cjs +279 -0
  43. package/dist/api/signOut.d.ts +16 -0
  44. package/dist/api/signOut.js +13 -0
  45. package/dist/assert.cjs +42 -9
  46. package/dist/assert.d.ts +8 -4
  47. package/dist/assert.js +5 -5
  48. package/dist/{chunk-KJBAQZX2.js → chunk-2A5B7GWR.js} +44 -11
  49. package/dist/chunk-2GQLSIJ2.js +40 -0
  50. package/dist/chunk-2IR674WX.js +44 -0
  51. package/dist/chunk-3J5TUH2I.js +50 -0
  52. package/dist/chunk-4RWSYUKX.js +98 -0
  53. package/dist/chunk-5X7JZMEF.js +0 -0
  54. package/dist/{chunk-TZB6MUXN.js → chunk-7BE46WWS.js} +21 -11
  55. package/dist/chunk-7YYXFKLR.js +35 -0
  56. package/dist/chunk-C3A37LQC.js +33 -0
  57. package/dist/chunk-CITNGXDA.js +31 -0
  58. package/dist/chunk-CWX724AG.js +78 -0
  59. package/dist/chunk-D2CSIUKP.js +74 -0
  60. package/dist/{chunk-ICAZ4OVS.js → chunk-FPCVZUVG.js} +2 -2
  61. package/dist/{chunk-XGLBNXL4.js → chunk-GNNBM2WJ.js} +17 -9
  62. package/dist/chunk-JOCGX3RP.js +59 -0
  63. package/dist/chunk-KBXWTD6E.js +94 -0
  64. package/dist/{chunk-XUP6KKNG.js → chunk-LATR3NIV.js} +48 -37
  65. package/dist/chunk-LAYPUDQF.js +39 -0
  66. package/dist/chunk-LX3TJ2TJ.js +294 -0
  67. package/dist/{chunk-6MXFPFR3.js → chunk-NHZBQNRR.js} +19 -19
  68. package/dist/{chunk-TM5IPSNF.js → chunk-PDP3PHB3.js} +33 -19
  69. package/dist/chunk-PHYNROD4.js +47 -0
  70. package/dist/chunk-QQEKY4XP.js +29 -0
  71. package/dist/{chunk-VNCNJKS2.js → chunk-U4RK4LKJ.js} +82 -1
  72. package/dist/{chunk-RRLIF4PQ.js → chunk-U5663F2U.js} +16 -1
  73. package/dist/chunk-UN7X6SU5.js +53 -0
  74. package/dist/chunk-UZQJJD6A.js +100 -0
  75. package/dist/{chunk-NUDITUKX.js → chunk-V6LLEAR4.js} +22 -15
  76. package/dist/{chunk-4MYWAOLG.js → chunk-WHNDRO3N.js} +20 -1
  77. package/dist/{chunk-5W4BRQYG.js → chunk-XY5R3EHH.js} +6 -3
  78. package/dist/client/client.cjs +135 -0
  79. package/dist/client/client.d.ts +85 -0
  80. package/dist/client/client.js +9 -0
  81. package/dist/client/index.cjs +135 -0
  82. package/dist/client/index.d.ts +14 -0
  83. package/dist/client/index.js +10 -0
  84. package/dist/context.cjs +1237 -0
  85. package/dist/context.d.ts +16 -0
  86. package/dist/context.js +28 -0
  87. package/dist/cookie.cjs +33 -2
  88. package/dist/cookie.d.ts +9 -5
  89. package/dist/cookie.js +3 -2
  90. package/dist/createAuth.cjs +2320 -0
  91. package/dist/createAuth.d.ts +12 -0
  92. package/dist/createAuth.js +48 -0
  93. package/dist/env.cjs +24 -2
  94. package/dist/env.d.ts +4 -1
  95. package/dist/env.js +9 -3
  96. package/dist/errors.cjs +17 -0
  97. package/dist/errors.d.ts +13 -3
  98. package/dist/errors.js +5 -1
  99. package/dist/{index-CSyIJmCM.d.ts → index-_aXtxb_s.d.ts} +383 -13
  100. package/dist/index.cjs +2135 -1547
  101. package/dist/index.d.ts +9 -30
  102. package/dist/index.js +46 -119
  103. package/dist/jose.cjs +52 -14
  104. package/dist/jose.d.ts +12 -25
  105. package/dist/jose.js +11 -3
  106. package/dist/logger.cjs +132 -0
  107. package/dist/logger.d.ts +6 -2
  108. package/dist/logger.js +10 -1
  109. package/dist/oauth/atlassian.cjs +57 -0
  110. package/dist/oauth/atlassian.d.ts +12 -0
  111. package/dist/oauth/atlassian.js +6 -0
  112. package/dist/oauth/bitbucket.d.ts +6 -2
  113. package/dist/oauth/discord.d.ts +6 -2
  114. package/dist/oauth/dropbox.cjs +53 -0
  115. package/dist/oauth/dropbox.d.ts +12 -0
  116. package/dist/oauth/dropbox.js +6 -0
  117. package/dist/oauth/figma.d.ts +6 -2
  118. package/dist/oauth/github.d.ts +6 -2
  119. package/dist/oauth/gitlab.d.ts +6 -2
  120. package/dist/oauth/index.cjs +278 -88
  121. package/dist/oauth/index.d.ts +6 -2
  122. package/dist/oauth/index.js +27 -11
  123. package/dist/oauth/mailchimp.d.ts +6 -2
  124. package/dist/oauth/notion.cjs +131 -0
  125. package/dist/oauth/notion.d.ts +12 -0
  126. package/dist/oauth/notion.js +9 -0
  127. package/dist/oauth/pinterest.d.ts +6 -2
  128. package/dist/oauth/spotify.d.ts +6 -2
  129. package/dist/oauth/strava.d.ts +6 -2
  130. package/dist/oauth/twitch.cjs +95 -0
  131. package/dist/oauth/twitch.d.ts +12 -0
  132. package/dist/oauth/twitch.js +7 -0
  133. package/dist/oauth/x.d.ts +6 -2
  134. package/dist/schemas.cjs +84 -51
  135. package/dist/schemas.d.ts +103 -23
  136. package/dist/schemas.js +1 -1
  137. package/dist/secure.cjs +36 -36
  138. package/dist/secure.d.ts +10 -4
  139. package/dist/secure.js +7 -6
  140. package/dist/utils.cjs +109 -3
  141. package/dist/utils.d.ts +15 -4
  142. package/dist/utils.js +11 -4
  143. package/package.json +9 -5
  144. package/dist/chunk-4EKY7655.js +0 -123
  145. package/dist/chunk-7QF22LHP.js +0 -67
  146. package/dist/chunk-ALG3GIV4.js +0 -95
  147. package/dist/chunk-FRJFWTOY.js +0 -70
  148. package/dist/chunk-PHFH2MGS.js +0 -36
  149. package/dist/chunk-QQVSRXGX.js +0 -149
package/dist/index.cjs CHANGED
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,23 +15,21 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
 
30
20
  // src/index.ts
31
21
  var index_exports = {};
32
22
  __export(index_exports, {
33
- createAuth: () => createAuth
23
+ builtInOAuthProviders: () => builtInOAuthProviders,
24
+ createAuth: () => createAuth,
25
+ createAuthClient: () => createAuthClient,
26
+ createClient: () => createClient,
27
+ createSyslogMessage: () => createSyslogMessage
34
28
  });
35
29
  module.exports = __toCommonJS(index_exports);
36
- var import_router7 = require("@aura-stack/router");
30
+
31
+ // src/createAuth.ts
32
+ var import_router9 = require("@aura-stack/router");
37
33
 
38
34
  // src/env.ts
39
35
  var import_meta = {};
@@ -63,6 +59,22 @@ var env = new Proxy({}, {
63
59
  }
64
60
  }
65
61
  });
62
+ var getEnv = (key) => {
63
+ const keys = [`AURA_AUTH_${key.toUpperCase()}`, `AURA_${key.toUpperCase()}`, `AUTH_${key.toUpperCase()}`, key.toUpperCase()];
64
+ return env[keys.find((k) => env[k]) ?? ""];
65
+ };
66
+ var getEnvBoolean = (key) => {
67
+ const value = getEnv(key);
68
+ if (value === void 0) return false;
69
+ const normalized = value.trim().toLowerCase();
70
+ if (["1", "true", "yes", "on", "debug"].includes(normalized)) return true;
71
+ return false;
72
+ };
73
+ var getEnvArray = (key, defaultValue = []) => {
74
+ const value = getEnv(key);
75
+ if (!value) return defaultValue;
76
+ return value.split(/[,;\n]+/).map((v) => v.trim()).filter(Boolean);
77
+ };
66
78
 
67
79
  // src/jose.ts
68
80
  var import_jose = require("@aura-stack/jose");
@@ -100,6 +112,16 @@ var AuthSecurityError = class extends Error {
100
112
  Error.captureStackTrace(this, new.target);
101
113
  }
102
114
  };
115
+ var AuthClientError = class extends Error {
116
+ type = "AUTH_CLIENT_ERROR";
117
+ code;
118
+ constructor(code, message, options2) {
119
+ super(message, options2);
120
+ this.code = code;
121
+ this.name = new.target.name;
122
+ Error.captureStackTrace(this, new.target);
123
+ }
124
+ };
103
125
  var isNativeError = (error) => {
104
126
  return error instanceof Error;
105
127
  };
@@ -114,15 +136,17 @@ var isAuthSecurityError = (error) => {
114
136
  };
115
137
 
116
138
  // src/jose.ts
139
+ var import_jose2 = require("@aura-stack/jose/jose");
140
+ var import_crypto = require("@aura-stack/jose/crypto");
117
141
  var createJoseInstance = (secret) => {
118
- secret ??= env.AURA_AUTH_SECRET ?? env.AUTH_SECRET;
142
+ secret ??= getEnv("SECRET");
119
143
  if (!secret) {
120
144
  throw new AuthInternalError(
121
145
  "JOSE_INITIALIZATION_FAILED",
122
146
  "AURA_AUTH_SECRET environment variable is not set and no secret was provided."
123
147
  );
124
148
  }
125
- const salt = env.AURA_AUTH_SALT ?? env.AUTH_SALT;
149
+ const salt = getEnv("SALT");
126
150
  if (!salt) {
127
151
  throw new AuthInternalError(
128
152
  "JOSE_INITIALIZATION_FAILED",
@@ -138,19 +162,43 @@ var createJoseInstance = (secret) => {
138
162
  { cause: error }
139
163
  );
140
164
  }
141
- const { derivedKey: derivedSigningKey } = (0, import_jose.createDeriveKey)(secret, salt, "signing");
142
- const { derivedKey: derivedEncryptionKey } = (0, import_jose.createDeriveKey)(secret, salt, "encryption");
143
- const { derivedKey: derivedCsrfTokenKey } = (0, import_jose.createDeriveKey)(secret, salt, "csrfToken");
144
- const { decodeJWT, encodeJWT } = (0, import_jose.createJWT)({ jws: derivedSigningKey, jwe: derivedEncryptionKey });
145
- const { signJWS, verifyJWS } = (0, import_jose.createJWS)(derivedCsrfTokenKey);
146
- const { encryptJWE, decryptJWE } = (0, import_jose.createJWE)(derivedEncryptionKey);
165
+ const jose = (async () => {
166
+ const derivedSigningKey = await (0, import_jose.createDeriveKey)(secret, salt, "signing");
167
+ const derivedEncryptionKey = await (0, import_jose.createDeriveKey)(secret, salt, "encryption");
168
+ const derivedCsrfTokenKey = await (0, import_jose.createDeriveKey)(secret, salt, "csrfToken");
169
+ return {
170
+ jwt: (0, import_jose.createJWT)({ jws: derivedSigningKey, jwe: derivedEncryptionKey }),
171
+ jws: (0, import_jose.createJWS)(derivedCsrfTokenKey),
172
+ jwe: (0, import_jose.createJWE)(derivedEncryptionKey)
173
+ };
174
+ })();
175
+ jose.catch(() => {
176
+ });
147
177
  return {
148
- decodeJWT,
149
- encodeJWT,
150
- signJWS,
151
- verifyJWS,
152
- encryptJWE,
153
- decryptJWE
178
+ decodeJWT: async (token, options2) => {
179
+ const { jwt } = await jose;
180
+ return jwt.decodeJWT(token, options2);
181
+ },
182
+ encodeJWT: async (payload) => {
183
+ const { jwt } = await jose;
184
+ return jwt.encodeJWT(payload);
185
+ },
186
+ signJWS: async (...args) => {
187
+ const { jws } = await jose;
188
+ return jws.signJWS(...args);
189
+ },
190
+ verifyJWS: async (...args) => {
191
+ const { jws } = await jose;
192
+ return jws.verifyJWS(...args);
193
+ },
194
+ encryptJWE: async (...args) => {
195
+ const { jwe } = await jose;
196
+ return jwe.encryptJWE(...args);
197
+ },
198
+ decryptJWE: async (...args) => {
199
+ const { jwe } = await jose;
200
+ return jwe.decryptJWE(...args);
201
+ }
154
202
  };
155
203
  };
156
204
  var jwtVerificationOptions = {
@@ -158,236 +206,158 @@ var jwtVerificationOptions = {
158
206
  typ: "JWT"
159
207
  };
160
208
 
161
- // src/cookie.ts
162
- var import_cookie = require("@aura-stack/router/cookie");
163
- var COOKIE_NAME = "aura-auth";
164
- var defaultCookieOptions = {
165
- httpOnly: true,
166
- sameSite: "lax",
167
- path: "/",
168
- maxAge: 60 * 60 * 24 * 15
169
- };
170
- var defaultStandardCookieConfig = {
171
- secure: false,
172
- httpOnly: true
173
- };
174
- var defaultSecureCookieConfig = {
175
- secure: true,
176
- httpOnly: true
177
- };
178
- var defaultHostCookieConfig = {
179
- secure: true,
180
- httpOnly: true,
181
- path: "/",
182
- domain: void 0
209
+ // src/utils.ts
210
+ var import_router = require("@aura-stack/router");
211
+
212
+ // src/assert.ts
213
+ var import_crypto2 = require("@aura-stack/jose/crypto");
214
+ var unsafeChars = [
215
+ "<",
216
+ ">",
217
+ '"',
218
+ "`",
219
+ " ",
220
+ "\r",
221
+ "\n",
222
+ " ",
223
+ "\\",
224
+ "%2F",
225
+ "%5C",
226
+ "%2f",
227
+ "%5c",
228
+ "\r\n",
229
+ "%0A",
230
+ "%0D",
231
+ "%0a",
232
+ "%0d",
233
+ "..",
234
+ "//",
235
+ "///",
236
+ "...",
237
+ "%20",
238
+ "\0"
239
+ ];
240
+ var isValidURL = (value) => {
241
+ if (!new RegExp(/^https?:\/\/[^/]/).test(value)) {
242
+ return false;
243
+ }
244
+ const match = value.match(/^(https?:\/\/)(.*)$/);
245
+ if (!match) return false;
246
+ const rest = match[2];
247
+ for (const char of unsafeChars) {
248
+ if (rest.includes(char)) return false;
249
+ }
250
+ const regex = /^https?:\/\/(?:[a-zA-Z0-9._-]+|localhost|\[[0-9a-fA-F:]+\])(?::\d{1,5})?(?:\/[a-zA-Z0-9._~!$&'()?#*+,;=:@-]*)*\/?$/;
251
+ return regex.test(match[0]);
183
252
  };
184
- var oauthCookieOptions = {
185
- httpOnly: true,
186
- maxAge: 5 * 60,
187
- sameSite: "lax",
188
- expires: new Date(Date.now() + 5 * 60 * 1e3)
253
+ var isJWTPayloadWithToken = (payload) => {
254
+ return typeof payload === "object" && payload !== null && "token" in payload && typeof payload?.token === "string";
189
255
  };
190
- var setCookie = (cookieName, value, options2) => {
191
- return (0, import_cookie.serialize)(cookieName, value, options2);
256
+ var isRelativeURL = (value) => {
257
+ if (value.length > 100) return false;
258
+ for (const char of unsafeChars) {
259
+ if (value.includes(char)) return false;
260
+ }
261
+ const regex = /^\/[a-zA-Z0-9\-_\/.?&=#]*\/?$/;
262
+ return regex.test(value);
192
263
  };
193
- var expiredCookieAttributes = {
194
- ...defaultCookieOptions,
195
- expires: /* @__PURE__ */ new Date(0),
196
- maxAge: 0,
197
- secure: true
264
+ var isSameOrigin = (origin, expected) => {
265
+ const originURL = new URL(origin);
266
+ const expectedURL = new URL(expected);
267
+ return equals(originURL.origin, expectedURL.origin);
198
268
  };
199
- var getCookie = (request, cookieName) => {
200
- const cookies = request.headers.get("Cookie");
201
- if (!cookies) {
202
- throw new AuthInternalError("COOKIE_NOT_FOUND", "No cookies found. There is no active session");
203
- }
204
- const value = (0, import_cookie.parse)(cookies)[cookieName];
205
- if (!value) {
206
- throw new AuthInternalError("COOKIE_NOT_FOUND", `Cookie "${cookieName}" not found. There is no active session`);
269
+ var patternToRegex = (pattern) => {
270
+ try {
271
+ if (pattern.length > 2048) return null;
272
+ pattern = pattern.replace(/\\/g, "");
273
+ const match = pattern.match(/^(https?):\/\/([a-zA-Z0-9.*-]{1,253})(?::(\d{1,5}|\*))?(?:\/.*)?$/);
274
+ if (!match) return null;
275
+ const [, protocol, host, port] = match;
276
+ const hasWildcard = host.includes("*");
277
+ if (hasWildcard && !host.startsWith("*.")) return null;
278
+ if (hasWildcard && host.slice(2).includes("*")) return null;
279
+ const domain = hasWildcard ? host.slice(2) : host;
280
+ const escapedDomain = domain.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
281
+ const hostRegex = hasWildcard ? `[^.]+\\.${escapedDomain}` : escapedDomain;
282
+ const portRegex = port === "*" ? ":\\d{1,5}" : port ? `:${port}` : "";
283
+ return new RegExp(`^${protocol}:\\/\\/${hostRegex}${portRegex}$`);
284
+ } catch {
285
+ return null;
207
286
  }
208
- return value;
209
287
  };
210
- var createSessionCookie = async (jose, session) => {
288
+ var isTrustedOrigin = (url, trustedOrigins) => {
289
+ if (!isValidURL(url) || trustedOrigins.length === 0) return false;
211
290
  try {
212
- const encoded = await jose.encodeJWT(session);
213
- return encoded;
214
- } catch (error) {
215
- throw new AuthInternalError("INVALID_JWT_TOKEN", "Failed to create session cookie", { cause: error });
291
+ const urlOrigin = new URL(url).origin;
292
+ for (const pattern of trustedOrigins) {
293
+ const regex = patternToRegex(pattern);
294
+ if (regex?.test(urlOrigin)) return true;
295
+ try {
296
+ if (isValidURL(pattern) && equals(new URL(pattern).origin, urlOrigin)) return true;
297
+ } catch {
298
+ }
299
+ }
300
+ } catch {
216
301
  }
302
+ return false;
217
303
  };
218
- var defineSecureCookieOptions = (useSecure, attributes, strategy, logger) => {
219
- if (!attributes.httpOnly) {
220
- logger?.log("COOKIE_HTTPONLY_DISABLED");
221
- }
222
- if (attributes.domain === "*") {
223
- attributes.domain = void 0;
224
- logger?.log("COOKIE_WILDCARD_DOMAIN");
304
+ var timingSafeEqual = (a, b) => {
305
+ const bufferA = import_crypto2.encoder.encode(a);
306
+ const bufferB = import_crypto2.encoder.encode(b);
307
+ const len = Math.max(bufferA.length, bufferB.length);
308
+ let diff = 0;
309
+ for (let i = 0; i < len; i++) {
310
+ diff |= (bufferA[i] ?? 0) ^ (bufferB[i] ?? 0);
225
311
  }
226
- if (!useSecure) {
227
- if (attributes.secure) {
228
- logger?.log("COOKIE_SECURE_DISABLED");
229
- }
230
- if (attributes.sameSite == "none") {
231
- attributes.sameSite = "lax";
232
- logger?.log("COOKIE_SAMESITE_NONE_WITHOUT_SECURE");
233
- }
234
- if (process.env.NODE_ENV === "production") {
235
- logger?.log("COOKIE_INSECURE_IN_PRODUCTION");
312
+ return diff === 0 && bufferA.length === bufferB.length;
313
+ };
314
+
315
+ // src/utils.ts
316
+ var AURA_AUTH_VERSION = "0.4.0";
317
+ var equals = (a, b) => {
318
+ if (a === null || b === null || a === void 0 || b === void 0) return false;
319
+ return a === b;
320
+ };
321
+ var createErrorHandler = (logger) => {
322
+ return (error) => {
323
+ if ((0, import_router.isRouterError)(error)) {
324
+ const { message, status, statusText } = error;
325
+ logger?.log("ROUTER_INTERNAL_ERROR");
326
+ return Response.json({ type: "ROUTER_ERROR", code: "ROUTER_INTERNAL_ERROR", message }, { status, statusText });
236
327
  }
237
- if (strategy === "host") {
238
- logger?.log("COOKIE_HOST_STRATEGY_INSECURE");
328
+ if ((0, import_router.isInvalidZodSchemaError)(error)) {
329
+ logger?.log("INVALID_REQUEST");
330
+ return Response.json({ type: "ROUTER_ERROR", code: "INVALID_REQUEST", message: error.errors }, { status: 422 });
239
331
  }
240
- return {
241
- ...defaultCookieOptions,
242
- ...attributes,
243
- ...defaultStandardCookieConfig
244
- };
245
- }
246
- return strategy === "host" ? {
247
- ...defaultCookieOptions,
248
- ...attributes,
249
- ...defaultHostCookieConfig
250
- } : { ...defaultCookieOptions, ...attributes, ...defaultSecureCookieConfig };
251
- };
252
- var createCookieStore = (useSecure, prefix, overrides, logger) => {
253
- prefix ??= COOKIE_NAME;
254
- const securePrefix = useSecure ? "__Secure-" : "";
255
- const hostPrefix = useSecure ? "__Host-" : "";
256
- return {
257
- sessionToken: {
258
- name: `${securePrefix}${prefix}.${overrides?.sessionToken?.name ?? "session_token"}`,
259
- attributes: defineSecureCookieOptions(
260
- useSecure,
332
+ if (isOAuthProtocolError(error)) {
333
+ const { error: errorCode, message, type, errorURI } = error;
334
+ logger?.log("OAUTH_PROTOCOL_ERROR", {
335
+ structuredData: {
336
+ error: errorCode,
337
+ error_description: message,
338
+ error_uri: errorURI ?? ""
339
+ }
340
+ });
341
+ return Response.json(
261
342
  {
262
- ...defaultCookieOptions,
263
- ...overrides?.sessionToken?.attributes
343
+ type,
344
+ message
264
345
  },
265
- overrides?.sessionToken?.attributes?.strategy ?? "secure",
266
- logger
267
- )
268
- },
269
- state: {
270
- name: `${securePrefix}${prefix}.${overrides?.state?.name ?? "state"}`,
271
- attributes: defineSecureCookieOptions(
272
- useSecure,
346
+ { status: 400 }
347
+ );
348
+ }
349
+ if (isAuthInternalError(error)) {
350
+ const { type, code, message } = error;
351
+ logger?.log("INVALID_OAUTH_CONFIGURATION", {
352
+ structuredData: {
353
+ error: code,
354
+ error_description: message
355
+ }
356
+ });
357
+ return Response.json(
273
358
  {
274
- ...oauthCookieOptions,
275
- ...overrides?.state?.attributes
276
- },
277
- overrides?.state?.attributes?.strategy ?? "secure",
278
- logger
279
- )
280
- },
281
- csrfToken: {
282
- name: `${hostPrefix}${prefix}.${overrides?.csrfToken?.name ?? "csrf_token"}`,
283
- attributes: defineSecureCookieOptions(
284
- useSecure,
285
- {
286
- ...overrides?.csrfToken?.attributes,
287
- ...defaultHostCookieConfig,
288
- sameSite: "strict"
289
- },
290
- overrides?.csrfToken?.attributes?.strategy ?? "host",
291
- logger
292
- )
293
- },
294
- redirectTo: {
295
- name: `${securePrefix}${prefix}.${overrides?.redirectTo?.name ?? "redirect_to"}`,
296
- attributes: defineSecureCookieOptions(
297
- useSecure,
298
- {
299
- ...oauthCookieOptions,
300
- ...overrides?.redirectTo?.attributes
301
- },
302
- overrides?.redirectTo?.attributes?.strategy ?? "secure",
303
- logger
304
- )
305
- },
306
- redirectURI: {
307
- name: `${securePrefix}${prefix}.${overrides?.redirectURI?.name ?? "redirect_uri"}`,
308
- attributes: defineSecureCookieOptions(
309
- useSecure,
310
- {
311
- ...oauthCookieOptions,
312
- ...overrides?.redirectURI?.attributes
313
- },
314
- overrides?.redirectURI?.attributes?.strategy ?? "secure",
315
- logger
316
- )
317
- },
318
- codeVerifier: {
319
- name: `${securePrefix}${prefix}.${overrides?.codeVerifier?.name ?? "code_verifier"}`,
320
- attributes: defineSecureCookieOptions(
321
- useSecure,
322
- {
323
- ...oauthCookieOptions,
324
- ...overrides?.codeVerifier?.attributes
325
- },
326
- overrides?.codeVerifier?.attributes?.strategy ?? "secure",
327
- logger
328
- )
329
- }
330
- };
331
- };
332
-
333
- // src/utils.ts
334
- var import_router = require("@aura-stack/router");
335
- var toSnakeCase = (str) => {
336
- return str.replace(/([a-z0-9])([A-Z])/g, "$1_$2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1_$2").toLowerCase().replace(/^_+/, "");
337
- };
338
- var toUpperCase = (str) => {
339
- return str.toUpperCase();
340
- };
341
- var toCastCase = (obj, type = "snake") => {
342
- return Object.entries(obj).reduce((previous, [key, value]) => {
343
- const newKey = type === "snake" ? toSnakeCase(key) : toUpperCase(key);
344
- return { ...previous, [newKey]: value };
345
- }, {});
346
- };
347
- var equals = (a, b) => {
348
- if (a === null || b === null || a === void 0 || b === void 0) return false;
349
- return a === b;
350
- };
351
- var createErrorHandler = (logger) => {
352
- return (error) => {
353
- if ((0, import_router.isRouterError)(error)) {
354
- const { message, status, statusText } = error;
355
- logger?.log("ROUTER_INTERNAL_ERROR");
356
- return Response.json({ type: "ROUTER_ERROR", code: "ROUTER_INTERNAL_ERROR", message }, { status, statusText });
357
- }
358
- if ((0, import_router.isInvalidZodSchemaError)(error)) {
359
- logger?.log("INVALID_REQUEST");
360
- return Response.json({ type: "ROUTER_ERROR", code: "INVALID_REQUEST", message: error.errors }, { status: 422 });
361
- }
362
- if (isOAuthProtocolError(error)) {
363
- const { error: errorCode, message, type, errorURI } = error;
364
- logger?.log("OAUTH_PROTOCOL_ERROR", {
365
- structuredData: {
366
- error: errorCode,
367
- error_description: message,
368
- error_uri: errorURI ?? ""
369
- }
370
- });
371
- return Response.json(
372
- {
373
- type,
374
- message
375
- },
376
- { status: 400 }
377
- );
378
- }
379
- if (isAuthInternalError(error)) {
380
- const { type, code, message } = error;
381
- logger?.log("INVALID_OAUTH_CONFIGURATION", {
382
- structuredData: {
383
- error: code,
384
- error_description: message
385
- }
386
- });
387
- return Response.json(
388
- {
389
- type,
390
- message
359
+ type,
360
+ message
391
361
  },
392
362
  { status: 400 }
393
363
  );
@@ -424,7 +394,9 @@ var toISOString = (date) => {
424
394
  return new Date(date).toISOString();
425
395
  };
426
396
  var useSecureCookies = (request, trustedProxyHeaders) => {
427
- 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://");
397
+ const headers = request instanceof Headers ? request : request.headers;
398
+ const url = request instanceof Headers ? null : request.url;
399
+ return trustedProxyHeaders ? url?.startsWith("https://") || headers.get("X-Forwarded-Proto") === "https" || (headers.get("Forwarded")?.includes("proto=https") ?? false) : url?.startsWith("https://") ?? false;
428
400
  };
429
401
  var formatZodError = (error) => {
430
402
  if (!error.issues || error.issues.length === 0) {
@@ -446,1394 +418,2010 @@ var extractPath = (url) => {
446
418
  const match = url.match(pathRegex);
447
419
  return match && match[2] ? match[2] : "/";
448
420
  };
421
+ var createStructuredData = (data, sdID = "metadata") => {
422
+ const entries = Object.entries(data);
423
+ if (entries.length === 0) return `[${sdID}]`;
424
+ const values = entries.map(([key, value]) => `${key}="${String(value).replace(/(["\\\]])/g, "\\$1")}"`).join(" ");
425
+ return `[${sdID} ${values}]`;
426
+ };
449
427
  var getErrorName = (error) => {
450
428
  if (error instanceof Error) {
451
429
  return error.name;
452
430
  }
453
431
  return typeof error === "string" ? error : "UnknownError";
454
432
  };
455
-
456
- // src/oauth/github.ts
457
- var github = (options2) => {
458
- return {
459
- id: "github",
460
- name: "GitHub",
461
- authorizeURL: "https://github.com/login/oauth/authorize",
462
- accessToken: "https://github.com/login/oauth/access_token",
463
- userInfo: "https://api.github.com/user",
464
- scope: "read:user user:email",
465
- responseType: "code",
466
- profile: (profile) => {
467
- return {
468
- sub: profile.id.toString(),
469
- name: profile.name ?? profile.login,
470
- email: profile.email ?? void 0,
471
- image: profile.avatar_url
472
- };
473
- },
474
- ...options2
475
- };
476
- };
477
-
478
- // src/oauth/bitbucket.ts
479
- var bitbucket = (options2) => {
480
- return {
481
- id: "bitbucket",
482
- name: "Bitbucket",
483
- authorizeURL: "https://bitbucket.org/site/oauth2/authorize",
484
- accessToken: "https://bitbucket.org/site/oauth2/access_token",
485
- userInfo: "https://api.bitbucket.org/2.0/user",
486
- scope: "account email",
487
- responseType: "code",
488
- profile(profile) {
489
- return {
490
- sub: profile.uuid ?? profile.account_id,
491
- name: profile.display_name ?? profile.nickname,
492
- image: profile.links.avatar?.href,
493
- email: void 0
494
- };
495
- },
496
- ...options2
497
- };
498
- };
499
-
500
- // src/oauth/figma.ts
501
- var figma = (options2) => {
502
- return {
503
- id: "figma",
504
- name: "Figma",
505
- authorizeURL: "https://www.figma.com/oauth",
506
- accessToken: "https://api.figma.com/v1/oauth/token",
507
- userInfo: "https://api.figma.com/v1/me",
508
- scope: "current_user:read",
509
- responseType: "code",
510
- profile(profile) {
511
- return {
512
- sub: profile.id,
513
- name: profile.handle,
514
- email: profile.email,
515
- image: profile.img_url
516
- };
517
- },
518
- ...options2
519
- };
520
- };
521
-
522
- // src/oauth/discord.ts
523
- var discord = (options2) => {
524
- return {
525
- id: "discord",
526
- name: "Discord",
527
- authorizeURL: "https://discord.com/oauth2/authorize",
528
- accessToken: "https://discord.com/api/oauth2/token",
529
- userInfo: "https://discord.com/api/users/@me",
530
- scope: "identify email",
531
- responseType: "code",
532
- profile(profile) {
533
- let image = "";
534
- if (profile.avatar === null) {
535
- const index = profile.discriminator === "0" ? (BigInt(profile.id) >> 22n) % 6n : Number(profile.discriminator) % 5;
536
- image = `https://cdn.discordapp.com/embed/avatars/${index}.png`;
537
- } else {
538
- const format = profile.avatar.startsWith("a_") ? "gif" : "png";
539
- image = `https://cdn.discordapp.com/avatars/${profile.id}/${profile.avatar}.${format}`;
540
- }
541
- return {
542
- sub: profile.id,
543
- name: profile.global_name ?? profile.username,
544
- email: profile.email ?? "",
545
- image
546
- };
547
- },
548
- ...options2
549
- };
550
- };
551
-
552
- // src/oauth/gitlab.ts
553
- var gitlab = (options2) => {
554
- return {
555
- id: "gitlab",
556
- name: "GitLab",
557
- authorizeURL: "https://gitlab.com/oauth/authorize",
558
- accessToken: "https://gitlab.com/oauth/token",
559
- userInfo: "https://gitlab.com/api/v4/user",
560
- scope: "read_user",
561
- responseType: "code",
562
- profile(profile) {
563
- return {
564
- sub: profile.id.toString(),
565
- name: profile.name ?? profile.username,
566
- email: profile.email,
567
- image: profile.avatar_url
568
- };
569
- },
570
- ...options2
571
- };
433
+ var createBasicAuthHeader = (username, password) => {
434
+ const getUsername = getEnv(username.toUpperCase()) ?? username;
435
+ const getPassword = getEnv(password.toUpperCase()) ?? password;
436
+ if (!getUsername || !getPassword) {
437
+ throw new AuthInternalError("INVALID_OAUTH_CONFIGURATION", "Missing client credentials for OAuth provider configuration.");
438
+ }
439
+ const credentials = `${getUsername}:${getPassword}`;
440
+ return `Basic ${btoa(credentials)}`;
572
441
  };
573
-
574
- // src/oauth/spotify.ts
575
- var spotify = (options2) => {
576
- return {
577
- id: "spotify",
578
- name: "Spotify",
579
- authorizeURL: "https://accounts.spotify.com/authorize",
580
- accessToken: "https://accounts.spotify.com/api/token",
581
- userInfo: "https://api.spotify.com/v1/me",
582
- scope: "user-read-private user-read-email",
583
- responseType: "code",
584
- profile(profile) {
585
- return {
586
- sub: profile.id,
587
- name: profile.display_name,
588
- email: profile.email,
589
- image: profile.images[0]?.url ?? void 0
590
- };
591
- },
592
- ...options2
593
- };
442
+ var validateRedirectTo = (url) => {
443
+ if (!isRelativeURL(url) && !isValidURL(url)) return "/";
444
+ if (isRelativeURL(url)) return url;
445
+ return "/";
594
446
  };
595
447
 
596
- // src/oauth/x.ts
597
- var x = (options2) => {
598
- return {
599
- id: "x",
600
- name: "X",
601
- authorizeURL: "https://twitter.com/i/oauth2/authorize",
602
- accessToken: "https://api.twitter.com/2/oauth2/token",
603
- userInfo: "https://api.twitter.com/2/users/me?user.fields=profile_image_url",
604
- scope: "tweet.read users.read offline.access",
605
- responseType: "code",
606
- profile(profile) {
607
- return {
608
- sub: profile.data.id,
609
- name: profile.data.name,
610
- image: profile.data.profile_image_url,
611
- email: void 0
612
- };
613
- },
614
- ...options2
615
- };
448
+ // src/logger.ts
449
+ var logMessages = {
450
+ ROUTER_INTERNAL_ERROR: {
451
+ facility: 10,
452
+ severity: "error",
453
+ msgId: "ROUTER_INTERNAL_ERROR",
454
+ message: "Unhandled router error while processing the request"
455
+ },
456
+ INVALID_REQUEST: {
457
+ facility: 10,
458
+ severity: "warning",
459
+ msgId: "INVALID_REQUEST",
460
+ message: "Request validation failed against the expected schema"
461
+ },
462
+ SERVER_ERROR: {
463
+ facility: 10,
464
+ severity: "error",
465
+ msgId: "SERVER_ERROR",
466
+ message: "Unexpected internal server error during authentication"
467
+ },
468
+ OAUTH_PROTOCOL_ERROR: {
469
+ facility: 10,
470
+ severity: "warning",
471
+ msgId: "OAUTH_PROTOCOL_ERROR",
472
+ message: "OAuth provider returned an invalid or unexpected protocol response"
473
+ },
474
+ OAUTH_AUTHORIZATION_ERROR: {
475
+ facility: 10,
476
+ severity: "error",
477
+ msgId: "OAUTH_AUTHORIZATION_ERROR",
478
+ message: "OAuth authorization request was rejected or failed"
479
+ },
480
+ INVALID_OAUTH_CONFIGURATION: {
481
+ facility: 10,
482
+ severity: "error",
483
+ msgId: "INVALID_OAUTH_CONFIGURATION",
484
+ message: "The OAuth provider configuration is invalid or incomplete"
485
+ },
486
+ OAUTH_ACCESS_TOKEN_REQUEST_INITIATED: {
487
+ facility: 10,
488
+ severity: "debug",
489
+ msgId: "OAUTH_ACCESS_TOKEN_REQUEST_INITIATED",
490
+ message: "Starting OAuth access token request to the provider"
491
+ },
492
+ INVALID_OAUTH_ACCESS_TOKEN_RESPONSE: {
493
+ facility: 10,
494
+ severity: "error",
495
+ msgId: "INVALID_OAUTH_ACCESS_TOKEN_RESPONSE",
496
+ message: "OAuth access token endpoint returned an invalid or malformed response"
497
+ },
498
+ OAUTH_ACCESS_TOKEN_ERROR: {
499
+ facility: 10,
500
+ severity: "error",
501
+ msgId: "OAUTH_ACCESS_TOKEN_ERROR",
502
+ message: "OAuth access token endpoint returned an error response"
503
+ },
504
+ OAUTH_ACCESS_TOKEN_SUCCESS: {
505
+ facility: 10,
506
+ severity: "info",
507
+ msgId: "OAUTH_ACCESS_TOKEN_SUCCESS",
508
+ message: "Successfully retrieved OAuth access token from the provider"
509
+ },
510
+ OAUTH_ACCESS_TOKEN_REQUEST_FAILED: {
511
+ facility: 10,
512
+ severity: "error",
513
+ msgId: "OAUTH_ACCESS_TOKEN_REQUEST_FAILED",
514
+ message: "Network or server error while requesting OAuth access token"
515
+ },
516
+ OAUTH_USERINFO_REQUEST_INITIATED: {
517
+ facility: 10,
518
+ severity: "debug",
519
+ msgId: "OAUTH_USERINFO_REQUEST_INITIATED",
520
+ message: "Starting OAuth userinfo request to the provider"
521
+ },
522
+ OAUTH_USERINFO_INVALID_RESPONSE: {
523
+ facility: 10,
524
+ severity: "error",
525
+ msgId: "OAUTH_USERINFO_INVALID_RESPONSE",
526
+ message: "OAuth userinfo endpoint returned an invalid or malformed response"
527
+ },
528
+ OAUTH_USERINFO_ERROR: {
529
+ facility: 10,
530
+ severity: "error",
531
+ msgId: "OAUTH_USERINFO_ERROR",
532
+ message: "OAuth userinfo endpoint returned an error response"
533
+ },
534
+ OAUTH_USERINFO_SUCCESS: {
535
+ facility: 10,
536
+ severity: "info",
537
+ msgId: "OAUTH_USERINFO_SUCCESS",
538
+ message: "Successfully retrieved user information from the OAuth provider"
539
+ },
540
+ OAUTH_USERINFO_REQUEST_FAILED: {
541
+ facility: 10,
542
+ severity: "error",
543
+ msgId: "OAUTH_USERINFO_REQUEST_FAILED",
544
+ message: "Network or server error while requesting user information from the OAuth provider"
545
+ },
546
+ OAUTH_CALLBACK_SUCCESS: {
547
+ facility: 4,
548
+ severity: "info",
549
+ msgId: "OAUTH_CALLBACK_SUCCESS",
550
+ message: "OAuth callback completed successfully and session was created"
551
+ },
552
+ MISMATCHING_STATE: {
553
+ facility: 4,
554
+ severity: "critical",
555
+ msgId: "MISMATCHING_STATE",
556
+ message: "OAuth response state parameter does not match the stored state value"
557
+ },
558
+ POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED: {
559
+ facility: 4,
560
+ severity: "critical",
561
+ msgId: "POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED",
562
+ message: "Blocked redirect to untrusted or external URL (potential open redirect attack)"
563
+ },
564
+ OPEN_REDIRECT_ATTACK: {
565
+ facility: 4,
566
+ severity: "warning",
567
+ msgId: "OPEN_REDIRECT_ATTACK",
568
+ message: "Detected redirect target that does not match the trusted origin"
569
+ },
570
+ SESSION_TOKEN_MISSING: {
571
+ facility: 4,
572
+ severity: "warning",
573
+ msgId: "SESSION_TOKEN_MISSING",
574
+ message: "Session cookie is missing from the request"
575
+ },
576
+ CSRF_TOKEN_MISSING: {
577
+ facility: 4,
578
+ severity: "warning",
579
+ msgId: "CSRF_TOKEN_MISSING",
580
+ message: "CSRF token cookie is missing from the request"
581
+ },
582
+ CSRF_HEADER_MISSING: {
583
+ facility: 4,
584
+ severity: "warning",
585
+ msgId: "CSRF_HEADER_MISSING",
586
+ message: "CSRF header is missing from the request"
587
+ },
588
+ CSRF_TOKEN_INVALID: {
589
+ facility: 4,
590
+ severity: "error",
591
+ msgId: "CSRF_TOKEN_INVALID",
592
+ message: "CSRF token verification failed or token is invalid"
593
+ },
594
+ SIGN_IN_INITIATED: {
595
+ facility: 4,
596
+ severity: "info",
597
+ msgId: "SIGN_IN_INITIATED",
598
+ message: "Starting OAuth sign-in flow for the selected provider"
599
+ },
600
+ SIGN_OUT_ATTEMPT: {
601
+ facility: 4,
602
+ severity: "debug",
603
+ msgId: "SIGN_OUT_ATTEMPT",
604
+ message: "Received sign-out request from client"
605
+ },
606
+ SIGN_OUT_CSRF_VERIFIED: {
607
+ facility: 4,
608
+ severity: "info",
609
+ msgId: "SIGN_OUT_CSRF_VERIFIED",
610
+ message: "CSRF token was successfully verified during sign-out"
611
+ },
612
+ SIGN_OUT_SUCCESS: {
613
+ facility: 4,
614
+ severity: "info",
615
+ msgId: "SIGN_OUT_SUCCESS",
616
+ message: "User session was cleared and sign-out completed successfully"
617
+ },
618
+ SIGN_OUT_REDIRECT: {
619
+ facility: 4,
620
+ severity: "debug",
621
+ msgId: "SIGN_OUT_REDIRECT",
622
+ message: "Redirecting client after successful sign-out"
623
+ },
624
+ AUTH_SESSION_VALID: {
625
+ facility: 4,
626
+ severity: "info",
627
+ msgId: "AUTH_SESSION_VALID",
628
+ message: "Session token is valid and user session was returned"
629
+ },
630
+ AUTH_SESSION_INVALID: {
631
+ facility: 4,
632
+ severity: "notice",
633
+ msgId: "AUTH_SESSION_INVALID",
634
+ message: "Session token is missing, expired, or invalid"
635
+ },
636
+ INVALID_JWT_TOKEN: {
637
+ facility: 4,
638
+ severity: "warning",
639
+ msgId: "INVALID_JWT_TOKEN",
640
+ message: "JWT session token failed validation during sign-out"
641
+ },
642
+ CSRF_TOKEN_REQUESTED: {
643
+ facility: 4,
644
+ severity: "debug",
645
+ msgId: "CSRF_TOKEN_REQUESTED",
646
+ message: "Client requested a CSRF token"
647
+ },
648
+ CSRF_TOKEN_ISSUED: {
649
+ facility: 4,
650
+ severity: "debug",
651
+ msgId: "CSRF_TOKEN_ISSUED",
652
+ message: "Issued a new CSRF token to the client"
653
+ },
654
+ INVALID_URL: {
655
+ facility: 10,
656
+ severity: "error",
657
+ msgId: "INVALID_URL",
658
+ message: "Derived origin URL is invalid or malformed"
659
+ },
660
+ COOKIE_HTTPONLY_DISABLED: {
661
+ facility: 10,
662
+ severity: "critical",
663
+ msgId: "COOKIE_HTTPONLY_DISABLED",
664
+ message: "Cookie is configured without HttpOnly. This allows JavaScript access via document.cookie and increases XSS exposure."
665
+ },
666
+ COOKIE_WILDCARD_DOMAIN: {
667
+ facility: 10,
668
+ severity: "critical",
669
+ msgId: "COOKIE_WILDCARD_DOMAIN",
670
+ message: "Cookie 'Domain' is set to a wildcard, which is insecure and should be avoided."
671
+ },
672
+ COOKIE_SECURE_DISABLED: {
673
+ facility: 10,
674
+ severity: "warning",
675
+ msgId: "COOKIE_SECURE_DISABLED",
676
+ message: "Cookie is configured with 'Secure' but the request is not HTTPS. The 'Secure' attribute will be ignored by the browser."
677
+ },
678
+ COOKIE_SAMESITE_NONE_WITHOUT_SECURE: {
679
+ facility: 10,
680
+ severity: "warning",
681
+ msgId: "COOKIE_SAMESITE_NONE_WITHOUT_SECURE",
682
+ message: "Cookie uses SameSite=None without Secure. Falling back to SameSite=Lax for safer defaults."
683
+ },
684
+ COOKIE_INSECURE_IN_PRODUCTION: {
685
+ facility: 10,
686
+ severity: "critical",
687
+ msgId: "COOKIE_INSECURE_IN_PRODUCTION",
688
+ message: "Cookies are being served over an insecure connection in production, which is a serious security risk."
689
+ },
690
+ COOKIE_HOST_STRATEGY_INSECURE: {
691
+ facility: 10,
692
+ severity: "critical",
693
+ msgId: "COOKIE_HOST_STRATEGY_INSECURE",
694
+ message: "__Host- cookies require a secure HTTPS context. Falling back to standard cookie settings."
695
+ },
696
+ UNTRUSTED_ORIGIN: {
697
+ facility: 10,
698
+ severity: "error",
699
+ msgId: "UNTRUSTED_ORIGIN",
700
+ message: "The constructed origin URL is not trusted."
701
+ }
616
702
  };
617
-
618
- // src/oauth/strava.ts
619
- var strava = (options2) => {
703
+ var createLogEntry = (key, overrides) => {
704
+ const message = logMessages[key];
620
705
  return {
621
- id: "strava",
622
- name: "Strava",
623
- authorizeURL: "https://www.strava.com/oauth/authorize",
624
- accessToken: "https://www.strava.com/oauth/token",
625
- userInfo: "https://www.strava.com/api/v3/athlete",
626
- scope: "read",
627
- responseType: "code",
628
- profile(profile) {
629
- return {
630
- sub: profile.id.toString(),
631
- name: `${profile.firstname} ${profile.lastname}`,
632
- image: profile.profile,
633
- email: void 0
634
- };
635
- },
636
- ...options2
706
+ ...message,
707
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
708
+ hostname: "aura-auth",
709
+ procId: typeof process !== "undefined" && process.pid ? process.pid.toString() : "-",
710
+ ...overrides
637
711
  };
638
712
  };
639
-
640
- // src/oauth/mailchimp.ts
641
- var mailchimp = (options2) => {
642
- return {
643
- id: "mailchimp",
644
- name: "Mailchimp",
645
- authorizeURL: "https://login.mailchimp.com/oauth2/authorize",
646
- accessToken: "https://login.mailchimp.com/oauth2/token",
647
- userInfo: "https://login.mailchimp.com/oauth2/metadata",
648
- scope: "",
649
- responseType: "code",
650
- profile(profile) {
651
- return {
652
- sub: profile.user_id,
653
- name: profile.accountname,
654
- email: profile.login.email,
655
- image: profile.login.avatar
656
- };
657
- },
658
- ...options2
713
+ var logLevelToSeverity = {
714
+ debug: ["debug", "info", "notice", "warning", "error", "critical", "alert", "emergency"],
715
+ info: ["info", "notice", "warning", "error", "critical", "alert", "emergency"],
716
+ warn: ["warning", "error", "critical", "alert", "emergency"],
717
+ error: ["error", "critical", "alert", "emergency"]
718
+ };
719
+ var isValidLogLevel = (value) => {
720
+ return value === "debug" || value === "info" || value === "warn" || value === "error";
721
+ };
722
+ var getSeverityLevel = (severity) => {
723
+ const severities = {
724
+ emergency: 0,
725
+ alert: 1,
726
+ critical: 2,
727
+ error: 3,
728
+ warning: 4,
729
+ notice: 5,
730
+ info: 6,
731
+ debug: 7
659
732
  };
733
+ return severities[severity] ?? 6;
660
734
  };
661
-
662
- // src/oauth/pinterest.ts
663
- var pinterest = (options2) => {
735
+ var createSyslogMessage = (options2) => {
736
+ const { timestamp, hostname, appName = "aura-auth", procId = "-", msgId, structuredData, message } = options2;
737
+ const pri = (options2.facility ?? 16) * 8 + getSeverityLevel(options2.severity);
738
+ const structuredDataStr = createStructuredData(structuredData ?? {});
739
+ return `<${pri}>1 ${timestamp} ${hostname} ${appName} ${procId} ${msgId} ${structuredDataStr} ${message}`;
740
+ };
741
+ var createLogger = (logger) => {
742
+ if (!logger) return void 0;
743
+ const level = logger.level;
744
+ const allowedSeverities = logLevelToSeverity[level] ?? [];
664
745
  return {
665
- id: "pinterest",
666
- name: "Pinterest",
667
- authorizeURL: "https://www.pinterest.com/oauth",
668
- accessToken: "https://api.pinterest.com/v5/oauth/token",
669
- userInfo: "https://api.pinterest.com/v5/user_account",
670
- scope: "user_accounts:read",
671
- responseType: "code",
672
- profile(profile) {
673
- return {
674
- sub: profile.id,
675
- name: profile.username,
676
- image: profile.profile_image,
677
- email: void 0
678
- };
679
- },
680
- ...options2
746
+ level,
747
+ log(key, overrides) {
748
+ const entry = createLogEntry(key, overrides);
749
+ if (!allowedSeverities.includes(entry.severity)) return entry;
750
+ logger.log({
751
+ timestamp: entry.timestamp,
752
+ appName: entry.appName ?? "aura-auth",
753
+ hostname: entry.hostname ?? "aura-auth",
754
+ ...entry
755
+ });
756
+ return entry;
757
+ }
681
758
  };
682
759
  };
760
+ var createProxyLogger = (config2) => {
761
+ const level = getEnv("LOG_LEVEL");
762
+ const debug = getEnvBoolean("DEBUG");
763
+ if (typeof config2?.logger === "object") {
764
+ return createLogger({
765
+ log: config2.logger?.log || createSyslogMessage,
766
+ level: isValidLogLevel(config2.logger?.level) ? config2.logger?.level : isValidLogLevel(level) ? level : "error"
767
+ });
768
+ }
769
+ if (debug || config2?.logger === true || level) {
770
+ return createLogger({
771
+ level: isValidLogLevel(level) ? level : "debug",
772
+ log: (options2) => {
773
+ const message = createSyslogMessage(options2);
774
+ console.log(message);
775
+ }
776
+ });
777
+ }
778
+ return void 0;
779
+ };
683
780
 
684
- // src/schemas.ts
685
- var import_zod = require("zod");
686
- var OAuthProviderCredentialsSchema = (0, import_zod.object)({
687
- id: (0, import_zod.string)(),
688
- name: (0, import_zod.string)(),
689
- authorizeURL: (0, import_zod.string)().url(),
690
- accessToken: (0, import_zod.string)().url(),
691
- scope: (0, import_zod.string)(),
692
- userInfo: (0, import_zod.string)().url(),
693
- responseType: (0, import_zod.enum)(["code", "token", "id_token"]),
694
- clientId: (0, import_zod.string)(),
695
- clientSecret: (0, import_zod.string)(),
696
- profile: import_zod.z.function().optional()
697
- });
698
- var OAuthProviderConfigSchema = (0, import_zod.object)({
699
- authorizeURL: (0, import_zod.string)().url(),
700
- accessToken: (0, import_zod.string)().url(),
701
- scope: (0, import_zod.string)().optional(),
702
- userInfo: (0, import_zod.string)().url(),
703
- responseType: (0, import_zod.enum)(["code", "token", "id_token"]),
704
- clientId: (0, import_zod.string)(),
705
- clientSecret: (0, import_zod.string)()
706
- });
707
- var OAuthAuthorization = OAuthProviderConfigSchema.extend({
708
- redirectURI: (0, import_zod.string)(),
709
- state: (0, import_zod.string)(),
710
- codeChallenge: (0, import_zod.string)(),
711
- codeChallengeMethod: (0, import_zod.enum)(["plain", "S256"])
712
- });
713
- var OAuthAuthorizationResponse = (0, import_zod.object)({
714
- state: (0, import_zod.string)({ message: "Missing state parameter in the OAuth authorization response." }),
715
- code: (0, import_zod.string)({ message: "Missing code parameter in the OAuth authorization response." })
716
- });
717
- var OAuthAuthorizationErrorResponse = (0, import_zod.object)({
718
- error: (0, import_zod.enum)([
719
- "invalid_request",
720
- "unauthorized_client",
721
- "access_denied",
722
- "unsupported_response_type",
723
- "invalid_scope",
724
- "server_error",
725
- "temporarily_unavailable"
726
- ]),
727
- error_description: (0, import_zod.string)().optional(),
728
- error_uri: (0, import_zod.string)().optional(),
729
- state: (0, import_zod.string)()
730
- });
731
- var OAuthAccessToken = OAuthProviderConfigSchema.extend({
732
- redirectURI: (0, import_zod.string)(),
733
- code: (0, import_zod.string)(),
734
- codeVerifier: (0, import_zod.string)().min(43).max(128)
735
- });
736
- var OAuthAccessTokenResponse = (0, import_zod.object)({
737
- access_token: (0, import_zod.string)(),
738
- token_type: (0, import_zod.string)().optional(),
739
- expires_in: (0, import_zod.number)().optional(),
740
- refresh_token: (0, import_zod.string)().optional(),
741
- scope: (0, import_zod.string)().optional().or((0, import_zod.null)())
742
- });
743
- var OAuthAccessTokenErrorResponse = (0, import_zod.object)({
744
- error: (0, import_zod.enum)([
745
- "invalid_request",
746
- "invalid_client",
747
- "invalid_grant",
748
- "unauthorized_client",
749
- "unsupported_grant_type",
750
- "invalid_scope"
751
- ]),
752
- error_description: (0, import_zod.string)().optional(),
753
- error_uri: (0, import_zod.string)().optional()
754
- });
755
- var OAuthErrorResponse = (0, import_zod.object)({
756
- error: (0, import_zod.string)(),
757
- error_description: (0, import_zod.string)().optional()
758
- });
759
- var OAuthEnvSchema = (0, import_zod.object)({
760
- clientId: import_zod.z.string().min(1, "OAuth Client ID is required in the environment variables."),
761
- clientSecret: import_zod.z.string().min(1, "OAuth Client Secret is required in the environment variables.")
762
- });
763
-
764
- // src/oauth/index.ts
765
- var builtInOAuthProviders = {
766
- github,
767
- bitbucket,
768
- figma,
769
- discord,
770
- gitlab,
771
- spotify,
772
- x,
773
- strava,
774
- mailchimp,
775
- pinterest
781
+ // src/cookie.ts
782
+ var import_cookie = require("@aura-stack/router/cookie");
783
+ var COOKIE_NAME = "aura-auth";
784
+ var defaultCookieOptions = {
785
+ httpOnly: true,
786
+ sameSite: "lax",
787
+ path: "/",
788
+ maxAge: 60 * 60 * 24 * 15
776
789
  };
777
- var defineOAuthEnvironment = (oauth) => {
778
- const clientIdSuffix = `${oauth.toUpperCase()}_CLIENT_ID`;
779
- const clientSecretSuffix = `${oauth.toUpperCase()}_CLIENT_SECRET`;
780
- const loadEnvs = OAuthEnvSchema.safeParse({
781
- clientId: env[`AURA_AUTH_${clientIdSuffix}`] ?? env[`AUTH_${clientIdSuffix}`] ?? env[`${clientIdSuffix}`],
782
- clientSecret: env[`AURA_AUTH_${clientSecretSuffix}`] ?? env[`AUTH_${clientSecretSuffix}`] ?? env[`${clientSecretSuffix}`]
783
- });
784
- if (!loadEnvs.success) {
785
- const msg = JSON.stringify(formatZodError(loadEnvs.error), null, 2);
786
- throw new AuthInternalError("INVALID_ENVIRONMENT_CONFIGURATION", msg);
787
- }
788
- return loadEnvs.data;
790
+ var defaultStandardCookieConfig = {
791
+ secure: false,
792
+ httpOnly: true
789
793
  };
790
- var defineOAuthProviderConfig = (config2) => {
791
- if (typeof config2 === "string") {
792
- const definition = defineOAuthEnvironment(config2);
793
- const oauthConfig = builtInOAuthProviders[config2]();
794
- const parsed2 = OAuthProviderCredentialsSchema.safeParse({ ...oauthConfig, ...definition });
795
- if (!parsed2.success) {
796
- const details = JSON.stringify(formatZodError(parsed2.error), null, 2);
797
- throw new AuthInternalError(
798
- "INVALID_OAUTH_PROVIDER_CONFIGURATION",
799
- `Invalid configuration for OAuth provider "${config2}": ${details}`
800
- );
801
- }
802
- return parsed2.data;
803
- }
804
- const hasCredentials = config2.clientId && config2.clientSecret;
805
- const envConfig = hasCredentials ? {} : defineOAuthEnvironment(config2.id);
806
- const parsed = OAuthProviderCredentialsSchema.safeParse({ ...envConfig, ...config2 });
807
- if (!parsed.success) {
808
- const details = JSON.stringify(formatZodError(parsed.error), null, 2);
809
- throw new AuthInternalError(
810
- "INVALID_OAUTH_PROVIDER_CONFIGURATION",
811
- `Invalid configuration for OAuth provider "${config2.id}": ${details}`
812
- );
813
- }
814
- return parsed.data;
794
+ var defaultSecureCookieConfig = {
795
+ secure: true,
796
+ httpOnly: true
815
797
  };
816
- var createBuiltInOAuthProviders = (oauth = []) => {
817
- return oauth.reduce((previous, config2) => {
818
- const oauthConfig = defineOAuthProviderConfig(config2);
819
- if (oauthConfig.id in previous) {
820
- throw new AuthInternalError(
821
- "DUPLICATED_OAUTH_PROVIDER_ID",
822
- `Duplicate OAuth provider id "${oauthConfig.id}" found. Each provider must have a unique id.`
823
- );
824
- }
825
- return { ...previous, [oauthConfig.id]: oauthConfig };
826
- }, {});
798
+ var defaultHostCookieConfig = {
799
+ secure: true,
800
+ httpOnly: true,
801
+ path: "/",
802
+ domain: void 0
827
803
  };
828
-
829
- // src/actions/signIn/signIn.ts
830
- var import_zod2 = require("zod");
831
- var import_router2 = require("@aura-stack/router");
832
-
833
- // src/headers.ts
834
- var cacheControl = {
835
- "Cache-Control": "no-store",
836
- Pragma: "no-cache",
837
- Expires: "0",
838
- Vary: "Cookie"
804
+ var oauthCookieOptions = {
805
+ httpOnly: true,
806
+ maxAge: 5 * 60,
807
+ sameSite: "lax",
808
+ expires: new Date(Date.now() + 5 * 60 * 1e3)
839
809
  };
840
- var contentSecurityPolicy = {
841
- "Content-Security-Policy": [
842
- "default-src 'none'",
843
- "script-src 'self'",
844
- "frame-src 'none'",
845
- "object-src 'none'",
846
- "frame-ancestors 'none'",
847
- "base-uri 'none'"
848
- ].join("; ")
810
+ var setCookie = (cookieName, value, options2) => {
811
+ return (0, import_cookie.serialize)(cookieName, value, options2);
849
812
  };
850
- var secureHeaders = {
851
- "X-Content-Type-Options": "nosniff",
852
- "X-Frame-Options": "DENY",
853
- "Referrer-Policy": "strict-origin-when-cross-origin"
813
+ var expiredCookieAttributes = {
814
+ ...defaultCookieOptions,
815
+ expires: /* @__PURE__ */ new Date(0),
816
+ maxAge: 0,
817
+ secure: true
854
818
  };
855
- var secureApiHeaders = {
856
- ...cacheControl,
857
- ...contentSecurityPolicy,
858
- ...secureHeaders
819
+ var getCookie = (request, cookieName) => {
820
+ const cookies = request instanceof Request ? request.headers.get("Cookie") : request.get("Cookie");
821
+ if (!cookies) {
822
+ throw new AuthInternalError("COOKIE_NOT_FOUND", "No cookies found. There is no active session");
823
+ }
824
+ const value = (0, import_cookie.parse)(cookies)[cookieName];
825
+ if (!value) {
826
+ throw new AuthInternalError("COOKIE_NOT_FOUND", `Cookie "${cookieName}" not found. There is no active session`);
827
+ }
828
+ return value;
859
829
  };
860
-
861
- // src/secure.ts
862
- var import_crypto2 = __toESM(require("crypto"), 1);
863
-
864
- // src/assert.ts
865
- var import_crypto = require("crypto");
866
- var unsafeChars = [
867
- "<",
868
- ">",
869
- '"',
870
- "`",
871
- " ",
872
- "\r",
873
- "\n",
874
- " ",
875
- "\\",
876
- "%2F",
877
- "%5C",
878
- "%2f",
879
- "%5c",
880
- "\r\n",
881
- "%0A",
882
- "%0D",
883
- "%0a",
884
- "%0d",
885
- "..",
886
- "//",
887
- "///",
888
- "...",
889
- "%20",
890
- "\0"
891
- ];
892
- var isValidURL = (value) => {
893
- if (!new RegExp(/^https?:\/\/[^/]/).test(value)) {
894
- return false;
895
- }
896
- const match = value.match(/^(https?:\/\/)(.*)$/);
897
- if (!match) return false;
898
- const rest = match[2];
899
- for (const char of unsafeChars) {
900
- if (rest.includes(char)) return false;
901
- }
902
- const regex = /^https?:\/\/(?:[a-zA-Z0-9._-]+|localhost|\[[0-9a-fA-F:]+\])(?::\d{1,5})?(?:\/[a-zA-Z0-9._~!$&'()?#*+,;=:@-]*)*\/?$/;
903
- return regex.test(match[0]);
904
- };
905
- var isJWTPayloadWithToken = (payload) => {
906
- return typeof payload === "object" && payload !== null && "token" in payload && typeof payload?.token === "string";
907
- };
908
- var isRelativeURL = (value) => {
909
- if (value.length > 100) return false;
910
- for (const char of unsafeChars) {
911
- if (value.includes(char)) return false;
912
- }
913
- const regex = /^\/[a-zA-Z0-9\-_\/.?&=#]*\/?$/;
914
- return regex.test(value);
915
- };
916
- var isSameOrigin = (origin, expected) => {
917
- const originURL = new URL(origin);
918
- const expectedURL = new URL(expected);
919
- return equals(originURL.origin, expectedURL.origin);
920
- };
921
- var patternToRegex = (pattern) => {
922
- try {
923
- if (pattern.length > 2048) return null;
924
- pattern = pattern.replace(/\\/g, "");
925
- const match = pattern.match(/^(https?):\/\/([a-zA-Z0-9.*-]{1,253})(?::(\d{1,5}|\*))?(?:\/.*)?$/);
926
- if (!match) return null;
927
- const [, protocol, host, port] = match;
928
- const hasWildcard = host.includes("*");
929
- if (hasWildcard && !host.startsWith("*.")) return null;
930
- if (hasWildcard && host.slice(2).includes("*")) return null;
931
- const domain = hasWildcard ? host.slice(2) : host;
932
- const escapedDomain = domain.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
933
- const hostRegex = hasWildcard ? `[^.]+\\.${escapedDomain}` : escapedDomain;
934
- const portRegex = port === "*" ? ":\\d{1,5}" : port ? `:${port}` : "";
935
- return new RegExp(`^${protocol}:\\/\\/${hostRegex}${portRegex}$`);
936
- } catch {
937
- return null;
938
- }
939
- };
940
- var isTrustedOrigin = (url, trustedOrigins) => {
941
- if (!isValidURL(url) || trustedOrigins.length === 0) return false;
830
+ var createSessionCookie = async (jose, session) => {
942
831
  try {
943
- const urlOrigin = new URL(url).origin;
944
- for (const pattern of trustedOrigins) {
945
- const regex = patternToRegex(pattern);
946
- if (regex?.test(urlOrigin)) return true;
947
- try {
948
- if (isValidURL(pattern) && equals(new URL(pattern).origin, urlOrigin)) return true;
949
- } catch {
950
- }
951
- }
952
- } catch {
953
- }
954
- return false;
955
- };
956
- var safeEquals = (a, b) => {
957
- const bufferA = Buffer.from(a);
958
- const bufferB = Buffer.from(b);
959
- if (bufferA.length !== bufferB.length) {
960
- return false;
832
+ const encoded = await jose.encodeJWT(session);
833
+ return encoded;
834
+ } catch (error) {
835
+ throw new AuthInternalError("INVALID_JWT_TOKEN", "Failed to create session cookie", { cause: error });
961
836
  }
962
- return (0, import_crypto.timingSafeEqual)(bufferA, bufferB);
963
837
  };
964
-
965
- // src/secure.ts
966
- var generateSecure = (length = 32) => {
967
- return import_crypto2.default.randomBytes(length).toString("base64url");
968
- };
969
- var createHash = (data, base = "hex") => {
970
- return import_crypto2.default.createHash("sha256").update(data).digest().toString(base);
971
- };
972
- var createPKCE = async (verifier) => {
973
- const byteLength = verifier ? void 0 : Math.floor(Math.random() * (96 - 32 + 1) + 32);
974
- const codeVerifier = verifier ?? generateSecure(byteLength ?? 64);
975
- if (codeVerifier.length < 43 || codeVerifier.length > 128) {
976
- throw new AuthSecurityError("PKCE_VERIFIER_INVALID", "The code verifier must be between 43 and 128 characters in length.");
838
+ var defineSecureCookieOptions = (useSecure, attributes, strategy, logger) => {
839
+ if (!attributes.httpOnly) {
840
+ logger?.log("COOKIE_HTTPONLY_DISABLED");
977
841
  }
978
- const codeChallenge = createHash(codeVerifier, "base64url");
979
- return { codeVerifier, codeChallenge, method: "S256" };
980
- };
981
- var createCSRF = async (jose, csrfCookie) => {
982
- try {
983
- const token = generateSecure(32);
984
- if (csrfCookie) {
985
- await jose.verifyJWS(csrfCookie, jwtVerificationOptions);
986
- return csrfCookie;
987
- }
988
- return jose.signJWS({ token });
989
- } catch {
990
- const token = generateSecure(32);
991
- return jose.signJWS({ token });
842
+ if (attributes.domain === "*") {
843
+ attributes.domain = void 0;
844
+ logger?.log("COOKIE_WILDCARD_DOMAIN");
992
845
  }
993
- };
994
- var verifyCSRF = async (jose, cookie, header) => {
995
- try {
996
- const cookiePayload = await jose.verifyJWS(cookie, jwtVerificationOptions);
997
- const headerPayload = await jose.verifyJWS(header, jwtVerificationOptions);
998
- if (!isJWTPayloadWithToken(cookiePayload)) {
999
- throw new AuthSecurityError("CSRF_TOKEN_INVALID", "Cookie payload missing token field.");
846
+ if (!useSecure) {
847
+ if (attributes.secure) {
848
+ logger?.log("COOKIE_SECURE_DISABLED");
1000
849
  }
1001
- if (!isJWTPayloadWithToken(headerPayload)) {
1002
- throw new AuthSecurityError("CSRF_TOKEN_INVALID", "Header payload missing token field.");
850
+ if (attributes.sameSite == "none") {
851
+ attributes.sameSite = "lax";
852
+ logger?.log("COOKIE_SAMESITE_NONE_WITHOUT_SECURE");
1003
853
  }
1004
- if (!equals(cookiePayload.token.length, headerPayload.token.length)) {
1005
- throw new AuthSecurityError("CSRF_TOKEN_INVALID", "The CSRF tokens do not match.");
854
+ if (env.NODE_ENV === "production") {
855
+ logger?.log("COOKIE_INSECURE_IN_PRODUCTION");
1006
856
  }
1007
- if (!safeEquals(cookiePayload.token, headerPayload.token)) {
1008
- throw new AuthSecurityError("CSRF_TOKEN_INVALID", "The CSRF tokens do not match.");
857
+ if (strategy === "host") {
858
+ logger?.log("COOKIE_HOST_STRATEGY_INSECURE");
1009
859
  }
1010
- return true;
1011
- } catch {
1012
- throw new AuthSecurityError("CSRF_TOKEN_INVALID", "The CSRF tokens do not match.");
1013
- }
1014
- };
1015
-
1016
- // src/actions/signIn/authorization.ts
1017
- var createAuthorizationURL = (oauthConfig, redirectURI, state, codeChallenge, codeChallengeMethod, logger) => {
1018
- const parsed = OAuthAuthorization.safeParse({ ...oauthConfig, redirectURI, state, codeChallenge, codeChallengeMethod });
1019
- if (!parsed.success) {
1020
- logger?.log("INVALID_OAUTH_CONFIGURATION", {
1021
- structuredData: {
1022
- scope: oauthConfig.scope,
1023
- redirect_uri: redirectURI,
1024
- has_state: Boolean(state),
1025
- has_code_challenge: Boolean(codeChallenge),
1026
- code_challenge_method: codeChallengeMethod
1027
- }
1028
- });
1029
- throw new AuthInternalError("INVALID_OAUTH_CONFIGURATION", "The OAuth provider configuration is invalid.");
1030
- }
1031
- const { authorizeURL, ...options2 } = parsed.data;
1032
- const { userInfo, accessToken, clientSecret, ...required } = options2;
1033
- const searchParams = new URLSearchParams(toCastCase(required));
1034
- return `${authorizeURL}?${searchParams}`;
1035
- };
1036
- var getTrustedOrigins = async (request, trustedOrigins) => {
1037
- if (!trustedOrigins) return [];
1038
- const raw = typeof trustedOrigins === "function" ? await trustedOrigins(request) : trustedOrigins;
1039
- return Array.isArray(raw) ? raw : typeof raw === "string" ? [raw] : [];
1040
- };
1041
- var getOriginURL = async (request, context) => {
1042
- const headers = request.headers;
1043
- let origin = new URL(request.url).origin;
1044
- const trustedOrigins = await getTrustedOrigins(request, context?.trustedOrigins);
1045
- trustedOrigins.push(origin);
1046
- if (context?.trustedProxyHeaders) {
1047
- const protocol = headers.get("Forwarded")?.match(/proto=([^;]+)/i)?.[1] ?? headers.get("X-Forwarded-Proto") ?? "http";
1048
- const host = headers.get("Host") ?? headers.get("Forwarded")?.match(/host=([^;]+)/i)?.[1] ?? headers.get("X-Forwarded-Host") ?? null;
1049
- origin = `${protocol}://${host}`;
1050
- }
1051
- if (!isTrustedOrigin(origin, trustedOrigins)) {
1052
- context?.logger?.log("UNTRUSTED_ORIGIN", { structuredData: { origin } });
1053
- throw new AuthInternalError("UNTRUSTED_ORIGIN", "The constructed origin URL is not trusted.");
1054
- }
1055
- return origin;
1056
- };
1057
- var createRedirectURI = async (request, oauth, context) => {
1058
- const origin = await getOriginURL(request, context);
1059
- return `${origin}${context.basePath}/callback/${oauth}`;
1060
- };
1061
- var createRedirectTo = async (request, redirectTo, context) => {
1062
- try {
1063
- const headers = request.headers;
1064
- const requestOrigin = await getOriginURL(request, context);
1065
- const origins = await getTrustedOrigins(request, context?.trustedOrigins);
1066
- const validateURL = (url) => {
1067
- if (!isRelativeURL(url) && !isValidURL(url)) return "/";
1068
- if (isRelativeURL(url)) return url;
1069
- if (origins.length > 0) {
1070
- if (isTrustedOrigin(url, origins)) {
1071
- const urlOrigin = new URL(url).origin;
1072
- for (const pattern of origins) {
1073
- const regex = patternToRegex(pattern);
1074
- if (regex?.test(urlOrigin)) {
1075
- return isSameOrigin(url, request.url) ? extractPath(url) : url;
1076
- }
1077
- if (isValidURL(pattern) && equals(new URL(pattern).origin, urlOrigin)) return url;
1078
- }
1079
- }
1080
- context?.logger?.log("OPEN_REDIRECT_ATTACK");
1081
- return "/";
1082
- }
1083
- if (isSameOrigin(url, requestOrigin)) {
1084
- return extractPath(url);
1085
- }
1086
- context?.logger?.log("OPEN_REDIRECT_ATTACK");
1087
- return "/";
860
+ return {
861
+ ...defaultCookieOptions,
862
+ ...attributes,
863
+ ...defaultStandardCookieConfig
1088
864
  };
1089
- return validateURL(redirectTo ?? headers.get("Referer") ?? headers.get("Origin") ?? "/");
1090
- } catch (error) {
1091
- context?.logger?.log("POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED");
1092
- return "/";
1093
865
  }
866
+ return strategy === "host" ? {
867
+ ...defaultCookieOptions,
868
+ ...attributes,
869
+ ...defaultHostCookieConfig
870
+ } : { ...defaultCookieOptions, ...attributes, ...defaultSecureCookieConfig };
1094
871
  };
1095
-
1096
- // src/actions/signIn/signIn.ts
1097
- var signInConfig = (oauth) => {
1098
- return (0, import_router2.createEndpointConfig)("/signIn/:oauth", {
1099
- schemas: {
1100
- params: import_zod2.z.object({
1101
- oauth: import_zod2.z.enum(
1102
- Object.keys(oauth),
1103
- "The OAuth provider is not supported or invalid."
1104
- )
1105
- }),
1106
- searchParams: import_zod2.z.object({
1107
- redirectTo: import_zod2.z.string().optional()
1108
- })
1109
- }
1110
- });
1111
- };
1112
- var signInAction = (oauth) => {
1113
- return (0, import_router2.createEndpoint)(
1114
- "GET",
1115
- "/signIn/:oauth",
1116
- async (ctx) => {
1117
- const {
1118
- request,
1119
- params: { oauth: oauth2 },
1120
- searchParams: { redirectTo },
1121
- context
1122
- } = ctx;
1123
- const { oauth: providers, cookies, logger } = context;
1124
- const state = generateSecure();
1125
- const redirectURI = await createRedirectURI(request, oauth2, context);
1126
- const redirectToValue = await createRedirectTo(request, redirectTo, context);
1127
- const { codeVerifier, codeChallenge, method } = await createPKCE();
1128
- const authorization = createAuthorizationURL(providers[oauth2], redirectURI, state, codeChallenge, method, logger);
1129
- logger?.log("SIGN_IN_INITIATED", {
1130
- structuredData: { oauth_provider: oauth2, code_challenge_method: method }
1131
- });
1132
- const headers = new import_router2.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();
1133
- return Response.json(
1134
- { oauth: oauth2 },
872
+ var createCookieStore = (useSecure, prefix, overrides, logger) => {
873
+ prefix ??= COOKIE_NAME;
874
+ const securePrefix = useSecure ? "__Secure-" : "";
875
+ const hostPrefix = useSecure ? "__Host-" : "";
876
+ return {
877
+ sessionToken: {
878
+ name: `${securePrefix}${prefix}.${overrides?.sessionToken?.name ?? "session_token"}`,
879
+ attributes: defineSecureCookieOptions(
880
+ useSecure,
1135
881
  {
1136
- status: 302,
1137
- headers
1138
- }
1139
- );
882
+ ...defaultCookieOptions,
883
+ ...overrides?.sessionToken?.attributes
884
+ },
885
+ overrides?.sessionToken?.attributes?.strategy ?? "secure",
886
+ logger
887
+ )
1140
888
  },
1141
- signInConfig(oauth)
1142
- );
1143
- };
1144
-
1145
- // src/actions/callback/callback.ts
1146
- var import_zod3 = require("zod");
1147
- var import_router3 = require("@aura-stack/router");
1148
-
1149
- // src/request.ts
1150
- var fetchAsync = async (url, options2 = {}, timeout = 5e3) => {
1151
- const controller = new AbortController();
1152
- const timeoutId = setTimeout(() => controller.abort(), timeout);
1153
- const response = await fetch(url, {
1154
- ...options2,
1155
- signal: controller.signal
1156
- }).finally(() => clearTimeout(timeoutId));
1157
- return response;
889
+ state: {
890
+ name: `${securePrefix}${prefix}.${overrides?.state?.name ?? "state"}`,
891
+ attributes: defineSecureCookieOptions(
892
+ useSecure,
893
+ {
894
+ ...oauthCookieOptions,
895
+ ...overrides?.state?.attributes
896
+ },
897
+ overrides?.state?.attributes?.strategy ?? "secure",
898
+ logger
899
+ )
900
+ },
901
+ csrfToken: {
902
+ name: `${hostPrefix}${prefix}.${overrides?.csrfToken?.name ?? "csrf_token"}`,
903
+ attributes: defineSecureCookieOptions(
904
+ useSecure,
905
+ {
906
+ ...overrides?.csrfToken?.attributes,
907
+ ...defaultHostCookieConfig,
908
+ sameSite: "strict"
909
+ },
910
+ overrides?.csrfToken?.attributes?.strategy ?? "host",
911
+ logger
912
+ )
913
+ },
914
+ redirectTo: {
915
+ name: `${securePrefix}${prefix}.${overrides?.redirectTo?.name ?? "redirect_to"}`,
916
+ attributes: defineSecureCookieOptions(
917
+ useSecure,
918
+ {
919
+ ...oauthCookieOptions,
920
+ ...overrides?.redirectTo?.attributes
921
+ },
922
+ overrides?.redirectTo?.attributes?.strategy ?? "secure",
923
+ logger
924
+ )
925
+ },
926
+ redirectURI: {
927
+ name: `${securePrefix}${prefix}.${overrides?.redirectURI?.name ?? "redirect_uri"}`,
928
+ attributes: defineSecureCookieOptions(
929
+ useSecure,
930
+ {
931
+ ...oauthCookieOptions,
932
+ ...overrides?.redirectURI?.attributes
933
+ },
934
+ overrides?.redirectURI?.attributes?.strategy ?? "secure",
935
+ logger
936
+ )
937
+ },
938
+ codeVerifier: {
939
+ name: `${securePrefix}${prefix}.${overrides?.codeVerifier?.name ?? "code_verifier"}`,
940
+ attributes: defineSecureCookieOptions(
941
+ useSecure,
942
+ {
943
+ ...oauthCookieOptions,
944
+ ...overrides?.codeVerifier?.attributes
945
+ },
946
+ overrides?.codeVerifier?.attributes?.strategy ?? "secure",
947
+ logger
948
+ )
949
+ }
950
+ };
1158
951
  };
1159
952
 
1160
- // src/actions/callback/userinfo.ts
1161
- var getDefaultUserInfo = (profile) => {
1162
- const sub = generateSecure(16);
953
+ // src/oauth/github.ts
954
+ var github = (options2) => {
1163
955
  return {
1164
- sub: profile?.id ?? profile?.sub ?? sub,
1165
- email: profile?.email,
1166
- name: profile?.name ?? profile?.username ?? profile?.nickname,
1167
- image: profile?.image ?? profile?.picture
956
+ id: "github",
957
+ name: "GitHub",
958
+ authorizeURL: "https://github.com/login/oauth/authorize",
959
+ accessToken: "https://github.com/login/oauth/access_token",
960
+ userInfo: "https://api.github.com/user",
961
+ scope: "read:user user:email",
962
+ responseType: "code",
963
+ profile: (profile) => {
964
+ return {
965
+ sub: profile.id.toString(),
966
+ name: profile.name ?? profile.login,
967
+ email: profile.email ?? void 0,
968
+ image: profile.avatar_url
969
+ };
970
+ },
971
+ ...options2
1168
972
  };
1169
973
  };
1170
- var getUserInfo = async (oauthConfig, accessToken, logger) => {
1171
- const userinfoEndpoint = oauthConfig.userInfo;
1172
- try {
1173
- logger?.log("OAUTH_USERINFO_REQUEST_INITIATED", {
1174
- structuredData: {
1175
- endpoint: userinfoEndpoint
1176
- }
1177
- });
1178
- const response = await fetchAsync(userinfoEndpoint, {
1179
- method: "GET",
1180
- headers: {
1181
- Accept: "application/json",
1182
- Authorization: `Bearer ${accessToken}`
1183
- }
1184
- });
1185
- if (!response.ok) {
1186
- logger?.log("OAUTH_USERINFO_INVALID_RESPONSE");
1187
- throw new OAuthProtocolError("INVALID_REQUEST", "Invalid userinfo response format");
1188
- }
1189
- const json = await response.json();
1190
- const { success, data } = OAuthErrorResponse.safeParse(json);
1191
- if (success) {
1192
- logger?.log("OAUTH_USERINFO_ERROR", {
1193
- message: "Error response received from OAuth userinfo endpoint",
1194
- structuredData: {
1195
- error: data.error,
1196
- error_description: data.error_description ?? ""
1197
- }
1198
- });
1199
- throw new OAuthProtocolError("INVALID_REQUEST", "An error was received from the OAuth userinfo endpoint.");
1200
- }
1201
- logger?.log("OAUTH_USERINFO_SUCCESS");
1202
- return oauthConfig?.profile ? oauthConfig.profile(json) : getDefaultUserInfo(json);
1203
- } catch (error) {
1204
- if (isOAuthProtocolError(error)) {
1205
- throw error;
1206
- }
1207
- logger?.log("OAUTH_USERINFO_REQUEST_FAILED");
1208
- if (isNativeError(error)) {
1209
- throw new OAuthProtocolError("SERVER_ERROR", "Failed to fetch user information from OAuth provider", "", {
1210
- cause: error
1211
- });
1212
- }
1213
- throw new OAuthProtocolError("SERVER_ERROR", "Failed to fetch user information", "", { cause: error });
1214
- }
974
+
975
+ // src/oauth/bitbucket.ts
976
+ var bitbucket = (options2) => {
977
+ return {
978
+ id: "bitbucket",
979
+ name: "Bitbucket",
980
+ authorizeURL: "https://bitbucket.org/site/oauth2/authorize",
981
+ accessToken: "https://bitbucket.org/site/oauth2/access_token",
982
+ userInfo: "https://api.bitbucket.org/2.0/user",
983
+ scope: "account email",
984
+ responseType: "code",
985
+ profile(profile) {
986
+ return {
987
+ sub: profile.uuid ?? profile.account_id,
988
+ name: profile.display_name ?? profile.nickname,
989
+ image: profile.links.avatar?.href,
990
+ email: void 0
991
+ };
992
+ },
993
+ ...options2
994
+ };
1215
995
  };
1216
996
 
1217
- // src/actions/callback/access-token.ts
1218
- var createAccessToken = async (oauthConfig, redirectURI, code, codeVerifier, logger) => {
1219
- const parsed = OAuthAccessToken.safeParse({ ...oauthConfig, redirectURI, code, codeVerifier });
1220
- if (!parsed.success) {
1221
- logger?.log("INVALID_OAUTH_CONFIGURATION");
1222
- throw new AuthInternalError("INVALID_OAUTH_CONFIGURATION", "The OAuth provider configuration is invalid.");
1223
- }
1224
- const { accessToken, clientId, clientSecret, code: codeParsed, redirectURI: redirectParsed } = parsed.data;
1225
- try {
1226
- logger?.log("OAUTH_ACCESS_TOKEN_REQUEST_INITIATED", {
1227
- structuredData: {
1228
- has_client_id: Boolean(clientId),
1229
- redirect_uri: redirectParsed,
1230
- grant_type: "authorization_code"
1231
- }
1232
- });
1233
- const response = await fetchAsync(accessToken, {
1234
- method: "POST",
1235
- headers: {
1236
- Accept: "application/json",
1237
- "Content-Type": "application/x-www-form-urlencoded"
1238
- },
1239
- body: new URLSearchParams({
1240
- client_id: clientId,
1241
- client_secret: clientSecret,
1242
- code: codeParsed,
1243
- redirect_uri: redirectParsed,
1244
- grant_type: "authorization_code",
1245
- code_verifier: codeVerifier
1246
- }).toString()
1247
- });
1248
- if (!response.ok) {
1249
- logger?.log("INVALID_OAUTH_ACCESS_TOKEN_RESPONSE");
1250
- throw new OAuthProtocolError("invalid_request", "Invalid access token response");
1251
- }
1252
- const json = await response.json();
1253
- const token = OAuthAccessTokenResponse.safeParse(json);
1254
- if (!token.success) {
1255
- const { success, data } = OAuthAccessTokenErrorResponse.safeParse(json);
1256
- if (!success) {
1257
- logger?.log("INVALID_OAUTH_ACCESS_TOKEN_RESPONSE");
1258
- throw new OAuthProtocolError("invalid_request", "Invalid access token response format");
997
+ // src/oauth/figma.ts
998
+ var figma = (options2) => {
999
+ return {
1000
+ id: "figma",
1001
+ name: "Figma",
1002
+ authorizeURL: "https://www.figma.com/oauth",
1003
+ accessToken: "https://api.figma.com/v1/oauth/token",
1004
+ userInfo: "https://api.figma.com/v1/me",
1005
+ scope: "current_user:read",
1006
+ responseType: "code",
1007
+ profile(profile) {
1008
+ return {
1009
+ sub: profile.id,
1010
+ name: profile.handle,
1011
+ email: profile.email,
1012
+ image: profile.img_url
1013
+ };
1014
+ },
1015
+ ...options2
1016
+ };
1017
+ };
1018
+
1019
+ // src/oauth/discord.ts
1020
+ var discord = (options2) => {
1021
+ return {
1022
+ id: "discord",
1023
+ name: "Discord",
1024
+ authorizeURL: "https://discord.com/oauth2/authorize",
1025
+ accessToken: "https://discord.com/api/oauth2/token",
1026
+ userInfo: "https://discord.com/api/users/@me",
1027
+ scope: "identify email",
1028
+ responseType: "code",
1029
+ profile(profile) {
1030
+ let image = "";
1031
+ if (profile.avatar === null) {
1032
+ const index = profile.discriminator === "0" ? (BigInt(profile.id) >> 22n) % 6n : Number(profile.discriminator) % 5;
1033
+ image = `https://cdn.discordapp.com/embed/avatars/${index}.png`;
1034
+ } else {
1035
+ const format = profile.avatar.startsWith("a_") ? "gif" : "png";
1036
+ image = `https://cdn.discordapp.com/avatars/${profile.id}/${profile.avatar}.${format}`;
1259
1037
  }
1260
- logger?.log("OAUTH_ACCESS_TOKEN_ERROR", {
1261
- structuredData: {
1262
- error: data.error,
1263
- error_description: data.error_description ?? ""
1264
- }
1265
- });
1266
- throw new OAuthProtocolError("INVALID_ACCESS_TOKEN", "Failed to retrieve access token");
1267
- }
1268
- logger?.log("OAUTH_ACCESS_TOKEN_SUCCESS");
1269
- return token.data;
1270
- } catch (error) {
1271
- logger?.log("OAUTH_ACCESS_TOKEN_REQUEST_FAILED");
1272
- if (error instanceof Error) {
1273
- throw new OAuthProtocolError("server_error", "Failed to communicate with OAuth provider", "", { cause: error });
1274
- }
1275
- throw error;
1276
- }
1038
+ return {
1039
+ sub: profile.id,
1040
+ name: profile.global_name ?? profile.username,
1041
+ email: profile.email ?? "",
1042
+ image
1043
+ };
1044
+ },
1045
+ ...options2
1046
+ };
1277
1047
  };
1278
1048
 
1279
- // src/actions/callback/callback.ts
1280
- var callbackConfig = (oauth) => {
1281
- return (0, import_router3.createEndpointConfig)("/callback/:oauth", {
1282
- schemas: {
1283
- params: import_zod3.z.object({
1284
- oauth: import_zod3.z.enum(
1285
- Object.keys(oauth),
1286
- "The OAuth provider is not supported or invalid."
1287
- )
1288
- }),
1289
- searchParams: import_zod3.z.object({
1290
- code: import_zod3.z.string("Missing code parameter in the OAuth authorization response."),
1291
- state: import_zod3.z.string("Missing state parameter in the OAuth authorization response.")
1292
- })
1049
+ // src/oauth/gitlab.ts
1050
+ var gitlab = (options2) => {
1051
+ return {
1052
+ id: "gitlab",
1053
+ name: "GitLab",
1054
+ authorizeURL: "https://gitlab.com/oauth/authorize",
1055
+ accessToken: "https://gitlab.com/oauth/token",
1056
+ userInfo: "https://gitlab.com/api/v4/user",
1057
+ scope: "read_user",
1058
+ responseType: "code",
1059
+ profile(profile) {
1060
+ return {
1061
+ sub: profile.id.toString(),
1062
+ name: profile.name ?? profile.username,
1063
+ email: profile.email,
1064
+ image: profile.avatar_url
1065
+ };
1293
1066
  },
1294
- middlewares: [
1295
- (ctx) => {
1296
- const {
1297
- searchParams,
1298
- context: { logger }
1299
- } = ctx;
1300
- const response = OAuthAuthorizationErrorResponse.safeParse(searchParams);
1301
- if (response.success) {
1302
- const { error, error_description } = response.data;
1303
- const criticalAuthErrors = ["access_denied", "server_error"];
1304
- const severity = criticalAuthErrors.includes(error.toLowerCase()) ? "critical" : "warning";
1305
- logger?.log("OAUTH_AUTHORIZATION_ERROR", {
1306
- severity,
1307
- structuredData: {
1308
- error,
1309
- error_description: error_description ?? ""
1310
- }
1311
- });
1312
- throw new OAuthProtocolError(error, error_description || "OAuth Authorization Error");
1313
- }
1314
- return ctx;
1315
- }
1316
- ]
1317
- });
1067
+ ...options2
1068
+ };
1318
1069
  };
1319
- var callbackAction = (oauth) => {
1320
- return (0, import_router3.createEndpoint)(
1321
- "GET",
1322
- "/callback/:oauth",
1323
- async (ctx) => {
1324
- const {
1325
- request,
1326
- params: { oauth: oauth2 },
1327
- searchParams: { code, state },
1328
- context
1329
- } = ctx;
1330
- const { oauth: providers, cookies, jose, logger, trustedOrigins } = context;
1331
- const oauthConfig = providers[oauth2];
1332
- const cookieState = getCookie(request, cookies.state.name);
1333
- const codeVerifier = getCookie(request, cookies.codeVerifier.name);
1334
- const cookieRedirectTo = getCookie(request, cookies.redirectTo.name);
1335
- const cookieRedirectURI = getCookie(request, cookies.redirectURI.name);
1336
- if (!safeEquals(cookieState, state)) {
1337
- logger?.log("MISMATCHING_STATE", {
1338
- structuredData: {
1339
- oauth_provider: oauth2
1340
- }
1341
- });
1342
- throw new AuthSecurityError(
1343
- "MISMATCHING_STATE",
1344
- "The provided state passed in the OAuth response does not match the stored state."
1345
- );
1346
- }
1347
- const accessToken = await createAccessToken(oauthConfig, cookieRedirectURI, code, codeVerifier, logger);
1348
- const origins = await getTrustedOrigins(request, trustedOrigins);
1349
- const requestOrigin = await getOriginURL(request, context);
1350
- if (!isRelativeURL(cookieRedirectTo)) {
1351
- const isValid = origins.length > 0 ? isTrustedOrigin(cookieRedirectTo, origins) : isSameOrigin(cookieRedirectTo, requestOrigin);
1352
- if (!isValid) {
1353
- logger?.log("POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED", {
1354
- structuredData: {
1355
- redirect_path: cookieRedirectTo,
1356
- provider: oauth2,
1357
- has_trusted_origins: origins.length > 0,
1358
- request_origin: requestOrigin
1359
- }
1360
- });
1361
- throw new AuthSecurityError(
1362
- "POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED",
1363
- "Invalid redirect path. Potential open redirect attack detected."
1364
- );
1365
- }
1366
- }
1367
- const userInfo = await getUserInfo(oauthConfig, accessToken.access_token, logger);
1368
- const sessionCookie = await createSessionCookie(jose, userInfo);
1369
- const csrfToken = await createCSRF(jose);
1370
- logger?.log("OAUTH_CALLBACK_SUCCESS", {
1371
- structuredData: {
1372
- provider: oauth2
1373
- }
1374
- });
1375
- const headers = new import_router3.HeadersBuilder(cacheControl).setHeader("Location", cookieRedirectTo).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();
1376
- return Response.json({ oauth: oauth2 }, { status: 302, headers });
1070
+
1071
+ // src/oauth/spotify.ts
1072
+ var spotify = (options2) => {
1073
+ return {
1074
+ id: "spotify",
1075
+ name: "Spotify",
1076
+ authorizeURL: "https://accounts.spotify.com/authorize",
1077
+ accessToken: "https://accounts.spotify.com/api/token",
1078
+ userInfo: "https://api.spotify.com/v1/me",
1079
+ scope: "user-read-private user-read-email",
1080
+ responseType: "code",
1081
+ profile(profile) {
1082
+ return {
1083
+ sub: profile.id,
1084
+ name: profile.display_name,
1085
+ email: profile.email,
1086
+ image: profile.images[0]?.url ?? void 0
1087
+ };
1377
1088
  },
1378
- callbackConfig(oauth)
1379
- );
1089
+ ...options2
1090
+ };
1380
1091
  };
1381
1092
 
1382
- // src/actions/session/session.ts
1383
- var import_router4 = require("@aura-stack/router");
1384
- var sessionAction = (0, import_router4.createEndpoint)("GET", "/session", async (ctx) => {
1385
- const {
1386
- request,
1387
- context: { jose, cookies, logger }
1388
- } = ctx;
1389
- try {
1390
- const session = getCookie(request, cookies.sessionToken.name);
1391
- const decoded = await jose.decodeJWT(session);
1392
- logger?.log("AUTH_SESSION_VALID");
1393
- const { exp, iat, jti, nbf, ...user } = decoded;
1394
- const headers = new Headers(secureApiHeaders);
1395
- return Response.json({ user, expires: toISOString(exp * 1e3) }, { headers });
1396
- } catch (error) {
1397
- logger?.log("AUTH_SESSION_INVALID", { structuredData: { error_type: getErrorName(error) } });
1398
- const headers = new import_router4.HeadersBuilder(secureApiHeaders).setCookie(cookies.sessionToken.name, "", expiredCookieAttributes).toHeaders();
1399
- return Response.json({ authenticated: false, message: "Unauthorized" }, { status: 401, headers });
1400
- }
1401
- });
1402
-
1403
- // src/actions/signOut/signOut.ts
1404
- var import_zod4 = require("zod");
1405
- var import_router5 = require("@aura-stack/router");
1406
- var config = (0, import_router5.createEndpointConfig)({
1407
- schemas: {
1408
- searchParams: import_zod4.z.object({
1409
- token_type_hint: import_zod4.z.literal("session_token"),
1410
- redirectTo: import_zod4.z.string().optional()
1411
- })
1412
- }
1413
- });
1414
- var signOutAction = (0, import_router5.createEndpoint)(
1415
- "POST",
1416
- "/signOut",
1417
- async (ctx) => {
1418
- const {
1419
- request,
1420
- headers,
1421
- searchParams: { redirectTo },
1422
- context
1423
- } = ctx;
1424
- const { jose, cookies, logger } = context;
1425
- const session = headers.getCookie(cookies.sessionToken.name);
1426
- const csrfToken = headers.getCookie(cookies.csrfToken.name);
1427
- const header = headers.getHeader("X-CSRF-Token");
1428
- logger?.log("SIGN_OUT_ATTEMPT", {
1429
- structuredData: {
1430
- has_session: Boolean(session),
1431
- has_csrf_token: Boolean(csrfToken),
1432
- has_csrf_header: Boolean(header)
1433
- }
1434
- });
1435
- if (!session) {
1436
- logger?.log("SESSION_TOKEN_MISSING");
1437
- throw new AuthSecurityError("SESSION_TOKEN_MISSING", "The sessionToken is missing.");
1438
- }
1439
- if (!csrfToken) {
1440
- logger?.log("CSRF_TOKEN_MISSING");
1441
- throw new AuthSecurityError("CSRF_TOKEN_MISSING", "The CSRF token is missing.");
1442
- }
1443
- if (!header) {
1444
- logger?.log("CSRF_HEADER_MISSING");
1445
- throw new AuthSecurityError("CSRF_HEADER_MISSING", "The CSRF header is missing.");
1446
- }
1447
- try {
1448
- await verifyCSRF(jose, csrfToken, header);
1449
- } catch (error) {
1450
- logger?.log("CSRF_TOKEN_INVALID", { structuredData: { error_type: getErrorName(error) } });
1451
- throw new AuthSecurityError("CSRF_TOKEN_INVALID", "CSRF token verification failed");
1452
- }
1453
- logger?.log("SIGN_OUT_CSRF_VERIFIED");
1454
- try {
1455
- await jose.decodeJWT(session);
1456
- logger?.log("SIGN_OUT_SUCCESS");
1457
- } catch (error) {
1458
- logger?.log("INVALID_JWT_TOKEN", { structuredData: { error_type: getErrorName(error) } });
1459
- }
1460
- const baseURL = getBaseURL(request);
1461
- const location = await createRedirectTo(
1462
- new Request(baseURL, {
1463
- headers: headers.toHeaders()
1464
- }),
1465
- redirectTo,
1466
- context
1467
- );
1468
- logger?.log("SIGN_OUT_REDIRECT", { structuredData: { location } });
1469
- const headersList = new import_router5.HeadersBuilder(secureApiHeaders).setHeader("Location", location).setCookie(cookies.csrfToken.name, "", expiredCookieAttributes).setCookie(cookies.sessionToken.name, "", expiredCookieAttributes).toHeaders();
1470
- return Response.json({ message: "Signed out successfully" }, { status: import_router5.statusCode.ACCEPTED, headers: headersList });
1471
- },
1472
- config
1473
- );
1093
+ // src/oauth/x.ts
1094
+ var x = (options2) => {
1095
+ return {
1096
+ id: "x",
1097
+ name: "X",
1098
+ authorizeURL: "https://twitter.com/i/oauth2/authorize",
1099
+ accessToken: "https://api.twitter.com/2/oauth2/token",
1100
+ userInfo: "https://api.twitter.com/2/users/me?user.fields=profile_image_url",
1101
+ scope: "tweet.read users.read offline.access",
1102
+ responseType: "code",
1103
+ profile(profile) {
1104
+ return {
1105
+ sub: profile.data.id,
1106
+ name: profile.data.name,
1107
+ image: profile.data.profile_image_url,
1108
+ email: void 0
1109
+ };
1110
+ },
1111
+ ...options2
1112
+ };
1113
+ };
1474
1114
 
1475
- // src/actions/csrfToken/csrfToken.ts
1476
- var import_router6 = require("@aura-stack/router");
1477
- var getCSRFToken = (request, cookieName) => {
1478
- try {
1479
- return getCookie(request, cookieName);
1480
- } catch {
1481
- return void 0;
1482
- }
1115
+ // src/oauth/strava.ts
1116
+ var strava = (options2) => {
1117
+ return {
1118
+ id: "strava",
1119
+ name: "Strava",
1120
+ authorizeURL: "https://www.strava.com/oauth/authorize",
1121
+ accessToken: "https://www.strava.com/oauth/token",
1122
+ userInfo: "https://www.strava.com/api/v3/athlete",
1123
+ scope: "read",
1124
+ responseType: "code",
1125
+ profile(profile) {
1126
+ return {
1127
+ sub: profile.id.toString(),
1128
+ name: `${profile.firstname} ${profile.lastname}`,
1129
+ image: profile.profile,
1130
+ email: void 0
1131
+ };
1132
+ },
1133
+ ...options2
1134
+ };
1483
1135
  };
1484
- var csrfTokenAction = (0, import_router6.createEndpoint)("GET", "/csrfToken", async (ctx) => {
1485
- const {
1486
- request,
1487
- context: { jose, cookies, logger }
1488
- } = ctx;
1489
- const token = getCSRFToken(request, cookies.csrfToken.name);
1490
- logger?.log("CSRF_TOKEN_REQUESTED", { structuredData: { has_token: Boolean(token) } });
1491
- const csrfToken = await createCSRF(jose, token);
1492
- logger?.log("CSRF_TOKEN_ISSUED", { structuredData: { issued: Boolean(csrfToken) } });
1493
- const headers = new Headers(secureApiHeaders);
1494
- headers.append("Set-Cookie", setCookie(cookies.csrfToken.name, csrfToken, cookies.csrfToken.attributes));
1495
- return Response.json({ csrfToken }, { headers });
1496
- });
1497
1136
 
1498
- // src/logger.ts
1499
- var logMessages = {
1500
- ROUTER_INTERNAL_ERROR: {
1501
- facility: 10,
1502
- severity: "error",
1503
- msgId: "ROUTER_INTERNAL_ERROR",
1504
- message: "Unhandled router error while processing the request"
1505
- },
1506
- INVALID_REQUEST: {
1507
- facility: 10,
1508
- severity: "warning",
1509
- msgId: "INVALID_REQUEST",
1510
- message: "Request validation failed against the expected schema"
1511
- },
1512
- SERVER_ERROR: {
1513
- facility: 10,
1514
- severity: "error",
1515
- msgId: "SERVER_ERROR",
1516
- message: "Unexpected internal server error during authentication"
1517
- },
1518
- OAUTH_PROTOCOL_ERROR: {
1519
- facility: 10,
1520
- severity: "warning",
1521
- msgId: "OAUTH_PROTOCOL_ERROR",
1522
- message: "OAuth provider returned an invalid or unexpected protocol response"
1523
- },
1524
- OAUTH_AUTHORIZATION_ERROR: {
1525
- facility: 10,
1526
- severity: "error",
1527
- msgId: "OAUTH_AUTHORIZATION_ERROR",
1528
- message: "OAuth authorization request was rejected or failed"
1529
- },
1530
- INVALID_OAUTH_CONFIGURATION: {
1531
- facility: 10,
1532
- severity: "error",
1533
- msgId: "INVALID_OAUTH_CONFIGURATION",
1534
- message: "The OAuth provider configuration is invalid or incomplete"
1535
- },
1536
- OAUTH_ACCESS_TOKEN_REQUEST_INITIATED: {
1537
- facility: 10,
1538
- severity: "debug",
1539
- msgId: "OAUTH_ACCESS_TOKEN_REQUEST_INITIATED",
1540
- message: "Starting OAuth access token request to the provider"
1541
- },
1542
- INVALID_OAUTH_ACCESS_TOKEN_RESPONSE: {
1543
- facility: 10,
1544
- severity: "error",
1545
- msgId: "INVALID_OAUTH_ACCESS_TOKEN_RESPONSE",
1546
- message: "OAuth access token endpoint returned an invalid or malformed response"
1547
- },
1548
- OAUTH_ACCESS_TOKEN_ERROR: {
1549
- facility: 10,
1550
- severity: "error",
1551
- msgId: "OAUTH_ACCESS_TOKEN_ERROR",
1552
- message: "OAuth access token endpoint returned an error response"
1553
- },
1554
- OAUTH_ACCESS_TOKEN_SUCCESS: {
1555
- facility: 10,
1556
- severity: "info",
1557
- msgId: "OAUTH_ACCESS_TOKEN_SUCCESS",
1558
- message: "Successfully retrieved OAuth access token from the provider"
1559
- },
1560
- OAUTH_ACCESS_TOKEN_REQUEST_FAILED: {
1561
- facility: 10,
1562
- severity: "error",
1563
- msgId: "OAUTH_ACCESS_TOKEN_REQUEST_FAILED",
1564
- message: "Network or server error while requesting OAuth access token"
1565
- },
1566
- OAUTH_USERINFO_REQUEST_INITIATED: {
1567
- facility: 10,
1568
- severity: "debug",
1569
- msgId: "OAUTH_USERINFO_REQUEST_INITIATED",
1570
- message: "Starting OAuth userinfo request to the provider"
1571
- },
1572
- OAUTH_USERINFO_INVALID_RESPONSE: {
1573
- facility: 10,
1574
- severity: "error",
1575
- msgId: "OAUTH_USERINFO_INVALID_RESPONSE",
1576
- message: "OAuth userinfo endpoint returned an invalid or malformed response"
1577
- },
1578
- OAUTH_USERINFO_ERROR: {
1579
- facility: 10,
1580
- severity: "error",
1581
- msgId: "OAUTH_USERINFO_ERROR",
1582
- message: "OAuth userinfo endpoint returned an error response"
1583
- },
1584
- OAUTH_USERINFO_SUCCESS: {
1585
- facility: 10,
1586
- severity: "info",
1587
- msgId: "OAUTH_USERINFO_SUCCESS",
1588
- message: "Successfully retrieved user information from the OAuth provider"
1589
- },
1590
- OAUTH_USERINFO_REQUEST_FAILED: {
1591
- facility: 10,
1592
- severity: "error",
1593
- msgId: "OAUTH_USERINFO_REQUEST_FAILED",
1594
- message: "Network or server error while requesting user information from the OAuth provider"
1595
- },
1596
- OAUTH_CALLBACK_SUCCESS: {
1597
- facility: 4,
1598
- severity: "info",
1599
- msgId: "OAUTH_CALLBACK_SUCCESS",
1600
- message: "OAuth callback completed successfully and session was created"
1601
- },
1602
- MISMATCHING_STATE: {
1603
- facility: 4,
1604
- severity: "critical",
1605
- msgId: "MISMATCHING_STATE",
1606
- message: "OAuth response state parameter does not match the stored state value"
1607
- },
1608
- POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED: {
1609
- facility: 4,
1610
- severity: "critical",
1611
- msgId: "POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED",
1612
- message: "Blocked redirect to untrusted or external URL (potential open redirect attack)"
1613
- },
1614
- OPEN_REDIRECT_ATTACK: {
1615
- facility: 4,
1616
- severity: "warning",
1617
- msgId: "OPEN_REDIRECT_ATTACK",
1618
- message: "Detected redirect target that does not match the trusted origin"
1619
- },
1620
- SESSION_TOKEN_MISSING: {
1621
- facility: 4,
1622
- severity: "warning",
1623
- msgId: "SESSION_TOKEN_MISSING",
1624
- message: "Session cookie is missing from the request"
1625
- },
1626
- CSRF_TOKEN_MISSING: {
1627
- facility: 4,
1628
- severity: "warning",
1629
- msgId: "CSRF_TOKEN_MISSING",
1630
- message: "CSRF token cookie is missing from the request"
1631
- },
1632
- CSRF_HEADER_MISSING: {
1633
- facility: 4,
1634
- severity: "warning",
1635
- msgId: "CSRF_HEADER_MISSING",
1636
- message: "CSRF header is missing from the request"
1637
- },
1638
- CSRF_TOKEN_INVALID: {
1639
- facility: 4,
1640
- severity: "error",
1641
- msgId: "CSRF_TOKEN_INVALID",
1642
- message: "CSRF token verification failed or token is invalid"
1643
- },
1644
- SIGN_IN_INITIATED: {
1645
- facility: 4,
1646
- severity: "info",
1647
- msgId: "SIGN_IN_INITIATED",
1648
- message: "Starting OAuth sign-in flow for the selected provider"
1649
- },
1650
- SIGN_OUT_ATTEMPT: {
1651
- facility: 4,
1652
- severity: "debug",
1653
- msgId: "SIGN_OUT_ATTEMPT",
1654
- message: "Received sign-out request from client"
1655
- },
1656
- SIGN_OUT_CSRF_VERIFIED: {
1657
- facility: 4,
1658
- severity: "info",
1659
- msgId: "SIGN_OUT_CSRF_VERIFIED",
1660
- message: "CSRF token was successfully verified during sign-out"
1661
- },
1662
- SIGN_OUT_SUCCESS: {
1663
- facility: 4,
1664
- severity: "info",
1665
- msgId: "SIGN_OUT_SUCCESS",
1666
- message: "User session was cleared and sign-out completed successfully"
1667
- },
1668
- SIGN_OUT_REDIRECT: {
1669
- facility: 4,
1670
- severity: "debug",
1671
- msgId: "SIGN_OUT_REDIRECT",
1672
- message: "Redirecting client after successful sign-out"
1673
- },
1674
- AUTH_SESSION_VALID: {
1675
- facility: 4,
1676
- severity: "info",
1677
- msgId: "AUTH_SESSION_VALID",
1678
- message: "Session token is valid and user session was returned"
1679
- },
1680
- AUTH_SESSION_INVALID: {
1681
- facility: 4,
1682
- severity: "notice",
1683
- msgId: "AUTH_SESSION_INVALID",
1684
- message: "Session token is missing, expired, or invalid"
1685
- },
1686
- INVALID_JWT_TOKEN: {
1687
- facility: 4,
1688
- severity: "warning",
1689
- msgId: "INVALID_JWT_TOKEN",
1690
- message: "JWT session token failed validation during sign-out"
1691
- },
1692
- CSRF_TOKEN_REQUESTED: {
1693
- facility: 4,
1694
- severity: "debug",
1695
- msgId: "CSRF_TOKEN_REQUESTED",
1696
- message: "Client requested a CSRF token"
1697
- },
1698
- CSRF_TOKEN_ISSUED: {
1699
- facility: 4,
1700
- severity: "debug",
1701
- msgId: "CSRF_TOKEN_ISSUED",
1702
- message: "Issued a new CSRF token to the client"
1703
- },
1704
- INVALID_URL: {
1705
- facility: 10,
1706
- severity: "error",
1707
- msgId: "INVALID_URL",
1708
- message: "Derived origin URL is invalid or malformed"
1709
- },
1710
- COOKIE_HTTPONLY_DISABLED: {
1711
- facility: 10,
1712
- severity: "critical",
1713
- msgId: "COOKIE_HTTPONLY_DISABLED",
1714
- message: "Cookie is configured without HttpOnly. This allows JavaScript access via document.cookie and increases XSS exposure."
1715
- },
1716
- COOKIE_WILDCARD_DOMAIN: {
1717
- facility: 10,
1718
- severity: "critical",
1719
- msgId: "COOKIE_WILDCARD_DOMAIN",
1720
- message: "Cookie 'Domain' is set to a wildcard, which is insecure and should be avoided."
1721
- },
1722
- COOKIE_SECURE_DISABLED: {
1723
- facility: 10,
1724
- severity: "warning",
1725
- msgId: "COOKIE_SECURE_DISABLED",
1726
- message: "Cookie is configured with 'Secure' but the request is not HTTPS. The 'Secure' attribute will be ignored by the browser."
1727
- },
1728
- COOKIE_SAMESITE_NONE_WITHOUT_SECURE: {
1729
- facility: 10,
1730
- severity: "warning",
1731
- msgId: "COOKIE_SAMESITE_NONE_WITHOUT_SECURE",
1732
- message: "Cookie uses SameSite=None without Secure. Falling back to SameSite=Lax for safer defaults."
1733
- },
1734
- COOKIE_INSECURE_IN_PRODUCTION: {
1735
- facility: 10,
1736
- severity: "critical",
1737
- msgId: "COOKIE_INSECURE_IN_PRODUCTION",
1738
- message: "Cookies are being served over an insecure connection in production, which is a serious security risk."
1739
- },
1740
- COOKIE_HOST_STRATEGY_INSECURE: {
1741
- facility: 10,
1742
- severity: "critical",
1743
- msgId: "COOKIE_HOST_STRATEGY_INSECURE",
1744
- message: "__Host- cookies require a secure HTTPS context. Falling back to standard cookie settings."
1745
- },
1746
- UNTRUSTED_ORIGIN: {
1747
- facility: 10,
1748
- severity: "error",
1749
- msgId: "UNTRUSTED_ORIGIN",
1750
- message: "The constructed origin URL is not trusted."
1137
+ // src/oauth/mailchimp.ts
1138
+ var mailchimp = (options2) => {
1139
+ return {
1140
+ id: "mailchimp",
1141
+ name: "Mailchimp",
1142
+ authorizeURL: "https://login.mailchimp.com/oauth2/authorize",
1143
+ accessToken: "https://login.mailchimp.com/oauth2/token",
1144
+ userInfo: "https://login.mailchimp.com/oauth2/metadata",
1145
+ scope: "",
1146
+ responseType: "code",
1147
+ profile(profile) {
1148
+ return {
1149
+ sub: profile.user_id,
1150
+ name: profile.accountname,
1151
+ email: profile.login.email,
1152
+ image: profile.login.avatar
1153
+ };
1154
+ },
1155
+ ...options2
1156
+ };
1157
+ };
1158
+
1159
+ // src/oauth/pinterest.ts
1160
+ var pinterest = (options2) => {
1161
+ return {
1162
+ id: "pinterest",
1163
+ name: "Pinterest",
1164
+ authorizeURL: "https://www.pinterest.com/oauth",
1165
+ accessToken: "https://api.pinterest.com/v5/oauth/token",
1166
+ userInfo: "https://api.pinterest.com/v5/user_account",
1167
+ scope: "user_accounts:read",
1168
+ responseType: "code",
1169
+ profile(profile) {
1170
+ return {
1171
+ sub: profile.id,
1172
+ name: profile.username,
1173
+ image: profile.profile_image,
1174
+ email: void 0
1175
+ };
1176
+ },
1177
+ ...options2
1178
+ };
1179
+ };
1180
+
1181
+ // src/oauth/twitch.ts
1182
+ var twitch = (options2) => {
1183
+ const clientId = options2?.clientId ?? getEnv("TWITCH_CLIENT_ID");
1184
+ return {
1185
+ id: "twitch",
1186
+ name: "Twitch",
1187
+ authorize: {
1188
+ url: "https://id.twitch.tv/oauth2/authorize",
1189
+ params: { scope: "user:read:email", responseType: "code" }
1190
+ },
1191
+ accessToken: "https://id.twitch.tv/oauth2/token",
1192
+ userInfo: {
1193
+ url: "https://api.twitch.tv/helix/users",
1194
+ headers: {
1195
+ "Client-ID": clientId
1196
+ }
1197
+ },
1198
+ profile(profile) {
1199
+ const user = profile.data[0];
1200
+ if (!user) {
1201
+ throw new Error("No user data found in Twitch profile response");
1202
+ }
1203
+ return {
1204
+ sub: user.id,
1205
+ name: user.display_name,
1206
+ email: user.email,
1207
+ picture: user.profile_image_url
1208
+ };
1209
+ },
1210
+ ...options2
1211
+ };
1212
+ };
1213
+
1214
+ // src/oauth/notion.ts
1215
+ var notion = (options2) => {
1216
+ return {
1217
+ id: "notion",
1218
+ name: "Notion",
1219
+ authorize: {
1220
+ url: "https://api.notion.com/v1/oauth/authorize",
1221
+ params: {
1222
+ owner: "user",
1223
+ scope: "user:read",
1224
+ responseType: "code"
1225
+ }
1226
+ },
1227
+ accessToken: {
1228
+ url: "https://api.notion.com/v1/oauth/token",
1229
+ headers: {
1230
+ Authorization: createBasicAuthHeader(
1231
+ options2?.clientId ?? "NOTION_CLIENT_ID",
1232
+ options2?.clientSecret ?? "NOTION_CLIENT_SECRET"
1233
+ )
1234
+ }
1235
+ },
1236
+ userInfo: {
1237
+ url: "https://api.notion.com/v1/users/me",
1238
+ headers: {
1239
+ "Notion-Version": "2022-06-28"
1240
+ }
1241
+ },
1242
+ profile(profile) {
1243
+ return {
1244
+ sub: profile.id,
1245
+ name: profile.name,
1246
+ image: profile.avatar_url ?? "",
1247
+ email: profile?.bot?.owner?.user?.person?.email
1248
+ };
1249
+ },
1250
+ ...options2
1251
+ };
1252
+ };
1253
+
1254
+ // src/oauth/dropbox.ts
1255
+ var dropbox = (options2) => {
1256
+ return {
1257
+ id: "dropbox",
1258
+ name: "Dropbox",
1259
+ authorize: {
1260
+ url: "https://www.dropbox.com/oauth2/authorize",
1261
+ params: { scope: "account_info.read" }
1262
+ },
1263
+ accessToken: "https://api.dropboxapi.com/oauth2/token",
1264
+ userInfo: {
1265
+ method: "POST",
1266
+ url: "https://api.dropboxapi.com/2/users/get_current_account"
1267
+ },
1268
+ profile(profile) {
1269
+ return {
1270
+ sub: profile.account_id,
1271
+ name: profile.name.display_name,
1272
+ email: profile.email,
1273
+ image: profile.profile_photo_url
1274
+ };
1275
+ },
1276
+ ...options2
1277
+ };
1278
+ };
1279
+
1280
+ // src/oauth/atlassian.ts
1281
+ var atlassian = (options2) => {
1282
+ return {
1283
+ id: "atlassian",
1284
+ name: "Atlassian",
1285
+ authorize: {
1286
+ url: "https://auth.atlassian.com/authorize",
1287
+ params: {
1288
+ audience: "api.atlassian.com",
1289
+ scope: "read:me read:account",
1290
+ prompt: "consent"
1291
+ }
1292
+ },
1293
+ authorizeURL: "https://auth.atlassian.com/authorize",
1294
+ accessToken: "https://auth.atlassian.com/oauth/token",
1295
+ userInfo: "https://api.atlassian.com/me",
1296
+ scope: "read:me read:account",
1297
+ responseType: "code",
1298
+ profile(profile) {
1299
+ return {
1300
+ sub: profile.account_id,
1301
+ name: profile.name,
1302
+ email: profile.email,
1303
+ image: profile.picture
1304
+ };
1305
+ },
1306
+ ...options2
1307
+ };
1308
+ };
1309
+
1310
+ // src/schemas.ts
1311
+ var import_v4 = require("zod/v4");
1312
+ var AuthorizeConfigSchema = import_v4.z.union([
1313
+ (0, import_v4.string)().url(),
1314
+ (0, import_v4.object)({
1315
+ url: (0, import_v4.string)().url(),
1316
+ params: (0, import_v4.object)({
1317
+ responseType: (0, import_v4.enum)(["code", "token", "id_token", "refresh_token"]).optional(),
1318
+ scope: (0, import_v4.string)().optional()
1319
+ })
1320
+ })
1321
+ ]);
1322
+ var AccessTokenConfigSchema = import_v4.z.union([
1323
+ (0, import_v4.string)().url(),
1324
+ (0, import_v4.object)({
1325
+ url: (0, import_v4.string)().url(),
1326
+ headers: import_v4.z.record((0, import_v4.string)(), (0, import_v4.string)()).optional()
1327
+ })
1328
+ ]);
1329
+ var UserInfoConfigSchema = import_v4.z.union([
1330
+ (0, import_v4.string)().url(),
1331
+ (0, import_v4.object)({
1332
+ url: (0, import_v4.string)().url(),
1333
+ headers: import_v4.z.record((0, import_v4.string)(), (0, import_v4.string)()).optional(),
1334
+ method: (0, import_v4.string)().optional()
1335
+ })
1336
+ ]);
1337
+ var OAuthProviderCredentialsSchema = (0, import_v4.object)({
1338
+ id: (0, import_v4.string)(),
1339
+ name: (0, import_v4.string)(),
1340
+ authorize: AuthorizeConfigSchema.optional(),
1341
+ /** @deprecated */
1342
+ authorizeURL: (0, import_v4.string)().url().optional(),
1343
+ accessToken: AccessTokenConfigSchema,
1344
+ /** @deprecated */
1345
+ scope: (0, import_v4.string)().optional(),
1346
+ userInfo: UserInfoConfigSchema,
1347
+ /** @deprecated */
1348
+ responseType: (0, import_v4.enum)(["code", "token", "id_token", "refresh_token"]).optional(),
1349
+ clientId: (0, import_v4.string)(),
1350
+ clientSecret: (0, import_v4.string)(),
1351
+ profile: import_v4.z.function().optional()
1352
+ });
1353
+ var OAuthProviderConfigSchema = (0, import_v4.object)({
1354
+ authorize: AuthorizeConfigSchema.optional(),
1355
+ /** @deprecated */
1356
+ authorizeURL: (0, import_v4.string)().url().optional(),
1357
+ accessToken: AccessTokenConfigSchema,
1358
+ /** @deprecated */
1359
+ scope: (0, import_v4.string)().optional(),
1360
+ userInfo: UserInfoConfigSchema,
1361
+ /** @deprecated */
1362
+ responseType: (0, import_v4.enum)(["code", "token", "id_token", "refresh_token"]).optional(),
1363
+ clientId: (0, import_v4.string)(),
1364
+ clientSecret: (0, import_v4.string)()
1365
+ });
1366
+ var OAuthAuthorization = OAuthProviderConfigSchema.extend({
1367
+ redirectURI: (0, import_v4.string)(),
1368
+ state: (0, import_v4.string)(),
1369
+ codeChallenge: (0, import_v4.string)(),
1370
+ codeChallengeMethod: (0, import_v4.enum)(["plain", "S256"])
1371
+ });
1372
+ var OAuthAuthorizationResponse = (0, import_v4.object)({
1373
+ state: (0, import_v4.string)({ message: "Missing state parameter in the OAuth authorization response." }),
1374
+ code: (0, import_v4.string)({ message: "Missing code parameter in the OAuth authorization response." })
1375
+ });
1376
+ var OAuthAuthorizationErrorResponse = (0, import_v4.object)({
1377
+ error: (0, import_v4.enum)([
1378
+ "invalid_request",
1379
+ "unauthorized_client",
1380
+ "access_denied",
1381
+ "unsupported_response_type",
1382
+ "invalid_scope",
1383
+ "server_error",
1384
+ "temporarily_unavailable"
1385
+ ]),
1386
+ error_description: (0, import_v4.string)().optional(),
1387
+ error_uri: (0, import_v4.string)().optional(),
1388
+ state: (0, import_v4.string)()
1389
+ });
1390
+ var OAuthAccessToken = OAuthProviderConfigSchema.extend({
1391
+ redirectURI: (0, import_v4.string)(),
1392
+ code: (0, import_v4.string)(),
1393
+ codeVerifier: (0, import_v4.string)().min(43).max(128)
1394
+ });
1395
+ var OAuthAccessTokenResponse = (0, import_v4.object)({
1396
+ access_token: (0, import_v4.string)(),
1397
+ token_type: (0, import_v4.string)().optional(),
1398
+ expires_in: (0, import_v4.number)().optional(),
1399
+ refresh_token: (0, import_v4.string)().optional(),
1400
+ scope: (0, import_v4.union)([(0, import_v4.string)().optional().or((0, import_v4.null)()), (0, import_v4.array)((0, import_v4.string)()).optional()])
1401
+ });
1402
+ var OAuthAccessTokenErrorResponse = (0, import_v4.object)({
1403
+ error: (0, import_v4.enum)([
1404
+ "invalid_request",
1405
+ "invalid_client",
1406
+ "invalid_grant",
1407
+ "unauthorized_client",
1408
+ "unsupported_grant_type",
1409
+ "invalid_scope"
1410
+ ]),
1411
+ error_description: (0, import_v4.string)().optional(),
1412
+ error_uri: (0, import_v4.string)().optional()
1413
+ });
1414
+ var OAuthErrorResponse = (0, import_v4.object)({
1415
+ error: (0, import_v4.string)(),
1416
+ error_description: (0, import_v4.string)().optional()
1417
+ });
1418
+ var OAuthEnvSchema = (0, import_v4.object)({
1419
+ clientId: import_v4.z.string().min(1, "OAuth Client ID is required in the environment variables."),
1420
+ clientSecret: import_v4.z.string().min(1, "OAuth Client Secret is required in the environment variables.")
1421
+ });
1422
+
1423
+ // src/oauth/index.ts
1424
+ var builtInOAuthProviders = {
1425
+ github,
1426
+ bitbucket,
1427
+ figma,
1428
+ discord,
1429
+ gitlab,
1430
+ spotify,
1431
+ x,
1432
+ strava,
1433
+ mailchimp,
1434
+ pinterest,
1435
+ twitch,
1436
+ notion,
1437
+ dropbox,
1438
+ atlassian
1439
+ };
1440
+ var defineOAuthEnvironment = (oauth) => {
1441
+ const loadEnvs = OAuthEnvSchema.safeParse({
1442
+ clientId: getEnv(`${oauth.toUpperCase()}_CLIENT_ID`),
1443
+ clientSecret: getEnv(`${oauth.toUpperCase()}_CLIENT_SECRET`)
1444
+ });
1445
+ if (!loadEnvs.success) {
1446
+ const msg = JSON.stringify({ [oauth]: formatZodError(loadEnvs.error) }, null, 2);
1447
+ throw new AuthInternalError("INVALID_ENVIRONMENT_CONFIGURATION", msg);
1448
+ }
1449
+ return loadEnvs.data;
1450
+ };
1451
+ var defineOAuthProviderConfig = (config2) => {
1452
+ if (typeof config2 === "string") {
1453
+ const definition = defineOAuthEnvironment(config2);
1454
+ const oauthConfig = builtInOAuthProviders[config2]();
1455
+ const parsed2 = OAuthProviderCredentialsSchema.safeParse({ ...oauthConfig, ...definition });
1456
+ if (!parsed2.success) {
1457
+ const details = JSON.stringify({ [config2]: formatZodError(parsed2.error) }, null, 2);
1458
+ throw new AuthInternalError(
1459
+ "INVALID_OAUTH_PROVIDER_CONFIGURATION",
1460
+ `Invalid configuration for OAuth provider "${config2}": ${details}`
1461
+ );
1462
+ }
1463
+ return parsed2.data;
1464
+ }
1465
+ const hasCredentials = config2.clientId && config2.clientSecret;
1466
+ const envConfig = hasCredentials ? {} : defineOAuthEnvironment(config2.id);
1467
+ const parsed = OAuthProviderCredentialsSchema.safeParse({ ...envConfig, ...config2 });
1468
+ if (!parsed.success) {
1469
+ const details = JSON.stringify({ [config2.id]: formatZodError(parsed.error) }, null, 2);
1470
+ throw new AuthInternalError(
1471
+ "INVALID_OAUTH_PROVIDER_CONFIGURATION",
1472
+ `Invalid configuration for OAuth provider "${config2.id}": ${details}`
1473
+ );
1474
+ }
1475
+ return parsed.data;
1476
+ };
1477
+ var createBuiltInOAuthProviders = (oauth = []) => {
1478
+ return oauth.reduce((previous, config2) => {
1479
+ const oauthConfig = defineOAuthProviderConfig(config2);
1480
+ if (oauthConfig.id in previous) {
1481
+ throw new AuthInternalError(
1482
+ "DUPLICATED_OAUTH_PROVIDER_ID",
1483
+ `Duplicate OAuth provider id "${oauthConfig.id}" found. Each provider must have a unique id.`
1484
+ );
1485
+ }
1486
+ return { ...previous, [oauthConfig.id]: oauthConfig };
1487
+ }, {});
1488
+ };
1489
+
1490
+ // src/context.ts
1491
+ var createContext = (config2) => {
1492
+ const trustedProxyHeadersEnv = getEnv("TRUSTED_PROXY_HEADERS");
1493
+ const useProxyHeaders = trustedProxyHeadersEnv === void 0 ? config2?.trustedProxyHeaders ?? false : getEnvBoolean("TRUSTED_PROXY_HEADERS");
1494
+ const logger = createProxyLogger(config2);
1495
+ const cookiePrefix = config2?.cookies?.prefix;
1496
+ const cookieOverrides = config2?.cookies?.overrides ?? {};
1497
+ const secureCookieStore = createCookieStore(true, cookiePrefix, cookieOverrides, logger);
1498
+ const standardCookieStore = createCookieStore(false, cookiePrefix, cookieOverrides, logger);
1499
+ return {
1500
+ oauth: createBuiltInOAuthProviders(config2?.oauth),
1501
+ cookies: standardCookieStore,
1502
+ jose: createJoseInstance(config2?.secret),
1503
+ secret: config2?.secret,
1504
+ basePath: config2?.basePath ?? "/auth",
1505
+ trustedProxyHeaders: useProxyHeaders,
1506
+ trustedOrigins: getEnvArray("TRUSTED_ORIGINS").length > 0 ? getEnvArray("TRUSTED_ORIGINS") : config2?.trustedOrigins,
1507
+ logger,
1508
+ cookieConfig: { secure: secureCookieStore, standard: standardCookieStore },
1509
+ baseURL: config2?.baseURL
1510
+ };
1511
+ };
1512
+
1513
+ // src/headers.ts
1514
+ var cacheControl = {
1515
+ "Cache-Control": "no-store",
1516
+ Pragma: "no-cache",
1517
+ Expires: "0",
1518
+ Vary: "Cookie"
1519
+ };
1520
+ var contentSecurityPolicy = {
1521
+ "Content-Security-Policy": [
1522
+ "default-src 'none'",
1523
+ "script-src 'self'",
1524
+ "frame-src 'none'",
1525
+ "object-src 'none'",
1526
+ "frame-ancestors 'none'",
1527
+ "base-uri 'none'"
1528
+ ].join("; ")
1529
+ };
1530
+ var secureHeaders = {
1531
+ "X-Content-Type-Options": "nosniff",
1532
+ "X-Frame-Options": "DENY",
1533
+ "Referrer-Policy": "strict-origin-when-cross-origin"
1534
+ };
1535
+ var secureApiHeaders = {
1536
+ ...cacheControl,
1537
+ ...contentSecurityPolicy,
1538
+ ...secureHeaders
1539
+ };
1540
+
1541
+ // src/api/signIn.ts
1542
+ var import_router2 = require("@aura-stack/router");
1543
+
1544
+ // src/secure.ts
1545
+ var generateSecure = (length = 32) => {
1546
+ return import_jose2.base64url.encode((0, import_crypto.getRandomBytes)(length));
1547
+ };
1548
+ var createSecretValue = (length = 32) => {
1549
+ return import_jose2.base64url.encode((0, import_crypto.getRandomBytes)(length));
1550
+ };
1551
+ var createHash = async (data) => {
1552
+ const subtle = (0, import_crypto.getSubtleCrypto)();
1553
+ const digest = await subtle.digest("SHA-256", import_crypto.encoder.encode(data));
1554
+ return import_jose2.base64url.encode(new Uint8Array(digest));
1555
+ };
1556
+ var createPKCE = async (verifier) => {
1557
+ const byteLength = verifier ? void 0 : Math.floor(Math.random() * (96 - 32 + 1) + 32);
1558
+ const codeVerifier = verifier ?? generateSecure(byteLength ?? 64);
1559
+ if (codeVerifier.length < 43 || codeVerifier.length > 128) {
1560
+ throw new AuthSecurityError("PKCE_VERIFIER_INVALID", "The code verifier must be between 43 and 128 characters in length.");
1561
+ }
1562
+ const codeChallenge = await createHash(codeVerifier);
1563
+ return { codeVerifier, codeChallenge, method: "S256" };
1564
+ };
1565
+ var createCSRF = async (jose, csrfCookie) => {
1566
+ try {
1567
+ const token = generateSecure(32);
1568
+ if (csrfCookie) {
1569
+ await jose.verifyJWS(csrfCookie, jwtVerificationOptions);
1570
+ return csrfCookie;
1571
+ }
1572
+ return jose.signJWS({ token });
1573
+ } catch {
1574
+ const token = generateSecure(32);
1575
+ return jose.signJWS({ token });
1576
+ }
1577
+ };
1578
+ var verifyCSRF = async (jose, cookie, header) => {
1579
+ try {
1580
+ const cookiePayload = await jose.verifyJWS(cookie, jwtVerificationOptions);
1581
+ const headerPayload = await jose.verifyJWS(header, jwtVerificationOptions);
1582
+ if (!isJWTPayloadWithToken(cookiePayload)) {
1583
+ throw new AuthSecurityError("CSRF_TOKEN_INVALID", "Cookie payload missing token field.");
1584
+ }
1585
+ if (!isJWTPayloadWithToken(headerPayload)) {
1586
+ throw new AuthSecurityError("CSRF_TOKEN_INVALID", "Header payload missing token field.");
1587
+ }
1588
+ if (!equals(cookiePayload.token.length, headerPayload.token.length)) {
1589
+ throw new AuthSecurityError("CSRF_TOKEN_INVALID", "The CSRF tokens do not match.");
1590
+ }
1591
+ if (!timingSafeEqual(cookiePayload.token, headerPayload.token)) {
1592
+ throw new AuthSecurityError("CSRF_TOKEN_INVALID", "The CSRF tokens do not match.");
1593
+ }
1594
+ return true;
1595
+ } catch {
1596
+ throw new AuthSecurityError("CSRF_TOKEN_INVALID", "The CSRF tokens do not match.");
1597
+ }
1598
+ };
1599
+
1600
+ // src/actions/signIn/authorization-url.ts
1601
+ var setSearchParams = (url, params) => {
1602
+ for (const [key, value] of Object.entries(params)) {
1603
+ if (value !== void 0 && value !== "") {
1604
+ url.searchParams.set(key, value);
1605
+ }
1606
+ }
1607
+ };
1608
+ var buildAuthorizationURL = (oauth, redirect_uri, state, code_challenge, code_challenge_method) => {
1609
+ const authorizeConfig = oauth.authorize;
1610
+ const baseURL = typeof authorizeConfig === "string" ? authorizeConfig : authorizeConfig?.url ?? oauth.authorizeURL;
1611
+ if (!baseURL) {
1612
+ throw new AuthInternalError("INVALID_OAUTH_CONFIGURATION", "Missing authorization URL in OAuth provider configuration.");
1613
+ }
1614
+ const url = new URL(baseURL);
1615
+ const authorizeParams = typeof authorizeConfig === "string" ? void 0 : authorizeConfig?.params;
1616
+ setSearchParams(url, {
1617
+ response_type: authorizeParams?.responseType ?? oauth.responseType ?? "code",
1618
+ client_id: oauth.clientId,
1619
+ redirect_uri,
1620
+ state,
1621
+ code_challenge,
1622
+ code_challenge_method,
1623
+ scope: authorizeParams?.scope ?? oauth.scope,
1624
+ prompt: authorizeParams?.prompt,
1625
+ response_mode: authorizeParams?.responseMode,
1626
+ login_hint: authorizeParams?.loginHint,
1627
+ nonce: authorizeParams?.nonce,
1628
+ display: authorizeParams?.display,
1629
+ audience: authorizeParams?.audience
1630
+ });
1631
+ return url.toString();
1632
+ };
1633
+ var createAuthorizationURL = async (oauth, redirectURI, ctx) => {
1634
+ const state = createSecretValue();
1635
+ const { codeVerifier, codeChallenge, method } = await createPKCE();
1636
+ const authorization = buildAuthorizationURL(oauth, redirectURI, state, codeChallenge, method);
1637
+ const parsed = OAuthAuthorization.safeParse({ ...oauth, redirectURI, state, codeChallenge, codeChallengeMethod: method });
1638
+ if (!parsed.success) {
1639
+ ctx?.logger?.log("INVALID_OAUTH_CONFIGURATION", {
1640
+ structuredData: {
1641
+ scope: oauth?.scope ?? "",
1642
+ redirect_uri: redirectURI,
1643
+ has_state: Boolean(state),
1644
+ has_code_challenge: Boolean(codeChallenge),
1645
+ code_challenge_method: method
1646
+ }
1647
+ });
1648
+ throw new AuthInternalError("INVALID_OAUTH_CONFIGURATION", "The OAuth provider configuration is invalid.");
1649
+ }
1650
+ return {
1651
+ authorization,
1652
+ state,
1653
+ codeVerifier,
1654
+ method
1655
+ };
1656
+ };
1657
+
1658
+ // src/actions/signIn/authorization.ts
1659
+ var getTrustedOrigins = async (request, trustedOrigins) => {
1660
+ if (!trustedOrigins) return [];
1661
+ const raw = typeof trustedOrigins === "function" ? await trustedOrigins(request) : trustedOrigins;
1662
+ return Array.isArray(raw) ? raw : typeof raw === "string" ? [raw] : [];
1663
+ };
1664
+ var getBaseURL2 = async ({
1665
+ ctx,
1666
+ request,
1667
+ headers: headersInit
1668
+ }) => {
1669
+ const origin = getEnv("BASE_URL") || ctx?.baseURL;
1670
+ if (origin && origin !== "/") return origin;
1671
+ if (ctx?.trustedProxyHeaders) {
1672
+ const headers = headersInit && new Headers(headersInit) || request?.headers;
1673
+ const protocol = headers?.get("Forwarded")?.match(/proto=([^;]+)/i)?.[1] ?? headers?.get("X-Forwarded-Proto") ?? "http";
1674
+ const host = headers?.get("Host") ?? headers?.get("Forwarded")?.match(/host=([^;]+)/i)?.[1] ?? headers?.get("X-Forwarded-Host") ?? null;
1675
+ if (host) return `${protocol}://${host}`;
1676
+ throw new AuthInternalError(
1677
+ "INVALID_OAUTH_CONFIGURATION",
1678
+ "The URL cannot be constructed. Please set the BASE_URL environment variable or provide trusted proxy host headers."
1679
+ );
1680
+ }
1681
+ try {
1682
+ return new URL(request?.url ?? "not-found").origin;
1683
+ } catch (error) {
1684
+ throw new AuthInternalError(
1685
+ "INVALID_OAUTH_CONFIGURATION",
1686
+ "The URL cannot be constructed. Please set the BASE_URL environment variable or enable trustedProxyHeaders.",
1687
+ { cause: error }
1688
+ );
1689
+ }
1690
+ };
1691
+ var getOriginURL = async (request, context) => {
1692
+ const trustedOrigins = await getTrustedOrigins(request, context?.trustedOrigins);
1693
+ trustedOrigins.push(new URL(request.url).origin);
1694
+ const origin = await getBaseURL2({ request, ctx: context });
1695
+ if (!isTrustedOrigin(origin, trustedOrigins)) {
1696
+ context?.logger?.log("UNTRUSTED_ORIGIN", { structuredData: { origin } });
1697
+ throw new AuthInternalError("UNTRUSTED_ORIGIN", "The constructed origin URL is not trusted.");
1698
+ }
1699
+ return origin;
1700
+ };
1701
+ var createRedirectURI = async (request, oauth, context) => {
1702
+ const origin = await getOriginURL(request, context);
1703
+ return `${origin}${context.basePath}/callback/${oauth}`;
1704
+ };
1705
+ var createSignInURL = async ({
1706
+ request,
1707
+ oauth,
1708
+ ctx,
1709
+ redirectTo
1710
+ }) => {
1711
+ const origin = await getOriginURL(request, ctx);
1712
+ const searchParams = new URLSearchParams();
1713
+ if (redirectTo !== void 0) searchParams.set("redirectTo", String(redirectTo));
1714
+ return `${origin}${ctx.basePath}/signIn/${oauth}?${searchParams.toString()}`;
1715
+ };
1716
+ var createRedirectTo = async (request, redirectTo, context) => {
1717
+ try {
1718
+ const headers = request.headers;
1719
+ const requestOrigin = await getOriginURL(request, context);
1720
+ const origins = await getTrustedOrigins(request, context?.trustedOrigins);
1721
+ const validateURL = (url) => {
1722
+ if (!isRelativeURL(url) && !isValidURL(url)) return "/";
1723
+ if (isRelativeURL(url)) return url;
1724
+ if (origins.length > 0) {
1725
+ if (isTrustedOrigin(url, origins)) {
1726
+ const urlOrigin = new URL(url).origin;
1727
+ for (const pattern of origins) {
1728
+ const regex = patternToRegex(pattern);
1729
+ if (regex?.test(urlOrigin)) {
1730
+ return isSameOrigin(url, request.url) ? extractPath(url) : url;
1731
+ }
1732
+ if (isValidURL(pattern) && equals(new URL(pattern).origin, urlOrigin)) return url;
1733
+ }
1734
+ }
1735
+ context?.logger?.log("OPEN_REDIRECT_ATTACK");
1736
+ return "/";
1737
+ }
1738
+ if (isSameOrigin(url, requestOrigin)) {
1739
+ return extractPath(url);
1740
+ }
1741
+ context?.logger?.log("OPEN_REDIRECT_ATTACK");
1742
+ return "/";
1743
+ };
1744
+ return validateURL(redirectTo ?? headers.get("Referer") ?? headers.get("Origin") ?? "/");
1745
+ } catch (error) {
1746
+ context?.logger?.log("POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED");
1747
+ return "/";
1748
+ }
1749
+ };
1750
+
1751
+ // src/api/signIn.ts
1752
+ var signIn = async (oauth, {
1753
+ ctx,
1754
+ headers: headersInit,
1755
+ redirectTo = "/",
1756
+ redirect,
1757
+ request: requestInit
1758
+ }) => {
1759
+ const headers = new Headers(headersInit);
1760
+ const provider = ctx.oauth[oauth];
1761
+ if (!provider) {
1762
+ throw new AuthInternalError("INVALID_OAUTH_CONFIGURATION", `The OAuth provider "${oauth}" is not configured.`);
1763
+ }
1764
+ let request = requestInit;
1765
+ if (!request) {
1766
+ const origin = await getBaseURL2({ ctx, headers });
1767
+ const url = `${origin}${ctx.basePath}/signIn/${oauth}`;
1768
+ request = new Request(url, { headers });
1769
+ }
1770
+ if (redirect === false) {
1771
+ const signInURL = await createSignInURL({ request, oauth, ctx, redirectTo });
1772
+ return { redirect: false, signInURL };
1773
+ }
1774
+ const redirectURI = await createRedirectURI(request, oauth, ctx);
1775
+ const redirectToValue = await createRedirectTo(request, redirectTo, ctx);
1776
+ const { authorization, state, codeVerifier } = await createAuthorizationURL(provider, redirectURI, ctx);
1777
+ ctx?.logger?.log("SIGN_IN_INITIATED", {
1778
+ structuredData: { oauth_provider: oauth }
1779
+ });
1780
+ const headersList = new import_router2.HeadersBuilder(cacheControl).setHeader("Location", authorization).setCookie(ctx.cookies.state.name, state, ctx.cookies.state.attributes).setCookie(ctx.cookies.redirectURI.name, redirectURI, ctx.cookies.redirectURI.attributes).setCookie(ctx.cookies.redirectTo.name, redirectToValue, ctx.cookies.redirectTo.attributes).setCookie(ctx.cookies.codeVerifier.name, codeVerifier, ctx.cookies.codeVerifier.attributes).toHeaders();
1781
+ return Response.json(
1782
+ { redirect: redirect ?? true, signInURL: authorization },
1783
+ {
1784
+ status: redirect ?? true ? 302 : 200,
1785
+ headers: headersList
1786
+ }
1787
+ );
1788
+ };
1789
+
1790
+ // src/api/signOut.ts
1791
+ var import_router3 = require("@aura-stack/router");
1792
+ var signOut = async ({
1793
+ ctx,
1794
+ headers: headersInit,
1795
+ redirectTo = "/",
1796
+ skipCSRFCheck = false
1797
+ }) => {
1798
+ const headers = new Headers(headersInit);
1799
+ const header = headers.get("X-CSRF-Token");
1800
+ let session = null;
1801
+ let csrfToken = null;
1802
+ try {
1803
+ session = getCookie(headers, ctx.cookies.sessionToken.name);
1804
+ } catch {
1805
+ throw new AuthSecurityError("SESSION_TOKEN_MISSING", "The sessionToken is missing.");
1806
+ }
1807
+ try {
1808
+ csrfToken = getCookie(headers, ctx.cookies.csrfToken.name);
1809
+ } catch {
1810
+ throw new AuthSecurityError("CSRF_TOKEN_MISSING", "The CSRF token is missing.");
1811
+ }
1812
+ ctx?.logger?.log("SIGN_OUT_ATTEMPT", {
1813
+ structuredData: {
1814
+ has_session: Boolean(session),
1815
+ has_csrf_token: Boolean(csrfToken),
1816
+ has_csrf_header: Boolean(header),
1817
+ skip_csrf_check: skipCSRFCheck
1818
+ }
1819
+ });
1820
+ if (!session) {
1821
+ ctx?.logger?.log("SESSION_TOKEN_MISSING");
1822
+ throw new AuthSecurityError("SESSION_TOKEN_MISSING", "The sessionToken is missing.");
1823
+ }
1824
+ if (!skipCSRFCheck) {
1825
+ if (!csrfToken) {
1826
+ ctx?.logger?.log("CSRF_TOKEN_MISSING");
1827
+ throw new AuthSecurityError("CSRF_TOKEN_MISSING", "The CSRF token is missing.");
1828
+ }
1829
+ if (!header) {
1830
+ ctx?.logger?.log("CSRF_HEADER_MISSING");
1831
+ throw new AuthSecurityError("CSRF_HEADER_MISSING", "The CSRF header is missing.");
1832
+ }
1833
+ try {
1834
+ await verifyCSRF(ctx.jose, csrfToken, header);
1835
+ } catch (error) {
1836
+ ctx?.logger?.log("CSRF_TOKEN_INVALID", { structuredData: { error_type: getErrorName(error) } });
1837
+ throw new AuthSecurityError("CSRF_TOKEN_INVALID", "CSRF token verification failed");
1838
+ }
1839
+ ctx?.logger?.log("SIGN_OUT_CSRF_VERIFIED");
1840
+ } else {
1841
+ try {
1842
+ await ctx.jose.verifyJWS(csrfToken);
1843
+ } catch (error) {
1844
+ ctx?.logger?.log("CSRF_TOKEN_INVALID", { structuredData: { error_type: getErrorName(error) } });
1845
+ throw new AuthSecurityError("CSRF_TOKEN_INVALID", "CSRF token verification failed");
1846
+ }
1847
+ }
1848
+ try {
1849
+ await ctx.jose.decodeJWT(session);
1850
+ ctx?.logger?.log("SIGN_OUT_SUCCESS");
1851
+ } catch (error) {
1852
+ ctx?.logger?.log("INVALID_JWT_TOKEN", { structuredData: { error_type: getErrorName(error) } });
1853
+ }
1854
+ const headersList = new import_router3.HeadersBuilder(secureApiHeaders).setHeader("Location", redirectTo).setCookie(ctx.cookies.csrfToken.name, "", expiredCookieAttributes).setCookie(ctx.cookies.sessionToken.name, "", expiredCookieAttributes).toHeaders();
1855
+ return Response.json(
1856
+ { redirect: Boolean(redirectTo), url: redirectTo },
1857
+ {
1858
+ status: 202,
1859
+ headers: headersList
1860
+ }
1861
+ );
1862
+ };
1863
+
1864
+ // src/api/getSession.ts
1865
+ var getSession = async ({ ctx, headers }) => {
1866
+ try {
1867
+ const session = getCookie(new Headers(headers), ctx.cookies.sessionToken.name);
1868
+ const decoded = await ctx.jose.decodeJWT(session);
1869
+ ctx?.logger?.log("AUTH_SESSION_VALID");
1870
+ const { exp, iat, jti, nbf, aud, iss, ...user } = decoded;
1871
+ return {
1872
+ session: {
1873
+ user,
1874
+ expires: toISOString(exp * 1e3)
1875
+ },
1876
+ authenticated: true
1877
+ };
1878
+ } catch (error) {
1879
+ ctx?.logger?.log("AUTH_SESSION_INVALID", { structuredData: { error_type: getErrorName(error) } });
1880
+ return { session: null, authenticated: false };
1881
+ }
1882
+ };
1883
+
1884
+ // src/api/createApi.ts
1885
+ var createAuthAPI = (ctx) => {
1886
+ return {
1887
+ getSession: async (options2) => {
1888
+ const session = await getSession({ ctx, headers: options2.headers });
1889
+ return session;
1890
+ },
1891
+ signIn: async (oauth, options2) => {
1892
+ return signIn(oauth, {
1893
+ ctx,
1894
+ headers: options2?.headers,
1895
+ request: options2?.request,
1896
+ redirect: options2?.redirect,
1897
+ redirectTo: options2?.redirectTo
1898
+ });
1899
+ },
1900
+ signOut: async (options2) => {
1901
+ const redirectTo = validateRedirectTo(options2?.redirectTo ?? "/");
1902
+ return signOut({ ctx, headers: options2.headers, redirectTo, skipCSRFCheck: true });
1903
+ }
1904
+ };
1905
+ };
1906
+
1907
+ // src/actions/signIn/signIn.ts
1908
+ var import_v42 = require("zod/v4");
1909
+ var import_router4 = require("@aura-stack/router");
1910
+ var signInConfig = (oauth) => {
1911
+ return (0, import_router4.createEndpointConfig)("/signIn/:oauth", {
1912
+ schemas: {
1913
+ params: import_v42.z.object({
1914
+ oauth: import_v42.z.enum(
1915
+ Object.keys(oauth),
1916
+ "The OAuth provider is not supported or invalid."
1917
+ )
1918
+ }),
1919
+ searchParams: import_v42.z.object({
1920
+ redirect: import_v42.z.stringbool().optional().default(true),
1921
+ redirectTo: import_v42.z.string().optional()
1922
+ })
1923
+ }
1924
+ });
1925
+ };
1926
+ var signInAction = (oauth) => {
1927
+ return (0, import_router4.createEndpoint)(
1928
+ "GET",
1929
+ "/signIn/:oauth",
1930
+ async (ctx) => {
1931
+ const {
1932
+ request,
1933
+ params: { oauth: oauth2 },
1934
+ searchParams: { redirectTo, redirect },
1935
+ context
1936
+ } = ctx;
1937
+ const signInResult = await signIn(oauth2, {
1938
+ ctx: context,
1939
+ headers: request.headers,
1940
+ redirect,
1941
+ redirectTo,
1942
+ request
1943
+ });
1944
+ if (!redirect) {
1945
+ return Response.json(signInResult, { status: 200 });
1946
+ }
1947
+ return signInResult;
1948
+ },
1949
+ signInConfig(oauth)
1950
+ );
1951
+ };
1952
+
1953
+ // src/actions/callback/callback.ts
1954
+ var import_v43 = require("zod/v4");
1955
+ var import_router5 = require("@aura-stack/router");
1956
+
1957
+ // src/request.ts
1958
+ var fetchAsync = async (url, options2 = {}, timeout = 5e3) => {
1959
+ const controller = new AbortController();
1960
+ const timeoutId = setTimeout(() => controller.abort(), timeout);
1961
+ const response = await fetch(url, {
1962
+ ...options2,
1963
+ signal: controller.signal
1964
+ }).finally(() => clearTimeout(timeoutId));
1965
+ return response;
1966
+ };
1967
+
1968
+ // src/actions/callback/userinfo.ts
1969
+ var getDefaultUserInfo = (profile) => {
1970
+ const sub = generateSecure(16);
1971
+ return {
1972
+ sub: profile?.id ?? profile?.sub ?? sub,
1973
+ email: profile?.email,
1974
+ name: profile?.name ?? profile?.username ?? profile?.nickname,
1975
+ image: profile?.image ?? profile?.picture
1976
+ };
1977
+ };
1978
+ var getUserInfo = async (oauthConfig, accessToken, logger) => {
1979
+ const userInfoConfig = oauthConfig.userInfo;
1980
+ const userinfoURL = typeof userInfoConfig === "string" ? userInfoConfig : userInfoConfig.url;
1981
+ const extraHeaders = typeof userInfoConfig === "string" ? void 0 : userInfoConfig.headers;
1982
+ const method = typeof userInfoConfig === "string" ? "GET" : (userInfoConfig.method ?? "GET").toUpperCase();
1983
+ try {
1984
+ logger?.log("OAUTH_USERINFO_REQUEST_INITIATED", {
1985
+ structuredData: {
1986
+ endpoint: userinfoURL
1987
+ }
1988
+ });
1989
+ const response = await fetchAsync(userinfoURL, {
1990
+ method,
1991
+ headers: {
1992
+ "User-Agent": `Aura Auth/${AURA_AUTH_VERSION}`,
1993
+ Accept: "application/json",
1994
+ Authorization: `Bearer ${accessToken}`,
1995
+ ...extraHeaders ?? {}
1996
+ }
1997
+ });
1998
+ if (!response.ok) {
1999
+ logger?.log("OAUTH_USERINFO_INVALID_RESPONSE");
2000
+ throw new OAuthProtocolError("INVALID_REQUEST", "Invalid userinfo response format");
2001
+ }
2002
+ const json = await response.json();
2003
+ const { success, data } = OAuthErrorResponse.safeParse(json);
2004
+ if (success) {
2005
+ logger?.log("OAUTH_USERINFO_ERROR", {
2006
+ message: "Error response received from OAuth userinfo endpoint",
2007
+ structuredData: {
2008
+ error: data.error,
2009
+ error_description: data.error_description ?? ""
2010
+ }
2011
+ });
2012
+ throw new OAuthProtocolError("INVALID_REQUEST", "An error was received from the OAuth userinfo endpoint.");
2013
+ }
2014
+ logger?.log("OAUTH_USERINFO_SUCCESS");
2015
+ return oauthConfig?.profile ? oauthConfig.profile(json) : getDefaultUserInfo(json);
2016
+ } catch (error) {
2017
+ if (isOAuthProtocolError(error)) {
2018
+ throw error;
2019
+ }
2020
+ logger?.log("OAUTH_USERINFO_REQUEST_FAILED");
2021
+ if (isNativeError(error)) {
2022
+ throw new OAuthProtocolError("SERVER_ERROR", "Failed to fetch user information from OAuth provider", "", {
2023
+ cause: error
2024
+ });
2025
+ }
2026
+ throw new OAuthProtocolError("SERVER_ERROR", "Failed to fetch user information", "", { cause: error });
2027
+ }
2028
+ };
2029
+
2030
+ // src/actions/callback/access-token.ts
2031
+ var createAccessToken = async (oauthConfig, redirectURI, code, codeVerifier, logger) => {
2032
+ const { accessToken, clientId, clientSecret } = oauthConfig;
2033
+ if (!clientId || !clientSecret || !redirectURI || !code || !codeVerifier || !accessToken) {
2034
+ logger?.log("INVALID_OAUTH_CONFIGURATION", {
2035
+ structuredData: {
2036
+ has_client_id: Boolean(clientId),
2037
+ has_client_secret: Boolean(clientSecret),
2038
+ has_access_token: Boolean(accessToken),
2039
+ has_redirect_uri: Boolean(redirectURI),
2040
+ has_code: Boolean(code),
2041
+ has_code_verifier: Boolean(codeVerifier)
2042
+ }
2043
+ });
2044
+ throw new AuthInternalError("INVALID_OAUTH_CONFIGURATION", "The OAuth provider configuration is invalid.");
2045
+ }
2046
+ const tokenURL = typeof accessToken === "string" ? accessToken : accessToken.url;
2047
+ const extraHeaders = typeof accessToken === "string" ? void 0 : accessToken.headers;
2048
+ try {
2049
+ logger?.log("OAUTH_ACCESS_TOKEN_REQUEST_INITIATED", {
2050
+ structuredData: {
2051
+ has_client_id: Boolean(clientId),
2052
+ redirect_uri: redirectURI,
2053
+ grant_type: "authorization_code"
2054
+ }
2055
+ });
2056
+ const response = await fetchAsync(tokenURL, {
2057
+ method: "POST",
2058
+ headers: {
2059
+ ...extraHeaders ?? {},
2060
+ Accept: "application/json",
2061
+ "Content-Type": "application/x-www-form-urlencoded"
2062
+ },
2063
+ body: new URLSearchParams({
2064
+ client_id: clientId,
2065
+ client_secret: clientSecret,
2066
+ code,
2067
+ redirect_uri: redirectURI,
2068
+ grant_type: "authorization_code",
2069
+ code_verifier: codeVerifier
2070
+ }).toString()
2071
+ });
2072
+ if (!response.ok) {
2073
+ logger?.log("INVALID_OAUTH_ACCESS_TOKEN_RESPONSE");
2074
+ throw new OAuthProtocolError("invalid_request", "Invalid access token response");
2075
+ }
2076
+ const json = await response.json();
2077
+ const token = OAuthAccessTokenResponse.safeParse(json);
2078
+ if (!token.success) {
2079
+ const { success, data } = OAuthAccessTokenErrorResponse.safeParse(json);
2080
+ if (!success) {
2081
+ logger?.log("INVALID_OAUTH_ACCESS_TOKEN_RESPONSE");
2082
+ throw new OAuthProtocolError("invalid_request", "Invalid access token response format");
2083
+ }
2084
+ logger?.log("OAUTH_ACCESS_TOKEN_ERROR", {
2085
+ structuredData: {
2086
+ error: data.error,
2087
+ error_description: data.error_description ?? ""
2088
+ }
2089
+ });
2090
+ throw new OAuthProtocolError("INVALID_ACCESS_TOKEN", "Failed to retrieve access token");
2091
+ }
2092
+ logger?.log("OAUTH_ACCESS_TOKEN_SUCCESS");
2093
+ return token.data;
2094
+ } catch (error) {
2095
+ logger?.log("OAUTH_ACCESS_TOKEN_REQUEST_FAILED");
2096
+ if (error instanceof Error) {
2097
+ throw new OAuthProtocolError("server_error", "Failed to communicate with OAuth provider", "", { cause: error });
2098
+ }
2099
+ throw error;
1751
2100
  }
1752
2101
  };
1753
- var createLogEntry = (key, overrides) => {
1754
- const message = logMessages[key];
1755
- return {
1756
- ...message,
1757
- ...overrides
1758
- };
1759
- };
1760
2102
 
1761
- // src/index.ts
1762
- var logLevelToSeverity = {
1763
- debug: ["debug", "info", "notice", "warning", "error", "critical", "alert", "emergency"],
1764
- info: ["info", "notice", "warning", "error", "critical", "alert", "emergency"],
1765
- warn: ["warning", "error", "critical", "alert", "emergency"],
1766
- error: ["error", "critical", "alert", "emergency"]
2103
+ // src/actions/callback/callback.ts
2104
+ var callbackConfig = (oauth) => {
2105
+ return (0, import_router5.createEndpointConfig)("/callback/:oauth", {
2106
+ schemas: {
2107
+ params: import_v43.z.object({
2108
+ oauth: import_v43.z.enum(
2109
+ Object.keys(oauth),
2110
+ "The OAuth provider is not supported or invalid."
2111
+ )
2112
+ }),
2113
+ searchParams: import_v43.z.object({
2114
+ code: import_v43.z.string("Missing code parameter in the OAuth authorization response."),
2115
+ state: import_v43.z.string("Missing state parameter in the OAuth authorization response.")
2116
+ })
2117
+ },
2118
+ use: [
2119
+ (ctx) => {
2120
+ const {
2121
+ searchParams,
2122
+ context: { logger }
2123
+ } = ctx;
2124
+ const response = OAuthAuthorizationErrorResponse.safeParse(searchParams);
2125
+ if (response.success) {
2126
+ const { error, error_description } = response.data;
2127
+ const criticalAuthErrors = ["access_denied", "server_error"];
2128
+ const severity = criticalAuthErrors.includes(error.toLowerCase()) ? "critical" : "warning";
2129
+ logger?.log("OAUTH_AUTHORIZATION_ERROR", {
2130
+ severity,
2131
+ structuredData: {
2132
+ error,
2133
+ error_description: error_description ?? ""
2134
+ }
2135
+ });
2136
+ throw new OAuthProtocolError(error, error_description || "OAuth Authorization Error");
2137
+ }
2138
+ return ctx;
2139
+ }
2140
+ ]
2141
+ });
1767
2142
  };
1768
- var createLoggerProxy = (logger) => {
1769
- if (!logger) return void 0;
1770
- const level = logger.level;
1771
- const allowedSeverities = logLevelToSeverity[level] ?? [];
1772
- const internalLogger = {
1773
- level,
1774
- log(key, overrides) {
1775
- const entry = createLogEntry(key, overrides);
1776
- if (!allowedSeverities.includes(entry.severity)) return entry;
1777
- logger.log({
1778
- timestamp: entry.timestamp ?? (/* @__PURE__ */ new Date()).toISOString(),
1779
- appName: entry.appName ?? "aura-auth",
1780
- hostname: entry.hostname ?? "aura-auth",
1781
- ...entry
2143
+ var callbackAction = (oauth) => {
2144
+ return (0, import_router5.createEndpoint)(
2145
+ "GET",
2146
+ "/callback/:oauth",
2147
+ async (ctx) => {
2148
+ const {
2149
+ request,
2150
+ params: { oauth: oauth2 },
2151
+ searchParams: { code, state },
2152
+ context
2153
+ } = ctx;
2154
+ const { oauth: providers, cookies, jose, logger, trustedOrigins } = context;
2155
+ const oauthConfig = providers[oauth2];
2156
+ const cookieState = getCookie(request, cookies.state.name);
2157
+ const codeVerifier = getCookie(request, cookies.codeVerifier.name);
2158
+ const cookieRedirectTo = getCookie(request, cookies.redirectTo.name);
2159
+ const cookieRedirectURI = getCookie(request, cookies.redirectURI.name);
2160
+ if (!timingSafeEqual(cookieState, state)) {
2161
+ logger?.log("MISMATCHING_STATE", {
2162
+ structuredData: {
2163
+ oauth_provider: oauth2
2164
+ }
2165
+ });
2166
+ throw new AuthSecurityError(
2167
+ "MISMATCHING_STATE",
2168
+ "The provided state passed in the OAuth response does not match the stored state."
2169
+ );
2170
+ }
2171
+ const accessToken = await createAccessToken(oauthConfig, cookieRedirectURI, code, codeVerifier, logger);
2172
+ const origins = await getTrustedOrigins(request, trustedOrigins);
2173
+ const requestOrigin = await getOriginURL(request, context);
2174
+ if (!isRelativeURL(cookieRedirectTo)) {
2175
+ const isValid = origins.length > 0 ? isTrustedOrigin(cookieRedirectTo, origins) : isSameOrigin(cookieRedirectTo, requestOrigin);
2176
+ if (!isValid) {
2177
+ logger?.log("POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED", {
2178
+ structuredData: {
2179
+ redirect_path: cookieRedirectTo,
2180
+ provider: oauth2,
2181
+ has_trusted_origins: origins.length > 0,
2182
+ request_origin: requestOrigin
2183
+ }
2184
+ });
2185
+ throw new AuthSecurityError(
2186
+ "POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED",
2187
+ "Invalid redirect path. Potential open redirect attack detected."
2188
+ );
2189
+ }
2190
+ }
2191
+ const userInfo = await getUserInfo(oauthConfig, accessToken.access_token, logger);
2192
+ const sessionCookie = await createSessionCookie(jose, userInfo);
2193
+ const csrfToken = await createCSRF(jose);
2194
+ logger?.log("OAUTH_CALLBACK_SUCCESS", {
2195
+ structuredData: {
2196
+ provider: oauth2
2197
+ }
1782
2198
  });
1783
- return entry;
2199
+ const headers = new import_router5.HeadersBuilder(cacheControl).setHeader("Location", cookieRedirectTo).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();
2200
+ return Response.json({ oauth: oauth2 }, { status: 302, headers });
2201
+ },
2202
+ callbackConfig(oauth)
2203
+ );
2204
+ };
2205
+
2206
+ // src/actions/session/session.ts
2207
+ var import_router6 = require("@aura-stack/router");
2208
+ var sessionAction = (0, import_router6.createEndpoint)("GET", "/session", async (ctx) => {
2209
+ const {
2210
+ request,
2211
+ context: { cookies }
2212
+ } = ctx;
2213
+ try {
2214
+ const session = await getSession({ ctx: ctx.context, headers: request.headers });
2215
+ if (!session.authenticated) {
2216
+ throw new AuthInternalError("INVALID_JWT_TOKEN", "Session not authenticated");
1784
2217
  }
1785
- };
1786
- return internalLogger;
2218
+ return Response.json(session, { headers: secureApiHeaders });
2219
+ } catch (error) {
2220
+ const headers = new import_router6.HeadersBuilder(secureApiHeaders).setCookie(cookies.sessionToken.name, "", expiredCookieAttributes).toHeaders();
2221
+ return Response.json({ session: null, authenticated: false }, { status: 401, headers });
2222
+ }
2223
+ });
2224
+
2225
+ // src/actions/signOut/signOut.ts
2226
+ var import_v44 = require("zod/v4");
2227
+ var import_router7 = require("@aura-stack/router");
2228
+ var config = (0, import_router7.createEndpointConfig)({
2229
+ schemas: {
2230
+ searchParams: import_v44.z.object({
2231
+ token_type_hint: import_v44.z.literal("session_token"),
2232
+ redirectTo: import_v44.z.string().optional()
2233
+ })
2234
+ }
2235
+ });
2236
+ var signOutAction = (0, import_router7.createEndpoint)(
2237
+ "POST",
2238
+ "/signOut",
2239
+ async (ctx) => {
2240
+ const {
2241
+ request,
2242
+ searchParams: { redirectTo },
2243
+ context
2244
+ } = ctx;
2245
+ const baseURL = getBaseURL(request);
2246
+ const location = await createRedirectTo(
2247
+ new Request(baseURL, {
2248
+ headers: request.headers
2249
+ }),
2250
+ redirectTo,
2251
+ context
2252
+ );
2253
+ return await signOut({
2254
+ ctx: context,
2255
+ headers: request.headers,
2256
+ redirectTo: location
2257
+ });
2258
+ },
2259
+ config
2260
+ );
2261
+
2262
+ // src/actions/csrfToken/csrfToken.ts
2263
+ var import_router8 = require("@aura-stack/router");
2264
+ var getCSRFToken = (request, cookieName) => {
2265
+ try {
2266
+ return getCookie(request, cookieName);
2267
+ } catch {
2268
+ return void 0;
2269
+ }
1787
2270
  };
2271
+ var csrfTokenAction = (0, import_router8.createEndpoint)("GET", "/csrfToken", async (ctx) => {
2272
+ const {
2273
+ request,
2274
+ context: { jose, cookies, logger }
2275
+ } = ctx;
2276
+ const token = getCSRFToken(request, cookies.csrfToken.name);
2277
+ logger?.log("CSRF_TOKEN_REQUESTED", { structuredData: { has_token: Boolean(token) } });
2278
+ const csrfToken = await createCSRF(jose, token);
2279
+ logger?.log("CSRF_TOKEN_ISSUED", { structuredData: { issued: Boolean(csrfToken) } });
2280
+ const headers = new Headers(secureApiHeaders);
2281
+ headers.append("Set-Cookie", setCookie(cookies.csrfToken.name, csrfToken, cookies.csrfToken.attributes));
2282
+ return Response.json({ csrfToken }, { headers });
2283
+ });
2284
+
2285
+ // src/createAuth.ts
1788
2286
  var createInternalConfig = (authConfig) => {
1789
- const useSecure = authConfig?.trustedProxyHeaders ?? false;
1790
- const logger = authConfig?.logger;
1791
- const internalLogger = createLoggerProxy(logger);
2287
+ const context = createContext(authConfig);
1792
2288
  return {
1793
2289
  basePath: authConfig?.basePath ?? "/auth",
1794
- onError: createErrorHandler(internalLogger),
1795
- context: {
1796
- oauth: createBuiltInOAuthProviders(authConfig?.oauth),
1797
- cookies: createCookieStore(
1798
- useSecure,
1799
- authConfig?.cookies?.prefix,
1800
- authConfig?.cookies?.overrides ?? {},
1801
- internalLogger
1802
- ),
1803
- jose: createJoseInstance(authConfig?.secret),
1804
- secret: authConfig?.secret,
1805
- basePath: authConfig?.basePath ?? "/auth",
1806
- trustedProxyHeaders: useSecure,
1807
- trustedOrigins: authConfig?.trustedOrigins,
1808
- logger: internalLogger
1809
- },
1810
- middlewares: [
2290
+ onError: createErrorHandler(context.logger),
2291
+ context,
2292
+ use: [
1811
2293
  (ctx) => {
1812
- const useSecure2 = useSecureCookies(ctx.request, ctx.context.trustedProxyHeaders);
1813
- const cookies = createCookieStore(
1814
- useSecure2,
1815
- authConfig?.cookies?.prefix,
1816
- authConfig?.cookies?.overrides ?? {},
1817
- internalLogger
1818
- );
1819
- ctx.context.cookies = cookies;
2294
+ const useSecure = useSecureCookies(ctx.request, ctx.context.trustedProxyHeaders);
2295
+ ctx.context.cookies = useSecure ? context.cookieConfig.secure : context.cookieConfig.standard;
1820
2296
  return ctx;
1821
2297
  }
1822
2298
  ]
1823
2299
  };
1824
2300
  };
1825
- var createAuth = (authConfig) => {
2301
+ var createAuthInstance = (authConfig) => {
1826
2302
  const config2 = createInternalConfig(authConfig);
1827
- const router = (0, import_router7.createRouter)(
2303
+ const router = (0, import_router9.createRouter)(
1828
2304
  [signInAction(config2.context.oauth), callbackAction(config2.context.oauth), sessionAction, signOutAction, csrfTokenAction],
1829
2305
  config2
1830
2306
  );
1831
2307
  return {
1832
2308
  handlers: router,
1833
- jose: config2.context.jose
2309
+ jose: config2.context.jose,
2310
+ api: createAuthAPI(config2.context)
2311
+ };
2312
+ };
2313
+ var createAuth = (config2) => {
2314
+ const authInstance = createAuthInstance(config2);
2315
+ authInstance.handlers.ALL = async (request) => {
2316
+ const method = request.method.toUpperCase();
2317
+ const methodHandlers = {
2318
+ GET: authInstance.handlers.GET,
2319
+ POST: authInstance.handlers.POST
2320
+ };
2321
+ if (method in methodHandlers) {
2322
+ return await methodHandlers[method](request);
2323
+ }
2324
+ return new Response("Method Not Allowed", {
2325
+ status: 405,
2326
+ headers: { Allow: Object.keys(methodHandlers).join(", ") }
2327
+ });
2328
+ };
2329
+ return authInstance;
2330
+ };
2331
+
2332
+ // src/client/client.ts
2333
+ var import_router10 = require("@aura-stack/router");
2334
+ var createClient = import_router10.createClient;
2335
+ var createAuthClient = (options2) => {
2336
+ if (typeof window === "undefined" && !options2.baseURL) {
2337
+ throw new AuthClientError("`baseURL` is required when createAuthClient is used outside the browser.");
2338
+ }
2339
+ const client = createClient({
2340
+ cache: "no-store",
2341
+ credentials: "include",
2342
+ baseURL: options2.baseURL ?? window.location.origin,
2343
+ ...options2
2344
+ });
2345
+ const getCSRFToken2 = async () => {
2346
+ try {
2347
+ const response = await client.get("/csrfToken");
2348
+ if (!response.ok) return null;
2349
+ const data = await response.json();
2350
+ return data.csrfToken;
2351
+ } catch (error) {
2352
+ console.error("Error fetching CSRF token:", error);
2353
+ return null;
2354
+ }
2355
+ };
2356
+ const getSession2 = async () => {
2357
+ try {
2358
+ const response = await client.get("/session");
2359
+ if (!response.ok) return null;
2360
+ const session = await response.json();
2361
+ if (!session?.authenticated) return null;
2362
+ return session.session;
2363
+ } catch (error) {
2364
+ console.error("Error fetching session:", error);
2365
+ return null;
2366
+ }
2367
+ };
2368
+ const signIn2 = async (oauth, options3) => {
2369
+ try {
2370
+ const response = await client.get("/signIn/:oauth", {
2371
+ params: {
2372
+ oauth
2373
+ },
2374
+ searchParams: {
2375
+ ...options3,
2376
+ redirect: false
2377
+ }
2378
+ });
2379
+ const json = await response.json();
2380
+ if ((options3?.redirect ?? true) && typeof window !== "undefined" && json?.signInURL) {
2381
+ window.location.assign(json.signInURL);
2382
+ }
2383
+ return json;
2384
+ } catch (error) {
2385
+ console.error("Error during sign-in:", error);
2386
+ return { redirect: false, signInURL: "/" };
2387
+ }
2388
+ };
2389
+ const signOut2 = async (options3) => {
2390
+ try {
2391
+ const csrfToken = await getCSRFToken2();
2392
+ if (!csrfToken) {
2393
+ throw new AuthClientError("Failed to fetch CSRF token for sign-out.");
2394
+ }
2395
+ const response = await client.post("/signOut", {
2396
+ searchParams: {
2397
+ redirectTo: options3?.redirectTo ?? "/",
2398
+ token_type_hint: "session_token"
2399
+ },
2400
+ headers: {
2401
+ "X-CSRF-Token": csrfToken
2402
+ }
2403
+ });
2404
+ const json = await response.json();
2405
+ if ((options3?.redirect ?? true) && typeof window !== "undefined" && json?.url) {
2406
+ window.location.assign(json.url);
2407
+ }
2408
+ return json;
2409
+ } catch (error) {
2410
+ console.error("Error during sign-out:", error);
2411
+ throw isNativeError(error) ? error : new AuthClientError("Sign-out failed.", "The sign-out request failed.", { cause: error });
2412
+ }
2413
+ };
2414
+ return {
2415
+ getSession: getSession2,
2416
+ signIn: signIn2,
2417
+ signOut: signOut2
1834
2418
  };
1835
2419
  };
1836
2420
  // Annotate the CommonJS export names for ESM import in node:
1837
2421
  0 && (module.exports = {
1838
- createAuth
2422
+ builtInOAuthProviders,
2423
+ createAuth,
2424
+ createAuthClient,
2425
+ createClient,
2426
+ createSyslogMessage
1839
2427
  });