@alethialabs/platform 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,51 @@
1
+ /** The request header Cloudflare Access sets with the signed application token. */
2
+ export declare const ACCESS_JWT_HEADER = "cf-access-jwt-assertion";
3
+ /** The cookie Cloudflare Access sets with the same signed application token. */
4
+ export declare const ACCESS_JWT_COOKIE = "CF_Authorization";
5
+ /**
6
+ * The request header the staff app forwards the staff member's Access token in, on its calls to
7
+ * the console's platform privacy routes. Deliberately not `Cf-Access-Jwt-Assertion`: that name is
8
+ * Cloudflare's, and this hop is a service call the staff app makes, not a request Access admitted.
9
+ */
10
+ export declare const STAFF_ACCESS_TOKEN_HEADER = "x-alethia-staff-access-token";
11
+ /** A validated Access configuration: the token issuer (team URL) and the application AUD tag. */
12
+ export interface AccessConfig {
13
+ /** `https://<team>.cloudflareaccess.com` — the expected `iss`, and the base of the certs URL. */
14
+ issuer: string;
15
+ /** The Access application's AUD tag — must appear in the token's `aud`. */
16
+ audience: string;
17
+ }
18
+ /** Everything the staff-identity decision depends on, gathered by the caller. */
19
+ export interface StaffIdentityInput {
20
+ /** The Access token from the request header or cookie, if any. */
21
+ token: string | null | undefined;
22
+ /** CF_ACCESS_TEAM_DOMAIN — `<team>.cloudflareaccess.com`, with or without `https://`. */
23
+ teamDomain: string | null | undefined;
24
+ /** CF_ACCESS_AUD — the Access application AUD tag. */
25
+ audience: string | null | undefined;
26
+ /** SUPPORT_ADMIN_DEV_EMAIL — the local-development stand-in identity. */
27
+ devEmail: string | null | undefined;
28
+ /** Whether this process runs in production (NODE_ENV=production). */
29
+ production: boolean;
30
+ }
31
+ /**
32
+ * Builds an {@link AccessConfig} from the two settings, or null when either is missing or the team
33
+ * domain is not a bare https host (a path, query, credentials or a non-https scheme are refused so
34
+ * the issuer can only ever be an origin).
35
+ */
36
+ export declare function accessConfigFrom(teamDomain: string | null | undefined, audience: string | null | undefined): AccessConfig | null;
37
+ /** Drops the cached key sets. Test-only: lets each test observe its own key fetch. */
38
+ export declare function resetAccessKeyCache(): void;
39
+ /**
40
+ * Verifies an Access application token and returns its lowercased `email` claim, or null when the
41
+ * signature, issuer, audience or validity window does not check out, or the token carries no email
42
+ * (an Access service token, for instance). Never throws.
43
+ */
44
+ export declare function verifyAccessToken(token: string, config: AccessConfig): Promise<string | null>;
45
+ /**
46
+ * Decides the caller's staff email. A configured Access setup with a token present is the only
47
+ * way to an identity, and a token that fails verification yields none. In production that is the
48
+ * whole rule; outside production, when Access is unconfigured or sent no token, the dev stand-in
49
+ * email is used instead.
50
+ */
51
+ export declare function resolveStaffEmail(input: StaffIdentityInput): Promise<string | null>;
@@ -0,0 +1,114 @@
1
+ // SPDX-FileCopyrightText: 2026 Alethia Labs <legal@alethialabs.io>
2
+ // SPDX-License-Identifier: AGPL-3.0-only
3
+ // Cloudflare Access application-token verification, shared by the staff app and the console.
4
+ //
5
+ // Access signs a JWT for every request it lets through to the staff app and delivers it in the
6
+ // `Cf-Access-Jwt-Assertion` header (and the `CF_Authorization` cookie). The staff identity is the
7
+ // `email` claim of THAT token, and only once the token verifies: RS256 signature against the team's
8
+ // published keys (https://<team>.cloudflareaccess.com/cdn-cgi/access/certs, cached by jose's
9
+ // remote key set), `iss` equal to the team URL, `aud` containing the staff app's Access AUD tag,
10
+ // and `exp`/`nbf` honoured. Nothing else on the request is read as identity.
11
+ //
12
+ // Fail-closed in production: no configuration, no token or a token that does not verify all mean
13
+ // "no staff identity". The SUPPORT_ADMIN_DEV_EMAIL stand-in exists for local development only and
14
+ // is never consulted when NODE_ENV=production.
15
+ //
16
+ // Two callers, one rule. apps/admin/lib/auth/staff.ts verifies the token Access delivered to it.
17
+ // The console's platform privacy routes (apps/console/lib/auth/staff-access.ts) verify the same
18
+ // token again when the staff app forwards it in {@link STAFF_ACCESS_TOKEN_HEADER}: the acting staff
19
+ // member a privacy step records is the one the token names, not one the caller states.
20
+ //
21
+ // This module is pure (no next/headers) so the decision can be unit-tested; each caller feeds it
22
+ // the request's token and the environment.
23
+ import { createRemoteJWKSet, jwtVerify } from "jose";
24
+ /** The request header Cloudflare Access sets with the signed application token. */
25
+ export const ACCESS_JWT_HEADER = "cf-access-jwt-assertion";
26
+ /** The cookie Cloudflare Access sets with the same signed application token. */
27
+ export const ACCESS_JWT_COOKIE = "CF_Authorization";
28
+ /**
29
+ * The request header the staff app forwards the staff member's Access token in, on its calls to
30
+ * the console's platform privacy routes. Deliberately not `Cf-Access-Jwt-Assertion`: that name is
31
+ * Cloudflare's, and this hop is a service call the staff app makes, not a request Access admitted.
32
+ */
33
+ export const STAFF_ACCESS_TOKEN_HEADER = "x-alethia-staff-access-token";
34
+ /**
35
+ * Builds an {@link AccessConfig} from the two settings, or null when either is missing or the team
36
+ * domain is not a bare https host (a path, query, credentials or a non-https scheme are refused so
37
+ * the issuer can only ever be an origin).
38
+ */
39
+ export function accessConfigFrom(teamDomain, audience) {
40
+ const domain = teamDomain?.trim();
41
+ const aud = audience?.trim();
42
+ if (!domain || !aud)
43
+ return null;
44
+ const withScheme = /^[a-z][a-z0-9+.-]*:\/\//i.test(domain)
45
+ ? domain
46
+ : `https://${domain}`;
47
+ let url;
48
+ try {
49
+ url = new URL(withScheme);
50
+ }
51
+ catch {
52
+ return null;
53
+ }
54
+ if (url.protocol !== "https:")
55
+ return null;
56
+ if (url.username || url.password || url.search || url.hash)
57
+ return null;
58
+ if (url.pathname !== "/" && url.pathname !== "")
59
+ return null;
60
+ return { issuer: url.origin, audience: aud };
61
+ }
62
+ /** Remote key sets, one per issuer, so jose's key cache survives across requests. */
63
+ const keySets = new Map();
64
+ /** The cached remote key set for an issuer's `/cdn-cgi/access/certs` endpoint. */
65
+ function keySetFor(issuer) {
66
+ let keys = keySets.get(issuer);
67
+ if (!keys) {
68
+ keys = createRemoteJWKSet(new URL("/cdn-cgi/access/certs", issuer));
69
+ keySets.set(issuer, keys);
70
+ }
71
+ return keys;
72
+ }
73
+ /** Drops the cached key sets. Test-only: lets each test observe its own key fetch. */
74
+ export function resetAccessKeyCache() {
75
+ keySets.clear();
76
+ }
77
+ /**
78
+ * Verifies an Access application token and returns its lowercased `email` claim, or null when the
79
+ * signature, issuer, audience or validity window does not check out, or the token carries no email
80
+ * (an Access service token, for instance). Never throws.
81
+ */
82
+ export async function verifyAccessToken(token, config) {
83
+ try {
84
+ const { payload } = await jwtVerify(token, keySetFor(config.issuer), {
85
+ issuer: config.issuer,
86
+ audience: config.audience,
87
+ algorithms: ["RS256"],
88
+ });
89
+ const email = payload["email"];
90
+ if (typeof email !== "string")
91
+ return null;
92
+ const normalized = email.trim().toLowerCase();
93
+ return normalized || null;
94
+ }
95
+ catch {
96
+ return null;
97
+ }
98
+ }
99
+ /**
100
+ * Decides the caller's staff email. A configured Access setup with a token present is the only
101
+ * way to an identity, and a token that fails verification yields none. In production that is the
102
+ * whole rule; outside production, when Access is unconfigured or sent no token, the dev stand-in
103
+ * email is used instead.
104
+ */
105
+ export async function resolveStaffEmail(input) {
106
+ const config = accessConfigFrom(input.teamDomain, input.audience);
107
+ const token = input.token?.trim();
108
+ if (config && token)
109
+ return verifyAccessToken(token, config);
110
+ if (input.production)
111
+ return null;
112
+ const dev = input.devEmail?.trim().toLowerCase();
113
+ return dev || null;
114
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alethialabs/platform",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Alethia platform records: enterprise contracts and the platform audit log, as Drizzle schema, enums and types.",
5
5
  "license": "AGPL-3.0-only",
6
6
  "author": "Alethia Labs <legal@alethialabs.io>",
@@ -37,13 +37,18 @@
37
37
  "./paddle": {
38
38
  "types": "./dist/paddle.d.ts",
39
39
  "default": "./dist/paddle.js"
40
+ },
41
+ "./access-jwt": {
42
+ "types": "./dist/access-jwt.d.ts",
43
+ "default": "./dist/access-jwt.js"
40
44
  }
41
45
  },
42
46
  "files": [
43
47
  "dist"
44
48
  ],
45
49
  "dependencies": {
46
- "drizzle-orm": "^0.45.4"
50
+ "drizzle-orm": "^0.45.4",
51
+ "jose": "^6.2.17"
47
52
  },
48
53
  "publishConfig": {
49
54
  "access": "public"