@aura-stack/auth 0.4.0-rc.5 → 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 (201) hide show
  1. package/dist/@types/index.d.ts +8 -3
  2. package/dist/@types/router.d.cjs +0 -17
  3. package/dist/@types/router.d.d.ts +7 -2
  4. package/dist/@types/router.d.js +0 -1
  5. package/dist/actions/callback/access-token.cjs +130 -71
  6. package/dist/actions/callback/access-token.d.ts +9 -4
  7. package/dist/actions/callback/access-token.js +3 -4
  8. package/dist/actions/callback/callback.cjs +428 -152
  9. package/dist/actions/callback/callback.d.ts +11 -3
  10. package/dist/actions/callback/callback.js +12 -10
  11. package/dist/actions/callback/userinfo.cjs +159 -65
  12. package/dist/actions/callback/userinfo.d.ts +8 -3
  13. package/dist/actions/callback/userinfo.js +7 -6
  14. package/dist/actions/csrfToken/csrfToken.cjs +70 -19
  15. package/dist/actions/csrfToken/csrfToken.js +8 -7
  16. package/dist/actions/index.cjs +780 -348
  17. package/dist/actions/index.d.ts +6 -2
  18. package/dist/actions/index.js +23 -18
  19. package/dist/actions/session/session.cjs +107 -26
  20. package/dist/actions/session/session.js +7 -5
  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 +209 -211
  25. package/dist/actions/signIn/authorization.d.ts +32 -21
  26. package/dist/actions/signIn/authorization.js +12 -9
  27. package/dist/actions/signIn/signIn.cjs +470 -235
  28. package/dist/actions/signIn/signIn.d.ts +12 -3
  29. package/dist/actions/signIn/signIn.js +11 -8
  30. package/dist/actions/signOut/signOut.cjs +376 -228
  31. package/dist/actions/signOut/signOut.d.ts +1 -1
  32. package/dist/actions/signOut/signOut.js +10 -9
  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 +150 -5
  46. package/dist/assert.d.ts +26 -3
  47. package/dist/assert.js +17 -3
  48. package/dist/{chunk-YRCB5FLE.js → chunk-2A5B7GWR.js} +52 -6
  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-4YHJ4IEQ.js +25 -0
  54. package/dist/chunk-54CZPKR4.js +25 -0
  55. package/dist/chunk-5LZ7TOM3.js +25 -0
  56. package/dist/chunk-7BE46WWS.js +88 -0
  57. package/dist/chunk-7YYXFKLR.js +35 -0
  58. package/dist/chunk-C3A37LQC.js +33 -0
  59. package/dist/chunk-CITNGXDA.js +31 -0
  60. package/dist/chunk-CWX724AG.js +78 -0
  61. package/dist/chunk-D2CSIUKP.js +74 -0
  62. package/dist/chunk-E6G5YCI6.js +25 -0
  63. package/dist/chunk-EBAMFRB7.js +34 -0
  64. package/dist/chunk-EEE7UM5T.js +25 -0
  65. package/dist/{chunk-HT4YLL7N.js → chunk-FPCVZUVG.js} +10 -8
  66. package/dist/chunk-FW4W3REU.js +25 -0
  67. package/dist/chunk-GNNBM2WJ.js +83 -0
  68. package/dist/chunk-IPKO6UQN.js +25 -0
  69. package/dist/chunk-JOCGX3RP.js +59 -0
  70. package/dist/chunk-KBXWTD6E.js +94 -0
  71. package/dist/chunk-KMMAZFSJ.js +25 -0
  72. package/dist/chunk-LATR3NIV.js +117 -0
  73. package/dist/chunk-LAYPUDQF.js +39 -0
  74. package/dist/chunk-LDU7A2JE.js +25 -0
  75. package/dist/chunk-LX3TJ2TJ.js +294 -0
  76. package/dist/chunk-NHZBQNRR.js +143 -0
  77. package/dist/chunk-OVHNRULD.js +33 -0
  78. package/dist/chunk-PDP3PHB3.js +127 -0
  79. package/dist/chunk-PHYNROD4.js +47 -0
  80. package/dist/chunk-QQEKY4XP.js +29 -0
  81. package/dist/chunk-U4RK4LKJ.js +348 -0
  82. package/dist/{chunk-RRLIF4PQ.js → chunk-U5663F2U.js} +16 -1
  83. package/dist/chunk-UN7X6SU5.js +53 -0
  84. package/dist/chunk-UZQJJD6A.js +100 -0
  85. package/dist/chunk-V6LLEAR4.js +80 -0
  86. package/dist/chunk-WHNDRO3N.js +50 -0
  87. package/dist/{chunk-W6LG7BFW.js → chunk-XY5R3EHH.js} +30 -23
  88. package/dist/client/client.cjs +135 -0
  89. package/dist/client/client.d.ts +85 -0
  90. package/dist/client/client.js +9 -0
  91. package/dist/client/index.cjs +135 -0
  92. package/dist/client/index.d.ts +14 -0
  93. package/dist/client/index.js +10 -0
  94. package/dist/context.cjs +1237 -0
  95. package/dist/context.d.ts +16 -0
  96. package/dist/context.js +28 -0
  97. package/dist/cookie.cjs +57 -22
  98. package/dist/cookie.d.ts +11 -6
  99. package/dist/cookie.js +3 -2
  100. package/dist/createAuth.cjs +2320 -0
  101. package/dist/createAuth.d.ts +12 -0
  102. package/dist/createAuth.js +48 -0
  103. package/dist/env.cjs +78 -0
  104. package/dist/env.d.ts +10 -0
  105. package/dist/env.js +12 -0
  106. package/dist/errors.cjs +17 -0
  107. package/dist/errors.d.ts +15 -4
  108. package/dist/errors.js +5 -1
  109. package/dist/headers.cjs +28 -2
  110. package/dist/headers.d.ts +25 -1
  111. package/dist/headers.js +9 -3
  112. package/dist/index-_aXtxb_s.d.ts +1377 -0
  113. package/dist/index.cjs +1843 -610
  114. package/dist/index.d.ts +11 -92
  115. package/dist/index.js +53 -85
  116. package/dist/jose.cjs +113 -38
  117. package/dist/jose.d.ts +12 -23
  118. package/dist/jose.js +17 -7
  119. package/dist/logger.cjs +424 -0
  120. package/dist/logger.d.ts +12 -0
  121. package/dist/logger.js +17 -0
  122. package/dist/oauth/atlassian.cjs +57 -0
  123. package/dist/oauth/atlassian.d.ts +12 -0
  124. package/dist/oauth/atlassian.js +6 -0
  125. package/dist/oauth/bitbucket.cjs +19 -15
  126. package/dist/oauth/bitbucket.d.ts +7 -2
  127. package/dist/oauth/bitbucket.js +1 -1
  128. package/dist/oauth/discord.cjs +27 -24
  129. package/dist/oauth/discord.d.ts +7 -2
  130. package/dist/oauth/discord.js +1 -1
  131. package/dist/oauth/dropbox.cjs +53 -0
  132. package/dist/oauth/dropbox.d.ts +12 -0
  133. package/dist/oauth/dropbox.js +6 -0
  134. package/dist/oauth/figma.cjs +19 -16
  135. package/dist/oauth/figma.d.ts +7 -2
  136. package/dist/oauth/figma.js +1 -1
  137. package/dist/oauth/github.cjs +19 -8
  138. package/dist/oauth/github.d.ts +7 -2
  139. package/dist/oauth/github.js +1 -1
  140. package/dist/oauth/gitlab.cjs +19 -16
  141. package/dist/oauth/gitlab.d.ts +7 -2
  142. package/dist/oauth/gitlab.js +1 -1
  143. package/dist/oauth/index.cjs +529 -239
  144. package/dist/oauth/index.d.ts +7 -2
  145. package/dist/oauth/index.js +39 -22
  146. package/dist/oauth/mailchimp.cjs +19 -16
  147. package/dist/oauth/mailchimp.d.ts +7 -2
  148. package/dist/oauth/mailchimp.js +1 -1
  149. package/dist/oauth/notion.cjs +131 -0
  150. package/dist/oauth/notion.d.ts +12 -0
  151. package/dist/oauth/notion.js +9 -0
  152. package/dist/oauth/pinterest.cjs +19 -16
  153. package/dist/oauth/pinterest.d.ts +7 -2
  154. package/dist/oauth/pinterest.js +1 -1
  155. package/dist/oauth/spotify.cjs +19 -16
  156. package/dist/oauth/spotify.d.ts +7 -2
  157. package/dist/oauth/spotify.js +1 -1
  158. package/dist/oauth/strava.cjs +19 -16
  159. package/dist/oauth/strava.d.ts +7 -2
  160. package/dist/oauth/strava.js +1 -1
  161. package/dist/oauth/twitch.cjs +95 -0
  162. package/dist/oauth/twitch.d.ts +12 -0
  163. package/dist/oauth/twitch.js +7 -0
  164. package/dist/oauth/x.cjs +19 -16
  165. package/dist/oauth/x.d.ts +7 -2
  166. package/dist/oauth/x.js +1 -1
  167. package/dist/schemas.cjs +89 -42
  168. package/dist/schemas.d.ts +114 -18
  169. package/dist/schemas.js +5 -3
  170. package/dist/secure.cjs +73 -31
  171. package/dist/secure.d.ts +11 -11
  172. package/dist/secure.js +7 -6
  173. package/dist/utils.cjs +203 -90
  174. package/dist/utils.d.ts +21 -40
  175. package/dist/utils.js +21 -12
  176. package/package.json +9 -6
  177. package/dist/chunk-3EUWD5BB.js +0 -63
  178. package/dist/chunk-42XB3YCW.js +0 -22
  179. package/dist/chunk-6R2YZ4AC.js +0 -22
  180. package/dist/chunk-A3N4PVAT.js +0 -70
  181. package/dist/chunk-B737EUJV.js +0 -22
  182. package/dist/chunk-CXLATHS5.js +0 -143
  183. package/dist/chunk-E3OXBRYF.js +0 -22
  184. package/dist/chunk-EIL2FPSS.js +0 -22
  185. package/dist/chunk-EMKJA2GJ.js +0 -89
  186. package/dist/chunk-FIPU4MLT.js +0 -21
  187. package/dist/chunk-FKRDCWBF.js +0 -22
  188. package/dist/chunk-GA2SMTJO.js +0 -58
  189. package/dist/chunk-HP34YGGJ.js +0 -22
  190. package/dist/chunk-IKHPGFCW.js +0 -14
  191. package/dist/chunk-IUYZQTJV.js +0 -30
  192. package/dist/chunk-IVET23KF.js +0 -58
  193. package/dist/chunk-JVFTCTTE.js +0 -33
  194. package/dist/chunk-KRNOMBXQ.js +0 -22
  195. package/dist/chunk-KSWLO5ZU.js +0 -102
  196. package/dist/chunk-N2APGLXA.js +0 -71
  197. package/dist/chunk-N4SX7TZT.js +0 -96
  198. package/dist/chunk-STHEPPUZ.js +0 -11
  199. package/dist/chunk-TLE4PXY3.js +0 -39
  200. package/dist/index-B8jeIElf.d.ts +0 -679
  201. /package/dist/{chunk-DIVDFNAP.js → chunk-5X7JZMEF.js} +0 -0
