@boxxy/caller-proof-imp 0.0.0-stage → 0.1.3
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 +21 -0
- package/README.md +41 -2
- package/dist/imp-identity-caller-proof.d.ts +3 -0
- package/dist/index.d.ts +2 -0
- package/dist/test-utils/create-test-issuer.d.ts +13 -0
- package/dist/types.d.ts +6 -0
- package/package.json +43 -4
- package/src/imp-identity-caller-proof.ts +31 -0
- package/src/index.ts +2 -0
- package/src/test-utils/create-test-issuer.ts +64 -0
- package/src/types.ts +10 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 the boxxy authors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,42 @@
|
|
|
1
|
-
|
|
1
|
+
<div align="center">
|
|
2
|
+
<h1>@boxxy/caller-proof-imp</h1>
|
|
2
3
|
|
|
3
|
-
|
|
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>
|
|
8
|
+
|
|
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.
|
package/dist/index.d.ts
ADDED
|
@@ -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 {};
|
package/dist/types.d.ts
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@boxxy/caller-proof-imp",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.3",
|
|
4
|
+
"description": "boxxy's caller proof port on the broker's identity token",
|
|
5
|
+
"homepage": "https://github.com/zgeoff/boxxy/tree/main/adapters/caller-proof-imp#readme",
|
|
6
|
+
"bugs": "https://github.com/zgeoff/boxxy/issues",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/zgeoff/boxxy.git",
|
|
11
|
+
"directory": "adapters/caller-proof-imp"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"src",
|
|
15
|
+
"!src/**/*.test.ts",
|
|
16
|
+
"dist"
|
|
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
|
+
},
|
|
26
|
+
"publishConfig": {
|
|
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.3",
|
|
35
|
+
"@impz/client": "6.12.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@boxxy/dispatch": "0.1.3",
|
|
39
|
+
"tiny-invariant": "1.3.3"
|
|
40
|
+
},
|
|
41
|
+
"engines": {
|
|
42
|
+
"bun": ">=1.3",
|
|
43
|
+
"node": ">=24"
|
|
44
|
+
}
|
|
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,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
|
+
}
|