@aura-stack/auth 0.1.0 → 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 (99) hide show
  1. package/dist/@types/index.d.ts +3 -4
  2. package/dist/@types/router.d.d.ts +3 -4
  3. package/dist/@types/utility.d.ts +1 -5
  4. package/dist/actions/callback/access-token.cjs +51 -41
  5. package/dist/actions/callback/access-token.d.ts +3 -4
  6. package/dist/actions/callback/access-token.js +4 -3
  7. package/dist/actions/callback/callback.cjs +115 -210
  8. package/dist/actions/callback/callback.d.ts +3 -4
  9. package/dist/actions/callback/callback.js +9 -10
  10. package/dist/actions/callback/userinfo.cjs +35 -22
  11. package/dist/actions/callback/userinfo.d.ts +3 -4
  12. package/dist/actions/callback/userinfo.js +6 -5
  13. package/dist/actions/csrfToken/csrfToken.cjs +34 -103
  14. package/dist/actions/csrfToken/csrfToken.js +6 -6
  15. package/dist/actions/index.cjs +234 -391
  16. package/dist/actions/index.d.ts +3 -4
  17. package/dist/actions/index.js +16 -17
  18. package/dist/actions/session/session.cjs +25 -109
  19. package/dist/actions/session/session.js +4 -5
  20. package/dist/actions/signIn/authorization.cjs +64 -55
  21. package/dist/actions/signIn/authorization.d.ts +3 -4
  22. package/dist/actions/signIn/authorization.js +5 -5
  23. package/dist/actions/signIn/signIn.cjs +84 -206
  24. package/dist/actions/signIn/signIn.d.ts +3 -4
  25. package/dist/actions/signIn/signIn.js +7 -9
  26. package/dist/actions/signOut/signOut.cjs +88 -234
  27. package/dist/actions/signOut/signOut.js +8 -9
  28. package/dist/assert.cjs +5 -0
  29. package/dist/assert.d.ts +9 -1
  30. package/dist/assert.js +3 -1
  31. package/dist/chunk-2RXNXMCZ.js +55 -0
  32. package/dist/{chunk-UJJ7R56J.js → chunk-4V4JNXVF.js} +13 -10
  33. package/dist/chunk-6R2YZ4AC.js +22 -0
  34. package/dist/{chunk-VFTYH33W.js → chunk-7H3OR6UU.js} +29 -9
  35. package/dist/{chunk-256KIVJL.js → chunk-CXLATHS5.js} +53 -9
  36. package/dist/{chunk-6SM22VVJ.js → chunk-EIL2FPSS.js} +5 -1
  37. package/dist/chunk-IMICRJ5U.js +197 -0
  38. package/dist/{chunk-EBPE35JT.js → chunk-IUYZQTJV.js} +0 -1
  39. package/dist/{chunk-GZU3RBTB.js → chunk-N2APGLXA.js} +19 -10
  40. package/dist/chunk-NEVKX6K2.js +70 -0
  41. package/dist/{chunk-XXJKNKGQ.js → chunk-PTJUYB33.js} +9 -13
  42. package/dist/chunk-QDO2KSRJ.js +35 -0
  43. package/dist/{chunk-CAKJT3KS.js → chunk-QEZL7EYN.js} +21 -17
  44. package/dist/chunk-RRLIF4PQ.js +55 -0
  45. package/dist/chunk-TLE4PXY3.js +39 -0
  46. package/dist/chunk-UEH3LVON.js +97 -0
  47. package/dist/{chunk-HMRKN75I.js → chunk-WD7AUHQ5.js} +12 -7
  48. package/dist/{chunk-RLT4RFKV.js → chunk-ZLR3LI6X.js} +19 -9
  49. package/dist/cookie.cjs +140 -99
  50. package/dist/cookie.d.ts +33 -43
  51. package/dist/cookie.js +10 -17
  52. package/dist/errors.cjs +85 -0
  53. package/dist/errors.d.ts +48 -0
  54. package/dist/errors.js +18 -0
  55. package/dist/{index-DpfbvTZ_.d.ts → index-EqsoyjrF.d.ts} +139 -57
  56. package/dist/index.cjs +427 -389
  57. package/dist/index.d.ts +4 -5
  58. package/dist/index.js +37 -26
  59. package/dist/jose.cjs +23 -12
  60. package/dist/jose.d.ts +4 -1
  61. package/dist/jose.js +5 -4
  62. package/dist/oauth/bitbucket.d.ts +3 -4
  63. package/dist/oauth/discord.cjs +0 -1
  64. package/dist/oauth/discord.d.ts +3 -4
  65. package/dist/oauth/discord.js +1 -1
  66. package/dist/oauth/figma.d.ts +3 -4
  67. package/dist/oauth/github.d.ts +3 -4
  68. package/dist/oauth/gitlab.d.ts +3 -4
  69. package/dist/oauth/index.cjs +132 -6
  70. package/dist/oauth/index.d.ts +3 -4
  71. package/dist/oauth/index.js +12 -5
  72. package/dist/oauth/spotify.d.ts +3 -4
  73. package/dist/{response.cjs → oauth/strava.cjs} +21 -9
  74. package/dist/oauth/strava.d.ts +6 -0
  75. package/dist/oauth/strava.js +6 -0
  76. package/dist/oauth/x.d.ts +3 -4
  77. package/dist/schemas.cjs +11 -5
  78. package/dist/schemas.d.ts +70 -67
  79. package/dist/schemas.js +3 -1
  80. package/dist/secure.cjs +27 -19
  81. package/dist/secure.d.ts +3 -4
  82. package/dist/secure.js +4 -3
  83. package/dist/utils.cjs +90 -15
  84. package/dist/utils.d.ts +11 -2
  85. package/dist/utils.js +8 -4
  86. package/package.json +5 -6
  87. package/dist/chunk-FJUDBLCP.js +0 -59
  88. package/dist/chunk-HGJ4TXY4.js +0 -137
  89. package/dist/chunk-JAPMIE6S.js +0 -10
  90. package/dist/chunk-LLR722CL.js +0 -96
  91. package/dist/chunk-SJPDVKUS.js +0 -112
  92. package/dist/chunk-SMQO5WD7.js +0 -30
  93. package/dist/chunk-UTDLUEEG.js +0 -31
  94. package/dist/chunk-ZV4BH47P.js +0 -154
  95. package/dist/error.cjs +0 -88
  96. package/dist/error.d.ts +0 -62
  97. package/dist/error.js +0 -16
  98. package/dist/response.d.ts +0 -10
  99. package/dist/response.js +0 -6
