@aura-stack/auth 0.1.0-rc.9 → 0.2.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 (140) hide show
  1. package/dist/@types/index.cjs +15 -15
  2. package/dist/@types/index.d.ts +6 -31
  3. package/dist/@types/index.js +1 -1
  4. package/dist/@types/router.d.cjs +1 -1
  5. package/dist/@types/router.d.d.ts +6 -7
  6. package/dist/@types/utility.cjs +15 -15
  7. package/dist/@types/utility.d.ts +4 -8
  8. package/dist/@types/utility.js +1 -1
  9. package/dist/actions/callback/access-token.cjs +159 -149
  10. package/dist/actions/callback/access-token.d.ts +14 -20
  11. package/dist/actions/callback/access-token.js +9 -4
  12. package/dist/actions/callback/callback.cjs +371 -484
  13. package/dist/actions/callback/callback.d.ts +9 -12
  14. package/dist/actions/callback/callback.js +15 -12
  15. package/dist/actions/callback/userinfo.cjs +149 -139
  16. package/dist/actions/callback/userinfo.d.ts +8 -9
  17. package/dist/actions/callback/userinfo.js +11 -6
  18. package/dist/actions/csrfToken/csrfToken.cjs +112 -190
  19. package/dist/actions/csrfToken/csrfToken.d.ts +3 -3
  20. package/dist/actions/csrfToken/csrfToken.js +12 -8
  21. package/dist/actions/index.cjs +623 -807
  22. package/dist/actions/index.d.ts +12 -13
  23. package/dist/actions/index.js +33 -18
  24. package/dist/actions/session/session.cjs +86 -176
  25. package/dist/actions/session/session.d.ts +3 -3
  26. package/dist/actions/session/session.js +10 -7
  27. package/dist/actions/signIn/authorization.cjs +248 -257
  28. package/dist/actions/signIn/authorization.d.ts +11 -18
  29. package/dist/actions/signIn/authorization.js +16 -6
  30. package/dist/actions/signIn/signIn.cjs +302 -451
  31. package/dist/actions/signIn/signIn.d.ts +9 -10
  32. package/dist/actions/signIn/signIn.js +12 -10
  33. package/dist/actions/signOut/signOut.cjs +299 -468
  34. package/dist/actions/signOut/signOut.d.ts +3 -3
  35. package/dist/actions/signOut/signOut.js +14 -11
  36. package/dist/assert.cjs +40 -36
  37. package/dist/assert.d.ts +12 -4
  38. package/dist/assert.js +12 -2
  39. package/dist/chunk-2RXNXMCZ.js +55 -0
  40. package/dist/chunk-42XB3YCW.js +19 -17
  41. package/dist/chunk-4V4JNXVF.js +55 -0
  42. package/dist/chunk-6R2YZ4AC.js +22 -0
  43. package/dist/chunk-7H3OR6UU.js +81 -0
  44. package/dist/chunk-CXLATHS5.js +143 -0
  45. package/dist/chunk-E3OXBRYF.js +19 -17
  46. package/dist/chunk-EIL2FPSS.js +22 -0
  47. package/dist/chunk-FIPU4MLT.js +18 -16
  48. package/dist/chunk-FKRDCWBF.js +19 -17
  49. package/dist/chunk-IKHPGFCW.js +11 -9
  50. package/dist/chunk-IMICRJ5U.js +197 -0
  51. package/dist/chunk-IUYZQTJV.js +30 -0
  52. package/dist/chunk-KRNOMBXQ.js +19 -17
  53. package/dist/chunk-N2APGLXA.js +71 -0
  54. package/dist/chunk-NEVKX6K2.js +70 -0
  55. package/dist/chunk-PTJUYB33.js +33 -0
  56. package/dist/chunk-QDO2KSRJ.js +35 -0
  57. package/dist/chunk-QEZL7EYN.js +96 -0
  58. package/dist/chunk-RRLIF4PQ.js +55 -0
  59. package/dist/chunk-STHEPPUZ.js +8 -6
  60. package/dist/chunk-TLE4PXY3.js +39 -0
  61. package/dist/chunk-UEH3LVON.js +97 -0
  62. package/dist/chunk-WD7AUHQ5.js +79 -0
  63. package/dist/chunk-ZLR3LI6X.js +55 -0
  64. package/dist/cookie.cjs +222 -187
  65. package/dist/cookie.d.ts +38 -76
  66. package/dist/cookie.js +27 -34
  67. package/dist/errors.cjs +85 -0
  68. package/dist/errors.d.ts +48 -0
  69. package/dist/errors.js +18 -0
  70. package/dist/headers.cjs +28 -28
  71. package/dist/headers.d.ts +2 -2
  72. package/dist/headers.js +6 -2
  73. package/dist/{index-DpfbvTZ_.d.ts → index-EqsoyjrF.d.ts} +351 -318
  74. package/dist/index.cjs +1006 -1001
  75. package/dist/index.d.ts +9 -31
  76. package/dist/index.js +82 -51
  77. package/dist/jose.cjs +74 -66
  78. package/dist/jose.d.ts +11 -8
  79. package/dist/jose.js +10 -5
  80. package/dist/oauth/bitbucket.cjs +38 -38
  81. package/dist/oauth/bitbucket.d.ts +6 -7
  82. package/dist/oauth/bitbucket.js +6 -2
  83. package/dist/oauth/discord.cjs +47 -48
  84. package/dist/oauth/discord.d.ts +6 -7
  85. package/dist/oauth/discord.js +6 -2
  86. package/dist/oauth/figma.cjs +39 -39
  87. package/dist/oauth/figma.d.ts +6 -7
  88. package/dist/oauth/figma.js +6 -2
  89. package/dist/oauth/github.cjs +31 -31
  90. package/dist/oauth/github.d.ts +6 -7
  91. package/dist/oauth/github.js +6 -2
  92. package/dist/oauth/gitlab.cjs +39 -39
  93. package/dist/oauth/gitlab.d.ts +6 -7
  94. package/dist/oauth/gitlab.js +6 -2
  95. package/dist/oauth/index.cjs +306 -180
  96. package/dist/oauth/index.d.ts +6 -26
  97. package/dist/oauth/index.js +43 -9
  98. package/dist/oauth/spotify.cjs +39 -39
  99. package/dist/oauth/spotify.d.ts +6 -7
  100. package/dist/oauth/spotify.js +6 -2
  101. package/dist/oauth/strava.cjs +46 -0
  102. package/dist/oauth/strava.d.ts +6 -0
  103. package/dist/oauth/strava.js +6 -0
  104. package/dist/oauth/x.cjs +39 -39
  105. package/dist/oauth/x.d.ts +6 -7
  106. package/dist/oauth/x.js +6 -2
  107. package/dist/schemas.cjs +97 -91
  108. package/dist/schemas.d.ts +96 -126
  109. package/dist/schemas.js +20 -18
  110. package/dist/secure.cjs +106 -101
  111. package/dist/secure.d.ts +16 -17
  112. package/dist/secure.js +19 -4
  113. package/dist/utils.cjs +197 -135
  114. package/dist/utils.d.ts +20 -26
  115. package/dist/utils.js +25 -21
  116. package/package.json +5 -6
  117. package/dist/chunk-256KIVJL.js +0 -110
  118. package/dist/chunk-6SM22VVJ.js +0 -15
  119. package/dist/chunk-CAKJT3KS.js +0 -77
  120. package/dist/chunk-EBPE35JT.js +0 -29
  121. package/dist/chunk-FJUDBLCP.js +0 -52
  122. package/dist/chunk-GZU3RBTB.js +0 -51
  123. package/dist/chunk-HGJ4TXY4.js +0 -105
  124. package/dist/chunk-HMRKN75I.js +0 -74
  125. package/dist/chunk-JAPMIE6S.js +0 -8
  126. package/dist/chunk-LLR722CL.js +0 -75
  127. package/dist/chunk-RLT4RFKV.js +0 -36
  128. package/dist/chunk-SJPDVKUS.js +0 -93
  129. package/dist/chunk-SMQO5WD7.js +0 -20
  130. package/dist/chunk-UJJ7R56J.js +0 -42
  131. package/dist/chunk-UTDLUEEG.js +0 -25
  132. package/dist/chunk-VFTYH33W.js +0 -44
  133. package/dist/chunk-XXJKNKGQ.js +0 -27
  134. package/dist/chunk-ZV4BH47P.js +0 -156
  135. package/dist/error.cjs +0 -88
  136. package/dist/error.d.ts +0 -62
  137. package/dist/error.js +0 -9
  138. package/dist/response.cjs +0 -34
  139. package/dist/response.d.ts +0 -10
  140. package/dist/response.js +0 -2
