@aura-stack/auth 0.1.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +42 -0
  3. package/dist/@types/index.cjs +18 -0
  4. package/dist/@types/index.d.cts +9 -0
  5. package/dist/@types/index.d.ts +9 -0
  6. package/dist/@types/index.js +1 -0
  7. package/dist/@types/router.d.cjs +1 -0
  8. package/dist/@types/router.d.d.cts +16 -0
  9. package/dist/@types/router.d.d.ts +16 -0
  10. package/dist/@types/router.d.js +0 -0
  11. package/dist/@types/utility.cjs +18 -0
  12. package/dist/@types/utility.d.cts +6 -0
  13. package/dist/@types/utility.d.ts +6 -0
  14. package/dist/@types/utility.js +1 -0
  15. package/dist/actions/callback/access-token.cjs +170 -0
  16. package/dist/actions/callback/access-token.d.cts +30 -0
  17. package/dist/actions/callback/access-token.d.ts +30 -0
  18. package/dist/actions/callback/access-token.js +8 -0
  19. package/dist/actions/callback/callback.cjs +517 -0
  20. package/dist/actions/callback/callback.d.cts +13 -0
  21. package/dist/actions/callback/callback.d.ts +13 -0
  22. package/dist/actions/callback/callback.js +16 -0
  23. package/dist/actions/callback/userinfo.cjs +165 -0
  24. package/dist/actions/callback/userinfo.d.cts +22 -0
  25. package/dist/actions/callback/userinfo.d.ts +22 -0
  26. package/dist/actions/callback/userinfo.js +10 -0
  27. package/dist/actions/csrfToken/csrfToken.cjs +207 -0
  28. package/dist/actions/csrfToken/csrfToken.d.cts +5 -0
  29. package/dist/actions/csrfToken/csrfToken.d.ts +5 -0
  30. package/dist/actions/csrfToken/csrfToken.js +12 -0
  31. package/dist/actions/index.cjs +864 -0
  32. package/dist/actions/index.d.cts +14 -0
  33. package/dist/actions/index.d.ts +14 -0
  34. package/dist/actions/index.js +34 -0
  35. package/dist/actions/session/session.cjs +191 -0
  36. package/dist/actions/session/session.d.cts +5 -0
  37. package/dist/actions/session/session.d.ts +5 -0
  38. package/dist/actions/session/session.js +11 -0
  39. package/dist/actions/signIn/authorization.cjs +274 -0
  40. package/dist/actions/signIn/authorization.d.cts +45 -0
  41. package/dist/actions/signIn/authorization.d.ts +45 -0
  42. package/dist/actions/signIn/authorization.js +16 -0
  43. package/dist/actions/signIn/signIn.cjs +469 -0
  44. package/dist/actions/signIn/signIn.d.cts +13 -0
  45. package/dist/actions/signIn/signIn.d.ts +13 -0
  46. package/dist/actions/signIn/signIn.js +14 -0
  47. package/dist/actions/signOut/signOut.cjs +490 -0
  48. package/dist/actions/signOut/signOut.d.cts +8 -0
  49. package/dist/actions/signOut/signOut.d.ts +8 -0
  50. package/dist/actions/signOut/signOut.js +15 -0
  51. package/dist/assert.cjs +44 -0
  52. package/dist/assert.d.ts +5 -0
  53. package/dist/assert.js +10 -0
  54. package/dist/chunk-256KIVJL.js +99 -0
  55. package/dist/chunk-42XB3YCW.js +22 -0
  56. package/dist/chunk-6SM22VVJ.js +18 -0
  57. package/dist/chunk-CAKJT3KS.js +92 -0
  58. package/dist/chunk-E3OXBRYF.js +22 -0
  59. package/dist/chunk-EBPE35JT.js +31 -0
  60. package/dist/chunk-FIPU4MLT.js +21 -0
  61. package/dist/chunk-FJUDBLCP.js +59 -0
  62. package/dist/chunk-FKRDCWBF.js +22 -0
  63. package/dist/chunk-GZU3RBTB.js +62 -0
  64. package/dist/chunk-HGJ4TXY4.js +137 -0
  65. package/dist/chunk-HMRKN75I.js +74 -0
  66. package/dist/chunk-IKHPGFCW.js +14 -0
  67. package/dist/chunk-ITQ7352M.js +0 -0
  68. package/dist/chunk-JAPMIE6S.js +10 -0
  69. package/dist/chunk-KRNOMBXQ.js +22 -0
  70. package/dist/chunk-LLR722CL.js +96 -0
  71. package/dist/chunk-PG7UYFG5.js +0 -0
  72. package/dist/chunk-RLT4RFKV.js +45 -0
  73. package/dist/chunk-SJPDVKUS.js +112 -0
  74. package/dist/chunk-SMQO5WD7.js +30 -0
  75. package/dist/chunk-STHEPPUZ.js +11 -0
  76. package/dist/chunk-UJJ7R56J.js +52 -0
  77. package/dist/chunk-VFTYH33W.js +61 -0
  78. package/dist/chunk-X7M4CQTN.js +25 -0
  79. package/dist/chunk-XXJKNKGQ.js +37 -0
  80. package/dist/chunk-ZV4BH47P.js +154 -0
  81. package/dist/cookie.cjs +201 -0
  82. package/dist/cookie.d.ts +95 -0
  83. package/dist/cookie.js +36 -0
  84. package/dist/error.cjs +88 -0
  85. package/dist/error.d.ts +64 -0
  86. package/dist/error.js +16 -0
  87. package/dist/headers.cjs +35 -0
  88. package/dist/headers.d.ts +9 -0
  89. package/dist/headers.js +6 -0
  90. package/dist/index-CGRZ0wrw.d.ts +530 -0
  91. package/dist/index.cjs +1089 -0
  92. package/dist/index.d.ts +43 -0
  93. package/dist/index.js +75 -0
  94. package/dist/jose.cjs +68 -0
  95. package/dist/jose.d.ts +20 -0
  96. package/dist/jose.js +9 -0
  97. package/dist/oauth/bitbucket.cjs +45 -0
  98. package/dist/oauth/bitbucket.d.cts +9 -0
  99. package/dist/oauth/bitbucket.d.ts +9 -0
  100. package/dist/oauth/bitbucket.js +6 -0
  101. package/dist/oauth/discord.cjs +55 -0
  102. package/dist/oauth/discord.d.cts +9 -0
  103. package/dist/oauth/discord.d.ts +9 -0
  104. package/dist/oauth/discord.js +6 -0
  105. package/dist/oauth/figma.cjs +46 -0
  106. package/dist/oauth/figma.d.cts +9 -0
  107. package/dist/oauth/figma.d.ts +9 -0
  108. package/dist/oauth/figma.js +6 -0
  109. package/dist/oauth/github.cjs +38 -0
  110. package/dist/oauth/github.d.cts +9 -0
  111. package/dist/oauth/github.d.ts +9 -0
  112. package/dist/oauth/github.js +6 -0
  113. package/dist/oauth/gitlab.cjs +46 -0
  114. package/dist/oauth/gitlab.d.cts +9 -0
  115. package/dist/oauth/gitlab.d.ts +9 -0
  116. package/dist/oauth/gitlab.js +6 -0
  117. package/dist/oauth/index.cjs +213 -0
  118. package/dist/oauth/index.d.cts +9 -0
  119. package/dist/oauth/index.d.ts +9 -0
  120. package/dist/oauth/index.js +36 -0
  121. package/dist/oauth/spotify.cjs +46 -0
  122. package/dist/oauth/spotify.d.cts +9 -0
  123. package/dist/oauth/spotify.d.ts +9 -0
  124. package/dist/oauth/spotify.js +6 -0
  125. package/dist/oauth/x.cjs +46 -0
  126. package/dist/oauth/x.d.cts +9 -0
  127. package/dist/oauth/x.d.ts +9 -0
  128. package/dist/oauth/x.js +6 -0
  129. package/dist/response.cjs +34 -0
  130. package/dist/response.d.ts +10 -0
  131. package/dist/response.js +6 -0
  132. package/dist/schemas.cjs +105 -0
  133. package/dist/schemas.d.ts +130 -0
  134. package/dist/schemas.js +20 -0
  135. package/dist/secure.cjs +120 -0
  136. package/dist/secure.d.ts +43 -0
  137. package/dist/secure.js +18 -0
  138. package/dist/utils.cjs +141 -0
  139. package/dist/utils.d.ts +45 -0
  140. package/dist/utils.js +23 -0
  141. package/package.json +70 -0
