@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,10 +1,9 @@
1
1
  import * as _aura_stack_router from '@aura-stack/router';
2
- import { A as AuthRuntimeConfig } from '../../index-DpfbvTZ_.js';
2
+ import { A as AuthRuntimeConfig } from '../../index-EqsoyjrF.js';
3
3
  import 'zod/v4';
4
- import '@aura-stack/jose/jose';
5
4
  import '../../schemas.js';
6
- import 'zod/v4/core';
7
- import 'cookie';
5
+ import '@aura-stack/router/cookie';
6
+ import '@aura-stack/jose/jose';
8
7
  import '../../@types/utility.js';
9
8
 
10
9
  declare const callbackAction: (oauth: AuthRuntimeConfig["oauth"]) => _aura_stack_router.RouteEndpoint<"GET", "/callback/:oauth", {}>;
@@ -1,16 +1,15 @@
1
1
  import {
2
2
  callbackAction
3
- } from "../../chunk-HGJ4TXY4.js";
4
- import "../../chunk-RLT4RFKV.js";
5
- import "../../chunk-UJJ7R56J.js";
6
- import "../../chunk-ZV4BH47P.js";
7
- import "../../chunk-6SM22VVJ.js";
3
+ } from "../../chunk-UEH3LVON.js";
4
+ import "../../chunk-ZLR3LI6X.js";
5
+ import "../../chunk-4V4JNXVF.js";
6
+ import "../../chunk-IMICRJ5U.js";
8
7
  import "../../chunk-STHEPPUZ.js";
9
- import "../../chunk-GZU3RBTB.js";
10
- import "../../chunk-256KIVJL.js";
11
- import "../../chunk-FJUDBLCP.js";
12
- import "../../chunk-JAPMIE6S.js";
13
- import "../../chunk-HMRKN75I.js";
8
+ import "../../chunk-WD7AUHQ5.js";
9
+ import "../../chunk-N2APGLXA.js";
10
+ import "../../chunk-CXLATHS5.js";
11
+ import "../../chunk-EIL2FPSS.js";
12
+ import "../../chunk-RRLIF4PQ.js";
14
13
  export {
15
14
  callbackAction
16
15
  };
@@ -40,24 +40,24 @@ var import_node_crypto = __toESM(require("crypto"), 1);
40
40
  // src/utils.ts
41
41
  var import_router = require("@aura-stack/router");
42
42
 
