@lunora/cloudflare-access 1.0.0-alpha.3 → 1.0.0-alpha.30

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 CHANGED
@@ -103,3 +103,129 @@ Unless required by applicable law or agreed to in writing, software distributed
103
103
  under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
104
104
  CONDITIONS OF ANY KIND, either express or implied. See the License for the
105
105
  specific language governing permissions and limitations under the License.
106
+
107
+ <!-- DEPENDENCIES -->
108
+ <!-- /DEPENDENCIES -->
109
+
110
+ <!-- TYPE_DEPENDENCIES -->
111
+
112
+ # Licenses of bundled types
113
+ The published @lunora/cloudflare-access artifact additionally contains code with the following licenses:
114
+ FSL-1.1-Apache-2.0
115
+
116
+ # Bundled types:
117
+ ## @lunora/server
118
+ License: FSL-1.1-Apache-2.0
119
+ By: Daniel Bannert
120
+ Repository: git+https://github.com/anolilab/lunora.git
121
+
122
+ > # Functional Source License, Version 1.1, Apache 2.0 Future License
123
+ >
124
+ > ## Abbreviation
125
+ >
126
+ > FSL-1.1-Apache-2.0
127
+ >
128
+ > ## Notice
129
+ >
130
+ > Copyright 2026 anolilab and contributors
131
+ >
132
+ > ## Terms and Conditions
133
+ >
134
+ > ### Licensor ("We")
135
+ >
136
+ > The party offering the Software under these Terms and Conditions.
137
+ >
138
+ > ### The Software
139
+ >
140
+ > The "Software" is each version of the software that we make available under
141
+ > these Terms and Conditions, as indicated by our inclusion of these Terms and
142
+ > Conditions with the Software.
143
+ >
144
+ > ### License Grant
145
+ >
146
+ > Subject to your compliance with this License Grant and the Patents,
147
+ > Redistribution and Trademark clauses below, we hereby grant you the right to
148
+ > use, copy, modify, create derivative works, publicly perform, publicly display
149
+ > and redistribute the Software for any Permitted Purpose identified below.
150
+ >
151
+ > ### Permitted Purpose
152
+ >
153
+ > A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
154
+ > means making the Software available to others in a commercial product or service
155
+ > that:
156
+ >
157
+ > 1. substitutes for the Software;
158
+ >
159
+ > 2. substitutes for any other product or service we offer using the Software that
160
+ > exists as of the date we make the Software available; or
161
+ >
162
+ > 3. offers the same or substantially similar functionality as the Software.
163
+ >
164
+ > Permitted Purposes specifically include using the Software:
165
+ >
166
+ > 1. for your internal use and access;
167
+ >
168
+ > 2. for non-commercial education;
169
+ >
170
+ > 3. for non-commercial research; and
171
+ >
172
+ > 4. in connection with professional services that you provide to a licensee using
173
+ > the Software in accordance with these Terms and Conditions.
174
+ >
175
+ > ### Patents
176
+ >
177
+ > To the extent your use for a Permitted Purpose would necessarily infringe our
178
+ > patents, the license grant above includes a license under our patents. If you
179
+ > make a claim against any party that the Software infringes or contributes to the
180
+ > infringement of any patent, then your patent license to the Software ends
181
+ > immediately.
182
+ >
183
+ > ### Redistribution
184
+ >
185
+ > The Terms and Conditions apply to all copies, modifications and derivatives of
186
+ > the Software.
187
+ >
188
+ > If you redistribute any copies, modifications or derivatives of the Software,
189
+ > you must include a copy of or a link to these Terms and Conditions and not
190
+ > remove any copyright notices provided in or with the Software.
191
+ >
192
+ > ### Disclaimer
193
+ >
194
+ > THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
195
+ > IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
196
+ > PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
197
+ >
198
+ > IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
199
+ > SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, EVEN
200
+ > IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
201
+ >
202
+ > ### Trademarks
203
+ >
204
+ > Except for displaying the License Details and identifying us as the origin of
205
+ > the Software, you have no right under these Terms and Conditions to use our
206
+ > trademarks, trade names, service marks or product names.
207
+ >
208
+ > ## Grant of Future License
209
+ >
210
+ > We hereby irrevocably grant you an additional license to use the Software under
211
+ > the Apache License, Version 2.0 that is effective on the second anniversary of
212
+ > the date we make the Software available. On or after that date, you may use the
213
+ > Software under the Apache License, Version 2.0, in which case the following will
214
+ > apply:
215
+ >
216
+ > Licensed under the Apache License, Version 2.0 (the "License"); you may not use
217
+ > this file except in compliance with the License.
218
+ >
219
+ > You may obtain a copy of the License at
220
+ >
221
+ > http://www.apache.org/licenses/LICENSE-2.0
222
+ >
223
+ > Unless required by applicable law or agreed to in writing, software distributed
224
+ > under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
225
+ > CONDITIONS OF ANY KIND, either express or implied. See the License for the
226
+ > specific language governing permissions and limitations under the License.
227
+ >
228
+ > <!-- DEPENDENCIES -->
229
+ > <!-- /DEPENDENCIES -->
230
+
231
+ <!-- /TYPE_DEPENDENCIES -->
package/dist/admin.d.mts CHANGED
@@ -1,36 +1,36 @@
1
- import { A as AccessClaims, R as RequestVerifyOptions } from "./packem_shared/types.d-BO8d74KI.mjs";
1
+ import { A as AccessClaims, R as RequestVerifyOptions } from "./packem_shared/types.d-C8c7Qwx1.mjs";
2
2
  import 'jose';
