@authowl/next 0.4.3 → 0.4.5

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.
package/README.md CHANGED
@@ -39,7 +39,10 @@ session bridge. It preserves AuthOwl's browser bearer fallback for browsers that
39
39
  block cross-site cookies while giving `auth()` a validated, host-only HttpOnly
40
40
  session on your application origin. `AUTHOWL_SECRET_KEY` is required and must
41
41
  belong to the same project and environment with the `sessions:read` scope. It is
42
- used only by the server route and must never use a `NEXT_PUBLIC_` prefix.
42
+ used only on the server and must never use a `NEXT_PUBLIC_` prefix. Both the
43
+ bridge route and `auth()` present it: configure it where `auth()` runs, through
44
+ the env or `initAuth({ secretKey })`, not only through the bridge's own
45
+ `secretKey` option. `auth()` throws when it finds a bridge cookie without one.
43
46
 
44
47
  ```ts
45
48
  // app/api/authowl/session/route.ts
package/dist/server.cjs CHANGED
@@ -21,24 +21,24 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var server_exports = {};
22
22
  __export(server_exports, {
23
23
  AuthServiceError: () => AuthServiceError,
24
- AuthorizationError: () => import_server5.AuthorizationError,
25
- TokenVerificationError: () => import_server4.TokenVerificationError,
24
+ AuthorizationError: () => import_server4.AuthorizationError,
25
+ TokenVerificationError: () => import_server3.TokenVerificationError,
26
26
  auth: () => auth,
27
27
  createAuthOwlSessionBridge: () => createAuthOwlSessionBridge,
28
- has: () => import_server4.has,
28
+ has: () => import_server3.has,
29
29
  hasAuthOwlSessionCookie: () => hasAuthOwlSessionCookie,
30
- hasPermission: () => import_server4.hasPermission,
30
+ hasPermission: () => import_server3.hasPermission,
31
31
  initAuth: () => initAuth,
32
- isAuthorizationError: () => import_server5.isAuthorizationError,
33
- requireAuth: () => import_server5.requireAuth,
34
- requireGrant: () => import_server5.requireGrant,
35
- requireOrg: () => import_server5.requireOrg,
36
- requirePermission: () => import_server5.requirePermission,
37
- verifyToken: () => import_server4.verifyToken
32
+ isAuthorizationError: () => import_server4.isAuthorizationError,
33
+ requireAuth: () => import_server4.requireAuth,
34
+ requireGrant: () => import_server4.requireGrant,
35
+ requireOrg: () => import_server4.requireOrg,
36
+ requirePermission: () => import_server4.requirePermission,
37
+ verifyToken: () => import_server3.verifyToken
38
38
  });
39
39
  module.exports = __toCommonJS(server_exports);
40
40
  var import_headers = require("next/headers.js");
41
- var import_server3 = require("@authowl/core/server");
41
+ var import_server2 = require("@authowl/core/server");
42
42
 
43
43
  // src/bridge-contract.ts
44
44
  var APP_SESSION_BRIDGE_HEADER = "x-authowl-session-bridge";
@@ -54,6 +54,16 @@ function appSessionCookieNames(projectId) {
54
54
 
55
55
  // src/server-config.ts
56
56
  var import_server = require("@authowl/core/server");
57
+ function configuredBridge(config) {
58
+ return config.secretKey ? { ...config, secretKey: config.secretKey } : null;
59
+ }
60
+ function bearerSessionHeaders(config, token) {
61
+ return {
62
+ authorization: `Bearer ${token}`,
63
+ [import_server.SESSION_TRANSPORT_HEADER]: import_server.SESSION_TRANSPORT_BEARER,
64
+ [AUTHOWL_SECRET_KEY_HEADER]: config.secretKey
65
+ };
66
+ }
57
67
  function serverConfig(config) {
58
68
  const resolved = (0, import_server.resolveAuthTarget)(config);
59
69
  const secretKey = config.secretKey ?? process.env.AUTHOWL_SECRET_KEY;
@@ -86,7 +96,6 @@ function resolveAuthConfig(config) {
86
96
  }
87
97
 
88
98
  // src/session-bridge.ts
89
- var import_server2 = require("@authowl/core/server");
90
99
  var MAX_BODY_BYTES = 16384;
91
100
  var AUTH_FETCH_TIMEOUT_MS = 1e4;
92
101
  function response(status, body) {
@@ -143,9 +152,6 @@ function clearCookies(projectId) {
143
152
  result.headers.append("set-cookie", serializeCookie(names.local, "", { secure: false, clear: true }));
144
153
  return result;
145
154
  }
146
- function configuredBridge(config) {
147
- return config.secretKey ? { ...config, secretKey: config.secretKey } : null;
148
- }
149
155
  function requireConfiguredBridge(config) {
150
156
  const configured = configuredBridge(config);
151
157
  if (configured) return configured;
@@ -186,10 +192,8 @@ async function validateSession(remoteFetch, config, token) {
186
192
  {
187
193
  method: "GET",
188
194
  headers: {
189
- authorization: `Bearer ${token}`,
190
- [import_server2.SESSION_TRANSPORT_HEADER]: import_server2.SESSION_TRANSPORT_BEARER,
191
- [AUTHOWL_SECRET_KEY_HEADER]: config.secretKey,
192
- "x-publishable-key": config.publishableKey
195
+ "x-publishable-key": config.publishableKey,
196
+ ...bearerSessionHeaders(config, token)
193
197
  },
194
198
  cache: "no-store",
195
199
  redirect: "error",
@@ -308,8 +312,8 @@ function createAuthOwlSessionBridge(input) {
308
312
  }
309
313
 
310
314
  // src/server.ts
315
+ var import_server3 = require("@authowl/core/server");
311
316
  var import_server4 = require("@authowl/core/server");
312
- var import_server5 = require("@authowl/core/server");
313
317
  var AUTH_FETCH_TIMEOUT_MS2 = 1e4;
314
318
  var AuthServiceError = class extends Error {
315
319
  status;
@@ -338,12 +342,22 @@ async function auth() {
338
342
  const cfg = getAuthConfig();
339
343
  const projectId = cfg.projectId;
340
344
  const secure = new URL(cfg.apiUrl).protocol === "https:";
341
- const cookieName = (0, import_server3.sessionCookieName)(projectId, { secure });
345
+ const cookieName = (0, import_server2.sessionCookieName)(projectId, { secure });
342
346
  const cookieStore = await (0, import_headers.cookies)();
343
347
  const allCookies = cookieStore.getAll();
344
348
  const cookieHeader = allCookies.filter((c) => c.name === cookieName).map((c) => `${c.name}=${encodeURIComponent(c.value)}`).join("; ");
345
349
  const bridgeNames = appSessionCookieNames(projectId);
346
350
  const bridgeToken = cookieHeader ? null : allCookies.find((cookie) => cookie.name === bridgeNames.secure)?.value ?? allCookies.find((cookie) => cookie.name === bridgeNames.local)?.value ?? null;
351
+ let bridgeHeaders = {};
352
+ if (bridgeToken) {
353
+ const bridge = configuredBridge(cfg);
354
+ if (!bridge) {
355
+ throw new Error(
356
+ "AuthOwl session bridge cookie found, but auth() has no secret key. Set AUTHOWL_SECRET_KEY or pass secretKey to initAuth(); the secretKey option of createAuthOwlSessionBridge() does not configure auth()."
357
+ );
358
+ }
359
+ bridgeHeaders = bearerSessionHeaders(bridge, bridgeToken);
360
+ }
347
361
  const hdrs = await (0, import_headers.headers)();
348
362
  const url = `${new URL(cfg.apiUrl).origin}/api/projects/${projectId}/auth/get-session`;
349
363
  const timeout = createTimeoutSignal(AUTH_FETCH_TIMEOUT_MS2);
@@ -355,11 +369,7 @@ async function auth() {
355
369
  cookie: cookieHeader,
356
370
  "x-publishable-key": cfg.publishableKey,
357
371
  "user-agent": hdrs.get("user-agent") ?? "next-auth-helper",
358
- ...bridgeToken ? {
359
- authorization: `Bearer ${bridgeToken}`,
360
- [import_server3.SESSION_TRANSPORT_HEADER]: import_server3.SESSION_TRANSPORT_BEARER,
361
- ...cfg.secretKey ? { [AUTHOWL_SECRET_KEY_HEADER]: cfg.secretKey } : {}
362
- } : {}
372
+ ...bridgeHeaders
363
373
  },
364
374
  cache: "no-store",
365
375
  signal: timeout.signal
@@ -389,7 +399,7 @@ async function auth() {
389
399
  async function hasAuthOwlSessionCookie() {
390
400
  const cfg = getAuthConfig();
391
401
  const secure = new URL(cfg.apiUrl).protocol === "https:";
392
- const native = (0, import_server3.sessionCookieName)(cfg.projectId, { secure });
402
+ const native = (0, import_server2.sessionCookieName)(cfg.projectId, { secure });
393
403
  const bridge = appSessionCookieNames(cfg.projectId);
394
404
  const names = /* @__PURE__ */ new Set([native, bridge.secure, bridge.local]);
395
405
  return (await (0, import_headers.cookies)()).getAll().some((cookie) => names.has(cookie.name));
package/dist/server.js CHANGED
@@ -7,14 +7,24 @@ import {
7
7
 
8
8
  // src/server.ts
9
9
  import { headers as nextHeaders, cookies as nextCookies } from "next/headers.js";
10
- import {
11
- SESSION_TRANSPORT_BEARER as SESSION_TRANSPORT_BEARER2,
12
- SESSION_TRANSPORT_HEADER as SESSION_TRANSPORT_HEADER2,
13
- sessionCookieName
14
- } from "@authowl/core/server";
10
+ import { sessionCookieName } from "@authowl/core/server";
15
11
 
16
12
  // src/server-config.ts
17
- import { resolveAuthTarget } from "@authowl/core/server";
13
+ import {
14
+ SESSION_TRANSPORT_BEARER,
15
+ SESSION_TRANSPORT_HEADER,
16
+ resolveAuthTarget
17
+ } from "@authowl/core/server";
18
+ function configuredBridge(config) {
19
+ return config.secretKey ? { ...config, secretKey: config.secretKey } : null;
20
+ }
21
+ function bearerSessionHeaders(config, token) {
22
+ return {
23
+ authorization: `Bearer ${token}`,
24
+ [SESSION_TRANSPORT_HEADER]: SESSION_TRANSPORT_BEARER,
25
+ [AUTHOWL_SECRET_KEY_HEADER]: config.secretKey
26
+ };
27
+ }
18
28
  function serverConfig(config) {
19
29
  const resolved = resolveAuthTarget(config);
20
30
  const secretKey = config.secretKey ?? process.env.AUTHOWL_SECRET_KEY;
@@ -47,10 +57,6 @@ function resolveAuthConfig(config) {
47
57
  }
48
58
 
49
59
  // src/session-bridge.ts
50
- import {
51
- SESSION_TRANSPORT_BEARER,
52
- SESSION_TRANSPORT_HEADER
53
- } from "@authowl/core/server";
54
60
  var MAX_BODY_BYTES = 16384;
55
61
  var AUTH_FETCH_TIMEOUT_MS = 1e4;
56
62
  function response(status, body) {
@@ -107,9 +113,6 @@ function clearCookies(projectId) {
107
113
  result.headers.append("set-cookie", serializeCookie(names.local, "", { secure: false, clear: true }));
108
114
  return result;
109
115
  }
110
- function configuredBridge(config) {
111
- return config.secretKey ? { ...config, secretKey: config.secretKey } : null;
112
- }
113
116
  function requireConfiguredBridge(config) {
114
117
  const configured = configuredBridge(config);
115
118
  if (configured) return configured;
@@ -150,10 +153,8 @@ async function validateSession(remoteFetch, config, token) {
150
153
  {
151
154
  method: "GET",
152
155
  headers: {
153
- authorization: `Bearer ${token}`,
154
- [SESSION_TRANSPORT_HEADER]: SESSION_TRANSPORT_BEARER,
155
- [AUTHOWL_SECRET_KEY_HEADER]: config.secretKey,
156
- "x-publishable-key": config.publishableKey
156
+ "x-publishable-key": config.publishableKey,
157
+ ...bearerSessionHeaders(config, token)
157
158
  },
158
159
  cache: "no-store",
159
160
  redirect: "error",
@@ -320,6 +321,16 @@ async function auth() {
320
321
  const cookieHeader = allCookies.filter((c) => c.name === cookieName).map((c) => `${c.name}=${encodeURIComponent(c.value)}`).join("; ");
321
322
  const bridgeNames = appSessionCookieNames(projectId);
322
323
  const bridgeToken = cookieHeader ? null : allCookies.find((cookie) => cookie.name === bridgeNames.secure)?.value ?? allCookies.find((cookie) => cookie.name === bridgeNames.local)?.value ?? null;
324
+ let bridgeHeaders = {};
325
+ if (bridgeToken) {
326
+ const bridge = configuredBridge(cfg);
327
+ if (!bridge) {
328
+ throw new Error(
329
+ "AuthOwl session bridge cookie found, but auth() has no secret key. Set AUTHOWL_SECRET_KEY or pass secretKey to initAuth(); the secretKey option of createAuthOwlSessionBridge() does not configure auth()."
330
+ );
331
+ }
332
+ bridgeHeaders = bearerSessionHeaders(bridge, bridgeToken);
333
+ }
323
334
  const hdrs = await nextHeaders();
324
335
  const url = `${new URL(cfg.apiUrl).origin}/api/projects/${projectId}/auth/get-session`;
325
336
  const timeout = createTimeoutSignal(AUTH_FETCH_TIMEOUT_MS2);
@@ -331,11 +342,7 @@ async function auth() {
331
342
  cookie: cookieHeader,
332
343
  "x-publishable-key": cfg.publishableKey,
333
344
  "user-agent": hdrs.get("user-agent") ?? "next-auth-helper",
334
- ...bridgeToken ? {
335
- authorization: `Bearer ${bridgeToken}`,
336
- [SESSION_TRANSPORT_HEADER2]: SESSION_TRANSPORT_BEARER2,
337
- ...cfg.secretKey ? { [AUTHOWL_SECRET_KEY_HEADER]: cfg.secretKey } : {}
338
- } : {}
345
+ ...bridgeHeaders
339
346
  },
340
347
  cache: "no-store",
341
348
  signal: timeout.signal
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@authowl/next",
3
- "version": "0.4.3",
3
+ "version": "0.4.5",
4
4
  "description": "Next.js (App Router) server helpers for AuthOwl, the multi-tenant auth SaaS.",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
@@ -61,7 +61,7 @@
61
61
  "LICENSE"
62
62
  ],
63
63
  "dependencies": {
64
- "@authowl/core": "0.24.2"
64
+ "@authowl/core": "0.25.0"
65
65
  },
66
66
  "peerDependencies": {
67
67
  "next": ">=14"