@atlasauth/pca-express 0.0.0-stage → 0.1.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.
package/README.md CHANGED
@@ -1,3 +1,36 @@
1
- # Temporary Holding Version
1
+ # @atlasauth/pca-express
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Express middleware for Proof-Carrying Authority: requirePCA() verifies the inbound PCActn and attaches the verdict, or answers 401/403 with a WWW-Authenticate challenge.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ npm i @atlasauth/pca-express
9
+ # peer dependency, install alongside:
10
+ npm i express
11
+ ```
12
+
13
+ `express` (v4 or v5) is an optional peer dependency.
14
+
15
+ ## Usage
16
+
17
+ ```ts
18
+ import { pcaExpress } from '@atlasauth/pca-express';
19
+
20
+ app.post(
21
+ '/refunds',
22
+ pcaExpress({
23
+ audience: 'ins_acme', // this resource server's / instance id the PCActn must name
24
+ resolveGrant, // (ref) => Capability — look up the root grant
25
+ budgetStore, // per-holder replay/budget store
26
+ }),
27
+ (req, res) => {
28
+ const { verdict, pcactn } = req.pca!; // attached on success
29
+ res.json({ ok: true });
30
+ },
31
+ );
32
+ ```
33
+
34
+ The client sends the PCActn as `PCA-Action: <base64url>` (see `@atlasauth/pca` `pcaHeaders`) or a JSON body `{ pcactn }` — the default extractor reads both. The middleware verifies and is default-deny (an unenforced required check denies); it is the verifier that decides, not the client's proof.
35
+
36
+ Part of Proof-Carrying Authority — see `@atlasauth/pca`.
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Express middleware for Proof-Carrying Authority.
3
+ *
4
+ * app.post('/refunds',
5
+ * pcaExpress({ audience: 'ins_acme', resolveGrant, budgetStore, hooks, context }),
6
+ * (req, res) => { const { verdict, pcactn } = req.pca!; ... });
7
+ *
8
+ * It verifies the inbound PCActn with the framework-agnostic `requirePCA` guard from `@atlasauth/backend`
9
+ * (default-deny: a required check that is unenforced DENIES) and, on success, attaches the verdict +
10
+ * PCActn to `req[attachAs]` (default `req.pca`). On failure it answers 401/403 with a WWW-Authenticate
11
+ * challenge. The client sends the PCActn as `PCA-Action: <base64url>` (see `@atlasauth/pca` `pcaHeaders`)
12
+ * or a JSON body `{ pcactn }`; that is exactly what the default extractor reads, so no wiring is needed.
13
+ *
14
+ * `express` is an OPTIONAL peer dependency and is never imported — the request/response/next shapes are
15
+ * structural, so this stays compatible across Express 4 and 5.
16
+ */
17
+ import { requirePCA, defaultExtract, memoryPcaStore } from '@atlasauth/backend';
18
+ import type { PcaGuardResult, RequirePcaOptions, PcaStateStore } from '@atlasauth/backend';
19
+ import type { PCActn } from '@atlasauth/pca';
20
+ /** Structural shim of an Express request (only `headers`/`body` are read; the rest passes through). */
21
+ export interface ExpressRequestLike {
22
+ headers: Record<string, string | string[] | undefined>;
23
+ body?: unknown;
24
+ [k: string]: unknown;
25
+ }
26
+ /** Structural shim of an Express response (the chainable subset the middleware uses). */
27
+ export interface ExpressResponseLike {
28
+ status(code: number): ExpressResponseLike;
29
+ set(field: string, value: string): ExpressResponseLike;
30
+ json(body: unknown): unknown;
31
+ }
32
+ export type ExpressNextLike = (err?: unknown) => void;
33
+ /** The verdict + PCActn attached to the request on success. */
34
+ export interface PcaAttachment {
35
+ verdict: Extract<PcaGuardResult, {
36
+ ok: true;
37
+ }>['verdict'];
38
+ pcactn: PCActn;
39
+ }
40
+ export interface PcaExpressOptions extends RequirePcaOptions {
41
+ /** Property on `req` to attach `{ verdict, pcactn }` to (default `pca`). */
42
+ attachAs?: string;
43
+ /** Override the 401/403 response (otherwise a JSON error + WWW-Authenticate is sent). */
44
+ onDeny?: (result: Extract<PcaGuardResult, {
45
+ ok: false;
46
+ }>, req: ExpressRequestLike, res: ExpressResponseLike) => void;
47
+ }
48
+ /** Build an Express middleware that requires a valid PCActn on the request. */
49
+ export declare function pcaExpress(options: PcaExpressOptions): (req: ExpressRequestLike, res: ExpressResponseLike, next: ExpressNextLike) => Promise<void>;
50
+ export { requirePCA, defaultExtract, memoryPcaStore };
51
+ export type { PcaGuardResult, RequirePcaOptions, PcaStateStore };
package/dist/index.js ADDED
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ /**
3
+ * Express middleware for Proof-Carrying Authority.
4
+ *
5
+ * app.post('/refunds',
6
+ * pcaExpress({ audience: 'ins_acme', resolveGrant, budgetStore, hooks, context }),
7
+ * (req, res) => { const { verdict, pcactn } = req.pca!; ... });
8
+ *
9
+ * It verifies the inbound PCActn with the framework-agnostic `requirePCA` guard from `@atlasauth/backend`
10
+ * (default-deny: a required check that is unenforced DENIES) and, on success, attaches the verdict +
11
+ * PCActn to `req[attachAs]` (default `req.pca`). On failure it answers 401/403 with a WWW-Authenticate
12
+ * challenge. The client sends the PCActn as `PCA-Action: <base64url>` (see `@atlasauth/pca` `pcaHeaders`)
13
+ * or a JSON body `{ pcactn }`; that is exactly what the default extractor reads, so no wiring is needed.
14
+ *
15
+ * `express` is an OPTIONAL peer dependency and is never imported — the request/response/next shapes are
16
+ * structural, so this stays compatible across Express 4 and 5.
17
+ */
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.memoryPcaStore = exports.defaultExtract = exports.requirePCA = void 0;
20
+ exports.pcaExpress = pcaExpress;
21
+ const backend_1 = require("@atlasauth/backend");
22
+ Object.defineProperty(exports, "requirePCA", { enumerable: true, get: function () { return backend_1.requirePCA; } });
23
+ Object.defineProperty(exports, "defaultExtract", { enumerable: true, get: function () { return backend_1.defaultExtract; } });
24
+ Object.defineProperty(exports, "memoryPcaStore", { enumerable: true, get: function () { return backend_1.memoryPcaStore; } });
25
+ /** Build an Express middleware that requires a valid PCActn on the request. */
26
+ function pcaExpress(options) {
27
+ const attachAs = options.attachAs ?? 'pca';
28
+ const onDeny = options.onDeny;
29
+ const guard = (0, backend_1.requirePCA)(options);
30
+ return async (req, res, next) => {
31
+ let result;
32
+ try {
33
+ result = await guard({ headers: req.headers, body: req.body });
34
+ }
35
+ catch (e) {
36
+ next(e);
37
+ return;
38
+ }
39
+ if (result.ok) {
40
+ req[attachAs] = { verdict: result.verdict, pcactn: result.pcactn };
41
+ next();
42
+ return;
43
+ }
44
+ if (onDeny) {
45
+ onDeny(result, req, res);
46
+ return;
47
+ }
48
+ res.status(result.status);
49
+ if (result.wwwAuthenticate)
50
+ res.set('WWW-Authenticate', result.wwwAuthenticate);
51
+ res.json({ error: result.verdict.reasons?.[0] ?? 'forbidden', verdict: result.verdict });
52
+ };
53
+ }
54
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;GAeG;;;AAoCH,gCAyBC;AA3DD,gDAAgF;AA6DvE,2FA7DA,oBAAU,OA6DA;AAAE,+FA7DA,wBAAc,OA6DA;AAAE,+FA7DA,wBAAc,OA6DA;AA5BnD,+EAA+E;AAC/E,SAAgB,UAAU,CAAC,OAA0B;IACnD,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,KAAK,CAAC;IAC3C,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAC9B,MAAM,KAAK,GAAG,IAAA,oBAAU,EAAC,OAAO,CAAC,CAAC;IAClC,OAAO,KAAK,EAAE,GAAuB,EAAE,GAAwB,EAAE,IAAqB,EAAiB,EAAE;QACvG,IAAI,MAAsB,CAAC;QAC3B,IAAI,CAAC;YACH,MAAM,GAAG,MAAM,KAAK,CAAC,EAAE,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,IAAI,EAAE,GAAG,CAAC,IAAI,EAAoB,CAAC,CAAC;QACnF,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,IAAI,CAAC,CAAC,CAAC,CAAC;YACR,OAAO;QACT,CAAC;QACD,IAAI,MAAM,CAAC,EAAE,EAAE,CAAC;YACb,GAA+B,CAAC,QAAQ,CAAC,GAAG,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC;YAChG,IAAI,EAAE,CAAC;YACP,OAAO;QACT,CAAC;QACD,IAAI,MAAM,EAAE,CAAC;YACX,MAAM,CAAC,MAAM,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;YACzB,OAAO;QACT,CAAC;QACD,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QAC1B,IAAI,MAAM,CAAC,eAAe;YAAE,GAAG,CAAC,GAAG,CAAC,kBAAkB,EAAE,MAAM,CAAC,eAAe,CAAC,CAAC;QAChF,GAAG,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,IAAI,WAAW,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC;IAC3F,CAAC,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,42 @@
1
1
  {
2
2
  "name": "@atlasauth/pca-express",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "Express middleware for Proof-Carrying Authority: requirePCA() verifies the inbound PCActn and attaches the verdict, or answers 401/403 with a WWW-Authenticate challenge.",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "default": "./dist/index.js"
11
+ },
12
+ "./package.json": "./package.json"
13
+ },
14
+ "dependencies": {
15
+ "@atlasauth/pca": "0.1.0",
16
+ "@atlasauth/backend": "0.3.0"
17
+ },
18
+ "peerDependencies": {
19
+ "express": "^4 || ^5"
20
+ },
21
+ "peerDependenciesMeta": {
22
+ "express": {
23
+ "optional": true
24
+ }
25
+ },
26
+ "devDependencies": {
27
+ "typescript": "^5.6.3",
28
+ "vitest": "^2.1.4"
29
+ },
30
+ "files": [
31
+ "dist",
32
+ "README.md"
33
+ ],
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
37
+ "scripts": {
38
+ "build": "tsc -p tsconfig.build.json",
39
+ "typecheck": "tsc -p tsconfig.json --noEmit",
40
+ "test": "vitest run"
41
+ }
6
42
  }