3
3
  /** Options for {@link accessAdminGate}; extends {@link RequestVerifyOptions}. */
4
4
  interface AccessAdminGateOptions extends RequestVerifyOptions {
5
5
  /**
6
- * Decide whether the **verified** claims authorize the Studio/admin plane —
7
- * e.g. `(claims) => claims.groups?.includes("ops") ?? false` or an email-domain
8
- * check. Required: there is no implicit grant, so a verified-but-unprivileged
9
- * identity is denied. Runs only after signature/issuer/audience/expiry pass.
10
- */
6
+ * Decide whether the **verified** claims authorize the Studio/admin plane —
7
+ * e.g. `(claims) => claims.groups?.includes("ops") ?? false` or an email-domain
8
+ * check. Required: there is no implicit grant, so a verified-but-unprivileged
9
+ * identity is denied. Runs only after signature/issuer/audience/expiry pass.
10
+ */
11
11
  isAdmin: (claims: AccessClaims) => boolean | Promise<boolean>;
12
12
  }
13
13
  /**
14
- * Build an admin gate for `@lunora/runtime`'s `WorkerOptions.adminGate`: a
15
- * request-only predicate that verifies the request's `Cf-Access-Jwt-Assertion`
16
- * JWT and applies your `isAdmin(claims)` test. When it resolves `true` the
17
- * request authorizes the `/_lunora/admin/*` plane (the Studio's HTTP + WS
18
- * endpoints) in addition to — or instead of — the static admin bearer, so the
19
- * Studio can sit behind Cloudflare Access.
20
- *
21
- * It is **fail-closed**: a missing token, a token that fails verification, or an
22
- * `isAdmin` that returns `false` all resolve to `false` (the bearer remains the
23
- * only other path). Verification needs no `env` binding (static team-domain/aud
24
- * config + the remote JWKS over `fetch`), so the gate takes only the request and
25
- * the runtime can evaluate it without threading async through every admin route.
26
- *
27
- * ```ts
28
- * options.adminGate = accessAdminGate({
29
- * teamDomain: env.CF_ACCESS_TEAM_DOMAIN,
30
- * aud: env.CF_ACCESS_ADMIN_AUD,
31
- * isAdmin: (claims) => claims.groups?.includes("lunora-admins") ?? false,
32
- * });
33
- * ```
34
- */
14
+ * Build an admin gate for `@lunora/runtime`'s `WorkerOptions.adminGate`: a
15
+ * request-only predicate that verifies the request's `Cf-Access-Jwt-Assertion`
16
+ * JWT and applies your `isAdmin(claims)` test. When it resolves `true` the
17
+ * request authorizes the `/_lunora/admin/*` plane (the Studio's HTTP + WS
18
+ * endpoints) in addition to — or instead of — the static admin bearer, so the
19
+ * Studio can sit behind Cloudflare Access.
20
+ *
21
+ * It is **fail-closed**: a missing token, a token that fails verification, or an
22
+ * `isAdmin` that returns `false` all resolve to `false` (the bearer remains the
23
+ * only other path). Verification needs no `env` binding (static team-domain/aud
24
+ * config + the remote JWKS over `fetch`), so the gate takes only the request and
25
+ * the runtime can evaluate it without threading async through every admin route.
26
+ *
27
+ * ```ts
28
+ * options.adminGate = accessAdminGate({
29
+ * teamDomain: env.CF_ACCESS_TEAM_DOMAIN,
30
+ * aud: env.CF_ACCESS_ADMIN_AUD,
31
+ * isAdmin: (claims) => claims.groups?.includes("lunora-admins") ?? false,
32
+ * });
33
+ * ```
34
+ */
35
35
  declare const accessAdminGate: (options: AccessAdminGateOptions) => ((request: Request) => Promise<boolean>);