@@ -1,137 +0,0 @@
1
- import {
2
- getUserInfo
3
- } from "./chunk-RLT4RFKV.js";
4
- import {
5
- createAccessToken
6
- } from "./chunk-UJJ7R56J.js";
7
- import {
8
- createSessionCookie,
9
- expireCookie,
10
- getCookie,
11
- secureCookieOptions,
12
- setCookie
13
- } from "./chunk-ZV4BH47P.js";
14
- import {
15
- cacheControl
16
- } from "./chunk-STHEPPUZ.js";
17
- import {
18
- createCSRF
19
- } from "./chunk-GZU3RBTB.js";
20
- import {
21
- equals,
22
- isValidRelativePath,
23
- sanitizeURL
24
- } from "./chunk-256KIVJL.js";
25
- import {
26
- AuthError,
27
- ERROR_RESPONSE,
28
- isAuthError
29
- } from "./chunk-FJUDBLCP.js";
30
- import {
31
- AuraResponse
32
- } from "./chunk-JAPMIE6S.js";
33
- import {
34
- OAuthAuthorizationErrorResponse,
35
- OAuthAuthorizationResponse
36
- } from "./chunk-HMRKN75I.js";
37
-
38
- // src/actions/callback/callback.ts
39
- import z from "zod";
40
- import { createEndpoint, createEndpointConfig, statusCode } from "@aura-stack/router";
41
- var callbackConfig = (oauth) => {
42
- return createEndpointConfig("/callback/:oauth", {
43
- schemas: {
44
- searchParams: OAuthAuthorizationResponse,
45
- params: z.object({
46
- oauth: z.enum(Object.keys(oauth))
47
- })
48
- },
49
- middlewares: [
50
- (ctx) => {
51
- const response = OAuthAuthorizationErrorResponse.safeParse(ctx.searchParams);
52
- if (response.success) {
53
- const { error, error_description } = response.data;
54
- throw new AuthError(error, error_description ?? "OAuth Authorization Error");
55
- }
56
- return ctx;
57
- }
58
- ]
59
- });
60
- };
61
- var callbackAction = (oauth) => {
62
- return createEndpoint(
63
- "GET",
64
- "/callback/:oauth",
65
- async (ctx) => {
66
- const {
67
- request,
68
- params: { oauth: oauth2 },
69
- searchParams: { code, state },
70
- context: { oauth: providers, cookies, jose, trustedProxyHeaders }
71
- } = ctx;
72
- try {
73
- const oauthConfig = providers[oauth2];
74
- const cookieOptions = secureCookieOptions(request, cookies, trustedProxyHeaders);
75
- const cookieState = getCookie(request, "state", cookieOptions);
76
- const cookieRedirectTo = getCookie(request, "redirect_to", cookieOptions);
77
- const cookieRedirectURI = getCookie(request, "redirect_uri", cookieOptions);
78
- const codeVerifier = getCookie(request, "code_verifier", cookieOptions);
79
- if (!equals(cookieState, state)) {
80
- throw new AuthError(ERROR_RESPONSE.ACCESS_TOKEN.INVALID_REQUEST, "Mismatching state");
81
- }
82
- const accessToken = await createAccessToken(oauthConfig, cookieRedirectURI, code, codeVerifier);
83
- const sanitized = sanitizeURL(cookieRedirectTo);
84
- if (!isValidRelativePath(sanitized)) {
85
- throw new AuthError(
86
- ERROR_RESPONSE.ACCESS_TOKEN.INVALID_REQUEST,
87
- "Invalid redirect path. Potential open redirect attack detected."
88
- );
89
- }
90
- const headers = new Headers(cacheControl);
91
- headers.set("Location", sanitized);
92
- const userInfo = await getUserInfo(oauthConfig, accessToken.access_token);
93
- const sessionCookie = await createSessionCookie(userInfo, cookieOptions, jose);
94
- const csrfToken = await createCSRF(jose);
95
- const csrfCookie = setCookie(
96
- "csrfToken",
97
- csrfToken,
98
- secureCookieOptions(
99
- request,
100
- {
101
- ...cookies,
102
- strategy: "host"
103
- },
104
- trustedProxyHeaders
105
- )
106
- );
107
- headers.set("Set-Cookie", sessionCookie);
108
- headers.append("Set-Cookie", expireCookie("state", cookieOptions));
109
- headers.append("Set-Cookie", expireCookie("redirect_uri", cookieOptions));
110
- headers.append("Set-Cookie", expireCookie("redirect_to", cookieOptions));
111
- headers.append("Set-Cookie", expireCookie("code_verifier", cookieOptions));
112
- headers.append("Set-Cookie", csrfCookie);
113
- return Response.json({ oauth: oauth2 }, { status: 302, headers });
114
- } catch (error) {
115
- if (isAuthError(error)) {
116
- const { type, message } = error;
117
- return AuraResponse.json(
118
- { error: type, error_description: message },
119
- { status: statusCode.BAD_REQUEST }
120
- );
121
- }
122
- return AuraResponse.json(
123
- {
124
- error: ERROR_RESPONSE.ACCESS_TOKEN.INVALID_CLIENT,
125
- error_description: "An unexpected error occurred"
126
- },
127
- { status: statusCode.INTERNAL_SERVER_ERROR }
128
- );
129
- }
130
- },
131
- callbackConfig(oauth)
132
- );
133
- };
134
-
135
- export {
136
- callbackAction
137
- };
@@ -1,10 +0,0 @@
1
- // src/response.ts
2
- var AuraResponse = class extends Response {
3
- static json(body, init) {
4
- return Response.json(body, init);
5
- }
6
- };
7
-
8
- export {
9
- AuraResponse
10
- };
@@ -1,96 +0,0 @@
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
- };
@@ -1,112 +0,0 @@
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
- };
@@ -1,30 +0,0 @@
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
- };
@@ -1,31 +0,0 @@
1
- import {
2
- createDerivedSalt
3
- } from "./chunk-GZU3RBTB.js";
4
- import {
5
- AuthError
6
- } from "./chunk-FJUDBLCP.js";
7
-
8
- // src/jose.ts
9
- import "dotenv/config";
10
- import { createJWT, createJWS, createDeriveKey } from "@aura-stack/jose";
11
- var createJoseInstance = (secret) => {
12
- secret ?? (secret = process.env.AURA_AUTH_SECRET);
13
- if (!secret) {
14
- throw new AuthError("JOSE_INIT_ERROR", "AURA_AUTH_SECRET environment variable is not set and no secret was provided.");
15
- }
16
- const salt = process.env.AURA_AUTH_SALT ?? createDerivedSalt(secret);
17
- const { derivedKey: derivedSessionKey } = createDeriveKey(secret, salt, "session");
18
- const { derivedKey: derivedCsrfTokenKey } = createDeriveKey(secret, salt, "csrfToken");
19
- const { decodeJWT, encodeJWT } = createJWT(derivedSessionKey);
20
- const { signJWS, verifyJWS } = createJWS(derivedCsrfTokenKey);
21
- return {
22
- decodeJWT,
23
- encodeJWT,
24
- signJWS,
25
- verifyJWS
26
- };
27
- };
28
-
29
- export {
30
- createJoseInstance
31
- };
@@ -1,154 +0,0 @@
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
- };
package/dist/error.cjs DELETED
@@ -1,88 +0,0 @@
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/error.ts
21
- var error_exports = {};
22
- __export(error_exports, {
23
- AuthError: () => AuthError,
24
- ERROR_RESPONSE: () => ERROR_RESPONSE,
25
- InvalidCsrfTokenError: () => InvalidCsrfTokenError,
26
- InvalidRedirectToError: () => InvalidRedirectToError,
27
- isAuthError: () => isAuthError,
28
- throwAuthError: () => throwAuthError
29
- });
30
- module.exports = __toCommonJS(error_exports);
31
- var AuthError = class extends Error {
32
- constructor(type, message) {
33
- super(message);
34
- this.type = type;
35
- this.name = "AuthError";
36
- }
37
- };
38
- var InvalidCsrfTokenError = class extends AuthError {
39
- constructor(message = "The provided CSRF token is invalid or has expired") {
40
- super("invalid_csrf_token", message);
41
- this.name = "InvalidCsrfTokenError";
42
- }
43
- };
44
- var InvalidRedirectToError = class extends AuthError {
45
- constructor(message = "The redirectTo parameter does not match the hosted origin.") {
46
- super("invalid_redirect_to", message);
47
- this.name = "InvalidRedirectToError";
48
- }
49
- };
50
- var isAuthError = (error) => {
51
- return error instanceof AuthError;
52
- };
53
- var throwAuthError = (error, message) => {
54
- if (error instanceof Error) {
55
- if (isAuthError(error)) {
56
- throw error;
57
- }
58
- throw new AuthError("invalid_request", error.message ?? message);
59
- }
60
- };
61
- var ERROR_RESPONSE = {
62
- AUTHORIZATION: {
63
- INVALID_REQUEST: "invalid_request",
64
- UNAUTHORIZED_CLIENT: "unauthorized_client",
65
- ACCESS_DENIED: "access_denied",
66
- UNSUPPORTED_RESPONSE_TYPE: "unsupported_response_type",
67
- INVALID_SCOPE: "invalid_scope",
68
- SERVER_ERROR: "server_error",
69
- TEMPORARILY_UNAVAILABLE: "temporarily_unavailable"
70
- },
71
- ACCESS_TOKEN: {
72
- INVALID_REQUEST: "invalid_request",
73
- INVALID_CLIENT: "invalid_client",
74
- INVALID_GRANT: "invalid_grant",
75
- UNAUTHORIZED_CLIENT: "unauthorized_client",
76
- UNSUPPORTED_GRANT_TYPE: "unsupported_grant_type",
77
- INVALID_SCOPE: "invalid_scope"
78
- }
79
- };
80
- // Annotate the CommonJS export names for ESM import in node:
81
- 0 && (module.exports = {
82
- AuthError,
83
- ERROR_RESPONSE,
84
- InvalidCsrfTokenError,
85
- InvalidRedirectToError,
86
- isAuthError,
87
- throwAuthError
88
- });