@@ -0,0 +1,490 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
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
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/actions/signOut/signOut.ts
31
+ var signOut_exports = {};
32
+ __export(signOut_exports, {
33
+ signOutAction: () => signOutAction
34
+ });
35
+ module.exports = __toCommonJS(signOut_exports);
36
+ var import_zod = __toESM(require("zod"), 1);
37
+ var import_router2 = require("@aura-stack/router");
38
+
39
+ // src/secure.ts
40
+ var import_node_crypto = __toESM(require("crypto"), 1);
41
+
42
+ // src/utils.ts
43
+ var import_router = require("@aura-stack/router");
44
+
45
+ // src/error.ts
46
+ var AuthError = class extends Error {
47
+ constructor(type, message) {
48
+ super(message);
49
+ this.type = type;
50
+ this.name = "AuthError";
51
+ }
52
+ };
53
+ var InvalidCsrfTokenError = class extends AuthError {
54
+ constructor(message = "The provided CSRF token is invalid or has expired") {
55
+ super("invalid_csrf_token", message);
56
+ this.name = "InvalidCsrfTokenError";
57
+ }
58
+ };
59
+ var InvalidRedirectToError = class extends AuthError {
60
+ constructor(message = "The redirectTo parameter does not match the hosted origin.") {
61
+ super("invalid_redirect_to", message);
62
+ this.name = "InvalidRedirectToError";
63
+ }
64
+ };
65
+ var isAuthError = (error) => {
66
+ return error instanceof AuthError;
67
+ };
68
+ var ERROR_RESPONSE = {
69
+ AUTHORIZATION: {
70
+ INVALID_REQUEST: "invalid_request",
71
+ UNAUTHORIZED_CLIENT: "unauthorized_client",
72
+ ACCESS_DENIED: "access_denied",
73
+ UNSUPPORTED_RESPONSE_TYPE: "unsupported_response_type",
74
+ INVALID_SCOPE: "invalid_scope",
75
+ SERVER_ERROR: "server_error",
76
+ TEMPORARILY_UNAVAILABLE: "temporarily_unavailable"
77
+ },
78
+ ACCESS_TOKEN: {
79
+ INVALID_REQUEST: "invalid_request",
80
+ INVALID_CLIENT: "invalid_client",
81
+ INVALID_GRANT: "invalid_grant",
82
+ UNAUTHORIZED_CLIENT: "unauthorized_client",
83
+ UNSUPPORTED_GRANT_TYPE: "unsupported_grant_type",
84
+ INVALID_SCOPE: "invalid_scope"
85
+ }
86
+ };
87
+
88
+ // src/utils.ts
89
+ var equals = (a, b) => {
90
+ if (a === null || b === null || a === void 0 || b === void 0) return false;
91
+ return a === b;
92
+ };
93
+ var sanitizeURL = (url2) => {
94
+ try {
95
+ let decodedURL = decodeURIComponent(url2).trim();
96
+ const protocolMatch = decodedURL.match(/^([a-zA-Z][a-zA-Z0-9+.-]*:\/\/)/);
97
+ let protocol = "";
98
+ let rest = decodedURL;
99
+ if (protocolMatch) {
100
+ protocol = protocolMatch[1];
101
+ rest = decodedURL.slice(protocol.length);
102
+ const slashIndex = rest.indexOf("/");
103
+ if (slashIndex === -1) {
104
+ return protocol + rest;
105
+ }
106
+ const domain = rest.slice(0, slashIndex);
107
+ let path = rest.slice(slashIndex).replace(/\/\.\.\//g, "/").replace(/\/\.\.$/, "").replace(/\.{2,}/g, "").replace(/\/{2,}/g, "/");
108
+ if (path !== "/" && path.endsWith("/")) {
109
+ path = path.replace(/\/+$/, "/");
110
+ } else if (path !== "/") {
111
+ path = path.replace(/\/+$/, "");
112
+ }
113
+ return protocol + domain + path;
114
+ }
115
+ let sanitized = decodedURL.replace(/\/\.\.\//g, "/").replace(/\/\.\.$/, "").replace(/\.{2,}/g, "").replace(/\/{2,}/g, "/");
116
+ if (sanitized !== "/" && sanitized.endsWith("/")) {
117
+ sanitized = sanitized.replace(/\/+$/, "/");
118
+ } else if (sanitized !== "/") {
119
+ sanitized = sanitized.replace(/\/+$/, "");
120
+ }
121
+ return sanitized;
122
+ } catch {
123
+ return url2.trim();
124
+ }
125
+ };
126
+ var getNormalizedOriginPath = (path) => {
127
+ try {
128
+ const url2 = new URL(path);
129
+ url2.hash = "";
130
+ url2.search = "";
131
+ return `${url2.origin}${url2.pathname}`;
132
+ } catch {
133
+ return sanitizeURL(path);
134
+ }
135
+ };
136
+
137
+ // src/secure.ts
138
+ var verifyCSRF = async (jose, cookie, header) => {
139
+ try {
140
+ const { token: cookieToken } = await jose.verifyJWS(cookie);
141
+ const { token: headerToken } = await jose.verifyJWS(header);
142
+ const cookieBuffer = Buffer.from(cookieToken);
143
+ const headerBuffer = Buffer.from(headerToken);
144
+ if (!equals(headerBuffer.length, cookieBuffer.length)) {
145
+ throw new InvalidCsrfTokenError();
146
+ }
147
+ if (!import_node_crypto.default.timingSafeEqual(cookieBuffer, headerBuffer)) {
148
+ throw new InvalidCsrfTokenError();
149
+ }
150
+ return true;
151
+ } catch {
152
+ throw new InvalidCsrfTokenError();
153
+ }
154
+ };
155
+
156
+ // src/headers.ts
157
+ var cacheControl = {
158
+ "Cache-Control": "no-store",
159
+ Pragma: "no-cache",
160
+ Expires: "0",
161
+ Vary: "Cookie"
162
+ };
163
+
164
+ // src/response.ts
165
+ var AuraResponse = class extends Response {
166
+ static json(body, init) {
167
+ return Response.json(body, init);
168
+ }
169
+ };
170
+
171
+ // src/assert.ts
172
+ var isRequest = (value) => {
173
+ return typeof Request !== "undefined" && value instanceof Request;
174
+ };
175
+ var isValidURL = (value) => {
176
+ if (value.includes("\r\n") || value.includes("\n") || value.includes("\r")) return false;
177
+ const regex = /^https?:\/\/(?:[a-zA-Z0-9._-]+|localhost|\[[0-9a-fA-F:]+\])(?::\d{1,5})?(?:\/[a-zA-Z0-9._~!$&'()*+,;=:@-]*)*\/?$/;
178
+ return regex.test(value);
179
+ };
180
+
181
+ // src/schemas.ts
182
+ var import_v4 = require("zod/v4");
183
+ var OAuthProviderConfigSchema = (0, import_v4.object)({
184
+ authorizeURL: (0, import_v4.url)(),
185
+ accessToken: (0, import_v4.url)(),
186
+ scope: (0, import_v4.string)().optional(),
187
+ userInfo: (0, import_v4.url)(),
188
+ responseType: (0, import_v4.enum)(["code", "token", "id_token"]),
189
+ clientId: (0, import_v4.string)(),
190
+ clientSecret: (0, import_v4.string)()
191
+ });
192
+ var OAuthAuthorization = OAuthProviderConfigSchema.extend({
193
+ redirectURI: (0, import_v4.string)(),
194
+ state: (0, import_v4.string)(),
195
+ codeChallenge: (0, import_v4.string)(),
196
+ codeChallengeMethod: (0, import_v4.enum)(["plain", "S256"])
197
+ });
198
+ var OAuthAuthorizationResponse = (0, import_v4.object)({
199
+ state: (0, import_v4.string)(),
200
+ code: (0, import_v4.string)()
201
+ });
202
+ var OAuthAuthorizationErrorResponse = (0, import_v4.object)({
203
+ error: (0, import_v4.enum)([
204
+ "invalid_request",
205
+ "unauthorized_client",
206
+ "access_denied",
207
+ "unsupported_response_type",
208
+ "invalid_scope",
209
+ "server_error",
210
+ "temporarily_unavailable"
211
+ ]),
212
+ error_description: (0, import_v4.string)().optional(),
213
+ error_uri: (0, import_v4.string)().optional(),
214
+ state: (0, import_v4.string)()
215
+ });
216
+ var OAuthAccessToken = OAuthProviderConfigSchema.extend({
217
+ redirectURI: (0, import_v4.string)(),
218
+ code: (0, import_v4.string)(),
219
+ codeVerifier: (0, import_v4.string)().min(43).max(128)
220
+ });
221
+ var OAuthAccessTokenResponse = (0, import_v4.object)({
222
+ access_token: (0, import_v4.string)(),
223
+ token_type: (0, import_v4.string)(),
224
+ expires_in: (0, import_v4.number)().optional(),
225
+ refresh_token: (0, import_v4.string)().optional(),
226
+ scope: (0, import_v4.string)().optional()
227
+ });
228
+ var OAuthAccessTokenErrorResponse = (0, import_v4.object)({
229
+ error: (0, import_v4.enum)([
230
+ "invalid_request",
231
+ "invalid_client",
232
+ "invalid_grant",
233
+ "unauthorized_client",
234
+ "unsupported_grant_type",
235
+ "invalid_scope"
236
+ ]),
237
+ error_description: (0, import_v4.string)().optional(),
238
+ error_uri: (0, import_v4.string)().optional()
239
+ });
240
+ var OAuthErrorResponse = (0, import_v4.object)({
241
+ error: (0, import_v4.string)(),
242
+ error_description: (0, import_v4.string)().optional()
243
+ });
244
+
245
+ // src/actions/signIn/authorization.ts
246
+ var getOriginURL = (request, trustedProxyHeaders) => {
247
+ const headers = request.headers;
248
+ if (trustedProxyHeaders) {
249
+ const protocol = headers.get("X-Forwarded-Proto") ?? headers.get("Forwarded")?.match(/proto=([^;]+)/i)?.[1] ?? "http";
250
+ const host = headers.get("X-Forwarded-Host") ?? headers.get("Host") ?? headers.get("Forwarded")?.match(/host=([^;]+)/i)?.[1] ?? null;
251
+ return new URL(`${protocol}://${host}${getNormalizedOriginPath(new URL(request.url).pathname)}`);
252
+ } else {
253
+ return new URL(getNormalizedOriginPath(request.url));
254
+ }
255
+ };
256
+ var createRedirectTo = (request, redirectTo, trustedProxyHeaders) => {
257
+ try {
258
+ const headers = request.headers;
259
+ const origin = headers.get("Origin");
260
+ const referer = headers.get("Referer");
261
+ let hostedURL = getOriginURL(request, trustedProxyHeaders);
262
+ if (redirectTo) {
263
+ if (redirectTo.startsWith("/")) {
264
+ return sanitizeURL(redirectTo);
265
+ }
266
+ const redirectToURL = new URL(sanitizeURL(getNormalizedOriginPath(redirectTo)));
267
+ if (!isValidURL(redirectTo) || !equals(redirectToURL.origin, hostedURL.origin)) {
268
+ throw new InvalidRedirectToError();
269
+ }
270
+ return sanitizeURL(redirectToURL.pathname);
271
+ }
272
+ if (referer) {
273
+ const refererURL = new URL(sanitizeURL(referer));
274
+ if (!isValidURL(referer) || !equals(refererURL.origin, hostedURL.origin)) {
275
+ throw new AuthError(
276
+ ERROR_RESPONSE.AUTHORIZATION.INVALID_REQUEST,
277
+ "The referer of the request does not match the hosted origin."
278
+ );
279
+ }
280
+ return sanitizeURL(refererURL.pathname);
281
+ }
282
+ if (origin) {
283
+ const originURL = new URL(sanitizeURL(getNormalizedOriginPath(origin)));
284
+ if (!isValidURL(origin) || !equals(originURL.origin, hostedURL.origin)) {
285
+ throw new AuthError(ERROR_RESPONSE.AUTHORIZATION.INVALID_REQUEST, "Invalid origin (potential CSRF).");
286
+ }
287
+ return sanitizeURL(originURL.pathname);
288
+ }
289
+ return "/";
290
+ } catch (error) {
291
+ if (isAuthError(error)) {
292
+ throw error;
293
+ }
294
+ throw new AuthError(ERROR_RESPONSE.AUTHORIZATION.INVALID_REQUEST, "Invalid origin (potential CSRF).");
295
+ }
296
+ };
297
+
298
+ // src/cookie.ts
299
+ var import_cookie = require("cookie");
300
+ var import_cookie2 = require("cookie");
301
+ var COOKIE_NAME = "aura-auth";
302
+ var defaultCookieOptions = {
303
+ httpOnly: true,
304
+ sameSite: "lax",
305
+ path: "/",
306
+ maxAge: 60 * 60 * 24 * 15
307
+ };
308
+ var defaultStandardCookieConfig = {
309
+ secure: false,
310
+ httpOnly: true,
311
+ prefix: ""
312
+ };
313
+ var defaultSecureCookieConfig = {
314
+ secure: true,
315
+ prefix: "__Secure-"
316
+ };
317
+ var defaultHostCookieConfig = {
318
+ secure: true,
319
+ prefix: "__Host-",
320
+ path: "/",
321
+ domain: void 0
322
+ };
323
+ var expiredCookieOptions = {
324
+ ...defaultCookieOptions,
325
+ expires: /* @__PURE__ */ new Date(0),
326
+ maxAge: 0
327
+ };
328
+ var defineDefaultCookieOptions = (options2) => {
329
+ return {
330
+ name: options2?.name ?? COOKIE_NAME,
331
+ prefix: options2?.prefix ?? (options2?.secure ? "__Secure-" : ""),
332
+ ...defaultCookieOptions,
333
+ ...options2
334
+ };
335
+ };
336
+ var setCookie = (cookieName, value, options2) => {
337
+ const { prefix, name } = defineDefaultCookieOptions(options2);
338
+ const cookieNameWithPrefix = `${prefix}${name}.${cookieName}`;
339
+ return (0, import_cookie.serialize)(cookieNameWithPrefix, value, {
340
+ ...defaultCookieOptions,
341
+ ...options2
342
+ });
343
+ };
344
+ var getCookie = (petition, cookie, options2, optional = false) => {
345
+ const cookies = isRequest(petition) ? petition.headers.get("Cookie") : petition.headers.getSetCookie().join("; ");
346
+ if (!cookies) {
347
+ if (optional) {
348
+ return "";
349
+ }
350
+ throw new AuthError("invalid_request", "No cookies found. There is no active session");
351
+ }
352
+ const { name, prefix } = defineDefaultCookieOptions(options2);
353
+ const parsedCookies = (0, import_cookie.parse)(cookies);
354
+ const value = parsedCookies[`${prefix}${name}.${cookie}`];
355
+ if (value === void 0) {
356
+ if (optional) {
357
+ return "";
358
+ }
359
+ throw new AuthError("invalid_request", `Cookie "${cookie}" not found. There is no active session`);
360
+ }
361
+ return value;
362
+ };
363
+ var secureCookieOptions = (request, cookieOptions, trustedProxyHeaders) => {
364
+ const name = cookieOptions.name ?? COOKIE_NAME;
365
+ const isSecure = trustedProxyHeaders ? request.url.startsWith("https://") || request.headers.get("X-Forwarded-Proto") === "https" || request.headers.get("Forwarded")?.includes("proto=https") : request.url.startsWith("https://");
366
+ if (!cookieOptions.options?.httpOnly) {
367
+ console.warn(
368
+ "[WARNING]: Cookie is configured without HttpOnly. This allows JavaScript access via document.cookie and increases XSS risk."
369
+ );
370
+ }
371
+ if (cookieOptions.options?.domain === "*") {
372
+ console.warn("[WARNING]: Cookie 'Domain' is set to '*', which is insecure. Avoid wildcard domains.");
373
+ }
374
+ if (!isSecure) {
375
+ const options2 = cookieOptions.options;
376
+ if (options2?.secure) {
377
+ console.warn(
378
+ "[WARNING]: The 'Secure' attribute will be disabled for this cookie. Serve over HTTPS to enforce Secure cookies."
379
+ );
380
+ }
381
+ if (options2?.sameSite == "none") {
382
+ console.warn("[WARNING]: SameSite=None without a secure connection can be blocked by browsers.");
383
+ }
384
+ if (process.env.NODE_ENV === "production") {
385
+ console.warn("[WARNING]: In production, ensure cookies are served over HTTPS to maintain security.");
386
+ }
387
+ return {
388
+ ...defaultCookieOptions,
389
+ ...cookieOptions.options,
390
+ sameSite: options2?.sameSite === "none" ? "lax" : options2?.sameSite ?? "lax",
391
+ ...defaultStandardCookieConfig,
392
+ name
393
+ };
394
+ }
395
+ return cookieOptions.strategy === "host" ? {
396
+ ...defaultCookieOptions,
397
+ ...cookieOptions.options,
398
+ ...defaultHostCookieConfig,
399
+ name
400
+ } : { ...defaultCookieOptions, ...cookieOptions.options, ...defaultSecureCookieConfig, name };
401
+ };
402
+ var expireCookie = (name, options2) => {
403
+ return setCookie(name, "", { ...options2, ...expiredCookieOptions });
404
+ };
405
+
406
+ // src/actions/signOut/signOut.ts
407
+ var config = (0, import_router2.createEndpointConfig)({
408
+ schemas: {
409
+ searchParams: import_zod.default.object({
410
+ token_type_hint: import_zod.default.literal("session_token"),
411
+ redirectTo: import_zod.default.string().optional()
412
+ })
413
+ }
414
+ });
415
+ var signOutAction = (0, import_router2.createEndpoint)(
416
+ "POST",
417
+ "/signOut",
418
+ async (ctx) => {
419
+ const {
420
+ request,
421
+ headers,
422
+ searchParams: { redirectTo },
423
+ context: { cookies, jose, trustedProxyHeaders }
424
+ } = ctx;
425
+ try {
426
+ const cookiesOptions = secureCookieOptions(request, cookies, trustedProxyHeaders);
427
+ const session = getCookie(request, "sessionToken", cookiesOptions);
428
+ const csrfToken = getCookie(request, "csrfToken", {
429
+ ...cookiesOptions,
430
+ prefix: cookiesOptions.secure ? "__Host-" : ""
431
+ });
432
+ const header = headers.get("X-CSRF-Token");
433
+ if (!header || !session || !csrfToken) {
434
+ throw new Error("Missing CSRF token or session token");
435
+ }
436
+ await verifyCSRF(jose, csrfToken, header);
437
+ await jose.decodeJWT(session);
438
+ const normalizedOriginPath = getNormalizedOriginPath(request.url);
439
+ const location = createRedirectTo(
440
+ new Request(normalizedOriginPath, {
441
+ headers
442
+ }),
443
+ redirectTo
444
+ );
445
+ const responseHeaders = new Headers(cacheControl);
446
+ responseHeaders.append("Set-Cookie", expireCookie("sessionToken", cookiesOptions));
447
+ responseHeaders.append(
448
+ "Set-Cookie",
449
+ expireCookie("csrfToken", { ...cookiesOptions, prefix: cookiesOptions.secure ? "__Host-" : "" })
450
+ );
451
+ responseHeaders.append("Location", location);
452
+ return Response.json(
453
+ { message: "Signed out successfully" },
454
+ { status: import_router2.statusCode.ACCEPTED, headers: responseHeaders }
455
+ );
456
+ } catch (error) {
457
+ if (error instanceof InvalidCsrfTokenError) {
458
+ return AuraResponse.json(
459
+ {
460
+ error: "invalid_csrf_token",
461
+ error_description: "The provided CSRF token is invalid or has expired"
462
+ },
463
+ { status: import_router2.statusCode.UNAUTHORIZED }
464
+ );
465
+ }
466
+ if (error instanceof InvalidRedirectToError) {
467
+ const { type, message } = error;
468
+ return AuraResponse.json(
469
+ {
470
+ error: type,
471
+ error_description: message
472
+ },
473
+ { status: import_router2.statusCode.BAD_REQUEST }
474
+ );
475
+ }
476
+ return AuraResponse.json(
477
+ {
478
+ error: "invalid_session_token",
479
+ error_description: "The provided sessionToken is invalid or has already expired"
480
+ },
481
+ { status: import_router2.statusCode.UNAUTHORIZED }
482
+ );
483
+ }
484
+ },
485
+ config
486
+ );
487
+ // Annotate the CommonJS export names for ESM import in node:
488
+ 0 && (module.exports = {
489
+ signOutAction
490
+ });
@@ -0,0 +1,8 @@
1
+ import * as _aura_stack_router from '@aura-stack/router';
2
+
3
+ /**
4
+ * @see https://datatracker.ietf.org/doc/html/rfc7009
5
+ */
6
+ declare const signOutAction: _aura_stack_router.RouteEndpoint<"POST", "/signOut", {}>;
7
+
8
+ export { signOutAction };
@@ -0,0 +1,8 @@
1
+ import * as _aura_stack_router from '@aura-stack/router';
2
+
3
+ /**
4
+ * @see https://datatracker.ietf.org/doc/html/rfc7009
5
+ */
6
+ declare const signOutAction: _aura_stack_router.RouteEndpoint<"POST", "/signOut", {}>;
7
+
8
+ export { signOutAction };
@@ -0,0 +1,15 @@
1
+ import {
2
+ signOutAction
3
+ } from "../../chunk-SJPDVKUS.js";
4
+ import "../../chunk-CAKJT3KS.js";
5
+ import "../../chunk-ZV4BH47P.js";
6
+ import "../../chunk-6SM22VVJ.js";
7
+ import "../../chunk-STHEPPUZ.js";
8
+ import "../../chunk-GZU3RBTB.js";
9
+ import "../../chunk-256KIVJL.js";
10
+ import "../../chunk-FJUDBLCP.js";
11
+ import "../../chunk-JAPMIE6S.js";
12
+ import "../../chunk-HMRKN75I.js";
13
+ export {
14
+ signOutAction
15
+ };
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/assert.ts
21
+ var assert_exports = {};
22
+ __export(assert_exports, {
23
+ isFalsy: () => isFalsy,
24
+ isRequest: () => isRequest,
25
+ isValidURL: () => isValidURL
26
+ });
27
+ module.exports = __toCommonJS(assert_exports);
28
+ var isFalsy = (value) => {
29
+ return value === false || value === 0 || value === "" || value === null || value === void 0 || Number.isNaN(value);
30
+ };
31
+ var isRequest = (value) => {
32
+ return typeof Request !== "undefined" && value instanceof Request;
33
+ };
34
+ var isValidURL = (value) => {
35
+ if (value.includes("\r\n") || value.includes("\n") || value.includes("\r")) return false;
36
+ const regex = /^https?:\/\/(?:[a-zA-Z0-9._-]+|localhost|\[[0-9a-fA-F:]+\])(?::\d{1,5})?(?:\/[a-zA-Z0-9._~!$&'()*+,;=:@-]*)*\/?$/;
37
+ return regex.test(value);
38
+ };
39
+ // Annotate the CommonJS export names for ESM import in node:
40
+ 0 && (module.exports = {
41
+ isFalsy,
42
+ isRequest,
43
+ isValidURL
44
+ });
@@ -0,0 +1,5 @@
1
+ declare const isFalsy: (value: unknown) => boolean;
2
+ declare const isRequest: (value: unknown) => value is Request;
3
+ declare const isValidURL: (value: string) => boolean;
4
+
5
+ export { isFalsy, isRequest, isValidURL };
package/dist/assert.js ADDED
@@ -0,0 +1,10 @@
1
+ import {
2
+ isFalsy,
3
+ isRequest,
4
+ isValidURL
5
+ } from "./chunk-6SM22VVJ.js";
6
+ export {
7
+ isFalsy,
8
+ isRequest,
9
+ isValidURL
10
+ };
@@ -0,0 +1,99 @@
1
+ import {
2
+ isAuthError
3
+ } from "./chunk-FJUDBLCP.js";
4
+
5
+ // src/utils.ts
6
+ import { isRouterError } from "@aura-stack/router";
7
+ var toSnakeCase = (str) => {
8
+ return str.replace(/([a-z0-9])([A-Z])/g, "$1_$2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1_$2").toLowerCase().replace(/^_+/, "");
9
+ };
10
+ var toUpperCase = (str) => {
11
+ return str.toUpperCase();
12
+ };
13
+ var toCastCase = (obj, type = "snake") => {
14
+ return Object.entries(obj).reduce((previous, [key, value]) => {
15
+ const newKey = type === "snake" ? toSnakeCase(key) : toUpperCase(key);
16
+ return { ...previous, [newKey]: value };
17
+ }, {});
18
+ };
19
+ var equals = (a, b) => {
20
+ if (a === null || b === null || a === void 0 || b === void 0) return false;
21
+ return a === b;
22
+ };
23
+ var sanitizeURL = (url) => {
24
+ try {
25
+ let decodedURL = decodeURIComponent(url).trim();
26
+ const protocolMatch = decodedURL.match(/^([a-zA-Z][a-zA-Z0-9+.-]*:\/\/)/);
27
+ let protocol = "";
28
+ let rest = decodedURL;
29
+ if (protocolMatch) {
30
+ protocol = protocolMatch[1];
31
+ rest = decodedURL.slice(protocol.length);
32
+ const slashIndex = rest.indexOf("/");
33
+ if (slashIndex === -1) {
34
+ return protocol + rest;
35
+ }
36
+ const domain = rest.slice(0, slashIndex);
37
+ let path = rest.slice(slashIndex).replace(/\/\.\.\//g, "/").replace(/\/\.\.$/, "").replace(/\.{2,}/g, "").replace(/\/{2,}/g, "/");
38
+ if (path !== "/" && path.endsWith("/")) {
39
+ path = path.replace(/\/+$/, "/");
40
+ } else if (path !== "/") {
41
+ path = path.replace(/\/+$/, "");
42
+ }
43
+ return protocol + domain + path;
44
+ }
45
+ let sanitized = decodedURL.replace(/\/\.\.\//g, "/").replace(/\/\.\.$/, "").replace(/\.{2,}/g, "").replace(/\/{2,}/g, "/");
46
+ if (sanitized !== "/" && sanitized.endsWith("/")) {
47
+ sanitized = sanitized.replace(/\/+$/, "/");
48
+ } else if (sanitized !== "/") {
49
+ sanitized = sanitized.replace(/\/+$/, "");
50
+ }
51
+ return sanitized;
52
+ } catch {
53
+ return url.trim();
54
+ }
55
+ };
56
+ var isValidRelativePath = (path) => {
57
+ if (!path || typeof path !== "string") return false;
58
+ if (!path.startsWith("/") || path.includes("://") || path.includes("\r") || path.includes("\n")) return false;
59
+ if (/[\x00-\x1F\x7F]/.test(path) || path.includes("\0")) return false;
60
+ const sanitized = sanitizeURL(path);
61
+ if (sanitized.includes("..")) return false;
62
+ return true;
63
+ };
64
+ var onErrorHandler = (error) => {
65
+ if (isRouterError(error)) {
66
+ const { message, status, statusText } = error;
67
+ return Response.json({ error: "invalid_request", error_description: message }, { status, statusText });
68
+ }
69
+ if (isAuthError(error)) {
70
+ const { type, message } = error;
71
+ return Response.json({ error: type, error_description: message }, { status: 400 });
72
+ }
73
+ return Response.json({ error: "server_error", error_description: "An unexpected error occurred" }, { status: 500 });
74
+ };
75
+ var getNormalizedOriginPath = (path) => {
76
+ try {
77
+ const url = new URL(path);
78
+ url.hash = "";
79
+ url.search = "";
80
+ return `${url.origin}${url.pathname}`;
81
+ } catch {
82
+ return sanitizeURL(path);
83
+ }
84
+ };
85
+ var toISOString = (date) => {
86
+ return new Date(date).toISOString();
87
+ };
88
+
89
+ export {
90
+ toSnakeCase,
91
+ toUpperCase,
92
+ toCastCase,
93
+ equals,
94
+ sanitizeURL,
95
+ isValidRelativePath,
96
+ onErrorHandler,
97
+ getNormalizedOriginPath,
98
+ toISOString
99
+ };