@boxxy/caller-proof-imp 0.0.1 → 0.1.4

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,7 +1,42 @@
1
- # @boxxy/caller-proof-imp
1
+ <div align="center">
2
+ <h1>@boxxy/caller-proof-imp</h1>
2
3
 
3
- This version, 0.0.1, is a placeholder. It holds no code. It reserves the name on npm so that the
4
- boxxy release can publish every real version from CI with provenance.
4
+ <p>
5
+ <a href="https://www.npmjs.com/package/@boxxy/caller-proof-imp"><img src="https://img.shields.io/npm/v/@boxxy/caller-proof-imp" alt="npm version"></a>
6
+ <a href="./LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue" alt="MIT license"></a>
7
+ </p>
5
8
 
6
- Install a version above 0.0.1. The source lives in [`adapters/caller-proof-imp`](https://github.com/zgeoff/boxxy/tree/main/adapters/caller-proof-imp)
7
- of the [boxxy repo](https://github.com/zgeoff/boxxy).
9
+ <p>
10
+ <a href="../../docs/design/boxxy/README.md">Documentation</a>
11
+ </p>
12
+ </div>
13
+
14
+ boxxy's `CallerProof` port on the imp broker's identity token: it proves which machine sent a
15
+ session's call to the boxxy server.
16
+
17
+ ## Install
18
+
19
+ Install the package from [npm](https://www.npmjs.com/package/@boxxy/caller-proof-imp) with Bun:
20
+
21
+ ```bash
22
+ bun add @boxxy/caller-proof-imp
23
+ ```
24
+
25
+ The package runs on [Bun](https://bun.sh) 1.3 or newer.
26
+
27
+ ## Features
28
+
29
+ - `impIdentityCallerProof({ issuers, audience, clock })` returns a `CallerProof`. The app passes the
30
+ trust documents as parsed JSON, the audience that the broker signs for, and the clock.
31
+ - The proof reads the `imp-identity` header and verifies it with `@impz/client/identity`. It gives
32
+ the token's issuer, and the machine ID as the subject.
33
+ - A missing, expired or forged token, a wrong audience or an unknown issuer proves no caller. With
34
+ no trust document, the proof proves no request.
35
+ - A bad trust document throws when the app builds the proof, so the server does not start.
36
+ - `./test-utils/create-test-issuer`, an issuer with a key made at run time that signs tokens as the
37
+ broker does, for tests.
38
+
39
+ ## Documentation
40
+
41
+ [The proven caller](../../docs/design/boxxy/v1-api-and-auth.md#the-proven-caller) describes how the
42
+ server uses the proof, and why the adapter makes no session check of its own.
@@ -0,0 +1,3 @@
1
+ import type { CallerProof } from '@boxxy/model';
2
+ import type { ImpIdentityCallerProofOptions } from './types.ts';
3
+ export declare function impIdentityCallerProof(options: ImpIdentityCallerProofOptions): CallerProof;
@@ -0,0 +1,2 @@
1
+ export { impIdentityCallerProof } from './imp-identity-caller-proof.ts';
2
+ export type { ImpIdentityCallerProofOptions } from './types.ts';
@@ -0,0 +1,13 @@
1
+ interface TestTokenClaims {
2
+ readonly sub: string;
3
+ readonly aud: string;
4
+ readonly iat: number;
5
+ readonly exp: number;
6
+ readonly impName: string;
7
+ }
8
+ export interface TestIssuer {
9
+ readonly document: unknown;
10
+ readonly signToken: (claims: TestTokenClaims) => Promise<string>;
11
+ }
12
+ export declare function createTestIssuer(issuer: string): Promise<TestIssuer>;
13
+ export {};
@@ -0,0 +1,6 @@
1
+ import type { Clock } from '@boxxy/model';
2
+ export interface ImpIdentityCallerProofOptions {
3
+ readonly issuers: readonly unknown[];
4
+ readonly audience: string;
5
+ readonly clock: Clock;
6
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@boxxy/caller-proof-imp",
3
- "version": "0.0.1",
4
- "description": "Placeholder that reserves the name; the first real version comes from the boxxy release",
3
+ "version": "0.1.4",
4
+ "description": "boxxy's caller proof port on the broker's identity token",
5
5
  "homepage": "https://github.com/zgeoff/boxxy/tree/main/adapters/caller-proof-imp#readme",
6
6
  "bugs": "https://github.com/zgeoff/boxxy/issues",
7
7
  "license": "MIT",
@@ -11,10 +11,35 @@
11
11
  "directory": "adapters/caller-proof-imp"
12
12
  },
13
13
  "files": [
14
- "README.md",
15
- "LICENSE"
14
+ "src",
15
+ "!src/**/*.test.ts",
16
+ "dist"
16
17
  ],
18
+ "type": "module",
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "default": "./src/index.ts"
23
+ },
24
+ "./test-utils/*": "./src/test-utils/*.ts"
25
+ },
17
26
  "publishConfig": {
18
27
  "access": "public"
28
+ },
29
+ "scripts": {
30
+ "build": "rm -rf dist && tsrs -p tsconfig.build.json",
31
+ "typecheck": "tsrs --noEmit"
32
+ },
33
+ "dependencies": {
34
+ "@boxxy/model": "0.1.4",
35
+ "@impz/client": "6.12.0"
36
+ },
37
+ "devDependencies": {
38
+ "@boxxy/dispatch": "0.1.4",
39
+ "tiny-invariant": "1.3.3"
40
+ },
41
+ "engines": {
42
+ "bun": ">=1.3",
43
+ "node": ">=24"
19
44
  }
20
45
  }
@@ -0,0 +1,31 @@
1
+ import type { CallerProof, Clock } from '@boxxy/model';
2
+ import { loadIdentityIssuers, verifyImpIdentity } from '@impz/client/identity';
3
+ import type { ImpIdentityCallerProofOptions } from './types.ts';
4
+
5
+ const identityHeader = 'imp-identity';
6
+
7
+ // The proof from the broker's identity token. The trust documents are checked once, here, so a
8
+ // bad document stops the server at start. With no document, the proof proves no request.
9
+ export function impIdentityCallerProof(options: ImpIdentityCallerProofOptions): CallerProof {
10
+ if (options.issuers.length === 0 || options.audience === '') {
11
+ return { prove: () => Promise.resolve(null) };
12
+ }
13
+
14
+ const issuers = loadIdentityIssuers(options.issuers);
15
+
16
+ return {
17
+ prove: async (request) => {
18
+ const verified = await verifyImpIdentity(request.headers.get(identityHeader), {
19
+ issuers,
20
+ audience: options.audience,
21
+ nowSeconds: toNowSeconds(options.clock),
22
+ });
23
+
24
+ return verified === null ? null : { issuer: verified.issuer, subject: verified.id };
25
+ },
26
+ };
27
+ }
28
+
29
+ function toNowSeconds(clock: Clock): number {
30
+ return Math.floor(clock.now() / 1000);
31
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { impIdentityCallerProof } from './imp-identity-caller-proof.ts';
2
+ export type { ImpIdentityCallerProofOptions } from './types.ts';
@@ -0,0 +1,64 @@
1
+ interface TestTokenClaims {
2
+ readonly sub: string;
3
+ readonly aud: string;
4
+ readonly iat: number;
5
+ readonly exp: number;
6
+ readonly impName: string;
7
+ }
8
+
9
+ export interface TestIssuer {
10
+ // the trust document of the issuer, as the deployment's file holds it
11
+ readonly document: unknown;
12
+ readonly signToken: (claims: TestTokenClaims) => Promise<string>;
13
+ }
14
+
15
+ // An issuer with a key pair made at run time, which signs identity tokens as the broker does: a
16
+ // compact ES256 JWT whose signature is the raw 64-byte r and s.
17
+ export async function createTestIssuer(issuer: string): Promise<TestIssuer> {
18
+ const kid = `${issuer}-key-1`;
19
+
20
+ const keys = await crypto.subtle.generateKey({ name: 'ECDSA', namedCurve: 'P-256' }, true, [
21
+ 'sign',
22
+ 'verify',
23
+ ]);
24
+
25
+ const publicKey = await crypto.subtle.exportKey('jwk', keys.publicKey);
26
+
27
+ return {
28
+ document: {
29
+ issuer,
30
+ keys: [
31
+ { kty: 'EC', crv: 'P-256', x: publicKey.x, y: publicKey.y, kid, alg: 'ES256', use: 'sig' },
32
+ ],
33
+ retire: [],
34
+ },
35
+ signToken: async (claims) => {
36
+ const header = toBase64URL(JSON.stringify({ alg: 'ES256', kid, typ: 'JWT' }));
37
+
38
+ const payload = toBase64URL(
39
+ JSON.stringify({
40
+ iss: issuer,
41
+ sub: claims.sub,
42
+ aud: claims.aud,
43
+ iat: claims.iat,
44
+ exp: claims.exp,
45
+ imp_name: claims.impName,
46
+ }),
47
+ );
48
+
49
+ const signature = await crypto.subtle.sign(
50
+ { name: 'ECDSA', hash: 'SHA-256' },
51
+ keys.privateKey,
52
+ new TextEncoder().encode(`${header}.${payload}`),
53
+ );
54
+
55
+ return `${header}.${payload}.${toBase64URL(new Uint8Array(signature))}`;
56
+ },
57
+ };
58
+ }
59
+
60
+ function toBase64URL(content: string | Uint8Array): string {
61
+ const bytes = typeof content === 'string' ? new TextEncoder().encode(content) : content;
62
+
63
+ return Buffer.from(bytes).toString('base64url');
64
+ }
package/src/types.ts ADDED
@@ -0,0 +1,10 @@
1
+ import type { Clock } from '@boxxy/model';
2
+
3
+ export interface ImpIdentityCallerProofOptions {
4
+ // the trust documents as parsed JSON, one per issuer; the app reads the files
5
+ readonly issuers: readonly unknown[];
6
+
7
+ // the made-up host that the sessions call, which the broker names as the token's audience
8
+ readonly audience: string;
9
+ readonly clock: Clock;
10
+ }