36
36
  export { type AccessAdminGateOptions, accessAdminGate };
package/dist/admin.d.ts CHANGED
@@ -1,36 +1,36 @@
1
- import { A as AccessClaims, R as RequestVerifyOptions } from "./packem_shared/types.d-BO8d74KI.js";
1
+ import { A as AccessClaims, R as RequestVerifyOptions } from "./packem_shared/types.d-C8c7Qwx1.js";
2
2
  import 'jose';
3
3
  /** Options for {@link accessAdminGate}; extends {@link RequestVerifyOptions}. */
4
4
  interface AccessAdminGateOptions extends RequestVerifyOptions {
5
5
  /**
6
- * Decide whether the **verified** claims authorize the Studio/admin plane —
7
- * e.g. `(claims) => claims.groups?.includes("ops") ?? false` or an email-domain
8
- * check. Required: there is no implicit grant, so a verified-but-unprivileged
9
- * identity is denied. Runs only after signature/issuer/audience/expiry pass.
10
- */
6
+ * Decide whether the **verified** claims authorize the Studio/admin plane —
7
+ * e.g. `(claims) => claims.groups?.includes("ops") ?? false` or an email-domain
8
+ * check. Required: there is no implicit grant, so a verified-but-unprivileged
9
+ * identity is denied. Runs only after signature/issuer/audience/expiry pass.
10
+ */
11
11
  isAdmin: (claims: AccessClaims) => boolean | Promise<boolean>;
12
12
  }
13
13
  /**
14
- * Build an admin gate for `@lunora/runtime`'s `WorkerOptions.adminGate`: a
15
- * request-only predicate that verifies the request's `Cf-Access-Jwt-Assertion`
16
- * JWT and applies your `isAdmin(claims)` test. When it resolves `true` the
17
- * request authorizes the `/_lunora/admin/*` plane (the Studio's HTTP + WS
18
- * endpoints) in addition to — or instead of — the static admin bearer, so the
19
- * Studio can sit behind Cloudflare Access.
20
- *
21
- * It is **fail-closed**: a missing token, a token that fails verification, or an
22
- * `isAdmin` that returns `false` all resolve to `false` (the bearer remains the
23
- * only other path). Verification needs no `env` binding (static team-domain/aud
24
- * config + the remote JWKS over `fetch`), so the gate takes only the request and
25
- * the runtime can evaluate it without threading async through every admin route.
26
- *
27
- * ```ts
28
- * options.adminGate = accessAdminGate({
29
- * teamDomain: env.CF_ACCESS_TEAM_DOMAIN,
30
- * aud: env.CF_ACCESS_ADMIN_AUD,
31
- * isAdmin: (claims) => claims.groups?.includes("lunora-admins") ?? false,
32
- * });
33
- * ```
34
- */
14
+ * Build an admin gate for `@lunora/runtime`'s `WorkerOptions.adminGate`: a
15
+ * request-only predicate that verifies the request's `Cf-Access-Jwt-Assertion`
16
+ * JWT and applies your `isAdmin(claims)` test. When it resolves `true` the
17
+ * request authorizes the `/_lunora/admin/*` plane (the Studio's HTTP + WS
18
+ * endpoints) in addition to — or instead of — the static admin bearer, so the
19
+ * Studio can sit behind Cloudflare Access.
20
+ *
21
+ * It is **fail-closed**: a missing token, a token that fails verification, or an
22
+ * `isAdmin` that returns `false` all resolve to `false` (the bearer remains the
23
+ * only other path). Verification needs no `env` binding (static team-domain/aud
24
+ * config + the remote JWKS over `fetch`), so the gate takes only the request and
25
+ * the runtime can evaluate it without threading async through every admin route.
26
+ *
27
+ * ```ts
28
+ * options.adminGate = accessAdminGate({
29
+ * teamDomain: env.CF_ACCESS_TEAM_DOMAIN,
30
+ * aud: env.CF_ACCESS_ADMIN_AUD,
31
+ * isAdmin: (claims) => claims.groups?.includes("lunora-admins") ?? false,
32
+ * });
33
+ * ```
34
+ */
35
35
  declare const accessAdminGate: (options: AccessAdminGateOptions) => ((request: Request) => Promise<boolean>);