@@ -1,20 +1,22 @@
1
1
  // src/oauth/figma.ts
2
2
  var figma = {
3
- id: "figma",
4
- name: "Figma",
5
- authorizeURL: "https://www.figma.com/oauth",
6
- accessToken: "https://api.figma.com/v1/oauth/token",
7
- userInfo: "https://api.figma.com/v1/me",
8
- scope: "current_user:read",
9
- responseType: "code",
10
- profile(profile) {
11
- return {
12
- sub: profile.id,
13
- name: profile.handle,
14
- email: profile.email,
15
- image: profile.img_url,
16
- }
17
- },
18
- }
3
+ id: "figma",
4
+ name: "Figma",
5
+ authorizeURL: "https://www.figma.com/oauth",
6
+ accessToken: "https://api.figma.com/v1/oauth/token",
7
+ userInfo: "https://api.figma.com/v1/me",
8
+ scope: "current_user:read",
9
+ responseType: "code",
10
+ profile(profile) {
11
+ return {
12
+ sub: profile.id,
13
+ name: profile.handle,
14
+ email: profile.email,
15
+ image: profile.img_url
16
+ };
17
+ }
18
+ };
19
19
 
20
- export { figma }
20
+ export {
21
+ figma
22
+ };
@@ -1,12 +1,14 @@
1
1
  // src/oauth/github.ts