@@ -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,14 +15,6 @@ 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/actions/signOut/signOut.ts
@@ -33,106 +23,247 @@ __export(signOut_exports, {
33
23
  signOutAction: () => signOutAction
34
24
  });
35
25
  module.exports = __toCommonJS(signOut_exports);
36
- var import_zod2 = require("zod");
37
- var import_router2 = require("@aura-stack/router");
38
-
39
- // src/secure.ts
40
- var import_crypto = __toESM(require("crypto"), 1);
26
+ var import_v4 = require("zod/v4");
27
+ var import_router3 = require("@aura-stack/router");
41
28
 
42
29
  // src/utils.ts
43
30
  var import_router = require("@aura-stack/router");
44
31
 
45
32
  // src/errors.ts
33
+ var AuthInternalError = class extends Error {
34
+ type = "AUTH_INTERNAL_ERROR";
35
+ code;
36
+ constructor(code, message, options) {
37
+ super(message, options);
38
+ this.code = code;
39
+ this.name = new.target.name;
40
+ Error.captureStackTrace(this, new.target);
41
+ }
42
+ };
46
43
  var AuthSecurityError = class extends Error {
47
44
  type = "AUTH_SECURITY_ERROR";
48
45
  code;
49
- constructor(code, message, options2) {
50
- super(message, options2);
46
+ constructor(code, message, options) {
47
+ super(message, options);
51
48
  this.code = code;
52
49
  this.name = new.target.name;
53
50
  Error.captureStackTrace(this, new.target);
54
51
  }
55
52
  };
56
- var isAuthSecurityError = (error) => {
57
- return error instanceof AuthSecurityError;
58
- };
59
53
 
60
- // src/utils.ts
61
- var equals = (a, b) => {
62
- if (a === null || b === null || a === void 0 || b === void 0) return false;
63
- return a === b;
64
- };
65
- var sanitizeURL = (url) => {
66
- try {
67
- let decodedURL = decodeURIComponent(url).trim();
68
- const protocolMatch = decodedURL.match(/^([a-zA-Z][a-zA-Z0-9+.-]*:\/\/)/);
69
- let protocol = "";
70
- let rest = decodedURL;
71
- if (protocolMatch) {
72
- protocol = protocolMatch[1];
73
- rest = decodedURL.slice(protocol.length);
74
- const slashIndex = rest.indexOf("/");
75
- if (slashIndex === -1) {
76
- return protocol + rest;
54
+ // src/env.ts
55
+ var import_meta = {};
56
+ var env = new Proxy({}, {
57
+ get(_, prop) {
58
+ if (typeof prop !== "string") return void 0;
59
+ const hasProperty = (process2) => {
60
+ return process2 && Object.prototype.hasOwnProperty.call(process2, prop);
61
+ };
62
+ try {
63
+ if (typeof process !== "undefined" && hasProperty(process.env)) {
64
+ return process.env[prop];
77
65
  }
78
- const domain = rest.slice(0, slashIndex);
79
- let path = rest.slice(slashIndex).replace(/\/\.\.\//g, "/").replace(/\/\.\.$/, "").replace(/\.{2,}/g, "").replace(/\/{2,}/g, "/");
80
- if (path !== "/" && path.endsWith("/")) {
81
- path = path.replace(/\/+$/, "/");
82
- } else if (path !== "/") {
83
- path = path.replace(/\/+$/, "");
66
+ if (typeof import_meta !== "undefined" && hasProperty(import_meta.env)) {
67
+ return import_meta.env[prop];
84
68
  }
85
- return protocol + domain + path;
86
- }
87
- let sanitized = decodedURL.replace(/\/\.\.\//g, "/").replace(/\/\.\.$/, "").replace(/\.{2,}/g, "").replace(/\/{2,}/g, "/");
88
- if (sanitized !== "/" && sanitized.endsWith("/")) {
89
- sanitized = sanitized.replace(/\/+$/, "/");
90
- } else if (sanitized !== "/") {
91
- sanitized = sanitized.replace(/\/+$/, "");
69
+ if (typeof Deno !== "undefined" && Deno.env?.get) {
70
+ return Deno.env.get(prop);
71
+ }
72
+ if (typeof Bun !== "undefined" && hasProperty(Bun.env)) {
73
+ return Bun.env[prop];
74
+ }
75
+ const globalValue = globalThis[prop];
76
+ return typeof globalValue === "string" ? globalValue : void 0;
77
+ } catch {
78
+ return void 0;
92
79
  }
93
- return sanitized;
80
+ }
81
+ });
82
+ var getEnv = (key) => {
83
+ const keys = [`AURA_AUTH_${key.toUpperCase()}`, `AURA_${key.toUpperCase()}`, `AUTH_${key.toUpperCase()}`, key.toUpperCase()];
84
+ return env[keys.find((k) => env[k]) ?? ""];
85
+ };
86
+
87
+ // src/assert.ts
88
+ var import_crypto = require("@aura-stack/jose/crypto");
89
+ var unsafeChars = [
90
+ "<",
91
+ ">",
92
+ '"',
93
+ "`",
94
+ " ",
95
+ "\r",
96
+ "\n",
97
+ " ",
98
+ "\\",
99
+ "%2F",
100
+ "%5C",
101
+ "%2f",
102
+ "%5c",
103
+ "\r\n",
104
+ "%0A",
105
+ "%0D",
106
+ "%0a",
107
+ "%0d",
108
+ "..",
109
+ "//",
110
+ "///",
111
+ "...",
112
+ "%20",
113
+ "\0"
114
+ ];
115
+ var isValidURL = (value) => {
116
+ if (!new RegExp(/^https?:\/\/[^/]/).test(value)) {
117
+ return false;
118
+ }
119
+ const match = value.match(/^(https?:\/\/)(.*)$/);
120
+ if (!match) return false;
121
+ const rest = match[2];
122
+ for (const char of unsafeChars) {
123
+ if (rest.includes(char)) return false;
124
+ }
125
+ const regex = /^https?:\/\/(?:[a-zA-Z0-9._-]+|localhost|\[[0-9a-fA-F:]+\])(?::\d{1,5})?(?:\/[a-zA-Z0-9._~!$&'()?#*+,;=:@-]*)*\/?$/;
126
+ return regex.test(match[0]);
127
+ };
128
+ var isJWTPayloadWithToken = (payload) => {
129
+ return typeof payload === "object" && payload !== null && "token" in payload && typeof payload?.token === "string";
130
+ };
131
+ var isRelativeURL = (value) => {
132
+ if (value.length > 100) return false;
133
+ for (const char of unsafeChars) {
134
+ if (value.includes(char)) return false;
135
+ }
136
+ const regex = /^\/[a-zA-Z0-9\-_\/.?&=#]*\/?$/;
137
+ return regex.test(value);
138
+ };
139
+ var isSameOrigin = (origin, expected) => {
140
+ const originURL = new URL(origin);
141
+ const expectedURL = new URL(expected);
142
+ return equals(originURL.origin, expectedURL.origin);
143
+ };
144
+ var patternToRegex = (pattern) => {
145
+ try {
146
+ if (pattern.length > 2048) return null;
147
+ pattern = pattern.replace(/\\/g, "");
148
+ const match = pattern.match(/^(https?):\/\/([a-zA-Z0-9.*-]{1,253})(?::(\d{1,5}|\*))?(?:\/.*)?$/);
149
+ if (!match) return null;
150
+ const [, protocol, host, port] = match;
151
+ const hasWildcard = host.includes("*");
152
+ if (hasWildcard && !host.startsWith("*.")) return null;
153
+ if (hasWildcard && host.slice(2).includes("*")) return null;
154
+ const domain = hasWildcard ? host.slice(2) : host;
155
+ const escapedDomain = domain.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
156
+ const hostRegex = hasWildcard ? `[^.]+\\.${escapedDomain}` : escapedDomain;
157
+ const portRegex = port === "*" ? ":\\d{1,5}" : port ? `:${port}` : "";
158
+ return new RegExp(`^${protocol}:\\/\\/${hostRegex}${portRegex}$`);
94
159
  } catch {
95
- return url.trim();
160
+ return null;
96
161
  }
97
162
  };
98
- var getNormalizedOriginPath = (path) => {
163
+ var isTrustedOrigin = (url, trustedOrigins) => {
164
+ if (!isValidURL(url) || trustedOrigins.length === 0) return false;
99
165
  try {
100
- const url = new URL(path);
101
- url.hash = "";
102
- url.search = "";
103
- return `${url.origin}${url.pathname}`;
166
+ const urlOrigin = new URL(url).origin;
167
+ for (const pattern of trustedOrigins) {
168
+ const regex = patternToRegex(pattern);
169
+ if (regex?.test(urlOrigin)) return true;
170
+ try {
171
+ if (isValidURL(pattern) && equals(new URL(pattern).origin, urlOrigin)) return true;
172
+ } catch {
173
+ }
174
+ }
104
175
  } catch {
105
- return sanitizeURL(path);
106
176
  }
177
+ return false;
178
+ };
179
+ var timingSafeEqual = (a, b) => {
180
+ const bufferA = import_crypto.encoder.encode(a);
181
+ const bufferB = import_crypto.encoder.encode(b);
182
+ const len = Math.max(bufferA.length, bufferB.length);
183
+ let diff = 0;
184
+ for (let i = 0; i < len; i++) {
185
+ diff |= (bufferA[i] ?? 0) ^ (bufferB[i] ?? 0);
186
+ }
187
+ return diff === 0 && bufferA.length === bufferB.length;
107
188
  };
108
189
 
109
- // src/assert.ts
110
- var isValidURL = (value) => {
111
- if (value.includes("\r\n") || value.includes("\n") || value.includes("\r")) return false;
112
- const regex = /^https?:\/\/(?:[a-zA-Z0-9._-]+|localhost|\[[0-9a-fA-F:]+\])(?::\d{1,5})?(?:\/[a-zA-Z0-9._~!$&'()*+,;=:@-]*)*\/?$/;
113
- return regex.test(value);
190
+ // src/utils.ts
191
+ var equals = (a, b) => {
192
+ if (a === null || b === null || a === void 0 || b === void 0) return false;
193
+ return a === b;
114
194
  };
115
- var isJWTPayloadWithToken = (payload) => {
116
- return typeof payload === "object" && payload !== null && "token" in payload && typeof payload?.token === "string";
195
+ var getBaseURL = (request) => {
196
+ const url = new URL(request.url);
197
+ return `${url.origin}${url.pathname}`;
198
+ };
199
+ var extractPath = (url) => {
200
+ const pathRegex = /^https?:\/\/[a-zA-Z0-9_\-\.]+(:\d+)?(\/.*)$/;
201
+ const match = url.match(pathRegex);
202
+ return match && match[2] ? match[2] : "/";
203
+ };
204
+ var getErrorName = (error) => {
205
+ if (error instanceof Error) {
206
+ return error.name;
207
+ }
208
+ return typeof error === "string" ? error : "UnknownError";
209
+ };
210
+
211
+ // src/cookie.ts
212
+ var import_cookie = require("@aura-stack/router/cookie");
213
+ var defaultCookieOptions = {
214
+ httpOnly: true,
215
+ sameSite: "lax",
216
+ path: "/",
217
+ maxAge: 60 * 60 * 24 * 15
218
+ };
219
+ var oauthCookieOptions = {
220
+ httpOnly: true,
221
+ maxAge: 5 * 60,
222
+ sameSite: "lax",
223
+ expires: new Date(Date.now() + 5 * 60 * 1e3)
224
+ };
225
+ var expiredCookieAttributes = {
226
+ ...defaultCookieOptions,
227
+ expires: /* @__PURE__ */ new Date(0),
228
+ maxAge: 0,
229
+ secure: true
230
+ };
231
+ var getCookie = (request, cookieName) => {
232
+ const cookies = request instanceof Request ? request.headers.get("Cookie") : request.get("Cookie");
233
+ if (!cookies) {
234
+ throw new AuthInternalError("COOKIE_NOT_FOUND", "No cookies found. There is no active session");
235
+ }
236
+ const value = (0, import_cookie.parse)(cookies)[cookieName];
237
+ if (!value) {
238
+ throw new AuthInternalError("COOKIE_NOT_FOUND", `Cookie "${cookieName}" not found. There is no active session`);
239
+ }
240
+ return value;
241
+ };
242
+
243
+ // src/jose.ts
244
+ var import_jose = require("@aura-stack/jose");
245
+ var import_jose2 = require("@aura-stack/jose/jose");
246
+ var import_crypto2 = require("@aura-stack/jose/crypto");
247
+ var jwtVerificationOptions = {
248
+ algorithms: ["HS256"],
249
+ typ: "JWT"
117
250
  };
118
251
 
119
252
  // src/secure.ts
120
253
  var verifyCSRF = async (jose, cookie, header) => {
121
254
  try {
122
- const cookiePayload = await jose.verifyJWS(cookie);
123
- const headerPayload = await jose.verifyJWS(header);
255
+ const cookiePayload = await jose.verifyJWS(cookie, jwtVerificationOptions);
256
+ const headerPayload = await jose.verifyJWS(header, jwtVerificationOptions);
124
257
  if (!isJWTPayloadWithToken(cookiePayload)) {
125
258
  throw new AuthSecurityError("CSRF_TOKEN_INVALID", "Cookie payload missing token field.");
126
259
  }
127
260
  if (!isJWTPayloadWithToken(headerPayload)) {
128
261
  throw new AuthSecurityError("CSRF_TOKEN_INVALID", "Header payload missing token field.");
129
262
  }
130
- const cookieBuffer = Buffer.from(cookiePayload.token);
131
- const headerBuffer = Buffer.from(headerPayload.token);
132
- if (!equals(headerBuffer.length, cookieBuffer.length)) {
263
+ if (!equals(cookiePayload.token.length, headerPayload.token.length)) {
133
264
  throw new AuthSecurityError("CSRF_TOKEN_INVALID", "The CSRF tokens do not match.");
134
265
  }
135
- if (!import_crypto.default.timingSafeEqual(cookieBuffer, headerBuffer)) {
266
+ if (!timingSafeEqual(cookiePayload.token, headerPayload.token)) {
136
267
  throw new AuthSecurityError("CSRF_TOKEN_INVALID", "The CSRF tokens do not match.");
137
268
  }
138
269
  return true;
@@ -148,193 +279,210 @@ var cacheControl = {
148
279
  Expires: "0",
149
280
  Vary: "Cookie"
150
281
  };
151
-
152
- // src/cookie.ts
153
- var import_cookie = require("@aura-stack/router/cookie");
154
- var defaultCookieOptions = {
155
- httpOnly: true,
156
- sameSite: "lax",
157
- path: "/",
158
- maxAge: 60 * 60 * 24 * 15
282
+ var contentSecurityPolicy = {
283
+ "Content-Security-Policy": [
284
+ "default-src 'none'",
285
+ "script-src 'self'",
286
+ "frame-src 'none'",
287
+ "object-src 'none'",
288
+ "frame-ancestors 'none'",
289
+ "base-uri 'none'"
290
+ ].join("; ")
159
291
  };
160
- var oauthCookieOptions = {
161
- httpOnly: true,
162
- maxAge: 5 * 60,
163
- sameSite: "lax",
164
- expires: new Date(Date.now() + 5 * 60 * 1e3)
292
+ var secureHeaders = {
293
+ "X-Content-Type-Options": "nosniff",
294
+ "X-Frame-Options": "DENY",
295
+ "Referrer-Policy": "strict-origin-when-cross-origin"
165
296
  };
166
- var expiredCookieAttributes = {
167
- ...defaultCookieOptions,
168
- expires: /* @__PURE__ */ new Date(0),
169
- maxAge: 0
297
+ var secureApiHeaders = {
298
+ ...cacheControl,
299
+ ...contentSecurityPolicy,
300
+ ...secureHeaders
170
301
  };
171
302
 
172
- // src/schemas.ts
173
- var import_zod = require("zod");
174
- var OAuthProviderConfigSchema = (0, import_zod.object)({
175
- authorizeURL: (0, import_zod.string)().url(),
176
- accessToken: (0, import_zod.string)().url(),
177
- scope: (0, import_zod.string)().optional(),
178
- userInfo: (0, import_zod.string)().url(),
179
- responseType: (0, import_zod.enum)(["code", "token", "id_token"]),
180
- clientId: (0, import_zod.string)(),
181
- clientSecret: (0, import_zod.string)()
182
- });
183
- var OAuthAuthorization = OAuthProviderConfigSchema.extend({
184
- redirectURI: (0, import_zod.string)(),
185
- state: (0, import_zod.string)(),
186
- codeChallenge: (0, import_zod.string)(),
187
- codeChallengeMethod: (0, import_zod.enum)(["plain", "S256"])
188
- });
189
- var OAuthAuthorizationResponse = (0, import_zod.object)({
190
- state: (0, import_zod.string)({ message: "Missing state parameter in the OAuth authorization response." }),
191
- code: (0, import_zod.string)({ message: "Missing code parameter in the OAuth authorization response." })
192
- });
193
- var OAuthAuthorizationErrorResponse = (0, import_zod.object)({
194
- error: (0, import_zod.enum)([
195
- "invalid_request",
196
- "unauthorized_client",
197
- "access_denied",
198
- "unsupported_response_type",
199
- "invalid_scope",
200
- "server_error",
201
- "temporarily_unavailable"
202
- ]),
203
- error_description: (0, import_zod.string)().optional(),
204
- error_uri: (0, import_zod.string)().optional(),
205
- state: (0, import_zod.string)()
206
- });
207
- var OAuthAccessToken = OAuthProviderConfigSchema.extend({
208
- redirectURI: (0, import_zod.string)(),
209
- code: (0, import_zod.string)(),
210
- codeVerifier: (0, import_zod.string)().min(43).max(128)
211
- });
212
- var OAuthAccessTokenResponse = (0, import_zod.object)({
213
- access_token: (0, import_zod.string)(),
214
- token_type: (0, import_zod.string)().optional(),
215
- expires_in: (0, import_zod.number)().optional(),
216
- refresh_token: (0, import_zod.string)().optional(),
217
- scope: (0, import_zod.string)().optional().or((0, import_zod.null)())
218
- });
219
- var OAuthAccessTokenErrorResponse = (0, import_zod.object)({
220
- error: (0, import_zod.enum)([
221
- "invalid_request",
222
- "invalid_client",
223
- "invalid_grant",
224
- "unauthorized_client",
225
- "unsupported_grant_type",
226
- "invalid_scope"
227
- ]),
228
- error_description: (0, import_zod.string)().optional(),
229
- error_uri: (0, import_zod.string)().optional()
230
- });
231
- var OAuthErrorResponse = (0, import_zod.object)({
232
- error: (0, import_zod.string)(),
233
- error_description: (0, import_zod.string)().optional()
234
- });
235
- var OAuthEnvSchema = (0, import_zod.object)({
236
- clientId: import_zod.z.string().min(1, "OAuth Client ID is required in the environment variables."),
237
- clientSecret: import_zod.z.string().min(1, "OAuth Client Secret is required in the environment variables.")
238
- });
303
+ // src/api/signOut.ts
304
+ var import_router2 = require("@aura-stack/router");
305
+ var signOut = async ({
306
+ ctx,
307
+ headers: headersInit,
308
+ redirectTo = "/",
309
+ skipCSRFCheck = false
310
+ }) => {
311
+ const headers = new Headers(headersInit);
312
+ const header = headers.get("X-CSRF-Token");
313
+ let session = null;
314
+ let csrfToken = null;
315
+ try {
316
+ session = getCookie(headers, ctx.cookies.sessionToken.name);
317
+ } catch {
318
+ throw new AuthSecurityError("SESSION_TOKEN_MISSING", "The sessionToken is missing.");
319
+ }
320
+ try {
321
+ csrfToken = getCookie(headers, ctx.cookies.csrfToken.name);
322
+ } catch {
323
+ throw new AuthSecurityError("CSRF_TOKEN_MISSING", "The CSRF token is missing.");
324
+ }
325
+ ctx?.logger?.log("SIGN_OUT_ATTEMPT", {
326
+ structuredData: {
327
+ has_session: Boolean(session),
328
+ has_csrf_token: Boolean(csrfToken),
329
+ has_csrf_header: Boolean(header),
330
+ skip_csrf_check: skipCSRFCheck
331
+ }
332
+ });
333
+ if (!session) {
334
+ ctx?.logger?.log("SESSION_TOKEN_MISSING");
335
+ throw new AuthSecurityError("SESSION_TOKEN_MISSING", "The sessionToken is missing.");
336
+ }
337
+ if (!skipCSRFCheck) {
338
+ if (!csrfToken) {
339
+ ctx?.logger?.log("CSRF_TOKEN_MISSING");
340
+ throw new AuthSecurityError("CSRF_TOKEN_MISSING", "The CSRF token is missing.");
341
+ }
342
+ if (!header) {
343
+ ctx?.logger?.log("CSRF_HEADER_MISSING");
344
+ throw new AuthSecurityError("CSRF_HEADER_MISSING", "The CSRF header is missing.");
345
+ }
346
+ try {
347
+ await verifyCSRF(ctx.jose, csrfToken, header);
348
+ } catch (error) {
349
+ ctx?.logger?.log("CSRF_TOKEN_INVALID", { structuredData: { error_type: getErrorName(error) } });
350
+ throw new AuthSecurityError("CSRF_TOKEN_INVALID", "CSRF token verification failed");
351
+ }
352
+ ctx?.logger?.log("SIGN_OUT_CSRF_VERIFIED");
353
+ } else {
354
+ try {
355
+ await ctx.jose.verifyJWS(csrfToken);
356
+ } catch (error) {
357
+ ctx?.logger?.log("CSRF_TOKEN_INVALID", { structuredData: { error_type: getErrorName(error) } });
358
+ throw new AuthSecurityError("CSRF_TOKEN_INVALID", "CSRF token verification failed");
359
+ }
360
+ }
361
+ try {
362
+ await ctx.jose.decodeJWT(session);
363
+ ctx?.logger?.log("SIGN_OUT_SUCCESS");
364
+ } catch (error) {
365
+ ctx?.logger?.log("INVALID_JWT_TOKEN", { structuredData: { error_type: getErrorName(error) } });
366
+ }
367
+ const headersList = new import_router2.HeadersBuilder(secureApiHeaders).setHeader("Location", redirectTo).setCookie(ctx.cookies.csrfToken.name, "", expiredCookieAttributes).setCookie(ctx.cookies.sessionToken.name, "", expiredCookieAttributes).toHeaders();
368
+ return Response.json(
369
+ { redirect: Boolean(redirectTo), url: redirectTo },
370
+ {
371
+ status: 202,
372
+ headers: headersList
373
+ }
374
+ );
375
+ };
239
376
 
240
377
  // src/actions/signIn/authorization.ts
241
- var getOriginURL = (request, trustedProxyHeaders) => {
242
- const headers = request.headers;
243
- if (trustedProxyHeaders) {
244
- const protocol = headers.get("X-Forwarded-Proto") ?? headers.get("Forwarded")?.match(/proto=([^;]+)/i)?.[1] ?? "http";
245
- const host = headers.get("X-Forwarded-Host") ?? headers.get("Host") ?? headers.get("Forwarded")?.match(/host=([^;]+)/i)?.[1] ?? null;
246
- return new URL(`${protocol}://${host}${getNormalizedOriginPath(new URL(request.url).pathname)}`);
247
- } else {
248
- return new URL(getNormalizedOriginPath(request.url));
378
+ var getTrustedOrigins = async (request, trustedOrigins) => {
379
+ if (!trustedOrigins) return [];
380
+ const raw = typeof trustedOrigins === "function" ? await trustedOrigins(request) : trustedOrigins;
381
+ return Array.isArray(raw) ? raw : typeof raw === "string" ? [raw] : [];
382
+ };
383
+ var getBaseURL2 = async ({
384
+ ctx,
385
+ request,
386
+ headers: headersInit
387
+ }) => {
388
+ const origin = getEnv("BASE_URL") || ctx?.baseURL;
389
+ if (origin && origin !== "/") return origin;
390
+ if (ctx?.trustedProxyHeaders) {
391
+ const headers = headersInit && new Headers(headersInit) || request?.headers;
392
+ const protocol = headers?.get("Forwarded")?.match(/proto=([^;]+)/i)?.[1] ?? headers?.get("X-Forwarded-Proto") ?? "http";
393
+ const host = headers?.get("Host") ?? headers?.get("Forwarded")?.match(/host=([^;]+)/i)?.[1] ?? headers?.get("X-Forwarded-Host") ?? null;
394
+ if (host) return `${protocol}://${host}`;
395
+ throw new AuthInternalError(
396
+ "INVALID_OAUTH_CONFIGURATION",
397
+ "The URL cannot be constructed. Please set the BASE_URL environment variable or provide trusted proxy host headers."
398
+ );
399
+ }
400
+ try {
401
+ return new URL(request?.url ?? "not-found").origin;
402
+ } catch (error) {
403
+ throw new AuthInternalError(
404
+ "INVALID_OAUTH_CONFIGURATION",
405
+ "The URL cannot be constructed. Please set the BASE_URL environment variable or enable trustedProxyHeaders.",
406
+ { cause: error }
407
+ );
249
408
  }
250
409
  };
251
- var createRedirectTo = (request, redirectTo, trustedProxyHeaders) => {
410
+ var getOriginURL = async (request, context) => {
411
+ const trustedOrigins = await getTrustedOrigins(request, context?.trustedOrigins);
412
+ trustedOrigins.push(new URL(request.url).origin);
413
+ const origin = await getBaseURL2({ request, ctx: context });
414
+ if (!isTrustedOrigin(origin, trustedOrigins)) {
415
+ context?.logger?.log("UNTRUSTED_ORIGIN", { structuredData: { origin } });
416
+ throw new AuthInternalError("UNTRUSTED_ORIGIN", "The constructed origin URL is not trusted.");
417
+ }
418
+ return origin;
419
+ };
420
+ var createRedirectTo = async (request, redirectTo, context) => {
252
421
  try {
253
422
  const headers = request.headers;
254
- const origin = headers.get("Origin");
255
- const referer = headers.get("Referer");
256
- let hostedURL = getOriginURL(request, trustedProxyHeaders);
257
- if (redirectTo) {
258
- if (redirectTo.startsWith("/")) {
259
- return sanitizeURL(redirectTo);
423
+ const requestOrigin = await getOriginURL(request, context);
424
+ const origins = await getTrustedOrigins(request, context?.trustedOrigins);
425
+ const validateURL = (url) => {
426
+ if (!isRelativeURL(url) && !isValidURL(url)) return "/";
427
+ if (isRelativeURL(url)) return url;
428
+ if (origins.length > 0) {
429
+ if (isTrustedOrigin(url, origins)) {
430
+ const urlOrigin = new URL(url).origin;
431
+ for (const pattern of origins) {
432
+ const regex = patternToRegex(pattern);
433
+ if (regex?.test(urlOrigin)) {
434
+ return isSameOrigin(url, request.url) ? extractPath(url) : url;
435
+ }
436
+ if (isValidURL(pattern) && equals(new URL(pattern).origin, urlOrigin)) return url;
437
+ }
438
+ }
439
+ context?.logger?.log("OPEN_REDIRECT_ATTACK");
440
+ return "/";
260
441
  }
261
- const redirectToURL = new URL(sanitizeURL(getNormalizedOriginPath(redirectTo)));
262
- if (!isValidURL(redirectTo) || !equals(redirectToURL.origin, hostedURL.origin)) {
263
- throw new AuthSecurityError(
264
- "POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED",
265
- "The redirectTo parameter does not match the hosted origin."
266
- );
442
+ if (isSameOrigin(url, requestOrigin)) {
443
+ return extractPath(url);
267
444
  }
268
- return sanitizeURL(redirectToURL.pathname);
269
- }
270
- if (referer) {
271
- const refererURL = new URL(sanitizeURL(referer));
272
- if (!isValidURL(referer) || !equals(refererURL.origin, hostedURL.origin)) {
273
- throw new AuthSecurityError(
274
- "POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED",
275
- "The referer of the request does not match the hosted origin."
276
- );
277
- }
278
- return sanitizeURL(refererURL.pathname);
279
- }
280
- if (origin) {
281
- const originURL = new URL(sanitizeURL(getNormalizedOriginPath(origin)));
282
- if (!isValidURL(origin) || !equals(originURL.origin, hostedURL.origin)) {
283
- throw new AuthSecurityError("POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED", "Invalid origin (potential CSRF).");
284
- }
285
- return sanitizeURL(originURL.pathname);
286
- }
287
- return "/";
445
+ context?.logger?.log("OPEN_REDIRECT_ATTACK");
446
+ return "/";
447
+ };
448
+ return validateURL(redirectTo ?? headers.get("Referer") ?? headers.get("Origin") ?? "/");
288
449
  } catch (error) {
289
- if (isAuthSecurityError(error)) {
290
- throw error;
291
- }
292
- throw new AuthSecurityError("POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED", "Invalid origin (potential CSRF).");
450
+ context?.logger?.log("POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED");
451
+ return "/";
293
452
  }
294
453
  };
295
454
 
296
455
  // src/actions/signOut/signOut.ts
297
- var config = (0, import_router2.createEndpointConfig)({
456
+ var config = (0, import_router3.createEndpointConfig)({
298
457
  schemas: {
299
- searchParams: import_zod2.z.object({
300
- token_type_hint: import_zod2.z.literal("session_token"),
301
- redirectTo: import_zod2.z.string().optional()
458
+ searchParams: import_v4.z.object({
459
+ token_type_hint: import_v4.z.literal("session_token"),
460
+ redirectTo: import_v4.z.string().optional()
302
461
  })
303
462
  }
304
463
  });
305
- var signOutAction = (0, import_router2.createEndpoint)(
464
+ var signOutAction = (0, import_router3.createEndpoint)(
306
465
  "POST",
307
466
  "/signOut",
308
467
  async (ctx) => {
309
468
  const {
310
469
  request,
311
- headers,
312
470
  searchParams: { redirectTo },
313
- context: { jose, cookies }
471
+ context
314
472
  } = ctx;
315
- const session = headers.getCookie(cookies.sessionToken.name);
316
- const csrfToken = headers.getCookie(cookies.csrfToken.name);
317
- const header = headers.getHeader("X-CSRF-Token");
318
- if (!session) {
319
- throw new AuthSecurityError("SESSION_TOKEN_MISSING", "The sessionToken is missing.");
320
- }
321
- if (!csrfToken) {
322
- throw new AuthSecurityError("CSRF_TOKEN_MISSING", "The CSRF token is missing.");
323
- }
324
- if (!header) {
325
- throw new AuthSecurityError("CSRF_TOKEN_MISSING", "The CSRF header is missing.");
326
- }
327
- await verifyCSRF(jose, csrfToken, header);
328
- await jose.decodeJWT(session);
329
- const normalizedOriginPath = getNormalizedOriginPath(request.url);
330
- const location = createRedirectTo(
331
- new Request(normalizedOriginPath, {
332
- headers: headers.toHeaders()
473
+ const baseURL = getBaseURL(request);
474
+ const location = await createRedirectTo(
475
+ new Request(baseURL, {
476
+ headers: request.headers
333
477
  }),
334
- redirectTo
478
+ redirectTo,
479
+ context
335
480
  );
336
- const headersList = new import_router2.HeadersBuilder(cacheControl).setHeader("Location", location).setCookie(cookies.csrfToken.name, "", expiredCookieAttributes).setCookie(cookies.sessionToken.name, "", expiredCookieAttributes).toHeaders();
337
- return Response.json({ message: "Signed out successfully" }, { status: import_router2.statusCode.ACCEPTED, headers: headersList });
481
+ return await signOut({
482
+ ctx: context,
483
+ headers: request.headers,
484
+ redirectTo: location
485
+ });
338
486
  },
339
487
  config
340
488
  );