@lunora/auth 1.0.0-alpha.4 → 1.0.0-alpha.41
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/LICENSE.md +6 -0
- package/README.md +55 -0
- package/dist/adapter.d.mts +35 -35
- package/dist/adapter.d.ts +35 -35
- package/dist/adapter.mjs +1 -47
- package/dist/audit.d.mts +114 -0
- package/dist/audit.d.ts +114 -0
- package/dist/audit.mjs +11 -0
- package/dist/email-guard.d.mts +122 -0
- package/dist/email-guard.d.ts +122 -0
- package/dist/email-guard.mjs +1 -0
- package/dist/index.d.mts +460 -146
- package/dist/index.d.ts +460 -146
- package/dist/index.mjs +1 -12
- package/dist/middleware.d.mts +156 -155
- package/dist/middleware.d.ts +156 -155
- package/dist/middleware.mjs +1 -53
- package/dist/packem_shared/DEFAULT_AUTH_BASE_PATH-kIwlEt8i.mjs +1 -0
- package/dist/packem_shared/LunoraAuthAdminError-BiNYZM9j.mjs +1 -0
- package/dist/packem_shared/authAuditHook-Dx3sqf3G.mjs +1 -0
- package/dist/packem_shared/compileMigrationsSql-ChiudSmt.mjs +1 -0
- package/dist/packem_shared/create-auth.d-De6IOirt.d.mts +128 -0
- package/dist/packem_shared/create-auth.d-De6IOirt.d.ts +128 -0
- package/dist/packem_shared/createAuth-DS6PL8Mb.mjs +1 -0
- package/dist/packem_shared/emailGateDatabaseHooks-DzBD1Qoq.mjs +1 -0
- package/dist/packem_shared/sessionPresets-DpEFjXKV.mjs +1 -0
- package/dist/plugins-client.mjs +1 -2
- package/dist/plugins.mjs +1 -22
- package/dist/schema.d.mts +39 -39
- package/dist/schema.d.ts +39 -39
- package/dist/schema.mjs +1 -62
- package/dist/sql-store.d.mts +28 -28
- package/dist/sql-store.d.ts +28 -28
- package/dist/sql-store.mjs +1 -162
- package/dist/store.d.mts +49 -31
- package/dist/store.d.ts +49 -31
- package/dist/store.mjs +1 -170
- package/dist/turnstile-middleware.d.mts +55 -55
- package/dist/turnstile-middleware.d.ts +55 -55
- package/dist/turnstile-middleware.mjs +1 -45
- package/dist/turnstile.d.mts +42 -59
- package/dist/turnstile.d.ts +42 -59
- package/dist/turnstile.mjs +1 -61
- package/package.json +19 -6
- package/dist/packem_shared/DEFAULT_AUTH_BASE_PATH-DjcUWEQl.mjs +0 -11
- package/dist/packem_shared/LunoraAuthAdminError-BxrfEeA_.mjs +0 -249
- package/dist/packem_shared/compileMigrationsSql-wZH3oXDu.mjs +0 -28
- package/dist/packem_shared/create-auth.d-M36jwG_Y.d.mts +0 -58
- package/dist/packem_shared/create-auth.d-M36jwG_Y.d.ts +0 -58
- package/dist/packem_shared/createAuth-B-tvsvQU.mjs +0 -56
- package/dist/packem_shared/sessionPresets-B95rXrd8.mjs +0 -35
package/dist/index.mjs
CHANGED
|
@@ -1,12 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export { LunoraAuthAdminError, createAuthAdmin } from './packem_shared/LunoraAuthAdminError-BxrfEeA_.mjs';
|
|
3
|
-
export { createAuth } from './packem_shared/createAuth-B-tvsvQU.mjs';
|
|
4
|
-
export { DEFAULT_AUTH_BASE_PATH, handleAuthRequest } from './packem_shared/DEFAULT_AUTH_BASE_PATH-DjcUWEQl.mjs';
|
|
5
|
-
export { LunoraAuthHeadersError, withAuthPlugins } from './middleware.mjs';
|
|
6
|
-
export { compileMigrationsSql, ensureMigrated } from './packem_shared/compileMigrationsSql-wZH3oXDu.mjs';
|
|
7
|
-
export { default as authTables } from './schema.mjs';
|
|
8
|
-
export { sessionPresets, validateSessionPolicy } from './packem_shared/sessionPresets-B95rXrd8.mjs';
|
|
9
|
-
export { createSqlAuthStore, d1Executor } from './sql-store.mjs';
|
|
10
|
-
export { createMemoryAuthStore, matchesWhere } from './store.mjs';
|
|
11
|
-
export { TURNSTILE_VERIFY_ENDPOINT, verifyTurnstile } from './turnstile.mjs';
|
|
12
|
-
export { verifyTurnstileMiddleware } from './turnstile-middleware.mjs';
|
|
1
|
+
import{lunoraAuthAdapter as t,lunoraD1Adapter as o}from"./adapter.mjs";import{LunoraAuthAdminError as i,createAuthAdmin as u}from"./packem_shared/LunoraAuthAdminError-BiNYZM9j.mjs";import{AUTH_AUDIT_TABLE as l,appendAuthAuditEntry as m,createAuthAuditReader as s,ensureAuthAuditTable as d,readAuthAuditLog as h}from"./audit.mjs";import{authAuditHook as n,buildAuditEntry as f,eventForPath as x,withAuthAudit as E}from"./packem_shared/authAuditHook-Dx3sqf3G.mjs";import{createAuth as c,resolveAuthOptions as y}from"./packem_shared/createAuth-DS6PL8Mb.mjs";import{emailGateDatabaseHooks as S,withEmailGate as _}from"./packem_shared/emailGateDatabaseHooks-DzBD1Qoq.mjs";import{assertEmailAllowed as D,classifyEmail as H,emailGateMiddleware as P,loadEmailDomainLists as v}from"./email-guard.mjs";import{DEFAULT_AUTH_BASE_PATH as U,handleAuthRequest as b}from"./packem_shared/DEFAULT_AUTH_BASE_PATH-kIwlEt8i.mjs";import{LunoraAuthHeadersError as I,withAuthPlugins as R}from"./middleware.mjs";import{compileMigrationsSql as F,ensureMigrated as G}from"./packem_shared/compileMigrationsSql-ChiudSmt.mjs";import{default as k}from"./schema.mjs";import{sessionPresets as O,validateSessionPolicy as V}from"./packem_shared/sessionPresets-DpEFjXKV.mjs";import{createSqlAuthStore as Y,d1Executor as j}from"./sql-store.mjs";import{createMemoryAuthStore as C,matchesWhere as J}from"./store.mjs";import{TURNSTILE_VERIFY_ENDPOINT as Q,verifyTurnstile as X}from"./turnstile.mjs";import{verifyTurnstileMiddleware as $}from"./turnstile-middleware.mjs";export{l as AUTH_AUDIT_TABLE,U as DEFAULT_AUTH_BASE_PATH,i as LunoraAuthAdminError,I as LunoraAuthHeadersError,Q as TURNSTILE_VERIFY_ENDPOINT,m as appendAuthAuditEntry,D as assertEmailAllowed,n as authAuditHook,k as authTables,f as buildAuditEntry,H as classifyEmail,F as compileMigrationsSql,c as createAuth,u as createAuthAdmin,s as createAuthAuditReader,C as createMemoryAuthStore,Y as createSqlAuthStore,j as d1Executor,S as emailGateDatabaseHooks,P as emailGateMiddleware,d as ensureAuthAuditTable,G as ensureMigrated,x as eventForPath,b as handleAuthRequest,v as loadEmailDomainLists,t as lunoraAuthAdapter,o as lunoraD1Adapter,J as matchesWhere,h as readAuthAuditLog,y as resolveAuthOptions,O as sessionPresets,V as validateSessionPolicy,X as verifyTurnstile,$ as verifyTurnstileMiddleware,E as withAuthAudit,R as withAuthPlugins,_ as withEmailGate};
|
package/dist/middleware.d.mts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LunoraError } from '@lunora/errors';
|
|
2
|
+
import { L as LunoraAuth } from "./packem_shared/create-auth.d-De6IOirt.mjs";
|
|
2
3
|
import 'better-auth';
|
|
3
4
|
/**
|
|
4
|
-
* Structural mirror of `@lunora/server`'s `MiddlewareNext` — the continuation
|
|
5
|
-
* callback handed to a middleware. Repeated here so this package does not take
|
|
6
|
-
* a runtime dependency on `@lunora/server`; the types are assignable from the
|
|
7
|
-
* fully-typed builder.
|
|
8
|
-
*/
|
|
5
|
+
* Structural mirror of `@lunora/server`'s `MiddlewareNext` — the continuation
|
|
6
|
+
* callback handed to a middleware. Repeated here so this package does not take
|
|
7
|
+
* a runtime dependency on `@lunora/server`; the types are assignable from the
|
|
8
|
+
* fully-typed builder.
|
|
9
|
+
*/
|
|
9
10
|
interface MiddlewareNext<ContextIn> {
|
|
10
11
|
(): Promise<ContextIn>;
|
|
11
12
|
<Extension extends Record<string, unknown>>(options: {
|
|
@@ -13,174 +14,174 @@ interface MiddlewareNext<ContextIn> {
|
|
|
13
14
|
}): Promise<ContextIn & Extension>;
|
|
14
15
|
}
|
|
15
16
|
/**
|
|
16
|
-
* Thrown by the runtime header guard when a privileged `ctx.authApi.*` endpoint
|
|
17
|
-
* is invoked without a `headers` property on its argument object. Carries the
|
|
18
|
-
* offending `method` name so callers can pinpoint the bad call site, and points
|
|
19
|
-
* at the explicit escape hatches.
|
|
20
|
-
*
|
|
21
|
-
* This is the runtime sibling of the static `auth_api_call_without_headers`
|
|
22
|
-
* advisor lint — both treat a header-less `ctx.authApi.*` call as an
|
|
23
|
-
* authorization bypass, so a call that trips the lint also trips this guard.
|
|
24
|
-
*/
|
|
25
|
-
declare class LunoraAuthHeadersError extends
|
|
17
|
+
* Thrown by the runtime header guard when a privileged `ctx.authApi.*` endpoint
|
|
18
|
+
* is invoked without a `headers` property on its argument object. Carries the
|
|
19
|
+
* offending `method` name so callers can pinpoint the bad call site, and points
|
|
20
|
+
* at the explicit escape hatches.
|
|
21
|
+
*
|
|
22
|
+
* This is the runtime sibling of the static `auth_api_call_without_headers`
|
|
23
|
+
* advisor lint — both treat a header-less `ctx.authApi.*` call as an
|
|
24
|
+
* authorization bypass, so a call that trips the lint also trips this guard.
|
|
25
|
+
*/
|
|
26
|
+
declare class LunoraAuthHeadersError extends LunoraError {
|
|
26
27
|
/** The `ctx.authApi.<method>` that was called without `headers`. */
|
|
27
28
|
readonly method: string;
|
|
28
29
|
constructor(method: string);
|
|
29
30
|
}
|
|
30
31
|
/**
|
|
31
|
-
* Options for {@link withAuthPlugins}.
|
|
32
|
-
*/
|
|
32
|
+
* Options for {@link withAuthPlugins}.
|
|
33
|
+
*/
|
|
33
34
|
interface WithAuthPluginsOptions {
|
|
34
35
|
/**
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
36
|
+
* Whether to install the runtime header guard around `ctx.authApi`.
|
|
37
|
+
*
|
|
38
|
+
* **Defaults to `true` — the safe default.** When enabled, every
|
|
39
|
+
* `ctx.authApi.<method>(…)` call that omits `headers` throws
|
|
40
|
+
* {@link LunoraAuthHeadersError} instead of silently running with full
|
|
41
|
+
* server-to-server privileges. For a deliberate, per-call unauthenticated
|
|
42
|
+
* invocation, use the explicit `ctx.authApi.withoutHeaders()` escape hatch
|
|
43
|
+
* rather than disabling the guard wholesale.
|
|
44
|
+
*
|
|
45
|
+
* Set to `false` only when you have audited every `ctx.authApi.*` call site
|
|
46
|
+
* and accept responsibility for passing headers yourself. This is the loud,
|
|
47
|
+
* all-or-nothing opt-out; prefer `withoutHeaders()` for one-offs.
|
|
48
|
+
*/
|
|
48
49
|
enforceHeaders?: boolean;
|
|
49
50
|
}
|
|
50
51
|
/**
|
|
51
|
-
* The Lunora context extension this middleware installs. It does *not* replace
|
|
52
|
-
* `ctx.auth` (the identity-only surface populated by the runtime — `userId` and
|
|
53
|
-
* `getIdentity()`); instead it adds a sibling `ctx.authApi` that points at the
|
|
54
|
-
* full better-auth plugin API surface.
|
|
55
|
-
*
|
|
56
|
-
* The shape of `authApi` is the shape of the better-auth instance's `api` —
|
|
57
|
-
* a flat record of endpoint functions like `createOrganization`, `banUser`,
|
|
58
|
-
* `listMembers`, … contributed by whichever plugins are configured on the auth
|
|
59
|
-
* instance. Because the type is `Auth["api"]` and `LunoraAuth` is generic over
|
|
60
|
-
* the auth instance, callers get end-to-end inference: the endpoints they see
|
|
61
|
-
* are exactly the ones their auth instance loaded.
|
|
62
|
-
*/
|
|
52
|
+
* The Lunora context extension this middleware installs. It does *not* replace
|
|
53
|
+
* `ctx.auth` (the identity-only surface populated by the runtime — `userId` and
|
|
54
|
+
* `getIdentity()`); instead it adds a sibling `ctx.authApi` that points at the
|
|
55
|
+
* full better-auth plugin API surface.
|
|
56
|
+
*
|
|
57
|
+
* The shape of `authApi` is the shape of the better-auth instance's `api` —
|
|
58
|
+
* a flat record of endpoint functions like `createOrganization`, `banUser`,
|
|
59
|
+
* `listMembers`, … contributed by whichever plugins are configured on the auth
|
|
60
|
+
* instance. Because the type is `Auth["api"]` and `LunoraAuth` is generic over
|
|
61
|
+
* the auth instance, callers get end-to-end inference: the endpoints they see
|
|
62
|
+
* are exactly the ones their auth instance loaded.
|
|
63
|
+
*/
|
|
63
64
|
interface LunoraAuthApiContext<Auth extends LunoraAuth> {
|
|
64
65
|
/**
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
66
|
+
* The better-auth endpoint surface — every endpoint contributed by every
|
|
67
|
+
* plugin configured on the auth instance, ready to call directly:
|
|
68
|
+
*
|
|
69
|
+
* ```ts
|
|
70
|
+
* await ctx.authApi.createOrganization({ body: { name: "Acme" }, headers });
|
|
71
|
+
* await ctx.authApi.banUser({ body: { userId: "u_1" }, headers });
|
|
72
|
+
* ```
|
|
73
|
+
*
|
|
74
|
+
* # ⚠️ SECURITY: privileged surface — you MUST pass `headers`
|
|
75
|
+
*
|
|
76
|
+
* This is the **full, privileged** better-auth API (`auth.api`) — it
|
|
77
|
+
* includes admin/management endpoints such as `banUser`, `setRole`,
|
|
78
|
+
* impersonation, `createOrganization`, `removeMember`, … better-auth
|
|
79
|
+
* authorizes these calls from the caller's session in the `headers` you
|
|
80
|
+
* pass. Invoked **without** `headers`, better-auth treats the call as a
|
|
81
|
+
* trusted server-side invocation and **bypasses session authorization
|
|
82
|
+
* entirely** — any procedure that can reach `ctx.authApi` could then ban a
|
|
83
|
+
* user, escalate a role, or read another tenant's data.
|
|
84
|
+
*
|
|
85
|
+
* To stop that bypass at runtime, `withAuthPlugins` installs a guard around
|
|
86
|
+
* `ctx.authApi` by default: a header-less call to any endpoint throws
|
|
87
|
+
* {@link LunoraAuthHeadersError} rather than running with full privileges.
|
|
88
|
+
* The same `auth_api_call_without_headers` advisor lint catches it
|
|
89
|
+
* statically; the guard is the runtime backstop for the cases the lint
|
|
90
|
+
* can't see (dynamic method names, indirected calls). For the rare,
|
|
91
|
+
* deliberate unauthenticated server-to-server call, opt out explicitly with
|
|
92
|
+
* `ctx.authApi.withoutHeaders().<method>(…)`.
|
|
93
|
+
*
|
|
94
|
+
* Lunora's procedure context carries only the resolved identity, not the
|
|
95
|
+
* raw inbound `Headers`, so this middleware CANNOT pre-bind them for you.
|
|
96
|
+
* Therefore: **thread the inbound `Headers` into every `ctx.authApi.*`
|
|
97
|
+
* call** (typically from an HTTP action — see {@link withAuthPlugins}). A
|
|
98
|
+
* header-less call is an authorization bypass, not a convenience.
|
|
99
|
+
*/
|
|
99
100
|
readonly authApi: {
|
|
100
101
|
/**
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
102
|
+
* Explicit, loud escape hatch from the runtime header guard. Returns
|
|
103
|
+
* the raw, **unguarded** `auth.api` surface — every endpoint reached
|
|
104
|
+
* through it runs as a trusted server-to-server call with session
|
|
105
|
+
* authorization skipped.
|
|
106
|
+
*
|
|
107
|
+
* ```ts
|
|
108
|
+
* // A scheduled job with no inbound request that must create the
|
|
109
|
+
* // system org. Audited and intentional:
|
|
110
|
+
* await ctx.authApi.withoutHeaders().createOrganization({ body: { name } });
|
|
111
|
+
* ```
|
|
112
|
+
*
|
|
113
|
+
* Use only for deliberate, audited unauthenticated calls. For ordinary
|
|
114
|
+
* request-driven calls, pass `headers` so authorization is enforced.
|
|
115
|
+
*/
|
|
115
116
|
withoutHeaders: () => Auth["api"];
|
|
116
117
|
} & Auth["api"];
|
|
117
118
|
}
|
|
118
119
|
/**
|
|
119
|
-
* Build a Lunora middleware that mounts a better-auth instance's plugin API
|
|
120
|
-
* onto `ctx.authApi`. Compose it with `.use(...)` once per builder and every
|
|
121
|
-
* downstream handler gets typed access to the plugin endpoints — no more
|
|
122
|
-
* importing the auth instance directly from every query/mutation file.
|
|
123
|
-
*
|
|
124
|
-
* # ⚠️ SECURITY: headers are load-bearing for authorization
|
|
125
|
-
*
|
|
126
|
-
* `ctx.authApi` is the **full privileged** better-auth surface (`auth.api`):
|
|
127
|
-
* `banUser`, `setRole`, impersonation, `createOrganization`, `removeMember`,
|
|
128
|
-
* and so on. better-auth authorizes these from the caller's session carried in
|
|
129
|
-
* the `headers` you pass. **Called without `headers`, better-auth treats the
|
|
130
|
-
* invocation as a trusted server-side call and skips session authorization
|
|
131
|
-
* altogether** — so a header-less `ctx.authApi.banUser(...)` from any procedure
|
|
132
|
-
* runs with full privileges regardless of who the caller is. This is an
|
|
133
|
-
* authorization bypass, not just a missing convenience.
|
|
134
|
-
*
|
|
135
|
-
* To make that bypass fail loudly instead of silently, this middleware wraps
|
|
136
|
-
* `ctx.authApi` in a **runtime header guard by default**: any endpoint called
|
|
137
|
-
* without a `headers` property throws {@link LunoraAuthHeadersError}. The guard
|
|
138
|
-
* mirrors the static `auth_api_call_without_headers` advisor lint exactly, so
|
|
139
|
-
* the two agree on what counts as a header-bearing call.
|
|
140
|
-
*
|
|
141
|
-
* - **Default (safe):** `withAuthPlugins(auth)` — header-less calls throw.
|
|
142
|
-
* - **Per-call opt-out (preferred):** `ctx.authApi.withoutHeaders().banUser(…)`
|
|
143
|
-
* for a deliberate, audited unauthenticated server-to-server call.
|
|
144
|
-
* - **Whole-middleware opt-out (loud):** `withAuthPlugins(auth, { enforceHeaders: false })`
|
|
145
|
-
* disables the guard entirely; only do this once every call site is audited.
|
|
146
|
-
*
|
|
147
|
-
* Lunora's procedure context does not currently carry the raw request headers
|
|
148
|
-
* (only the resolved identity — see `AuthState` in `@lunora/server`), so
|
|
149
|
-
* this middleware **cannot** pre-bind headers for you and does **not** do so.
|
|
150
|
-
* You MUST pass the inbound `Headers` explicitly into **every** `ctx.authApi.*`
|
|
151
|
-
* call, from a transport that has them — typically an HTTP action:
|
|
152
|
-
*
|
|
153
|
-
* ```ts
|
|
154
|
-
* // lunora/orgs.ts
|
|
155
|
-
* import { httpAction } from "@lunora/server";
|
|
156
|
-
* import { withAuthPlugins } from "@lunora/auth/middleware";
|
|
157
|
-
* import { auth } from "./auth.js";
|
|
158
|
-
*
|
|
159
|
-
* export const createOrg = httpAction(async (ctx, request) => {
|
|
160
|
-
* const { name } = await request.json();
|
|
161
|
-
*
|
|
162
|
-
* // ctx.authApi is installed by withAuthPlugins(auth) on the builder.
|
|
163
|
-
* const org = await ctx.authApi.createOrganization({
|
|
164
|
-
* body: { name },
|
|
165
|
-
* headers: request.headers,
|
|
166
|
-
* });
|
|
167
|
-
*
|
|
168
|
-
* return Response.json(org);
|
|
169
|
-
* });
|
|
170
|
-
* ```
|
|
171
|
-
*
|
|
172
|
-
* For internal server-to-server calls where there is no inbound request
|
|
173
|
-
* (e.g. a scheduled job that creates the system org), opt out explicitly with
|
|
174
|
-
* `ctx.authApi.withoutHeaders()` and authenticate with whatever bearer token
|
|
175
|
-
* your auth instance is configured to honour.
|
|
176
|
-
*/
|
|
120
|
+
* Build a Lunora middleware that mounts a better-auth instance's plugin API
|
|
121
|
+
* onto `ctx.authApi`. Compose it with `.use(...)` once per builder and every
|
|
122
|
+
* downstream handler gets typed access to the plugin endpoints — no more
|
|
123
|
+
* importing the auth instance directly from every query/mutation file.
|
|
124
|
+
*
|
|
125
|
+
* # ⚠️ SECURITY: headers are load-bearing for authorization
|
|
126
|
+
*
|
|
127
|
+
* `ctx.authApi` is the **full privileged** better-auth surface (`auth.api`):
|
|
128
|
+
* `banUser`, `setRole`, impersonation, `createOrganization`, `removeMember`,
|
|
129
|
+
* and so on. better-auth authorizes these from the caller's session carried in
|
|
130
|
+
* the `headers` you pass. **Called without `headers`, better-auth treats the
|
|
131
|
+
* invocation as a trusted server-side call and skips session authorization
|
|
132
|
+
* altogether** — so a header-less `ctx.authApi.banUser(...)` from any procedure
|
|
133
|
+
* runs with full privileges regardless of who the caller is. This is an
|
|
134
|
+
* authorization bypass, not just a missing convenience.
|
|
135
|
+
*
|
|
136
|
+
* To make that bypass fail loudly instead of silently, this middleware wraps
|
|
137
|
+
* `ctx.authApi` in a **runtime header guard by default**: any endpoint called
|
|
138
|
+
* without a `headers` property throws {@link LunoraAuthHeadersError}. The guard
|
|
139
|
+
* mirrors the static `auth_api_call_without_headers` advisor lint exactly, so
|
|
140
|
+
* the two agree on what counts as a header-bearing call.
|
|
141
|
+
*
|
|
142
|
+
* - **Default (safe):** `withAuthPlugins(auth)` — header-less calls throw.
|
|
143
|
+
* - **Per-call opt-out (preferred):** `ctx.authApi.withoutHeaders().banUser(…)`
|
|
144
|
+
* for a deliberate, audited unauthenticated server-to-server call.
|
|
145
|
+
* - **Whole-middleware opt-out (loud):** `withAuthPlugins(auth, { enforceHeaders: false })`
|
|
146
|
+
* disables the guard entirely; only do this once every call site is audited.
|
|
147
|
+
*
|
|
148
|
+
* Lunora's procedure context does not currently carry the raw request headers
|
|
149
|
+
* (only the resolved identity — see `AuthState` in `@lunora/server`), so
|
|
150
|
+
* this middleware **cannot** pre-bind headers for you and does **not** do so.
|
|
151
|
+
* You MUST pass the inbound `Headers` explicitly into **every** `ctx.authApi.*`
|
|
152
|
+
* call, from a transport that has them — typically an HTTP action:
|
|
153
|
+
*
|
|
154
|
+
* ```ts
|
|
155
|
+
* // lunora/orgs.ts
|
|
156
|
+
* import { httpAction } from "@lunora/server";
|
|
157
|
+
* import { withAuthPlugins } from "@lunora/auth/middleware";
|
|
158
|
+
* import { auth } from "./auth.js";
|
|
159
|
+
*
|
|
160
|
+
* export const createOrg = httpAction(async (ctx, request) => {
|
|
161
|
+
* const { name } = await request.json();
|
|
162
|
+
*
|
|
163
|
+
* // ctx.authApi is installed by withAuthPlugins(auth) on the builder.
|
|
164
|
+
* const org = await ctx.authApi.createOrganization({
|
|
165
|
+
* body: { name },
|
|
166
|
+
* headers: request.headers,
|
|
167
|
+
* });
|
|
168
|
+
*
|
|
169
|
+
* return Response.json(org);
|
|
170
|
+
* });
|
|
171
|
+
* ```
|
|
172
|
+
*
|
|
173
|
+
* For internal server-to-server calls where there is no inbound request
|
|
174
|
+
* (e.g. a scheduled job that creates the system org), opt out explicitly with
|
|
175
|
+
* `ctx.authApi.withoutHeaders()` and authenticate with whatever bearer token
|
|
176
|
+
* your auth instance is configured to honour.
|
|
177
|
+
*/
|
|
177
178
|
/**
|
|
178
|
-
* Shape of the middleware {@link withAuthPlugins} returns: a callable generic
|
|
179
|
-
* over the incoming ctx so chaining `.use(...)` preserves whatever ctx fields
|
|
180
|
-
* the upstream middleware already installed. Lives as its own interface
|
|
181
|
-
* because TypeScript doesn't allow declaring `const fn: <CtxIn>() => ...` —
|
|
182
|
-
* the generic must live on a callable type alias or interface.
|
|
183
|
-
*/
|
|
179
|
+
* Shape of the middleware {@link withAuthPlugins} returns: a callable generic
|
|
180
|
+
* over the incoming ctx so chaining `.use(...)` preserves whatever ctx fields
|
|
181
|
+
* the upstream middleware already installed. Lives as its own interface
|
|
182
|
+
* because TypeScript doesn't allow declaring `const fn: <CtxIn>() => ...` —
|
|
183
|
+
* the generic must live on a callable type alias or interface.
|
|
184
|
+
*/
|
|
184
185
|
type WithAuthPluginsMiddleware<Auth extends LunoraAuth> = <ContextIn>(options: {
|
|
185
186
|
ctx: ContextIn;
|
|
186
187
|
next: MiddlewareNext<ContextIn>;
|