43
- // src/error.ts
44
- var AuthError = class extends Error {
45
- constructor(type, message) {
46
- super(message);
47
- this.type = type;
48
- this.name = "AuthError";
43
+ // src/errors.ts
44
+ var OAuthProtocolError = class extends Error {
45
+ type = "OAUTH_PROTOCOL_ERROR";
46
+ error;
47
+ errorURI;
48
+ constructor(error, description, errorURI, options2) {
49
+ super(description, options2);
50
+ this.error = error;
51
+ this.errorURI = errorURI;
52
+ this.name = new.target.name;
53
+ Error.captureStackTrace(this, new.target);
49
54
  }
50
55
  };
51
- var isAuthError = (error) => {
52
- return error instanceof AuthError;
56
+ var isNativeError = (error) => {
57
+ return error instanceof Error;
53
58
  };
54
- var throwAuthError = (error, message) => {
55
- if (error instanceof Error) {
56
- if (isAuthError(error)) {
57
- throw error;
58
- }
59
- throw new AuthError("invalid_request", error.message ?? message);
60
- }
59
+ var isOAuthProtocolError = (error) => {
60
+ return error instanceof OAuthProtocolError;
61
61
  };
62
62
 
63
63
  // src/secure.ts
@@ -68,10 +68,10 @@ var generateSecure = (length = 32) => {
68
68
  // src/schemas.ts
69
69
  var import_v4 = require("zod/v4");
70
70
  var OAuthProviderConfigSchema = (0, import_v4.object)({
71
- authorizeURL: (0, import_v4.url)(),
72
- accessToken: (0, import_v4.url)(),
71
+ authorizeURL: (0, import_v4.httpUrl)(),
72
+ accessToken: (0, import_v4.httpUrl)(),
73
73
  scope: (0, import_v4.string)().optional(),
74
- userInfo: (0, import_v4.url)(),
74
+ userInfo: (0, import_v4.httpUrl)(),
75
75
  responseType: (0, import_v4.enum)(["code", "token", "id_token"]),
76
76
  clientId: (0, import_v4.string)(),
77
77
  clientSecret: (0, import_v4.string)()
@@ -83,8 +83,8 @@ var OAuthAuthorization = OAuthProviderConfigSchema.extend({
83
83
  codeChallengeMethod: (0, import_v4.enum)(["plain", "S256"])
84
84
  });
85
85
  var OAuthAuthorizationResponse = (0, import_v4.object)({
86
- state: (0, import_v4.string)(),
87
- code: (0, import_v4.string)()
86
+ state: (0, import_v4.string)("Missing state parameter in the OAuth authorization response."),
87
+ code: (0, import_v4.string)("Missing code parameter in the OAuth authorization response.")
88
88
  });
89
89
  var OAuthAuthorizationErrorResponse = (0, import_v4.object)({
90
90
  error: (0, import_v4.enum)([
@@ -128,6 +128,10 @@ var OAuthErrorResponse = (0, import_v4.object)({
128
128
  error: (0, import_v4.string)(),
129
129
  error_description: (0, import_v4.string)().optional()
130
130
  });
131
+ var OAuthEnvSchema = (0, import_v4.object)({
132
+ clientId: import_v4.z.string().min(1, "OAuth Client ID is required in the environment variables."),
133
+ clientSecret: import_v4.z.string().min(1, "OAuth Client Secret is required in the environment variables.")
134
+ });
131
135
 
132
136
  // src/actions/callback/userinfo.ts
133
137
  var getDefaultUserInfo = (profile) => {
@@ -152,11 +156,20 @@ var getUserInfo = async (oauthConfig, accessToken) => {
152
156
  const json = await response.json();
153
157
  const { success, data } = OAuthErrorResponse.safeParse(json);
154
158
  if (success) {
155
- throw new AuthError(data.error, data?.error_description ?? "An error occurred while fetching user information.");
159
+ throw new OAuthProtocolError(
160
+ data.error,
161
+ data?.error_description ?? "An error occurred while fetching user information."
162
+ );
156
163
  }
157
164
  return oauthConfig?.profile ? oauthConfig.profile(json) : getDefaultUserInfo(json);
158
165
  } catch (error) {
159
- throw throwAuthError(error, "Failed to retrieve userinfo");
166
+ if (isOAuthProtocolError(error)) {
167
+ throw error;
168
+ }
169
+ if (isNativeError(error)) {
170
+ throw new OAuthProtocolError("invalid_request", error.message, "", { cause: error });
171
+ }
172
+ throw new OAuthProtocolError("invalid_request", "Failed to fetch user information.", "", { cause: error });
160
173
  }
161
174
  };
162
175
  // Annotate the CommonJS export names for ESM import in node:
@@ -1,9 +1,8 @@
1
- import { f as OAuthProviderCredentials, U as User } from '../../index-DpfbvTZ_.js';
1
+ import { h as OAuthProviderCredentials, U as User } from '../../index-EqsoyjrF.js';
2
2
  import 'zod/v4';
3
- import '@aura-stack/jose/jose';
4
3
  import '../../schemas.js';
5
- import 'zod/v4/core';
6
- import 'cookie';
4
+ import '@aura-stack/router/cookie';
5
+ import '@aura-stack/jose/jose';
7
6
  import '../../@types/utility.js';
8
7
 
9
8
  /**
@@ -1,10 +1,11 @@
1
1
  import {
2
2
  getUserInfo
3
- } from "../../chunk-RLT4RFKV.js";
4
- import "../../chunk-GZU3RBTB.js";
5
- import "../../chunk-256KIVJL.js";
6
- import "../../chunk-FJUDBLCP.js";
7
- import "../../chunk-HMRKN75I.js";
3
+ } from "../../chunk-ZLR3LI6X.js";
4
+ import "../../chunk-WD7AUHQ5.js";
5
+ import "../../chunk-N2APGLXA.js";
6
+ import "../../chunk-CXLATHS5.js";
7
+ import "../../chunk-EIL2FPSS.js";
8
+ import "../../chunk-RRLIF4PQ.js";
8
9
  export {
9
10
  getUserInfo
10
11
  };
@@ -41,12 +41,15 @@ var import_node_crypto = __toESM(require("crypto"), 1);
41
41
  // src/utils.ts
42
42
  var import_router = require("@aura-stack/router");
43
43
 
44
- // src/error.ts
45
- var AuthError = class extends Error {
46
- constructor(type, message) {
47
- super(message);
48
- this.type = type;
49
- this.name = "AuthError";
44
+ // src/errors.ts
45
+ var AuthInternalError = class extends Error {
46
+ type = "AUTH_INTERNAL_ERROR";
47
+ code;
48
+ constructor(code, message, options) {
49
+ super(message, options);
50
+ this.code = code;
51
+ this.name = new.target.name;
52
+ Error.captureStackTrace(this, new.target);
50
53
  }
51
54
  };
52
55
 
@@ -77,128 +80,56 @@ var cacheControl = {
77
80
  };
78
81
 
79
82
  // src/cookie.ts
80
- var import_cookie = require("cookie");
81
-
82
- // src/assert.ts
83
- var isRequest = (value) => {
84
- return typeof Request !== "undefined" && value instanceof Request;
85
- };
86
-
87
- // src/cookie.ts
88
- var import_cookie2 = require("cookie");
89
- var COOKIE_NAME = "aura-auth";
83
+ var import_cookie = require("@aura-stack/router/cookie");
90
84
  var defaultCookieOptions = {
91
85
  httpOnly: true,
92
86
  sameSite: "lax",
93
87
  path: "/",
94
88
  maxAge: 60 * 60 * 24 * 15
95
89
  };
96
- var defaultStandardCookieConfig = {
97
- secure: false,
90
+ var oauthCookieOptions = {
98
91
  httpOnly: true,
99
- prefix: ""
100
- };
101
- var defaultSecureCookieConfig = {
102
- secure: true,
103
- prefix: "__Secure-"
92
+ maxAge: 5 * 60,
93
+ sameSite: "lax",
94
+ expires: new Date(Date.now() + 5 * 60 * 1e3)
104
95
  };
105
- var defaultHostCookieConfig = {
106
- secure: true,
107
- prefix: "__Host-",
108
- path: "/",
109
- domain: void 0
96
+ var setCookie = (cookieName, value, options) => {
97
+ return (0, import_cookie.serialize)(cookieName, value, options);
110
98
  };
111
- var expiredCookieOptions = {
99
+ var expiredCookieAttributes = {
112
100
  ...defaultCookieOptions,
113
101
  expires: /* @__PURE__ */ new Date(0),
114
102
  maxAge: 0
115
103
  };
116
- var defineDefaultCookieOptions = (options) => {
117
- return {
118
- name: options?.name ?? COOKIE_NAME,
119
- prefix: options?.prefix ?? (options?.secure ? "__Secure-" : ""),
120
- ...defaultCookieOptions,
121
- ...options
122
- };
123
- };
124
- var setCookie = (cookieName, value, options) => {
125
- const { prefix, name } = defineDefaultCookieOptions(options);
126
- const cookieNameWithPrefix = `${prefix}${name}.${cookieName}`;
127
- return (0, import_cookie.serialize)(cookieNameWithPrefix, value, {
128
- ...defaultCookieOptions,
129
- ...options
130
- });
131
- };
132
- var getCookie = (petition, cookie, options, optional = false) => {
133
- const cookies = isRequest(petition) ? petition.headers.get("Cookie") : petition.headers.getSetCookie().join("; ");
104
+ var getCookie = (request, cookieName) => {
105
+ const cookies = request.headers.get("Cookie");
134
106
  if (!cookies) {
135
- if (optional) {
136
- return "";
137
- }
138
- throw new AuthError("invalid_request", "No cookies found. There is no active session");
107
+ throw new AuthInternalError("COOKIE_NOT_FOUND", "No cookies found. There is no active session");
139
108
  }
140
- const { name, prefix } = defineDefaultCookieOptions(options);
141
- const parsedCookies = (0, import_cookie.parse)(cookies);
142
- const value = parsedCookies[`${prefix}${name}.${cookie}`];
143
- if (value === void 0) {
144
- if (optional) {
145
- return "";
146
- }
147
- throw new AuthError("invalid_request", `Cookie "${cookie}" not found. There is no active session`);
109
+ const value = (0, import_cookie.parse)(cookies)[cookieName];
110
+ if (!value) {
111
+ throw new AuthInternalError("COOKIE_NOT_FOUND", `Cookie "${cookieName}" not found. There is no active session`);
148
112
  }
149
113
  return value;
150
114
  };
151
- var secureCookieOptions = (request, cookieOptions, trustedProxyHeaders) => {
152
- const name = cookieOptions.name ?? COOKIE_NAME;
153
- 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://");
154
- if (!cookieOptions.options?.httpOnly) {
155
- console.warn(
156
- "[WARNING]: Cookie is configured without HttpOnly. This allows JavaScript access via document.cookie and increases XSS risk."
157
- );
158
- }
159
- if (cookieOptions.options?.domain === "*") {
160
- console.warn("[WARNING]: Cookie 'Domain' is set to '*', which is insecure. Avoid wildcard domains.");
161
- }
162
- if (!isSecure) {
163
- const options = cookieOptions.options;
164
- if (options?.secure) {
165
- console.warn(
166
- "[WARNING]: The 'Secure' attribute will be disabled for this cookie. Serve over HTTPS to enforce Secure cookies."
167
- );
168
- }
169
- if (options?.sameSite == "none") {
170
- console.warn("[WARNING]: SameSite=None without a secure connection can be blocked by browsers.");
171
- }
172
- if (process.env.NODE_ENV === "production") {
173
- console.warn("[WARNING]: In production, ensure cookies are served over HTTPS to maintain security.");
174
- }
175
- return {
176
- ...defaultCookieOptions,
177
- ...cookieOptions.options,
178
- sameSite: options?.sameSite === "none" ? "lax" : options?.sameSite ?? "lax",
179
- ...defaultStandardCookieConfig,
180
- name
181
- };
182
- }
183
- return cookieOptions.strategy === "host" ? {
184
- ...defaultCookieOptions,
185
- ...cookieOptions.options,
186
- ...defaultHostCookieConfig,
187
- name
188
- } : { ...defaultCookieOptions, ...cookieOptions.options, ...defaultSecureCookieConfig, name };
189
- };
190
115
 
191
116
  // src/actions/csrfToken/csrfToken.ts
117
+ var getCSRFToken = (request, cookieName) => {
118
+ try {
119
+ return getCookie(request, cookieName);
120
+ } catch {
121
+ return void 0;
122
+ }
123
+ };
192
124
  var csrfTokenAction = (0, import_router2.createEndpoint)("GET", "/csrfToken", async (ctx) => {
193
125
  const {
194
126
  request,
195
- context: { cookies, jose, trustedProxyHeaders }
127
+ context: { jose, cookies }
196
128
  } = ctx;
197
- const cookieOptions = secureCookieOptions(request, { ...cookies, strategy: "host" }, trustedProxyHeaders);
198
- const existingCSRFToken = getCookie(request, "csrfToken", cookieOptions, true);
199
- const csrfToken = await createCSRF(jose, existingCSRFToken);
129
+ const token = getCSRFToken(request, cookies.csrfToken.name);
130
+ const csrfToken = await createCSRF(jose, token);
200
131
  const headers = new Headers(cacheControl);
201
- headers.set("Set-Cookie", setCookie("csrfToken", csrfToken, cookieOptions));
132
+ headers.append("Set-Cookie", setCookie(cookies.csrfToken.name, csrfToken, cookies.csrfToken.attributes));
202
133
  return Response.json({ csrfToken }, { headers });
203
134
  });
204
135
  // Annotate the CommonJS export names for ESM import in node:
@@ -1,12 +1,12 @@
1
1
  import {
2
2
  csrfTokenAction
3
- } from "../../chunk-SMQO5WD7.js";
4
- import "../../chunk-ZV4BH47P.js";
5
- import "../../chunk-6SM22VVJ.js";
3
+ } from "../../chunk-QDO2KSRJ.js";
4
+ import "../../chunk-IMICRJ5U.js";
6
5
  import "../../chunk-STHEPPUZ.js";
7
- import "../../chunk-GZU3RBTB.js";
8
- import "../../chunk-256KIVJL.js";
9
- import "../../chunk-FJUDBLCP.js";
6
+ import "../../chunk-N2APGLXA.js";
7
+ import "../../chunk-CXLATHS5.js";
8
+ import "../../chunk-EIL2FPSS.js";
9
+ import "../../chunk-RRLIF4PQ.js";
10
10
  export {
11
11
  csrfTokenAction
12
12
  };