2
2
  var github = {
3
- id: "github",
4
- name: "GitHub",
5
- authorizeURL: "https://github.com/login/oauth/authorize",
6
- accessToken: "https://github.com/login/oauth/access_token",
7
- userInfo: "https://api.github.com/user",
8
- scope: "read:user user:email",
9
- responseType: "code",
10
- }
3
+ id: "github",
4
+ name: "GitHub",
5
+ authorizeURL: "https://github.com/login/oauth/authorize",
6
+ accessToken: "https://github.com/login/oauth/access_token",
7
+ userInfo: "https://api.github.com/user",
8
+ scope: "read:user user:email",
9
+ responseType: "code"
10
+ };
11
11
 
12
- export { github }
12
+ export {
13
+ github
14
+ };
@@ -0,0 +1,197 @@
1
+ import {
2
+ AuthInternalError
3
+ } from "./chunk-RRLIF4PQ.js";
4
+
5
+ // src/cookie.ts
6
+ import { parse, parseSetCookie, serialize } from "@aura-stack/router/cookie";
7
+ var COOKIE_NAME = "aura-auth";
8
+ var defaultCookieOptions = {
9
+ httpOnly: true,
10
+ sameSite: "lax",
11
+ path: "/",
12
+ maxAge: 60 * 60 * 24 * 15
13
+ };
14
+ var defaultStandardCookieConfig = {
15
+ secure: false,
16
+ httpOnly: true
17
+ };
18
+ var defaultSecureCookieConfig = {
19
+ secure: true,
20
+ httpOnly: true
21
+ };
22
+ var defaultHostCookieConfig = {
23
+ secure: true,
24
+ httpOnly: true,
25
+ path: "/",
26
+ domain: void 0
27
+ };
28
+ var oauthCookieOptions = {
29
+ httpOnly: true,
30
+ maxAge: 5 * 60,
31
+ sameSite: "lax",
32
+ expires: new Date(Date.now() + 5 * 60 * 1e3)
33
+ };
34
+ var setCookie = (cookieName, value, options) => {
35
+ return serialize(cookieName, value, options);
36
+ };
37
+ var expiredCookieAttributes = {
38
+ ...defaultCookieOptions,
39
+ expires: /* @__PURE__ */ new Date(0),
40
+ maxAge: 0
41
+ };
42
+ var getCookie = (request, cookieName) => {
43
+ const cookies = request.headers.get("Cookie");
44
+ if (!cookies) {
45
+ throw new AuthInternalError("COOKIE_NOT_FOUND", "No cookies found. There is no active session");
46
+ }
47
+ const value = parse(cookies)[cookieName];
48
+ if (!value) {
49
+ throw new AuthInternalError("COOKIE_NOT_FOUND", `Cookie "${cookieName}" not found. There is no active session`);
50
+ }
51
+ return value;
52
+ };
53
+ var getSetCookie = (response, cookieName) => {
54
+ const cookies = response.headers.getSetCookie();
55
+ if (!cookies) {
56
+ throw new AuthInternalError("COOKIE_NOT_FOUND", "No cookies found in response.");
57
+ }
58
+ const strCookie = cookies.find((cookie) => cookie.startsWith(`${cookieName}=`));
59
+ if (!strCookie) {
60
+ throw new AuthInternalError("COOKIE_NOT_FOUND", `Cookie "${cookieName}" not found in response.`);
61
+ }
62
+ return parseSetCookie(strCookie).value;
63
+ };
64
+ var createSessionCookie = async (jose, session) => {
65
+ try {
66
+ const encoded = await jose.encodeJWT(session);
67
+ return encoded;
68
+ } catch (error) {
69
+ throw new AuthInternalError("INVALID_JWT_TOKEN", "Failed to create session cookie", { cause: error });
70
+ }
71
+ };
72
+ var defineSecureCookieOptions = (useSecure, attributes, strategy) => {
73
+ if (!attributes.httpOnly) {
74
+ console.warn(
75
+ "[WARNING]: Cookie is configured without HttpOnly. This allows JavaScript access via document.cookie and increases XSS risk."
76
+ );
77
+ }
78
+ if (attributes.domain === "*") {
79
+ attributes.domain = void 0;
80
+ console.warn("[WARNING]: Cookie 'Domain' is set to '*', which is insecure. Avoid wildcard domains.");
81
+ }
82
+ if (!useSecure) {
83
+ if (attributes.secure) {
84
+ console.warn(
85
+ "[WARNING]: The 'Secure' attribute will be disabled for this cookie. Serve over HTTPS to enforce Secure cookies."
86
+ );
87
+ }
88
+ if (attributes.sameSite == "none") {
89
+ attributes.sameSite = "lax";
90
+ console.warn("[WARNING]: SameSite=None requires Secure attribute. Changing SameSite to 'Lax'.");
91
+ }
92
+ if (process.env.NODE_ENV === "production") {
93
+ console.warn("[WARNING]: In production, ensure cookies are served over HTTPS to maintain security.");
94
+ }
95
+ if (strategy === "host") {
96
+ console.warn("[WARNING]: __Host- cookies require a secure context. Falling back to standard cookie settings.");
97
+ }
98
+ return {
99
+ ...defaultCookieOptions,
100
+ ...attributes,
101
+ ...defaultStandardCookieConfig
102
+ };
103
+ }
104
+ return strategy === "host" ? {
105
+ ...defaultCookieOptions,
106
+ ...attributes,
107
+ ...defaultHostCookieConfig
108
+ } : { ...defaultCookieOptions, ...attributes, ...defaultSecureCookieConfig };
109
+ };
110
+ var createCookieStore = (useSecure, prefix, overrides) => {
111
+ prefix ??= COOKIE_NAME;
112
+ const securePrefix = useSecure ? "__Secure-" : "";
113
+ const hostPrefix = useSecure ? "__Host-" : "";
114
+ return {
115
+ sessionToken: {
116
+ name: `${securePrefix}${prefix}.${overrides?.sessionToken?.name ?? "sessionToken"}`,
117
+ attributes: defineSecureCookieOptions(
118
+ useSecure,
119
+ {
120
+ ...defaultCookieOptions,
121
+ ...overrides?.sessionToken?.attributes
122
+ },
123
+ overrides?.sessionToken?.attributes?.strategy ?? "secure"
124
+ )
125
+ },
126
+ state: {
127
+ name: `${securePrefix}${prefix}.${overrides?.state?.name ?? "state"}`,
128
+ attributes: defineSecureCookieOptions(
129
+ useSecure,
130
+ {
131
+ ...oauthCookieOptions,
132
+ ...overrides?.state?.attributes
133
+ },
134
+ overrides?.state?.attributes?.strategy ?? "secure"
135
+ )
136
+ },
137
+ csrfToken: {
138
+ name: `${hostPrefix}${prefix}.${overrides?.csrfToken?.name ?? "csrfToken"}`,
139
+ attributes: defineSecureCookieOptions(
140
+ useSecure,
141
+ {
142
+ ...overrides?.csrfToken?.attributes,
143
+ ...defaultHostCookieConfig
144
+ },
145
+ overrides?.csrfToken?.attributes?.strategy ?? "host"
146
+ )
147
+ },
148
+ redirect_to: {
149
+ name: `${securePrefix}${prefix}.${overrides?.redirect_to?.name ?? "redirect_to"}`,
150
+ attributes: defineSecureCookieOptions(
151
+ useSecure,
152
+ {
153
+ ...oauthCookieOptions,
154
+ ...overrides?.redirect_to?.attributes
155
+ },
156
+ overrides?.redirect_to?.attributes?.strategy ?? "secure"
157
+ )
158
+ },
159
+ redirect_uri: {
160
+ name: `${securePrefix}${prefix}.${overrides?.redirect_uri?.name ?? "redirect_uri"}`,
161
+ attributes: defineSecureCookieOptions(
162
+ useSecure,
163
+ {
164
+ ...oauthCookieOptions,
165
+ ...overrides?.redirect_uri?.attributes
166
+ },
167
+ overrides?.redirect_uri?.attributes?.strategy ?? "secure"
168
+ )
169
+ },
170
+ code_verifier: {
171
+ name: `${securePrefix}${prefix}.${overrides?.code_verifier?.name ?? "code_verifier"}`,
172
+ attributes: defineSecureCookieOptions(
173
+ useSecure,
174
+ {
175
+ ...oauthCookieOptions,
176
+ ...overrides?.code_verifier?.attributes
177
+ },
178
+ overrides?.code_verifier?.attributes?.strategy ?? "secure"
179
+ )
180
+ }
181
+ };
182
+ };
183
+
184
+ export {
185
+ COOKIE_NAME,
186
+ defaultCookieOptions,
187
+ defaultStandardCookieConfig,
188
+ defaultSecureCookieConfig,
189
+ defaultHostCookieConfig,
190
+ setCookie,
191
+ expiredCookieAttributes,
192
+ getCookie,
193
+ getSetCookie,
194
+ createSessionCookie,
195
+ defineSecureCookieOptions,
196
+ createCookieStore
197
+ };
@@ -0,0 +1,30 @@
1
+ // src/oauth/discord.ts
2
+ var discord = {
3
+ id: "discord",
4
+ name: "Discord",
5
+ authorizeURL: "https://discord.com/oauth2/authorize",
6
+ accessToken: "https://discord.com/api/oauth2/token",
7
+ userInfo: "https://discord.com/api/users/@me",
8
+ scope: "identify email",
9
+ responseType: "code",
10
+ profile(profile) {
11
+ let image = "";
12
+ if (profile.avatar === null) {
13
+ const index = profile.discriminator === "0" ? (BigInt(profile.id) >> 22n) % 6n : Number(profile.discriminator) % 5;
14
+ image = `https://cdn.discordapp.com/embed/avatars/${index}.png`;
15
+ } else {
16
+ const format = profile.avatar.startsWith("a_") ? "gif" : "png";
17
+ image = `https://cdn.discordapp.com/avatars/${profile.id}/${profile.avatar}.${format}`;
18
+ }
19
+ return {
20
+ sub: profile.id,
21
+ name: profile.global_name ?? profile.username,
22
+ email: profile.email ?? "",
23
+ image
24
+ };
25
+ }
26
+ };
27
+
28
+ export {
29
+ discord
30
+ };
@@ -1,20 +1,22 @@
1
1
  // src/oauth/gitlab.ts