36
36
  export { type AccessAdminGateOptions, accessAdminGate };
package/dist/admin.mjs CHANGED
@@ -1,8 +1 @@
1
- import { verifyRequest } from './packem_shared/accessIssuer-DhKaNoyU.mjs';
2
-
3
- const accessAdminGate = (options) => async (request) => {
4
- const claims = await verifyRequest(request, options);
5
- return claims === void 0 ? false : options.isAdmin(claims);
6
- };
7
-
8
- export { accessAdminGate };
1
+ import{assertVerifyOptions as i,verifyRequest as e}from"./packem_shared/accessIssuer-BdF2LQuC.mjs";const r=s=>(i(s),async t=>{const a=await e(t,s);return a===void 0?!1:s.isAdmin(a)});export{r as accessAdminGate};
@@ -1,13 +1,14 @@
1
- import { M as Middleware } from "./packem_shared/index.d-C7lOF4ZA.mjs";
2
- import { A as AccessClaims } from "./packem_shared/types.d-BO8d74KI.mjs";
1
+ import { M as Middleware } from "./packem_shared/index.d-5O8LZ3gE.mjs";
2
+ import { A as AccessClaims } from "./packem_shared/types.d-C8c7Qwx1.mjs";
3
+ import '@lunora/errors';
3
4
  import 'jose';
