@lunora/cloudflare-access 1.0.0-alpha.13 → 1.0.0-alpha.130
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 +214 -0
- package/dist/admin.d.mts +64 -30
- package/dist/admin.d.ts +64 -30
- package/dist/admin.mjs +1 -8
- package/dist/context.d.mts +64 -46
- package/dist/context.d.ts +64 -46
- package/dist/context.mjs +1 -36
- package/dist/index.d.mts +67 -62
- package/dist/index.d.ts +67 -62
- package/dist/index.mjs +1 -2
- package/dist/packem_shared/accessIssuer-CLO6p5MF.mjs +1 -0
- package/dist/packem_shared/composeResolvers-DQFDgtOL.mjs +1 -0
- package/dist/packem_shared/identity-groups-BnaBEyE_.mjs +1 -0
- package/dist/packem_shared/platform-identity-Dy6G1EoF.mjs +1 -0
- package/dist/packem_shared/types.d-Dp399TMk.d.mts +280 -0
- package/dist/packem_shared/types.d-Dp399TMk.d.ts +280 -0
- package/package.json +4 -8
- package/dist/packem_shared/accessIssuer-83KYwaCp.mjs +0 -93
- package/dist/packem_shared/composeResolvers-BI3envaN.mjs +0 -38
- package/dist/packem_shared/index.d-ByAnpUzP.d.mts +0 -23
- package/dist/packem_shared/index.d-ByAnpUzP.d.ts +0 -23
- package/dist/packem_shared/types.d-BO8d74KI.d.mts +0 -134
- package/dist/packem_shared/types.d-BO8d74KI.d.ts +0 -134
- package/dist/roles.d.mts +0 -53
- package/dist/roles.d.ts +0 -53
- package/dist/roles.mjs +0 -35
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { JWTPayload, JWTVerifyGetKey, KeyObject } from 'jose';
|
|
2
|
+
/**
|
|
3
|
+
* The subset of the Cloudflare `ExecutionContext` the Lunora worker entry and
|
|
4
|
+
* the framework mount seams rely on — `waitUntil` for fire-and-forget work that
|
|
5
|
+
* must outlive the response, and `passThroughOnException` for the top-level
|
|
6
|
+
* error posture.
|
|
7
|
+
*
|
|
8
|
+
* It is deliberately **not** a package. `@lunora/runtime` is the leaf server
|
|
9
|
+
* runtime and `@lunora/nuxt` is a framework integration that intentionally does
|
|
10
|
+
* not depend on `@lunora/runtime`'s worker types, yet both need this exact
|
|
11
|
+
* shape: the runtime to build/forward the worker `fetch`, Nuxt to forward an
|
|
12
|
+
* inbound request to the user's composed worker. Each imports this file by
|
|
13
|
+
* relative path and the bundler (packem/rollup) inlines it: no runtime
|
|
14
|
+
* dependency edge is created, the helper is duplicated only in emitted output,
|
|
15
|
+
* never in source. One source of truth, zero deps. See AGENTS.md → "Top-level
|
|
16
|
+
* `shared/` — bundler-inlined source".
|
|
17
|
+
*
|
|
18
|
+
* Both methods are **optional**: a real Cloudflare `ExecutionContext` always
|
|
19
|
+
* supplies them, but a host that mounts Lunora as a sub-handler (Nitro/H3, a
|
|
20
|
+
* non-Cloudflare preview, a unit test) may hand over a partial context or none
|
|
21
|
+
* at all. Callers therefore invoke them defensively (`ctx.waitUntil?.(…)`) or
|
|
22
|
+
* fall back to {@link NOOP_EXECUTION_CONTEXT}.
|
|
23
|
+
*/
|
|
24
|
+
interface ExecutionContextLike {
|
|
25
|
+
/**
|
|
26
|
+
* Present only when Cloudflare Access authenticated the request against a
|
|
27
|
+
* policy attached to the **Worker** (rather than to a hostname). `undefined`
|
|
28
|
+
* on every unauthenticated request, so its presence is itself the "Access
|
|
29
|
+
* authorized this caller" signal — see {@link AccessContextLike}.
|
|
30
|
+
*/
|
|
31
|
+
access?: AccessContextLike;
|
|
32
|
+
cache?: {
|
|
33
|
+
purge: (options: {
|
|
34
|
+
purgeEverything?: boolean;
|
|
35
|
+
tags?: string[];
|
|
36
|
+
}) => Promise<unknown>;
|
|
37
|
+
};
|
|
38
|
+
passThroughOnException?: () => void;
|
|
39
|
+
waitUntil?: (promise: Promise<unknown>) => void;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The identity Cloudflare Access attaches to a Worker-protected request.
|
|
43
|
+
*
|
|
44
|
+
* Shape follows the Access application-token payload: `sub` is the stable per-user
|
|
45
|
+
* id, `email` the verified address, `common_name` the service-token name (machine
|
|
46
|
+
* callers, whose `sub` is empty), and `exp` the credential expiry in epoch
|
|
47
|
+
* **seconds**. Group membership is whatever the Access policy emits — a list of
|
|
48
|
+
* names, or of `{ id, name }` objects — hence `unknown`; normalize before use.
|
|
49
|
+
*
|
|
50
|
+
* Cloudflare may add further fields, so the index signature keeps them rather
|
|
51
|
+
* than dropping them: this is a view of a payload we do not own.
|
|
52
|
+
*/
|
|
53
|
+
interface AccessIdentityLike {
|
|
54
|
+
[claim: string]: unknown;
|
|
55
|
+
/** Service-token name. Present for non-interactive (machine) callers instead of `email`. */
|
|
56
|
+
common_name?: string;
|
|
57
|
+
/** Verified user email. Present for interactive (SSO) callers. */
|
|
58
|
+
email?: string;
|
|
59
|
+
/** Credential expiry, epoch **seconds**. */
|
|
60
|
+
exp?: number;
|
|
61
|
+
/** IdP group membership — names or `{ id, name }` objects, depending on the policy. */
|
|
62
|
+
groups?: unknown;
|
|
63
|
+
/** Display name from the identity provider, when it emits one. */
|
|
64
|
+
name?: string;
|
|
65
|
+
/** Stable per-user id, and what consumers key a user on. Empty for service tokens. */
|
|
66
|
+
sub?: string;
|
|
67
|
+
/** Cloudflare's per-user UUID. Carried through, but deliberately not used as an id — only this path emits it, so keying on it would not match the JWT path. */
|
|
68
|
+
user_uuid?: string;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* The `ctx.access` facade Cloudflare exposes on a Worker protected by Access.
|
|
72
|
+
*
|
|
73
|
+
* Reading the identity from here is preferable to verifying the
|
|
74
|
+
* `Cf-Access-Jwt-Assertion` header: the platform has already authenticated the
|
|
75
|
+
* caller, so there is no JWKS fetch, no audience check to get wrong, and nothing
|
|
76
|
+
* a request can forge — the field simply does not exist unless Access authorized
|
|
77
|
+
* the call. The header path remains the fallback for hostname-scoped Access
|
|
78
|
+
* applications, which do not populate this.
|
|
79
|
+
*/
|
|
80
|
+
interface AccessContextLike {
|
|
81
|
+
getIdentity: () => AccessIdentityLike | null | undefined | Promise<AccessIdentityLike | null | undefined>;
|
|
82
|
+
}
|
|
83
|
+
/** A group→role(s) lookup table, or a function returning the role(s) for one group. */
|
|
84
|
+
type AccessRoleMap = ((group: string) => string | string[] | undefined) | Record<string, string | string[]>;
|
|
85
|
+
/**
|
|
86
|
+
* The verified claims of a Cloudflare Access caller — from the
|
|
87
|
+
* `Cf-Access-Jwt-Assertion` JWT (hostname-scoped Access applications) or from the
|
|
88
|
+
* platform-supplied `ctx.access.getIdentity()` (Access policies attached to the
|
|
89
|
+
* Worker). Both paths produce this one shape, so nothing downstream branches on
|
|
90
|
+
* how the caller was authenticated.
|
|
91
|
+
*
|
|
92
|
+
* Extends the standard `JWTPayload` (`iss`/`aud`/`sub`/`exp`/`iat`/…) with the
|
|
93
|
+
* Access-specific fields. Which optional fields are present depends on the
|
|
94
|
+
* caller and the Access application config. SSO users carry `email` (and
|
|
95
|
+
* `groups` when the policy emits them), with `sub` as the stable user id.
|
|
96
|
+
* Service tokens carry `common_name` and an empty `sub`; there is no `email`.
|
|
97
|
+
*
|
|
98
|
+
* Cloudflare may add further custom claims — they pass through verbatim via the
|
|
99
|
+
* index signature so the claims stay a faithful view of the identity.
|
|
100
|
+
*/
|
|
101
|
+
interface AccessClaims extends JWTPayload {
|
|
102
|
+
/** Service-token name. Present for non-interactive (machine) callers instead of `email`. */
|
|
103
|
+
common_name?: string;
|
|
104
|
+
/** ISO-3166-1 alpha-2 country the request was authorized from, when available. */
|
|
105
|
+
country?: string;
|
|
106
|
+
/** Verified user email. Present for interactive (SSO) callers. */
|
|
107
|
+
email?: string;
|
|
108
|
+
/** Identity-provider group memberships, when the Access policy is configured to emit them. */
|
|
109
|
+
groups?: string[];
|
|
110
|
+
/** Per-session nonce Cloudflare rotates on re-authentication. */
|
|
111
|
+
identity_nonce?: string;
|
|
112
|
+
/** Display name from the identity provider. Populated on the platform-supplied identity, not on the JWT. */
|
|
113
|
+
name?: string;
|
|
114
|
+
/** Token kind, e.g. `"app"`. */
|
|
115
|
+
type?: string;
|
|
116
|
+
/** Cloudflare's per-user UUID. Populated on the platform-supplied identity, not on the JWT — so `userId` is deliberately never derived from it. */
|
|
117
|
+
user_uuid?: string;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* The minimal `resolveIdentity` return contract shared with `@lunora/runtime`'s
|
|
121
|
+
* `WorkerOptions.resolveIdentity` (`ResolvedIdentity`). Declared structurally so
|
|
122
|
+
* this package takes no runtime dependency on `@lunora/runtime`; the value is
|
|
123
|
+
* assignable to the runtime hook.
|
|
124
|
+
*
|
|
125
|
+
* `userId` becomes `ctx.auth.userId`; every other key is forwarded (server-side,
|
|
126
|
+
* unforgeable) into `x-lunora-identity` and surfaced via `ctx.auth.getIdentity()`.
|
|
127
|
+
* `exp` (JWT epoch **seconds**) drives WebSocket credential expiry — omit it and
|
|
128
|
+
* a live subscription socket never expires.
|
|
129
|
+
*/
|
|
130
|
+
interface ResolvedIdentityLike {
|
|
131
|
+
/** All other claims pass through into `ctx.auth.getIdentity()`. */
|
|
132
|
+
[claim: string]: unknown;
|
|
133
|
+
/** JWT `exp` in epoch **seconds** (NOT milliseconds). Drives WS socket expiry. */
|
|
134
|
+
exp?: number;
|
|
135
|
+
/** Absolute expiry in epoch **milliseconds**. Alternative to `exp`; takes precedence in the runtime. */
|
|
136
|
+
expiresAtMs?: number;
|
|
137
|
+
/** The stable caller id. Becomes `ctx.auth.userId` and what `serverDefault(({auth}) => auth.userId)` stamps. */
|
|
138
|
+
userId: string;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* The verified Access identity produced by `createAccessResolver`. A
|
|
142
|
+
* {@link ResolvedIdentityLike} with the commonly-used Access claims promoted to
|
|
143
|
+
* named, camelCased fields (so policies read `auth.identity.groups` etc.) plus
|
|
144
|
+
* the full raw claim set under `access` for fidelity.
|
|
145
|
+
*/
|
|
146
|
+
interface ResolvedAccessIdentity extends ResolvedIdentityLike {
|
|
147
|
+
/** The full, verified claim set (snake_cased wire names preserved). */
|
|
148
|
+
access: AccessClaims;
|
|
149
|
+
/** Service-token name (`common_name`), for machine callers. */
|
|
150
|
+
commonName?: string;
|
|
151
|
+
/** Verified email, for SSO callers. */
|
|
152
|
+
email?: string;
|
|
153
|
+
/** IdP group memberships, when emitted by the Access policy. */
|
|
154
|
+
groups?: string[];
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* A key source for `verifyAccessJwt`. Either a `jose` remote/local JWKS getter,
|
|
158
|
+
* or a single public key (handy for tests that mint their own RS256 tokens).
|
|
159
|
+
* When omitted, a cached remote JWKS is built from `teamDomain`.
|
|
160
|
+
*/
|
|
161
|
+
type AccessKeySet = CryptoKey | JWTVerifyGetKey | KeyObject | Uint8Array;
|
|
162
|
+
/** Options for `verifyAccessJwt`. */
|
|
163
|
+
interface VerifyAccessJwtOptions {
|
|
164
|
+
/**
|
|
165
|
+
* The Access application **AUD tag(s)** (the application audience from the
|
|
166
|
+
* Access app's Overview). Verification rejects a token whose `aud` does not
|
|
167
|
+
* include one of these — this is what scopes a token to *your* app.
|
|
168
|
+
*/
|
|
169
|
+
aud: string | string[];
|
|
170
|
+
/** Clock-skew tolerance in **seconds** applied to `exp`/`nbf`/`iat`. Default `0`. */
|
|
171
|
+
clockToleranceSec?: number;
|
|
172
|
+
/**
|
|
173
|
+
* Override the verification key source. Primarily for tests; in production
|
|
174
|
+
* leave unset to use the cached remote JWKS derived from `teamDomain`.
|
|
175
|
+
*/
|
|
176
|
+
keySet?: AccessKeySet;
|
|
177
|
+
/**
|
|
178
|
+
* Your Cloudflare Access team domain. Accepts the short team name (`acme`),
|
|
179
|
+
* the host (`acme.cloudflareaccess.com`), or a full URL
|
|
180
|
+
* (`https://acme.cloudflareaccess.com`). Determines both the expected issuer
|
|
181
|
+
* and the JWKS endpoint.
|
|
182
|
+
*/
|
|
183
|
+
teamDomain: string;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Common options for the request-driven Access primitives — how to read the JWT
|
|
187
|
+
* off the request and what to do when verification fails. Shared by
|
|
188
|
+
* {@link CreateAccessResolverOptions} and `AccessAdminGateOptions`, which add
|
|
189
|
+
* their distinct mapping / authorization step on top.
|
|
190
|
+
*/
|
|
191
|
+
interface RequestVerifyOptions extends VerifyAccessJwtOptions {
|
|
192
|
+
/**
|
|
193
|
+
* Cookie name carrying the Access JWT when the header is absent (browser
|
|
194
|
+
* navigations). Default `"CF_Authorization"`.
|
|
195
|
+
*/
|
|
196
|
+
cookieName?: string;
|
|
197
|
+
/**
|
|
198
|
+
* Request header carrying the Access JWT. Default `"cf-access-jwt-assertion"`
|
|
199
|
+
* (matched case-insensitively).
|
|
200
|
+
*/
|
|
201
|
+
headerName?: string;
|
|
202
|
+
/**
|
|
203
|
+
* Invoked when a token is present but fails verification (bad signature,
|
|
204
|
+
* wrong audience, expired, …). The caller still fails closed (resolver
|
|
205
|
+
* returns `null`, admin gate returns `false`); this is your hook to
|
|
206
|
+
* log/observe. It is **not** called when no token is present at all.
|
|
207
|
+
*/
|
|
208
|
+
onError?: (error: unknown, request: Request) => void;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* {@link RequestVerifyOptions} with the JWT-verification config made optional,
|
|
212
|
+
* for the primitives that can also authenticate off the platform-supplied
|
|
213
|
+
* identity (`ctx.access`) and therefore may legitimately be given no JWT config
|
|
214
|
+
* at all.
|
|
215
|
+
*
|
|
216
|
+
* The two fields are **all-or-nothing**: supply both to enable the
|
|
217
|
+
* `Cf-Access-Jwt-Assertion` fallback (needed for hostname-scoped Access
|
|
218
|
+
* applications, which do not populate `ctx.access`), or neither to run
|
|
219
|
+
* platform-identity-only. Supplying exactly one throws at construction — that is
|
|
220
|
+
* always a misconfiguration (classically an unset `env.CF_ACCESS_AUD`), and
|
|
221
|
+
* silently degrading it to "no JWT fallback" would turn a broken deployment into
|
|
222
|
+
* a quietly anonymous one.
|
|
223
|
+
*/
|
|
224
|
+
interface AccessJwtFallbackOptions extends Omit<RequestVerifyOptions, "aud" | "teamDomain"> {
|
|
225
|
+
/**
|
|
226
|
+
* The Access application **AUD tag(s)**. Required together with `teamDomain`
|
|
227
|
+
* to enable JWT verification; omit both to authenticate only off the
|
|
228
|
+
* platform-supplied identity.
|
|
229
|
+
*/
|
|
230
|
+
aud?: string | string[];
|
|
231
|
+
/**
|
|
232
|
+
* Your Cloudflare Access team domain. Required together with `aud` to enable
|
|
233
|
+
* JWT verification; omit both to authenticate only off the platform-supplied
|
|
234
|
+
* identity.
|
|
235
|
+
*/
|
|
236
|
+
teamDomain?: string;
|
|
237
|
+
}
|
|
238
|
+
/** Options for `createAccessResolver`; extends {@link AccessJwtFallbackOptions}. */
|
|
239
|
+
interface CreateAccessResolverOptions extends AccessJwtFallbackOptions {
|
|
240
|
+
/**
|
|
241
|
+
* Remap verified claims into the resolved identity. Return an object to
|
|
242
|
+
* shallow-merge over the defaults; return a `userId` to override the derived
|
|
243
|
+
* caller id. Runs only after signature/issuer/audience/expiry are verified.
|
|
244
|
+
*
|
|
245
|
+
* It is merged OVER the `roles` claim {@link CreateAccessResolverOptions.roles}
|
|
246
|
+
* mints, so returning `roles` here replaces the mapped set outright.
|
|
247
|
+
*/
|
|
248
|
+
mapClaims?: (claims: AccessClaims) => Record<string, unknown>;
|
|
249
|
+
/**
|
|
250
|
+
* Map the verified Access `groups` claim onto RLS role names, minted as the
|
|
251
|
+
* identity's `roles` claim — the list `rls()` unions permissions over.
|
|
252
|
+
*
|
|
253
|
+
* A table (`{ "idp-admins": "admin", "idp-eng": ["editor", "viewer"] }`) or a
|
|
254
|
+
* function; either may return one role, an array, or `undefined` to drop the
|
|
255
|
+
* group. Omit the option and no `roles` claim is minted at all — granting
|
|
256
|
+
* every group name as a role by default would hand existing deployments
|
|
257
|
+
* permissions their policies never intended. Pass `(group) => group` to opt
|
|
258
|
+
* into group names as role names verbatim.
|
|
259
|
+
*
|
|
260
|
+
* Roles live ON THE IDENTITY rather than being derived per-procedure so that
|
|
261
|
+
* every consumer sees the same set. A middleware that writes `ctx.auth.roles`
|
|
262
|
+
* reaches queries and mutations but NOT live shapes — a shape runs no
|
|
263
|
+
* procedure, so no middleware fires — and a role-gated policy then resolves
|
|
264
|
+
* differently for a query and the subscription that mirrors it. The identity
|
|
265
|
+
* is the one place both paths read.
|
|
266
|
+
*/
|
|
267
|
+
roles?: AccessRoleMap;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* A `resolveIdentity`-shaped function: maps an inbound request to a verified
|
|
271
|
+
* identity (or `null` for anonymous). Assignable to `@lunora/runtime`'s
|
|
272
|
+
* `WorkerOptions.resolveIdentity`.
|
|
273
|
+
*
|
|
274
|
+
* The third argument is the request's `ExecutionContext`, which the runtime
|
|
275
|
+
* forwards so a resolver can read the identity Cloudflare Access attaches to a
|
|
276
|
+
* Worker-protected request (`context.access`). It is `undefined` on paths that
|
|
277
|
+
* have no context to give, so a resolver must handle its absence.
|
|
278
|
+
*/
|
|
279
|
+
type ResolveIdentityFunction = (request: Request, env?: unknown, context?: ExecutionContextLike) => (ResolvedIdentityLike | null) | Promise<ResolvedIdentityLike | null>;
|
|
280
|
+
export { AccessClaims as A, CreateAccessResolverOptions as C, ExecutionContextLike as E, ResolveIdentityFunction as R, VerifyAccessJwtOptions as V, AccessJwtFallbackOptions as a, AccessKeySet as b, AccessRoleMap as c, ResolvedAccessIdentity as d, ResolvedIdentityLike as e };
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { JWTPayload, JWTVerifyGetKey, KeyObject } from 'jose';
|
|
2
|
+
/**
|
|
3
|
+
* The subset of the Cloudflare `ExecutionContext` the Lunora worker entry and
|
|
4
|
+
* the framework mount seams rely on — `waitUntil` for fire-and-forget work that
|
|
5
|
+
* must outlive the response, and `passThroughOnException` for the top-level
|
|
6
|
+
* error posture.
|
|
7
|
+
*
|
|
8
|
+
* It is deliberately **not** a package. `@lunora/runtime` is the leaf server
|
|
9
|
+
* runtime and `@lunora/nuxt` is a framework integration that intentionally does
|
|
10
|
+
* not depend on `@lunora/runtime`'s worker types, yet both need this exact
|
|
11
|
+
* shape: the runtime to build/forward the worker `fetch`, Nuxt to forward an
|
|
12
|
+
* inbound request to the user's composed worker. Each imports this file by
|
|
13
|
+
* relative path and the bundler (packem/rollup) inlines it: no runtime
|
|
14
|
+
* dependency edge is created, the helper is duplicated only in emitted output,
|
|
15
|
+
* never in source. One source of truth, zero deps. See AGENTS.md → "Top-level
|
|
16
|
+
* `shared/` — bundler-inlined source".
|
|
17
|
+
*
|
|
18
|
+
* Both methods are **optional**: a real Cloudflare `ExecutionContext` always
|
|
19
|
+
* supplies them, but a host that mounts Lunora as a sub-handler (Nitro/H3, a
|
|
20
|
+
* non-Cloudflare preview, a unit test) may hand over a partial context or none
|
|
21
|
+
* at all. Callers therefore invoke them defensively (`ctx.waitUntil?.(…)`) or
|
|
22
|
+
* fall back to {@link NOOP_EXECUTION_CONTEXT}.
|
|
23
|
+
*/
|
|
24
|
+
interface ExecutionContextLike {
|
|
25
|
+
/**
|
|
26
|
+
* Present only when Cloudflare Access authenticated the request against a
|
|
27
|
+
* policy attached to the **Worker** (rather than to a hostname). `undefined`
|
|
28
|
+
* on every unauthenticated request, so its presence is itself the "Access
|
|
29
|
+
* authorized this caller" signal — see {@link AccessContextLike}.
|
|
30
|
+
*/
|
|
31
|
+
access?: AccessContextLike;
|
|
32
|
+
cache?: {
|
|
33
|
+
purge: (options: {
|
|
34
|
+
purgeEverything?: boolean;
|
|
35
|
+
tags?: string[];
|
|
36
|
+
}) => Promise<unknown>;
|
|
37
|
+
};
|
|
38
|
+
passThroughOnException?: () => void;
|
|
39
|
+
waitUntil?: (promise: Promise<unknown>) => void;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The identity Cloudflare Access attaches to a Worker-protected request.
|
|
43
|
+
*
|
|
44
|
+
* Shape follows the Access application-token payload: `sub` is the stable per-user
|
|
45
|
+
* id, `email` the verified address, `common_name` the service-token name (machine
|
|
46
|
+
* callers, whose `sub` is empty), and `exp` the credential expiry in epoch
|
|
47
|
+
* **seconds**. Group membership is whatever the Access policy emits — a list of
|
|
48
|
+
* names, or of `{ id, name }` objects — hence `unknown`; normalize before use.
|
|
49
|
+
*
|
|
50
|
+
* Cloudflare may add further fields, so the index signature keeps them rather
|
|
51
|
+
* than dropping them: this is a view of a payload we do not own.
|
|
52
|
+
*/
|
|
53
|
+
interface AccessIdentityLike {
|
|
54
|
+
[claim: string]: unknown;
|
|
55
|
+
/** Service-token name. Present for non-interactive (machine) callers instead of `email`. */
|
|
56
|
+
common_name?: string;
|
|
57
|
+
/** Verified user email. Present for interactive (SSO) callers. */
|
|
58
|
+
email?: string;
|
|
59
|
+
/** Credential expiry, epoch **seconds**. */
|
|
60
|
+
exp?: number;
|
|
61
|
+
/** IdP group membership — names or `{ id, name }` objects, depending on the policy. */
|
|
62
|
+
groups?: unknown;
|
|
63
|
+
/** Display name from the identity provider, when it emits one. */
|
|
64
|
+
name?: string;
|
|
65
|
+
/** Stable per-user id, and what consumers key a user on. Empty for service tokens. */
|
|
66
|
+
sub?: string;
|
|
67
|
+
/** Cloudflare's per-user UUID. Carried through, but deliberately not used as an id — only this path emits it, so keying on it would not match the JWT path. */
|
|
68
|
+
user_uuid?: string;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* The `ctx.access` facade Cloudflare exposes on a Worker protected by Access.
|
|
72
|
+
*
|
|
73
|
+
* Reading the identity from here is preferable to verifying the
|
|
74
|
+
* `Cf-Access-Jwt-Assertion` header: the platform has already authenticated the
|
|
75
|
+
* caller, so there is no JWKS fetch, no audience check to get wrong, and nothing
|
|
76
|
+
* a request can forge — the field simply does not exist unless Access authorized
|
|
77
|
+
* the call. The header path remains the fallback for hostname-scoped Access
|
|
78
|
+
* applications, which do not populate this.
|
|
79
|
+
*/
|
|
80
|
+
interface AccessContextLike {
|
|
81
|
+
getIdentity: () => AccessIdentityLike | null | undefined | Promise<AccessIdentityLike | null | undefined>;
|
|
82
|
+
}
|
|
83
|
+
/** A group→role(s) lookup table, or a function returning the role(s) for one group. */
|
|
84
|
+
type AccessRoleMap = ((group: string) => string | string[] | undefined) | Record<string, string | string[]>;
|
|
85
|
+
/**
|
|
86
|
+
* The verified claims of a Cloudflare Access caller — from the
|
|
87
|
+
* `Cf-Access-Jwt-Assertion` JWT (hostname-scoped Access applications) or from the
|
|
88
|
+
* platform-supplied `ctx.access.getIdentity()` (Access policies attached to the
|
|
89
|
+
* Worker). Both paths produce this one shape, so nothing downstream branches on
|
|
90
|
+
* how the caller was authenticated.
|
|
91
|
+
*
|
|
92
|
+
* Extends the standard `JWTPayload` (`iss`/`aud`/`sub`/`exp`/`iat`/…) with the
|
|
93
|
+
* Access-specific fields. Which optional fields are present depends on the
|
|
94
|
+
* caller and the Access application config. SSO users carry `email` (and
|
|
95
|
+
* `groups` when the policy emits them), with `sub` as the stable user id.
|
|
96
|
+
* Service tokens carry `common_name` and an empty `sub`; there is no `email`.
|
|
97
|
+
*
|
|
98
|
+
* Cloudflare may add further custom claims — they pass through verbatim via the
|
|
99
|
+
* index signature so the claims stay a faithful view of the identity.
|
|
100
|
+
*/
|
|
101
|
+
interface AccessClaims extends JWTPayload {
|
|
102
|
+
/** Service-token name. Present for non-interactive (machine) callers instead of `email`. */
|
|
103
|
+
common_name?: string;
|
|
104
|
+
/** ISO-3166-1 alpha-2 country the request was authorized from, when available. */
|
|
105
|
+
country?: string;
|
|
106
|
+
/** Verified user email. Present for interactive (SSO) callers. */
|
|
107
|
+
email?: string;
|
|
108
|
+
/** Identity-provider group memberships, when the Access policy is configured to emit them. */
|
|
109
|
+
groups?: string[];
|
|
110
|
+
/** Per-session nonce Cloudflare rotates on re-authentication. */
|
|
111
|
+
identity_nonce?: string;
|
|
112
|
+
/** Display name from the identity provider. Populated on the platform-supplied identity, not on the JWT. */
|
|
113
|
+
name?: string;
|
|
114
|
+
/** Token kind, e.g. `"app"`. */
|
|
115
|
+
type?: string;
|
|
116
|
+
/** Cloudflare's per-user UUID. Populated on the platform-supplied identity, not on the JWT — so `userId` is deliberately never derived from it. */
|
|
117
|
+
user_uuid?: string;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* The minimal `resolveIdentity` return contract shared with `@lunora/runtime`'s
|
|
121
|
+
* `WorkerOptions.resolveIdentity` (`ResolvedIdentity`). Declared structurally so
|
|
122
|
+
* this package takes no runtime dependency on `@lunora/runtime`; the value is
|
|
123
|
+
* assignable to the runtime hook.
|
|
124
|
+
*
|
|
125
|
+
* `userId` becomes `ctx.auth.userId`; every other key is forwarded (server-side,
|
|
126
|
+
* unforgeable) into `x-lunora-identity` and surfaced via `ctx.auth.getIdentity()`.
|
|
127
|
+
* `exp` (JWT epoch **seconds**) drives WebSocket credential expiry — omit it and
|
|
128
|
+
* a live subscription socket never expires.
|
|
129
|
+
*/
|
|
130
|
+
interface ResolvedIdentityLike {
|
|
131
|
+
/** All other claims pass through into `ctx.auth.getIdentity()`. */
|
|
132
|
+
[claim: string]: unknown;
|
|
133
|
+
/** JWT `exp` in epoch **seconds** (NOT milliseconds). Drives WS socket expiry. */
|
|
134
|
+
exp?: number;
|
|
135
|
+
/** Absolute expiry in epoch **milliseconds**. Alternative to `exp`; takes precedence in the runtime. */
|
|
136
|
+
expiresAtMs?: number;
|
|
137
|
+
/** The stable caller id. Becomes `ctx.auth.userId` and what `serverDefault(({auth}) => auth.userId)` stamps. */
|
|
138
|
+
userId: string;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* The verified Access identity produced by `createAccessResolver`. A
|
|
142
|
+
* {@link ResolvedIdentityLike} with the commonly-used Access claims promoted to
|
|
143
|
+
* named, camelCased fields (so policies read `auth.identity.groups` etc.) plus
|
|
144
|
+
* the full raw claim set under `access` for fidelity.
|
|
145
|
+
*/
|
|
146
|
+
interface ResolvedAccessIdentity extends ResolvedIdentityLike {
|
|
147
|
+
/** The full, verified claim set (snake_cased wire names preserved). */
|
|
148
|
+
access: AccessClaims;
|
|
149
|
+
/** Service-token name (`common_name`), for machine callers. */
|
|
150
|
+
commonName?: string;
|
|
151
|
+
/** Verified email, for SSO callers. */
|
|
152
|
+
email?: string;
|
|
153
|
+
/** IdP group memberships, when emitted by the Access policy. */
|
|
154
|
+
groups?: string[];
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* A key source for `verifyAccessJwt`. Either a `jose` remote/local JWKS getter,
|
|
158
|
+
* or a single public key (handy for tests that mint their own RS256 tokens).
|
|
159
|
+
* When omitted, a cached remote JWKS is built from `teamDomain`.
|
|
160
|
+
*/
|
|
161
|
+
type AccessKeySet = CryptoKey | JWTVerifyGetKey | KeyObject | Uint8Array;
|
|
162
|
+
/** Options for `verifyAccessJwt`. */
|
|
163
|
+
interface VerifyAccessJwtOptions {
|
|
164
|
+
/**
|
|
165
|
+
* The Access application **AUD tag(s)** (the application audience from the
|
|
166
|
+
* Access app's Overview). Verification rejects a token whose `aud` does not
|
|
167
|
+
* include one of these — this is what scopes a token to *your* app.
|
|
168
|
+
*/
|
|
169
|
+
aud: string | string[];
|
|
170
|
+
/** Clock-skew tolerance in **seconds** applied to `exp`/`nbf`/`iat`. Default `0`. */
|
|
171
|
+
clockToleranceSec?: number;
|
|
172
|
+
/**
|
|
173
|
+
* Override the verification key source. Primarily for tests; in production
|
|
174
|
+
* leave unset to use the cached remote JWKS derived from `teamDomain`.
|
|
175
|
+
*/
|
|
176
|
+
keySet?: AccessKeySet;
|
|
177
|
+
/**
|
|
178
|
+
* Your Cloudflare Access team domain. Accepts the short team name (`acme`),
|
|
179
|
+
* the host (`acme.cloudflareaccess.com`), or a full URL
|
|
180
|
+
* (`https://acme.cloudflareaccess.com`). Determines both the expected issuer
|
|
181
|
+
* and the JWKS endpoint.
|
|
182
|
+
*/
|
|
183
|
+
teamDomain: string;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Common options for the request-driven Access primitives — how to read the JWT
|
|
187
|
+
* off the request and what to do when verification fails. Shared by
|
|
188
|
+
* {@link CreateAccessResolverOptions} and `AccessAdminGateOptions`, which add
|
|
189
|
+
* their distinct mapping / authorization step on top.
|
|
190
|
+
*/
|
|
191
|
+
interface RequestVerifyOptions extends VerifyAccessJwtOptions {
|
|
192
|
+
/**
|
|
193
|
+
* Cookie name carrying the Access JWT when the header is absent (browser
|
|
194
|
+
* navigations). Default `"CF_Authorization"`.
|
|
195
|
+
*/
|
|
196
|
+
cookieName?: string;
|
|
197
|
+
/**
|
|
198
|
+
* Request header carrying the Access JWT. Default `"cf-access-jwt-assertion"`
|
|
199
|
+
* (matched case-insensitively).
|
|
200
|
+
*/
|
|
201
|
+
headerName?: string;
|
|
202
|
+
/**
|
|
203
|
+
* Invoked when a token is present but fails verification (bad signature,
|
|
204
|
+
* wrong audience, expired, …). The caller still fails closed (resolver
|
|
205
|
+
* returns `null`, admin gate returns `false`); this is your hook to
|
|
206
|
+
* log/observe. It is **not** called when no token is present at all.
|
|
207
|
+
*/
|
|
208
|
+
onError?: (error: unknown, request: Request) => void;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* {@link RequestVerifyOptions} with the JWT-verification config made optional,
|
|
212
|
+
* for the primitives that can also authenticate off the platform-supplied
|
|
213
|
+
* identity (`ctx.access`) and therefore may legitimately be given no JWT config
|
|
214
|
+
* at all.
|
|
215
|
+
*
|
|
216
|
+
* The two fields are **all-or-nothing**: supply both to enable the
|
|
217
|
+
* `Cf-Access-Jwt-Assertion` fallback (needed for hostname-scoped Access
|
|
218
|
+
* applications, which do not populate `ctx.access`), or neither to run
|
|
219
|
+
* platform-identity-only. Supplying exactly one throws at construction — that is
|
|
220
|
+
* always a misconfiguration (classically an unset `env.CF_ACCESS_AUD`), and
|
|
221
|
+
* silently degrading it to "no JWT fallback" would turn a broken deployment into
|
|
222
|
+
* a quietly anonymous one.
|
|
223
|
+
*/
|
|
224
|
+
interface AccessJwtFallbackOptions extends Omit<RequestVerifyOptions, "aud" | "teamDomain"> {
|
|
225
|
+
/**
|
|
226
|
+
* The Access application **AUD tag(s)**. Required together with `teamDomain`
|
|
227
|
+
* to enable JWT verification; omit both to authenticate only off the
|
|
228
|
+
* platform-supplied identity.
|
|
229
|
+
*/
|
|
230
|
+
aud?: string | string[];
|
|
231
|
+
/**
|
|
232
|
+
* Your Cloudflare Access team domain. Required together with `aud` to enable
|
|
233
|
+
* JWT verification; omit both to authenticate only off the platform-supplied
|
|
234
|
+
* identity.
|
|
235
|
+
*/
|
|
236
|
+
teamDomain?: string;
|
|
237
|
+
}
|
|
238
|
+
/** Options for `createAccessResolver`; extends {@link AccessJwtFallbackOptions}. */
|
|
239
|
+
interface CreateAccessResolverOptions extends AccessJwtFallbackOptions {
|
|
240
|
+
/**
|
|
241
|
+
* Remap verified claims into the resolved identity. Return an object to
|
|
242
|
+
* shallow-merge over the defaults; return a `userId` to override the derived
|
|
243
|
+
* caller id. Runs only after signature/issuer/audience/expiry are verified.
|
|
244
|
+
*
|
|
245
|
+
* It is merged OVER the `roles` claim {@link CreateAccessResolverOptions.roles}
|
|
246
|
+
* mints, so returning `roles` here replaces the mapped set outright.
|
|
247
|
+
*/
|
|
248
|
+
mapClaims?: (claims: AccessClaims) => Record<string, unknown>;
|
|
249
|
+
/**
|
|
250
|
+
* Map the verified Access `groups` claim onto RLS role names, minted as the
|
|
251
|
+
* identity's `roles` claim — the list `rls()` unions permissions over.
|
|
252
|
+
*
|
|
253
|
+
* A table (`{ "idp-admins": "admin", "idp-eng": ["editor", "viewer"] }`) or a
|
|
254
|
+
* function; either may return one role, an array, or `undefined` to drop the
|
|
255
|
+
* group. Omit the option and no `roles` claim is minted at all — granting
|
|
256
|
+
* every group name as a role by default would hand existing deployments
|
|
257
|
+
* permissions their policies never intended. Pass `(group) => group` to opt
|
|
258
|
+
* into group names as role names verbatim.
|
|
259
|
+
*
|
|
260
|
+
* Roles live ON THE IDENTITY rather than being derived per-procedure so that
|
|
261
|
+
* every consumer sees the same set. A middleware that writes `ctx.auth.roles`
|
|
262
|
+
* reaches queries and mutations but NOT live shapes — a shape runs no
|
|
263
|
+
* procedure, so no middleware fires — and a role-gated policy then resolves
|
|
264
|
+
* differently for a query and the subscription that mirrors it. The identity
|
|
265
|
+
* is the one place both paths read.
|
|
266
|
+
*/
|
|
267
|
+
roles?: AccessRoleMap;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* A `resolveIdentity`-shaped function: maps an inbound request to a verified
|
|
271
|
+
* identity (or `null` for anonymous). Assignable to `@lunora/runtime`'s
|
|
272
|
+
* `WorkerOptions.resolveIdentity`.
|
|
273
|
+
*
|
|
274
|
+
* The third argument is the request's `ExecutionContext`, which the runtime
|
|
275
|
+
* forwards so a resolver can read the identity Cloudflare Access attaches to a
|
|
276
|
+
* Worker-protected request (`context.access`). It is `undefined` on paths that
|
|
277
|
+
* have no context to give, so a resolver must handle its absence.
|
|
278
|
+
*/
|
|
279
|
+
type ResolveIdentityFunction = (request: Request, env?: unknown, context?: ExecutionContextLike) => (ResolvedIdentityLike | null) | Promise<ResolvedIdentityLike | null>;
|
|
280
|
+
export { AccessClaims as A, CreateAccessResolverOptions as C, ExecutionContextLike as E, ResolveIdentityFunction as R, VerifyAccessJwtOptions as V, AccessJwtFallbackOptions as a, AccessKeySet as b, AccessRoleMap as c, ResolvedAccessIdentity as d, ResolvedIdentityLike as e };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lunora/cloudflare-access",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.130",
|
|
4
4
|
"description": "Cloudflare Access (Zero Trust) identity for Lunora — verify the Cf-Access-Jwt-Assertion JWT against your team JWKS and feed the verified identity into ctx.auth / RLS via a resolveIdentity adapter",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"access",
|
|
@@ -44,21 +44,17 @@
|
|
|
44
44
|
"types": "./dist/context.d.ts",
|
|
45
45
|
"import": "./dist/context.mjs"
|
|
46
46
|
},
|
|
47
|
-
"./roles": {
|
|
48
|
-
"types": "./dist/roles.d.ts",
|
|
49
|
-
"import": "./dist/roles.mjs"
|
|
50
|
-
},
|
|
51
47
|
"./package.json": "./package.json"
|
|
52
48
|
},
|
|
53
49
|
"publishConfig": {
|
|
54
50
|
"access": "public"
|
|
55
51
|
},
|
|
56
52
|
"dependencies": {
|
|
57
|
-
"@lunora/errors": "1.0.0-alpha.
|
|
58
|
-
"jose": "^6.2.
|
|
53
|
+
"@lunora/errors": "1.0.0-alpha.39",
|
|
54
|
+
"jose": "^6.2.12"
|
|
59
55
|
},
|
|
60
56
|
"peerDependencies": {
|
|
61
|
-
"@lunora/server": "1.0.0-alpha.
|
|
57
|
+
"@lunora/server": ">=1.0.0-alpha.24 <2.0.0-0"
|
|
62
58
|
},
|
|
63
59
|
"peerDependenciesMeta": {
|
|
64
60
|
"@lunora/server": {
|