@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,96 @@
1
+ import {
2
+ createAuthorizationURL,
3
+ createRedirectTo,
4
+ createRedirectURI
5
+ } from "./chunk-CAKJT3KS.js";
6
+ import {
7
+ oauthCookie,
8
+ secureCookieOptions,
9
+ setCookie
10
+ } from "./chunk-ZV4BH47P.js";
11
+ import {
12
+ createPKCE,
13
+ generateSecure
14
+ } from "./chunk-GZU3RBTB.js";
15
+ import {
16
+ ERROR_RESPONSE,
17
+ isAuthError
18
+ } from "./chunk-FJUDBLCP.js";
19
+ import {
20
+ AuraResponse
21
+ } from "./chunk-JAPMIE6S.js";
22
+
23
+ // src/actions/signIn/signIn.ts
24
+ import z from "zod";
25
+ import { createEndpoint, createEndpointConfig, statusCode } from "@aura-stack/router";
26
+ var signInConfig = (oauth) => {
27
+ return createEndpointConfig("/signIn/:oauth", {
28
+ schemas: {
29
+ params: z.object({
30
+ oauth: z.enum(Object.keys(oauth)),
31
+ redirectTo: z.string().optional()
32
+ })
33
+ }
34
+ });
35
+ };
36
+ var signInAction = (oauth) => {
37
+ return createEndpoint(
38
+ "GET",
39
+ "/signIn/:oauth",
40
+ async (ctx) => {
41
+ const {
42
+ request,
43
+ params: { oauth: oauth2, redirectTo },
44
+ context: { oauth: providers, cookies, trustedProxyHeaders, basePath }
45
+ } = ctx;
46
+ try {
47
+ const cookieOptions = secureCookieOptions(request, cookies, trustedProxyHeaders);
48
+ const state = generateSecure();
49
+ const redirectURI = createRedirectURI(request, oauth2, basePath, trustedProxyHeaders);
50
+ const stateCookie = setCookie("state", state, oauthCookie(cookieOptions));
51
+ const redirectURICookie = setCookie("redirect_uri", redirectURI, oauthCookie(cookieOptions));
52
+ const redirectToCookie = setCookie(
53
+ "redirect_to",
54
+ createRedirectTo(request, redirectTo, trustedProxyHeaders),
55
+ oauthCookie(cookieOptions)
56
+ );
57
+ const { codeVerifier, codeChallenge, method } = await createPKCE();
58
+ const codeVerifierCookie = setCookie("code_verifier", codeVerifier, oauthCookie(cookieOptions));
59
+ const authorization = createAuthorizationURL(providers[oauth2], redirectURI, state, codeChallenge, method);
60
+ const headers = new Headers();
61
+ headers.set("Location", authorization);
62
+ headers.append("Set-Cookie", stateCookie);
63
+ headers.append("Set-Cookie", redirectURICookie);
64
+ headers.append("Set-Cookie", redirectToCookie);
65
+ headers.append("Set-Cookie", codeVerifierCookie);
66
+ return Response.json(
67
+ { oauth: oauth2 },
68
+ {
69
+ status: 302,
70
+ headers
71
+ }
72
+ );
73
+ } catch (error) {
74
+ if (isAuthError(error)) {
75
+ const { type, message } = error;
76
+ return AuraResponse.json(
77
+ { error: type, error_description: message },
78
+ { status: statusCode.BAD_REQUEST }
79
+ );
80
+ }
81
+ return AuraResponse.json(
82
+ {
83
+ error: ERROR_RESPONSE.AUTHORIZATION.SERVER_ERROR,
84
+ error_description: "An unexpected error occurred"
85
+ },
86
+ { status: statusCode.INTERNAL_SERVER_ERROR }
87
+ );
88
+ }
89
+ },
90
+ signInConfig(oauth)
91
+ );
92
+ };
93
+
94
+ export {
95
+ signInAction
96
+ };
File without changes
@@ -0,0 +1,45 @@
1
+ import {
2
+ generateSecure
3
+ } from "./chunk-GZU3RBTB.js";
4
+ import {
5
+ AuthError,
6
+ throwAuthError
7
+ } from "./chunk-FJUDBLCP.js";
8
+ import {
9
+ OAuthErrorResponse
10
+ } from "./chunk-HMRKN75I.js";
11
+
12
+ // src/actions/callback/userinfo.ts
13
+ var getDefaultUserInfo = (profile) => {
14
+ const sub = generateSecure(16);
15
+ return {
16
+ sub: profile?.id ?? profile?.sub ?? sub,
17
+ email: profile?.email,
18
+ name: profile?.name ?? profile?.username ?? profile?.nickname,
19
+ image: profile?.image ?? profile?.picture
20
+ };
21
+ };
22
+ var getUserInfo = async (oauthConfig, accessToken) => {
23
+ const userinfoEndpoint = oauthConfig.userInfo;
24
+ try {
25
+ const response = await fetch(userinfoEndpoint, {
26
+ method: "GET",
27
+ headers: {
28
+ Accept: "application/json",
29
+ Authorization: `Bearer ${accessToken}`
30
+ }
31
+ });
32
+ const json = await response.json();
33
+ const { success, data } = OAuthErrorResponse.safeParse(json);
34
+ if (success) {
35
+ throw new AuthError(data.error, data?.error_description ?? "An error occurred while fetching user information.");
36
+ }
37
+ return oauthConfig?.profile ? oauthConfig.profile(json) : getDefaultUserInfo(json);
38
+ } catch (error) {
39
+ throw throwAuthError(error, "Failed to retrieve userinfo");
40
+ }
41
+ };
42
+
43
+ export {
44
+ getUserInfo
45
+ };
@@ -0,0 +1,112 @@
1
+ import {
2
+ createRedirectTo
3
+ } from "./chunk-CAKJT3KS.js";
4
+ import {
5
+ expireCookie,
6
+ getCookie,
7
+ secureCookieOptions
8
+ } from "./chunk-ZV4BH47P.js";
9
+ import {
10
+ cacheControl
11
+ } from "./chunk-STHEPPUZ.js";
12
+ import {
13
+ verifyCSRF
14
+ } from "./chunk-GZU3RBTB.js";
15
+ import {
16
+ getNormalizedOriginPath
17
+ } from "./chunk-256KIVJL.js";
18
+ import {
19
+ InvalidCsrfTokenError,
20
+ InvalidRedirectToError
21
+ } from "./chunk-FJUDBLCP.js";
22
+ import {
23
+ AuraResponse
24
+ } from "./chunk-JAPMIE6S.js";
25
+
26
+ // src/actions/signOut/signOut.ts
27
+ import z from "zod";
28
+ import { createEndpoint, createEndpointConfig, statusCode } from "@aura-stack/router";
29
+ var config = createEndpointConfig({
30
+ schemas: {
31
+ searchParams: z.object({
32
+ token_type_hint: z.literal("session_token"),
33
+ redirectTo: z.string().optional()
34
+ })
35
+ }
36
+ });
37
+ var signOutAction = createEndpoint(
38
+ "POST",
39
+ "/signOut",
40
+ async (ctx) => {
41
+ const {
42
+ request,
43
+ headers,
44
+ searchParams: { redirectTo },
45
+ context: { cookies, jose, trustedProxyHeaders }
46
+ } = ctx;
47
+ try {
48
+ const cookiesOptions = secureCookieOptions(request, cookies, trustedProxyHeaders);
49
+ const session = getCookie(request, "sessionToken", cookiesOptions);
50
+ const csrfToken = getCookie(request, "csrfToken", {
51
+ ...cookiesOptions,
52
+ prefix: cookiesOptions.secure ? "__Host-" : ""
53
+ });
54
+ const header = headers.get("X-CSRF-Token");
55
+ if (!header || !session || !csrfToken) {
56
+ throw new Error("Missing CSRF token or session token");
57
+ }
58
+ await verifyCSRF(jose, csrfToken, header);
59
+ await jose.decodeJWT(session);
60
+ const normalizedOriginPath = getNormalizedOriginPath(request.url);
61
+ const location = createRedirectTo(
62
+ new Request(normalizedOriginPath, {
63
+ headers
64
+ }),
65
+ redirectTo
66
+ );
67
+ const responseHeaders = new Headers(cacheControl);
68
+ responseHeaders.append("Set-Cookie", expireCookie("sessionToken", cookiesOptions));
69
+ responseHeaders.append(
70
+ "Set-Cookie",
71
+ expireCookie("csrfToken", { ...cookiesOptions, prefix: cookiesOptions.secure ? "__Host-" : "" })
72
+ );
73
+ responseHeaders.append("Location", location);
74
+ return Response.json(
75
+ { message: "Signed out successfully" },
76
+ { status: statusCode.ACCEPTED, headers: responseHeaders }
77
+ );
78
+ } catch (error) {
79
+ if (error instanceof InvalidCsrfTokenError) {
80
+ return AuraResponse.json(
81
+ {
82
+ error: "invalid_csrf_token",
83
+ error_description: "The provided CSRF token is invalid or has expired"
84
+ },
85
+ { status: statusCode.UNAUTHORIZED }
86
+ );
87
+ }
88
+ if (error instanceof InvalidRedirectToError) {
89
+ const { type, message } = error;
90
+ return AuraResponse.json(
91
+ {
92
+ error: type,
93
+ error_description: message
94
+ },
95
+ { status: statusCode.BAD_REQUEST }
96
+ );
97
+ }
98
+ return AuraResponse.json(
99
+ {
100
+ error: "invalid_session_token",
101
+ error_description: "The provided sessionToken is invalid or has already expired"
102
+ },
103
+ { status: statusCode.UNAUTHORIZED }
104
+ );
105
+ }
106
+ },
107
+ config
108
+ );
109
+
110
+ export {
111
+ signOutAction
112
+ };
@@ -0,0 +1,30 @@
1
+ import {
2
+ getCookie,
3
+ secureCookieOptions,
4
+ setCookie
5
+ } from "./chunk-ZV4BH47P.js";
6
+ import {
7
+ cacheControl
8
+ } from "./chunk-STHEPPUZ.js";
9
+ import {
10
+ createCSRF
11
+ } from "./chunk-GZU3RBTB.js";
12
+
13
+ // src/actions/csrfToken/csrfToken.ts
14
+ import { createEndpoint } from "@aura-stack/router";
15
+ var csrfTokenAction = createEndpoint("GET", "/csrfToken", async (ctx) => {
16
+ const {
17
+ request,
18
+ context: { cookies, jose, trustedProxyHeaders }
19
+ } = ctx;
20
+ const cookieOptions = secureCookieOptions(request, { ...cookies, strategy: "host" }, trustedProxyHeaders);
21
+ const existingCSRFToken = getCookie(request, "csrfToken", cookieOptions, true);
22
+ const csrfToken = await createCSRF(jose, existingCSRFToken);
23
+ const headers = new Headers(cacheControl);
24
+ headers.set("Set-Cookie", setCookie("csrfToken", csrfToken, cookieOptions));
25
+ return Response.json({ csrfToken }, { headers });
26
+ });
27
+
28
+ export {
29
+ csrfTokenAction
30
+ };
@@ -0,0 +1,11 @@
1
+ // src/headers.ts
2
+ var cacheControl = {
3
+ "Cache-Control": "no-store",
4
+ Pragma: "no-cache",
5
+ Expires: "0",
6
+ Vary: "Cookie"
7
+ };
8
+
9
+ export {
10
+ cacheControl
11
+ };
@@ -0,0 +1,52 @@
1
+ import {
2
+ AuthError,
3
+ ERROR_RESPONSE,
4
+ throwAuthError
5
+ } from "./chunk-FJUDBLCP.js";
6
+ import {
7
+ OAuthAccessToken,
8
+ OAuthAccessTokenErrorResponse,
9
+ OAuthAccessTokenResponse
10
+ } from "./chunk-HMRKN75I.js";
11
+
12
+ // src/actions/callback/access-token.ts
13
+ var createAccessToken = async (oauthConfig, redirectURI, code, codeVerifier) => {
14
+ const parsed = OAuthAccessToken.safeParse({ ...oauthConfig, redirectURI, code, codeVerifier });
15
+ if (!parsed.success) {
16
+ throw new AuthError(ERROR_RESPONSE.ACCESS_TOKEN.INVALID_REQUEST, "Invalid OAuth configuration");
17
+ }
18
+ const { accessToken, clientId, clientSecret, code: codeParsed, redirectURI: redirectParsed } = parsed.data;
19
+ try {
20
+ const response = await fetch(accessToken, {
21
+ method: "POST",
22
+ headers: {
23
+ Accept: "application/json",
24
+ "Content-Type": "application/x-www-form-urlencoded"
25
+ },
26
+ body: new URLSearchParams({
27
+ client_id: clientId,
28
+ client_secret: clientSecret,
29
+ code: codeParsed,
30
+ redirect_uri: redirectParsed,
31
+ grant_type: "authorization_code",
32
+ code_verifier: codeVerifier
33
+ }).toString()
34
+ });
35
+ const json = await response.json();
36
+ const token = OAuthAccessTokenResponse.safeParse(json);
37
+ if (!token.success) {
38
+ const { success, data } = OAuthAccessTokenErrorResponse.safeParse(json);
39
+ if (!success) {
40
+ throw new AuthError(ERROR_RESPONSE.ACCESS_TOKEN.INVALID_GRANT, "Invalid access token response format");
41
+ }
42
+ throw new AuthError(data.error, data?.error_description ?? "Failed to retrieve access token");
43
+ }
44
+ return token.data;
45
+ } catch (error) {
46
+ throw throwAuthError(error, "Failed to create access token");
47
+ }
48
+ };
49
+
50
+ export {
51
+ createAccessToken
52
+ };
@@ -0,0 +1,61 @@
1
+ import {
2
+ figma
3
+ } from "./chunk-FKRDCWBF.js";
4
+ import {
5
+ github
6
+ } from "./chunk-IKHPGFCW.js";
7
+ import {
8
+ gitlab
9
+ } from "./chunk-KRNOMBXQ.js";
10
+ import {
11
+ spotify
12
+ } from "./chunk-E3OXBRYF.js";
13
+ import {
14
+ x
15
+ } from "./chunk-42XB3YCW.js";
16
+ import {
17
+ bitbucket
18
+ } from "./chunk-FIPU4MLT.js";
19
+ import {
20
+ discord
21
+ } from "./chunk-EBPE35JT.js";
22
+
23
+ // src/oauth/index.ts
24
+ var builtInOAuthProviders = {
25
+ github,
26
+ bitbucket,
27
+ figma,
28
+ discord,
29
+ gitlab,
30
+ spotify,
31
+ x
32
+ };
33
+ var defineOAuthEnvironment = (oauth) => {
34
+ const env = process.env;
35
+ return {
36
+ clientId: env[`AURA_AUTH_${oauth.toUpperCase()}_CLIENT_ID`],
37
+ clientSecret: env[`AURA_AUTH_${oauth.toUpperCase()}_CLIENT_SECRET`]
38
+ };
39
+ };
40
+ var defineOAuthProviderConfig = (config) => {
41
+ if (typeof config === "string") {
42
+ const definition = defineOAuthEnvironment(config);
43
+ const oauthConfig = builtInOAuthProviders[config];
44
+ return {
45
+ ...oauthConfig,
46
+ ...definition
47
+ };
48
+ }
49
+ return config;
50
+ };
51
+ var createBuiltInOAuthProviders = (oauth = []) => {
52
+ return oauth.reduce((previous, config) => {
53
+ const oauthConfig = defineOAuthProviderConfig(config);
54
+ return { ...previous, [oauthConfig.id]: oauthConfig };
55
+ }, {});
56
+ };
57
+
58
+ export {
59
+ builtInOAuthProviders,
60
+ createBuiltInOAuthProviders
61
+ };
@@ -0,0 +1,25 @@
1
+ import {
2
+ createDerivedSalt
3
+ } from "./chunk-GZU3RBTB.js";
4
+
5
+ // src/jose.ts
6
+ import "dotenv/config";
7
+ import { createJWT, createJWS, createDeriveKey } from "@aura-stack/jose";
8
+ var createJoseInstance = (secret) => {
9
+ secret ?? (secret = process.env.AURA_AUTH_SECRET);
10
+ const salt = process.env.AURA_AUTH_SALT ?? createDerivedSalt(secret);
11
+ const { derivedKey: derivedSessionKey } = createDeriveKey(secret, salt, "session");
12
+ const { derivedKey: derivedCsrfTokenKey } = createDeriveKey(secret, salt, "csrfToken");
13
+ const { decodeJWT, encodeJWT } = createJWT(derivedSessionKey);
14
+ const { signJWS, verifyJWS } = createJWS(derivedCsrfTokenKey);
15
+ return {
16
+ decodeJWT,
17
+ encodeJWT,
18
+ signJWS,
19
+ verifyJWS
20
+ };
21
+ };
22
+
23
+ export {
24
+ createJoseInstance
25
+ };
@@ -0,0 +1,37 @@
1
+ import {
2
+ expireCookie,
3
+ getCookie,
4
+ secureCookieOptions
5
+ } from "./chunk-ZV4BH47P.js";
6
+ import {
7
+ cacheControl
8
+ } from "./chunk-STHEPPUZ.js";
9
+ import {
10
+ toISOString
11
+ } from "./chunk-256KIVJL.js";
12
+
13
+ // src/actions/session/session.ts
14
+ import { createEndpoint } from "@aura-stack/router";
15
+ var sessionAction = createEndpoint("GET", "/session", async (ctx) => {
16
+ const {
17
+ request,
18
+ context: { cookies, jose, trustedProxyHeaders }
19
+ } = ctx;
20
+ const cookieOptions = secureCookieOptions(request, cookies, trustedProxyHeaders);
21
+ try {
22
+ const session = getCookie(request, "sessionToken", cookieOptions);
23
+ const decoded = await jose.decodeJWT(session);
24
+ const { exp, iat, jti, nbf, ...user } = decoded;
25
+ const headers = new Headers(cacheControl);
26
+ return Response.json({ user, expires: toISOString(exp * 1e3) }, { headers });
27
+ } catch {
28
+ const headers = new Headers(cacheControl);
29
+ const sessionCookie = expireCookie("sessionToken", cookieOptions);
30
+ headers.set("Set-Cookie", sessionCookie);
31
+ return Response.json({ authenticated: false, message: "Unauthorized" }, { status: 401, headers });
32
+ }
33
+ });
34
+
35
+ export {
36
+ sessionAction
37
+ };
@@ -0,0 +1,154 @@
1
+ import {
2
+ isRequest
3
+ } from "./chunk-6SM22VVJ.js";
4
+ import {
5
+ AuthError
6
+ } from "./chunk-FJUDBLCP.js";
7
+
8
+ // src/cookie.ts
9
+ import { parse, serialize } from "cookie";
10
+ import { parse as parse2 } from "cookie";
11
+ var COOKIE_NAME = "aura-auth";
12
+ var defaultCookieOptions = {
13
+ httpOnly: true,
14
+ sameSite: "lax",
15
+ path: "/",
16
+ maxAge: 60 * 60 * 24 * 15
17
+ };
18
+ var defaultCookieConfig = {
19
+ strategy: "standard",
20
+ name: COOKIE_NAME,
21
+ options: defaultCookieOptions
22
+ };
23
+ var defaultStandardCookieConfig = {
24
+ secure: false,
25
+ httpOnly: true,
26
+ prefix: ""
27
+ };
28
+ var defaultSecureCookieConfig = {
29
+ secure: true,
30
+ prefix: "__Secure-"
31
+ };
32
+ var defaultHostCookieConfig = {
33
+ secure: true,
34
+ prefix: "__Host-",
35
+ path: "/",
36
+ domain: void 0
37
+ };
38
+ var expiredCookieOptions = {
39
+ ...defaultCookieOptions,
40
+ expires: /* @__PURE__ */ new Date(0),
41
+ maxAge: 0
42
+ };
43
+ var defineDefaultCookieOptions = (options) => {
44
+ return {
45
+ name: options?.name ?? COOKIE_NAME,
46
+ prefix: options?.prefix ?? (options?.secure ? "__Secure-" : ""),
47
+ ...defaultCookieOptions,
48
+ ...options
49
+ };
50
+ };
51
+ var setCookie = (cookieName, value, options) => {
52
+ const { prefix, name } = defineDefaultCookieOptions(options);
53
+ const cookieNameWithPrefix = `${prefix}${name}.${cookieName}`;
54
+ return serialize(cookieNameWithPrefix, value, {
55
+ ...defaultCookieOptions,
56
+ ...options
57
+ });
58
+ };
59
+ var getCookie = (petition, cookie, options, optional = false) => {
60
+ const cookies = isRequest(petition) ? petition.headers.get("Cookie") : petition.headers.getSetCookie().join("; ");
61
+ if (!cookies) {
62
+ if (optional) {
63
+ return "";
64
+ }
65
+ throw new AuthError("invalid_request", "No cookies found. There is no active session");
66
+ }
67
+ const { name, prefix } = defineDefaultCookieOptions(options);
68
+ const parsedCookies = parse(cookies);
69
+ const value = parsedCookies[`${prefix}${name}.${cookie}`];
70
+ if (value === void 0) {
71
+ if (optional) {
72
+ return "";
73
+ }
74
+ throw new AuthError("invalid_request", `Cookie "${cookie}" not found. There is no active session`);
75
+ }
76
+ return value;
77
+ };
78
+ var createSessionCookie = async (session, cookieOptions, jose) => {
79
+ try {
80
+ const encoded = await jose.encodeJWT(session);
81
+ return setCookie("sessionToken", encoded, cookieOptions);
82
+ } catch (error) {
83
+ throw new AuthError("server_error", "Failed to create session cookie", { cause: error });
84
+ }
85
+ };
86
+ var secureCookieOptions = (request, cookieOptions, trustedProxyHeaders) => {
87
+ const name = cookieOptions.name ?? COOKIE_NAME;
88
+ 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://");
89
+ if (!cookieOptions.options?.httpOnly) {
90
+ console.warn(
91
+ "[WARNING]: Cookie is configured without HttpOnly. This allows JavaScript access via document.cookie and increases XSS risk."
92
+ );
93
+ }
94
+ if (cookieOptions.options?.domain === "*") {
95
+ console.warn("[WARNING]: Cookie 'Domain' is set to '*', which is insecure. Avoid wildcard domains.");
96
+ }
97
+ if (!isSecure) {
98
+ const options = cookieOptions.options;
99
+ if (options?.secure) {
100
+ console.warn(
101
+ "[WARNING]: The 'Secure' attribute will be disabled for this cookie. Serve over HTTPS to enforce Secure cookies."
102
+ );
103
+ }
104
+ if (options?.sameSite == "none") {
105
+ console.warn("[WARNING]: SameSite=None without a secure connection can be blocked by browsers.");
106
+ }
107
+ if (process.env.NODE_ENV === "production") {
108
+ console.warn("[WARNING]: In production, ensure cookies are served over HTTPS to maintain security.");
109
+ }
110
+ return {
111
+ ...defaultCookieOptions,
112
+ ...cookieOptions.options,
113
+ sameSite: options?.sameSite === "none" ? "lax" : options?.sameSite ?? "lax",
114
+ ...defaultStandardCookieConfig,
115
+ name
116
+ };
117
+ }
118
+ return cookieOptions.strategy === "host" ? {
119
+ ...defaultCookieOptions,
120
+ ...cookieOptions.options,
121
+ ...defaultHostCookieConfig,
122
+ name
123
+ } : { ...defaultCookieOptions, ...cookieOptions.options, ...defaultSecureCookieConfig, name };
124
+ };
125
+ var expireCookie = (name, options) => {
126
+ return setCookie(name, "", { ...options, ...expiredCookieOptions });
127
+ };
128
+ var oauthCookie = (options) => {
129
+ return {
130
+ ...options,
131
+ secure: options.secure,
132
+ httpOnly: options.httpOnly,
133
+ maxAge: 5 * 60,
134
+ expires: new Date(Date.now() + 5 * 60 * 1e3)
135
+ };
136
+ };
137
+
138
+ export {
139
+ COOKIE_NAME,
140
+ defaultCookieOptions,
141
+ defaultCookieConfig,
142
+ defaultStandardCookieConfig,
143
+ defaultSecureCookieConfig,
144
+ defaultHostCookieConfig,
145
+ expiredCookieOptions,
146
+ defineDefaultCookieOptions,
147
+ setCookie,
148
+ getCookie,
149
+ createSessionCookie,
150
+ secureCookieOptions,
151
+ expireCookie,
152
+ oauthCookie,
153
+ parse2 as parse
154
+ };