4
5
  /**
5
- * The slice of context {@link accessContext} reads: the `auth` facade every
6
- * Lunora ctx carries. `getIdentity()` returns the verified identity envelope —
7
- * `createAccessResolver`'s {@link import("./types").ResolvedAccessIdentity}
8
- * output, which carries the full claim set under `access` plus the promoted
9
- * `email` / `groups` / `commonName` fields.
10
- */
6
+ * The slice of context {@link accessContext} reads: the `auth` facade every
7
+ * Lunora ctx carries. `getIdentity()` returns the verified identity envelope —
8
+ * `createAccessResolver`'s {@link import("./types").ResolvedAccessIdentity}
9
+ * output, which carries the full claim set under `access` plus the promoted
10
+ * `email` / `groups` / `commonName` fields.
11
+ */
11
12
  interface AccessContextInput {
12
13
  auth?: {
13
14
  getIdentity?: () => (Record<string, unknown> | null) | Promise<Record<string, unknown> | null>;
@@ -15,11 +16,11 @@ interface AccessContextInput {
15
16
  };
16
17
  }
17
18
  /**
18
- * The typed, per-request `ctx.access` facade {@link accessContext} attaches. A
19
- * synchronous, Access-shaped read over the already-resolved identity — so a
20
- * handler reads `ctx.access.email` / `ctx.access.hasGroup("ops")` without an
21
- * `await` or a cast off the generic `ctx.auth.getIdentity()` envelope.
22
- */
19
+ * The typed, per-request `ctx.access` facade {@link accessContext} attaches. A
20
+ * synchronous, Access-shaped read over the already-resolved identity — so a
21
+ * handler reads `ctx.access.email` / `ctx.access.hasGroup("ops")` without an
22
+ * `await` or a cast off the generic `ctx.auth.getIdentity()` envelope.
23
+ */
23
24
  interface AccessFacade {
24
25
  /** True when a verified Access identity is present on the request. */
25
26
  readonly authenticated: boolean;
@@ -41,39 +42,39 @@ interface AccessContextOutput extends AccessContextInput {
41
42
  access: AccessFacade;
42
43
  }
43
44
  /**
44
- * Build the `ctx.access` facade from a (possibly absent) resolved identity
45
- * envelope. Returns the anonymous facade when no identity is present, so callers
46
- * never null-check. Shared by {@link accessContext} and the codegen-wired global
47
- * `ctx.access` (which calls this synchronously from the resolved identity locals
48
- * at ctx-build time, so a global `ctx.access` adds only this object construction
49
- * per request — no extra I/O or re-verification).
50
- */
45
+ * Build the `ctx.access` facade from a (possibly absent) resolved identity
46
+ * envelope. Returns the anonymous facade when no identity is present, so callers
47
+ * never null-check. Shared by {@link accessContext} and the codegen-wired global
48
+ * `ctx.access` (which calls this synchronously from the resolved identity locals
49
+ * at ctx-build time, so a global `ctx.access` adds only this object construction
50
+ * per request — no extra I/O or re-verification).
51
+ */
51
52
  declare const accessFacade: (identity: Record<string, unknown> | null | undefined, userId: string | null | undefined) => AccessFacade;
52
53
  /**
53
- * Middleware that attaches a typed `ctx.access` facade derived from the verified
54
- * Cloudflare Access identity. It resolves `ctx.auth.getIdentity()` once and
55
- * exposes a **synchronous**, Access-shaped read — `ctx.access.email`,
56
- * `ctx.access.groups`, `ctx.access.hasGroup("ops")`, `ctx.access.claims` — so a
57
- * handler reads the verified identity ergonomically and with full typing instead
58
- * of casting off the generic `getIdentity()` envelope.
59
- *
60
- * When no identity is resolved (anonymous request) it attaches the anonymous
61
- * facade — `authenticated: false`, empty `groups`, `hasGroup` always `false` —
62
- * so reads stay safe without a null check, and authorization decisions still
63
- * fail closed.
64
- *
65
- * It does not gate the request; pair it with `rls(...)` (or
66
- * `accessRoles(...)` → `rls(...)`) when you need enforcement. It only surfaces
67
- * the identity for branching inside a handler.
68
- *
69
- * ```ts
70
- * export const whoAmI = query
71
- * .use(accessContext())
72
- * .query(async ({ ctx }) => ({
73
- * email: ctx.access.email,
74
- * isOps: ctx.access.hasGroup("ops"),
75
- * }));
76
- * ```
77
- */
54
+ * Middleware that attaches a typed `ctx.access` facade derived from the verified
55
+ * Cloudflare Access identity. It resolves `ctx.auth.getIdentity()` once and
56
+ * exposes a **synchronous**, Access-shaped read — `ctx.access.email`,
57
+ * `ctx.access.groups`, `ctx.access.hasGroup("ops")`, `ctx.access.claims` — so a
58
+ * handler reads the verified identity ergonomically and with full typing instead
59
+ * of casting off the generic `getIdentity()` envelope.
60
+ *
61
+ * When no identity is resolved (anonymous request) it attaches the anonymous
62
+ * facade — `authenticated: false`, empty `groups`, `hasGroup` always `false` —
63
+ * so reads stay safe without a null check, and authorization decisions still
64
+ * fail closed.
65
+ *
66
+ * It does not gate the request; pair it with `rls(...)` (or
67
+ * `accessRoles(...)` → `rls(...)`) when you need enforcement. It only surfaces
68
+ * the identity for branching inside a handler.
69
+ *
70
+ * ```ts
71
+ * export const whoAmI = query
72
+ * .use(accessContext())
73
+ * .query(async ({ ctx }) => ({
74
+ * email: ctx.access.email,
75
+ * isOps: ctx.access.hasGroup("ops"),
76
+ * }));
77
+ * ```
78
+ */
78
79
  declare const accessContext: <Context extends AccessContextInput>() => Middleware<Context, AccessContextOutput & Context>;
79
80
  export { type AccessContextInput, type AccessContextOutput, type AccessFacade, accessContext, accessFacade };
package/dist/context.d.ts CHANGED
@@ -1,13 +1,14 @@
1
- import { M as Middleware } from "./packem_shared/index.d-C7lOF4ZA.js";
2
- import { A as AccessClaims } from "./packem_shared/types.d-BO8d74KI.js";
1
+ import { M as Middleware } from "./packem_shared/index.d-5O8LZ3gE.js";
2
+ import { A as AccessClaims } from "./packem_shared/types.d-C8c7Qwx1.js";
3
+ import '@lunora/errors';
3
4
  import 'jose';
4
5
  /**
5
- * The slice of context {@link accessContext} reads: the `auth` facade every
6
- * Lunora ctx carries. `getIdentity()` returns the verified identity envelope —
7
- * `createAccessResolver`'s {@link import("./types").ResolvedAccessIdentity}
8
- * output, which carries the full claim set under `access` plus the promoted
9
- * `email` / `groups` / `commonName` fields.
10
- */
6
+ * The slice of context {@link accessContext} reads: the `auth` facade every
7
+ * Lunora ctx carries. `getIdentity()` returns the verified identity envelope —
8
+ * `createAccessResolver`'s {@link import("./types").ResolvedAccessIdentity}
9
+ * output, which carries the full claim set under `access` plus the promoted
10
+ * `email` / `groups` / `commonName` fields.
11
+ */
11
12
  interface AccessContextInput {
12
13
  auth?: {
13
14
  getIdentity?: () => (Record<string, unknown> | null) | Promise<Record<string, unknown> | null>;
@@ -15,11 +16,11 @@ interface AccessContextInput {
15
16
  };
16
17
  }
17
18
  /**
18
- * The typed, per-request `ctx.access` facade {@link accessContext} attaches. A
19
- * synchronous, Access-shaped read over the already-resolved identity — so a
20
- * handler reads `ctx.access.email` / `ctx.access.hasGroup("ops")` without an
21
- * `await` or a cast off the generic `ctx.auth.getIdentity()` envelope.
22
- */
19
+ * The typed, per-request `ctx.access` facade {@link accessContext} attaches. A
20
+ * synchronous, Access-shaped read over the already-resolved identity — so a
21
+ * handler reads `ctx.access.email` / `ctx.access.hasGroup("ops")` without an
22
+ * `await` or a cast off the generic `ctx.auth.getIdentity()` envelope.
23
+ */
23
24
  interface AccessFacade {
24
25
  /** True when a verified Access identity is present on the request. */
25
26
  readonly authenticated: boolean;
@@ -41,39 +42,39 @@ interface AccessContextOutput extends AccessContextInput {
41
42
  access: AccessFacade;
42
43
  }
43
44
  /**
44
- * Build the `ctx.access` facade from a (possibly absent) resolved identity
45
- * envelope. Returns the anonymous facade when no identity is present, so callers
46
- * never null-check. Shared by {@link accessContext} and the codegen-wired global
47
- * `ctx.access` (which calls this synchronously from the resolved identity locals
48
- * at ctx-build time, so a global `ctx.access` adds only this object construction
49
- * per request — no extra I/O or re-verification).
50
- */
45
+ * Build the `ctx.access` facade from a (possibly absent) resolved identity
46
+ * envelope. Returns the anonymous facade when no identity is present, so callers
47
+ * never null-check. Shared by {@link accessContext} and the codegen-wired global
48
+ * `ctx.access` (which calls this synchronously from the resolved identity locals
49
+ * at ctx-build time, so a global `ctx.access` adds only this object construction
50
+ * per request — no extra I/O or re-verification).
51
+ */
51
52
  declare const accessFacade: (identity: Record<string, unknown> | null | undefined, userId: string | null | undefined) => AccessFacade;
52
53
  /**
53
- * Middleware that attaches a typed `ctx.access` facade derived from the verified
54
- * Cloudflare Access identity. It resolves `ctx.auth.getIdentity()` once and
55
- * exposes a **synchronous**, Access-shaped read — `ctx.access.email`,
56
- * `ctx.access.groups`, `ctx.access.hasGroup("ops")`, `ctx.access.claims` — so a
57
- * handler reads the verified identity ergonomically and with full typing instead
58
- * of casting off the generic `getIdentity()` envelope.
59
- *
60
- * When no identity is resolved (anonymous request) it attaches the anonymous
61
- * facade — `authenticated: false`, empty `groups`, `hasGroup` always `false` —
62
- * so reads stay safe without a null check, and authorization decisions still
63
- * fail closed.
64
- *
65
- * It does not gate the request; pair it with `rls(...)` (or
66
- * `accessRoles(...)` → `rls(...)`) when you need enforcement. It only surfaces
67
- * the identity for branching inside a handler.
68
- *
69
- * ```ts
70
- * export const whoAmI = query
71
- * .use(accessContext())
72
- * .query(async ({ ctx }) => ({
73
- * email: ctx.access.email,
74
- * isOps: ctx.access.hasGroup("ops"),
75
- * }));
76
- * ```
77
- */
54
+ * Middleware that attaches a typed `ctx.access` facade derived from the verified
55
+ * Cloudflare Access identity. It resolves `ctx.auth.getIdentity()` once and
56
+ * exposes a **synchronous**, Access-shaped read — `ctx.access.email`,
57
+ * `ctx.access.groups`, `ctx.access.hasGroup("ops")`, `ctx.access.claims` — so a
58
+ * handler reads the verified identity ergonomically and with full typing instead
59
+ * of casting off the generic `getIdentity()` envelope.
60
+ *
61
+ * When no identity is resolved (anonymous request) it attaches the anonymous
62
+ * facade — `authenticated: false`, empty `groups`, `hasGroup` always `false` —
63
+ * so reads stay safe without a null check, and authorization decisions still
64
+ * fail closed.
65
+ *
66
+ * It does not gate the request; pair it with `rls(...)` (or
67
+ * `accessRoles(...)` → `rls(...)`) when you need enforcement. It only surfaces
68
+ * the identity for branching inside a handler.
69
+ *
70
+ * ```ts
71
+ * export const whoAmI = query
72
+ * .use(accessContext())
73
+ * .query(async ({ ctx }) => ({
74
+ * email: ctx.access.email,
75
+ * isOps: ctx.access.hasGroup("ops"),
76
+ * }));
77
+ * ```
78
+ */
78
79
  declare const accessContext: <Context extends AccessContextInput>() => Middleware<Context, AccessContextOutput & Context>;
79
80
  export { type AccessContextInput, type AccessContextOutput, type AccessFacade, accessContext, accessFacade };
package/dist/context.mjs CHANGED
@@ -1,36 +1 @@
1
- const stringList = (value) => Array.isArray(value) ? value.filter((entry) => typeof entry === "string") : [];
2
- const stringClaim = (value) => typeof value === "string" ? value : void 0;
3
- const ANONYMOUS_FACADE = {
4
- authenticated: false,
5
- claims: void 0,
6
- commonName: void 0,
7
- email: void 0,
8
- groups: [],
9
- hasGroup: () => false,
10
- userId: void 0
11
- };
12
- const facadeFor = (identity, userId) => {
13
- const raw = identity["access"];
14
- if (typeof raw !== "object" || raw === null) {
15
- return ANONYMOUS_FACADE;
16
- }
17
- const claims = raw;
18
- const groups = stringList(identity["groups"] ?? claims.groups);
19
- return {
20
- authenticated: true,
21
- claims,
22
- commonName: stringClaim(identity["commonName"]) ?? stringClaim(claims.common_name),
23
- email: stringClaim(identity["email"]) ?? stringClaim(claims.email),
24
- groups,
25
- hasGroup: (group) => groups.includes(group),
26
- userId
27
- };
28
- };
29
- const accessFacade = (identity, userId) => identity ? facadeFor(identity, userId ?? void 0) : ANONYMOUS_FACADE;
30
- const accessContext = () => async ({ ctx, next }) => {
31
- const identity = await ctx.auth?.getIdentity?.();
32
- const access = accessFacade(identity, ctx.auth?.userId);
33
- return next({ ctx: { access } });
34
- };
35
-
36
- export { accessContext, accessFacade };
1
+ import{t as m}from"./packem_shared/identity-groups-lzvxGu3k.mjs";const e=t=>typeof t=="string"?t:void 0,i={authenticated:!1,claims:void 0,commonName:void 0,email:void 0,groups:[],hasGroup:()=>!1,userId:void 0},r=(t,a)=>{const o=t.access;if(typeof o!="object"||o===null)return i;const s=o,c=m(t)??[];return{authenticated:!0,claims:s,commonName:e(t.commonName)??e(s.common_name),email:e(t.email)??e(s.email),groups:c,hasGroup:n=>c.includes(n),userId:a}},u=(t,a)=>t?r(t,a??void 0):i,l=()=>async({ctx:t,next:a})=>{const o=await t.auth?.getIdentity?.(),s=u(o,t.auth?.userId);return a({ctx:{access:s}})};export{l as accessContext,u as accessFacade};