2
2
  var gitlab = {
3
- id: "gitlab",
4
- name: "GitLab",
5
- authorizeURL: "https://gitlab.com/oauth/authorize",
6
- accessToken: "https://gitlab.com/oauth/token",
7
- userInfo: "https://gitlab.com/api/v4/user",
8
- scope: "read_user",
9
- responseType: "code",
10
- profile(profile) {
11
- return {
12
- sub: profile.id.toString(),
13
- name: profile.name ?? profile.username,
14
- email: profile.email,
15
- avatar: profile.avatar_url,
16
- }
17
- },
18
- }
3
+ id: "gitlab",
4
+ name: "GitLab",
5
+ authorizeURL: "https://gitlab.com/oauth/authorize",
6
+ accessToken: "https://gitlab.com/oauth/token",
7
+ userInfo: "https://gitlab.com/api/v4/user",
8
+ scope: "read_user",
9
+ responseType: "code",
10
+ profile(profile) {
11
+ return {
12
+ sub: profile.id.toString(),
13
+ name: profile.name ?? profile.username,
14
+ email: profile.email,
15
+ avatar: profile.avatar_url
16
+ };
17
+ }
18
+ };
19
19
 
20
- export { gitlab }
20
+ export {
21
+ gitlab
22
+ };
@@ -0,0 +1,71 @@
1
+ import {
2
+ equals
3
+ } from "./chunk-CXLATHS5.js";
4
+ import {
5
+ isJWTPayloadWithToken
6
+ } from "./chunk-EIL2FPSS.js";
7
+ import {
8
+ AuthSecurityError
9
+ } from "./chunk-RRLIF4PQ.js";
10
+
11
+ // src/secure.ts
12
+ import crypto from "crypto";
13
+ var generateSecure = (length = 32) => {
14
+ return crypto.randomBytes(length).toString("base64url");
15
+ };
16
+ var createHash = (data, base = "hex") => {
17
+ return crypto.createHash("sha256").update(data).digest().toString(base);
18
+ };
19
+ var createPKCE = async (verifier) => {
20
+ const codeVerifier = verifier ?? generateSecure(86);
21
+ const codeChallenge = createHash(codeVerifier, "base64url");
22
+ return { codeVerifier, codeChallenge, method: "S256" };
23
+ };
24
+ var createCSRF = async (jose, csrfCookie) => {
25
+ try {
26
+ const token = generateSecure(32);
27
+ if (csrfCookie) {
28
+ await jose.verifyJWS(csrfCookie);
29
+ return csrfCookie;
30
+ }
31
+ return jose.signJWS({ token });
32
+ } catch {
33
+ const token = generateSecure(32);
34
+ return jose.signJWS({ token });
35
+ }
36
+ };
37
+ var verifyCSRF = async (jose, cookie, header) => {
38
+ try {
39
+ const cookiePayload = await jose.verifyJWS(cookie);
40
+ const headerPayload = await jose.verifyJWS(header);
41
+ if (!isJWTPayloadWithToken(cookiePayload)) {
42
+ throw new AuthSecurityError("CSRF_TOKEN_INVALID", "Cookie payload missing token field.");
43
+ }
44
+ if (!isJWTPayloadWithToken(headerPayload)) {
45
+ throw new AuthSecurityError("CSRF_TOKEN_INVALID", "Header payload missing token field.");
46
+ }
47
+ const cookieBuffer = Buffer.from(cookiePayload.token);
48
+ const headerBuffer = Buffer.from(headerPayload.token);
49
+ if (!equals(headerBuffer.length, cookieBuffer.length)) {
50
+ throw new AuthSecurityError("CSRF_TOKEN_INVALID", "The CSRF tokens do not match.");
51
+ }
52
+ if (!crypto.timingSafeEqual(cookieBuffer, headerBuffer)) {
53
+ throw new AuthSecurityError("CSRF_TOKEN_INVALID", "The CSRF tokens do not match.");
54
+ }
55
+ return true;
56
+ } catch {
57
+ throw new AuthSecurityError("CSRF_TOKEN_INVALID", "The CSRF tokens do not match.");
58
+ }
59
+ };
60
+ var createDerivedSalt = (secret) => {
61
+ return crypto.createHash("sha256").update(secret).update("aura-auth-salt").digest("hex");
62
+ };
63
+
64
+ export {
65
+ generateSecure,
66
+ createHash,
67
+ createPKCE,
68
+ createCSRF,
69
+ verifyCSRF,
70
+ createDerivedSalt
71
+ };
@@ -0,0 +1,70 @@
1
+ import {
2
+ createRedirectTo
3
+ } from "./chunk-QEZL7EYN.js";
4
+ import {
5
+ expiredCookieAttributes
6
+ } from "./chunk-IMICRJ5U.js";
7
+ import {
8
+ cacheControl
9
+ } from "./chunk-STHEPPUZ.js";
10
+ import {
11
+ verifyCSRF
12
+ } from "./chunk-N2APGLXA.js";
13
+ import {
14
+ getNormalizedOriginPath
15
+ } from "./chunk-CXLATHS5.js";
16
+ import {
17
+ AuthSecurityError
18
+ } from "./chunk-RRLIF4PQ.js";
19
+
20
+ // src/actions/signOut/signOut.ts
21
+ import z from "zod";
22
+ import { createEndpoint, createEndpointConfig, HeadersBuilder, statusCode } from "@aura-stack/router";
23
+ var config = createEndpointConfig({
24
+ schemas: {
25
+ searchParams: z.object({
26
+ token_type_hint: z.literal("session_token"),
27
+ redirectTo: z.string().optional()
28
+ })
29
+ }
30
+ });
31
+ var signOutAction = createEndpoint(
32
+ "POST",
33
+ "/signOut",
34
+ async (ctx) => {
35
+ const {
36
+ request,
37
+ headers,
38
+ searchParams: { redirectTo },
39
+ context: { jose, cookies }
40
+ } = ctx;
41
+ const session = headers.getCookie(cookies.sessionToken.name);
42
+ const csrfToken = headers.getCookie(cookies.csrfToken.name);
43
+ const header = headers.getHeader("X-CSRF-Token");
44
+ if (!session) {
45
+ throw new AuthSecurityError("SESSION_TOKEN_MISSING", "The sessionToken is missing.");
46
+ }
47
+ if (!csrfToken) {
48
+ throw new AuthSecurityError("CSRF_TOKEN_MISSING", "The CSRF token is missing.");
49
+ }
50
+ if (!header) {
51
+ throw new AuthSecurityError("CSRF_TOKEN_MISSING", "The CSRF header is missing.");
52
+ }
53
+ await verifyCSRF(jose, csrfToken, header);
54
+ await jose.decodeJWT(session);
55
+ const normalizedOriginPath = getNormalizedOriginPath(request.url);
56
+ const location = createRedirectTo(
57
+ new Request(normalizedOriginPath, {
58
+ headers: headers.toHeaders()
59
+ }),
60
+ redirectTo
61
+ );
62
+ const headersList = new HeadersBuilder(cacheControl).setHeader("Location", location).setCookie(cookies.csrfToken.name, "", expiredCookieAttributes).setCookie(cookies.sessionToken.name, "", expiredCookieAttributes).toHeaders();
63
+ return Response.json({ message: "Signed out successfully" }, { status: statusCode.ACCEPTED, headers: headersList });
64
+ },
65
+ config
66
+ );
67
+
68
+ export {
69
+ signOutAction
70
+ };
@@ -0,0 +1,33 @@
1
+ import {
2
+ expiredCookieAttributes,
3
+ getCookie
4
+ } from "./chunk-IMICRJ5U.js";
5
+ import {
6
+ cacheControl
7
+ } from "./chunk-STHEPPUZ.js";
8
+ import {
9
+ toISOString
10
+ } from "./chunk-CXLATHS5.js";
11
+
12
+ // src/actions/session/session.ts
13
+ import { createEndpoint, HeadersBuilder } from "@aura-stack/router";
14
+ var sessionAction = createEndpoint("GET", "/session", async (ctx) => {
15
+ const {
16
+ request,
17
+ context: { jose, cookies }
18
+ } = ctx;
19
+ try {
20
+ const session = getCookie(request, cookies.sessionToken.name);
21
+ const decoded = await jose.decodeJWT(session);
22
+ const { exp, iat, jti, nbf, ...user } = decoded;
23
+ const headers = new Headers(cacheControl);
24
+ return Response.json({ user, expires: toISOString(exp * 1e3) }, { headers });
25
+ } catch (error) {
26
+ const headers = new HeadersBuilder(cacheControl).setCookie(cookies.sessionToken.name, "", expiredCookieAttributes).toHeaders();
27
+ return Response.json({ authenticated: false, message: "Unauthorized" }, { status: 401, headers });
28
+ }
29
+ });
30
+
31
+ export {
32
+ sessionAction
33
+ };
@@ -0,0 +1,35 @@
1
+ import {
2
+ getCookie,
3
+ setCookie
4
+ } from "./chunk-IMICRJ5U.js";
5
+ import {
6
+ cacheControl
7
+ } from "./chunk-STHEPPUZ.js";
8
+ import {
9
+ createCSRF
10
+ } from "./chunk-N2APGLXA.js";
11
+
12
+ // src/actions/csrfToken/csrfToken.ts
13
+ import { createEndpoint } from "@aura-stack/router";
14
+ var getCSRFToken = (request, cookieName) => {
15
+ try {
16
+ return getCookie(request, cookieName);
17
+ } catch {
18
+ return void 0;
19
+ }
20
+ };
21
+ var csrfTokenAction = createEndpoint("GET", "/csrfToken", async (ctx) => {
22
+ const {
23
+ request,
24
+ context: { jose, cookies }
25
+ } = ctx;
26
+ const token = getCSRFToken(request, cookies.csrfToken.name);
27
+ const csrfToken = await createCSRF(jose, token);
28
+ const headers = new Headers(cacheControl);
29
+ headers.append("Set-Cookie", setCookie(cookies.csrfToken.name, csrfToken, cookies.csrfToken.attributes));
30
+ return Response.json({ csrfToken }, { headers });
31
+ });
32
+
33
+ export {
34
+ csrfTokenAction
35
+ };
@@ -0,0 +1,96 @@
1
+ import {
2
+ OAuthAuthorization
3
+ } from "./chunk-WD7AUHQ5.js";
4
+ import {
5
+ equals,
6
+ formatZodError,
7
+ getNormalizedOriginPath,
8
+ sanitizeURL,
9
+ toCastCase
10
+ } from "./chunk-CXLATHS5.js";
11
+ import {
12
+ isValidURL
13
+ } from "./chunk-EIL2FPSS.js";
14
+ import {
15
+ AuthInternalError,
16
+ AuthSecurityError,
17
+ isAuthSecurityError
18
+ } from "./chunk-RRLIF4PQ.js";
19
+
20
+ // src/actions/signIn/authorization.ts
21
+ var createAuthorizationURL = (oauthConfig, redirectURI, state, codeChallenge, codeChallengeMethod) => {
22
+ const parsed = OAuthAuthorization.safeParse({ ...oauthConfig, redirectURI, state, codeChallenge, codeChallengeMethod });
23
+ if (!parsed.success) {
24
+ const msg = JSON.stringify(formatZodError(parsed.error), null, 2);
25
+ throw new AuthInternalError("INVALID_OAUTH_CONFIGURATION", msg);
26
+ }
27
+ const { authorizeURL, ...options } = parsed.data;
28
+ const { userInfo, accessToken, clientSecret, ...required } = options;
29
+ const searchParams = new URLSearchParams(toCastCase(required));
30
+ return `${authorizeURL}?${searchParams}`;
31
+ };
32
+ var getOriginURL = (request, trustedProxyHeaders) => {
33
+ const headers = request.headers;
34
+ if (trustedProxyHeaders) {
35
+ const protocol = headers.get("X-Forwarded-Proto") ?? headers.get("Forwarded")?.match(/proto=([^;]+)/i)?.[1] ?? "http";
36
+ const host = headers.get("X-Forwarded-Host") ?? headers.get("Host") ?? headers.get("Forwarded")?.match(/host=([^;]+)/i)?.[1] ?? null;
37
+ return new URL(`${protocol}://${host}${getNormalizedOriginPath(new URL(request.url).pathname)}`);
38
+ } else {
39
+ return new URL(getNormalizedOriginPath(request.url));
40
+ }
41
+ };
42
+ var createRedirectURI = (request, oauth, basePath, trustedProxyHeaders) => {
43
+ const url = getOriginURL(request, trustedProxyHeaders);
44
+ return `${url.origin}${basePath}/callback/${oauth}`;
45
+ };
46
+ var createRedirectTo = (request, redirectTo, trustedProxyHeaders) => {
47
+ try {
48
+ const headers = request.headers;
49
+ const origin = headers.get("Origin");
50
+ const referer = headers.get("Referer");
51
+ let hostedURL = getOriginURL(request, trustedProxyHeaders);
52
+ if (redirectTo) {
53
+ if (redirectTo.startsWith("/")) {
54
+ return sanitizeURL(redirectTo);
55
+ }
56
+ const redirectToURL = new URL(sanitizeURL(getNormalizedOriginPath(redirectTo)));
57
+ if (!isValidURL(redirectTo) || !equals(redirectToURL.origin, hostedURL.origin)) {
58
+ throw new AuthSecurityError(
59
+ "POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED",
60
+ "The redirectTo parameter does not match the hosted origin."
61
+ );
62
+ }
63
+ return sanitizeURL(redirectToURL.pathname);
64
+ }
65
+ if (referer) {
66
+ const refererURL = new URL(sanitizeURL(referer));
67
+ if (!isValidURL(referer) || !equals(refererURL.origin, hostedURL.origin)) {
68
+ throw new AuthSecurityError(
69
+ "POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED",
70
+ "The referer of the request does not match the hosted origin."
71
+ );
72
+ }
73
+ return sanitizeURL(refererURL.pathname);
74
+ }
75
+ if (origin) {
76
+ const originURL = new URL(sanitizeURL(getNormalizedOriginPath(origin)));
77
+ if (!isValidURL(origin) || !equals(originURL.origin, hostedURL.origin)) {
78
+ throw new AuthSecurityError("POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED", "Invalid origin (potential CSRF).");
79
+ }
80
+ return sanitizeURL(originURL.pathname);
81
+ }
82
+ return "/";
83
+ } catch (error) {
84
+ if (isAuthSecurityError(error)) {
85
+ throw error;
86
+ }
87
+ throw new AuthSecurityError("POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED", "Invalid origin (potential CSRF).");
88
+ }
89
+ };
90
+
91
+ export {
92
+ createAuthorizationURL,
93
+ getOriginURL,
94
+ createRedirectURI,
95
+ createRedirectTo
96
+ };