open-action-consent 0.0.0-stage → 0.2.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/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +23 -2
- package/dist/conformance.d.ts +5 -0
- package/dist/conformance.js +81 -0
- package/dist/consent.d.ts +39 -0
- package/dist/consent.js +95 -0
- package/dist/discovery.d.ts +5 -0
- package/dist/discovery.js +44 -0
- package/dist/errors.d.ts +7 -0
- package/dist/errors.js +28 -0
- package/dist/gate.d.ts +48 -0
- package/dist/gate.js +143 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +13 -0
- package/dist/jcs.d.ts +8 -0
- package/dist/jcs.js +177 -0
- package/dist/jws.d.ts +39 -0
- package/dist/jws.js +175 -0
- package/dist/mandate.d.ts +24 -0
- package/dist/mandate.js +130 -0
- package/dist/mcp/index.d.ts +57 -0
- package/dist/mcp/index.js +158 -0
- package/dist/preview.d.ts +15 -0
- package/dist/preview.js +60 -0
- package/dist/receipts.d.ts +22 -0
- package/dist/receipts.js +73 -0
- package/dist/shapes.d.ts +24 -0
- package/dist/shapes.js +218 -0
- package/dist/status.d.ts +21 -0
- package/dist/status.js +91 -0
- package/dist/testing.d.ts +2 -0
- package/dist/testing.js +8 -0
- package/dist/tiers.d.ts +7 -0
- package/dist/tiers.js +28 -0
- package/dist/undo.d.ts +28 -0
- package/dist/undo.js +55 -0
- package/dist/vercel-ai/index.d.ts +27 -0
- package/dist/vercel-ai/index.js +51 -0
- package/package.json +72 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
https://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
https://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,24 @@
|
|
|
1
|
-
#
|
|
1
|
+
# open-action-consent (JavaScript / TypeScript)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**A verifiable "yes" for things AI agents do for people.** Preview → signed Consent → the action runs only if it
|
|
4
|
+
checks out → signed Receipt with Undo. Zero runtime dependencies for the core.
|
|
5
|
+
Spec and source: <https://github.com/Ishan2036924/open-action-consent>
|
|
6
|
+
|
|
7
|
+
## Everything in this one package
|
|
8
|
+
|
|
9
|
+
| Part | Import | Also install | Guide |
|
|
10
|
+
|---|---|---|---|
|
|
11
|
+
| Core: previews, consents, mandates, receipts, undo, status lists, key discovery, `Gate` | `open-action-consent` | nothing | [README](https://github.com/Ishan2036924/open-action-consent#readme) |
|
|
12
|
+
| MCP servers (official MCP TypeScript SDK v2) | `open-action-consent/mcp` | `@modelcontextprotocol/server` | [guide](https://github.com/Ishan2036924/open-action-consent/blob/main/guides/mcp-typescript.md) |
|
|
13
|
+
| Vercel AI SDK 7 | `open-action-consent/vercel-ai` | `ai` | [guide](https://github.com/Ishan2036924/open-action-consent/blob/main/guides/vercel-ai.md) |
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
npm install open-action-consent
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { Gate, buildPreview, makeConsent, verifyConsent, KeySet } from "open-action-consent";
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Needs Node 22.14+ (or a modern bundler). Python, the Gated Browser and the approval page: `pip install
|
|
24
|
+
open-action-consent`. License: Apache-2.0 (code), CC BY 4.0 (spec text).
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// Conformance runner: `node src/conformance.ts <vectors dir>`. Runs every case in vectors/*.json and exits non-zero
|
|
2
|
+
// if any result differs from the expected one. Same files as the Python runner.
|
|
3
|
+
import * as fs from "node:fs";
|
|
4
|
+
import * as path from "node:path";
|
|
5
|
+
import { OACError } from "./errors.js";
|
|
6
|
+
import { canonical, parse } from "./jcs.js";
|
|
7
|
+
import { digest, KeySet, sign } from "./jws.js";
|
|
8
|
+
import * as shapes from "./shapes.js";
|
|
9
|
+
import { tierFor } from "./tiers.js";
|
|
10
|
+
import { checkBinding } from "./preview.js";
|
|
11
|
+
import { MemoryNonces, verifyConsent } from "./consent.js";
|
|
12
|
+
import { checkMandate, loosens } from "./mandate.js";
|
|
13
|
+
import { verifyChain } from "./receipts.js";
|
|
14
|
+
import { checkUndo } from "./undo.js";
|
|
15
|
+
import { vectorKey } from "./testing.js";
|
|
16
|
+
import { bit, decode } from "./status.js";
|
|
17
|
+
import { keysetFrom, metadataUrl } from "./discovery.js";
|
|
18
|
+
const OK = { ok: true };
|
|
19
|
+
const OPS = {
|
|
20
|
+
jcs: (i) => ({ canonical: canonical(i.value) }),
|
|
21
|
+
parse: (i) => ({ canonical: canonical(parse(i.text)) }),
|
|
22
|
+
digest: (i) => ({ digest: digest(i.value) }),
|
|
23
|
+
tier: (i) => ({ tier: tierFor(i.profile, { policyFloor: i.policy_floor, tainted: i.tainted, hint: i.hint }) }),
|
|
24
|
+
sign: (i) => ({ token: sign(i.payload, vectorKey(i.key, i.alg ?? "EdDSA"), i.typ) }),
|
|
25
|
+
profile: (i) => (shapes.profile(i.profile), OK),
|
|
26
|
+
binding: (i) => (checkBinding(i.preview, i.action, i.profile), OK),
|
|
27
|
+
verify_consent: (i) => (verifyConsent(i.token, {
|
|
28
|
+
keys: new KeySet(i.keys), executor: i.executor, sub: i.sub, previews: i.previews, actions: i.actions, profiles: i.profiles,
|
|
29
|
+
now: i.now, nonces: new MemoryNonces(i.seen_nonces ?? []), policyFloor: i.policy_floor, tainted: i.tainted ?? false,
|
|
30
|
+
}), OK),
|
|
31
|
+
check_mandate: (i) => (checkMandate(i.token, {
|
|
32
|
+
keys: new KeySet(i.keys), action: i.action, profile: i.profile, holder: i.holder, sub: i.sub, now: i.now,
|
|
33
|
+
tainted: i.tainted, usage: i.usage, revoked: i.revoked ?? [], statusLists: i.status_lists ?? {},
|
|
34
|
+
}), OK),
|
|
35
|
+
loosens: (i) => ({ reasons: loosens(i.old, i.new).sort() }),
|
|
36
|
+
verify_chain: (i) => (verifyChain(i.tokens, { keys: new KeySet(i.keys), executor: i.executor, sub: i.sub, seals: i.seals ?? [],
|
|
37
|
+
sealKeys: i.seal_keys ? new KeySet(i.seal_keys) : null }), OK),
|
|
38
|
+
check_undo: (i) => (checkUndo(i.token, {
|
|
39
|
+
keys: new KeySet(i.keys), executor: i.executor, receiptToken: i.receipt_token, receiptKeys: new KeySet(i.receipt_keys),
|
|
40
|
+
now: i.now, nonces: new MemoryNonces(i.seen_nonces ?? []), undone: i.undone ?? [], compensation: i.compensation,
|
|
41
|
+
}), OK),
|
|
42
|
+
status_bit: (i) => ({ revoked: bit(decode(i.encoded_list), i.index) }),
|
|
43
|
+
metadata_url: (i) => ({ url: metadataUrl(i.iss) }),
|
|
44
|
+
keyset_from: (i) => ({ issuer: keysetFrom(i.docs.map((d) => [d[0], d[1]])).issuer(i.kid) }),
|
|
45
|
+
};
|
|
46
|
+
export function runCase(c) {
|
|
47
|
+
try {
|
|
48
|
+
return OPS[c.op](c.input);
|
|
49
|
+
}
|
|
50
|
+
catch (e) {
|
|
51
|
+
if (e instanceof OACError)
|
|
52
|
+
return { error: e.code };
|
|
53
|
+
throw e;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
export function run(dir) {
|
|
57
|
+
let total = 0;
|
|
58
|
+
const failures = [];
|
|
59
|
+
for (const name of fs.readdirSync(dir).filter((f) => f.endsWith(".json")).sort()) {
|
|
60
|
+
for (const c of parse(fs.readFileSync(path.join(dir, name))).cases) { // strict parser, as for the wire
|
|
61
|
+
total += 1;
|
|
62
|
+
let got;
|
|
63
|
+
try {
|
|
64
|
+
got = runCase(c);
|
|
65
|
+
}
|
|
66
|
+
catch (e) {
|
|
67
|
+
got = { crash: String(e) };
|
|
68
|
+
}
|
|
69
|
+
if (canonical(got) !== canonical(c.expect))
|
|
70
|
+
failures.push(`${name} :: ${c.name}: expected ${JSON.stringify(c.expect)}, got ${JSON.stringify(got)}`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return { total, failures };
|
|
74
|
+
}
|
|
75
|
+
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
76
|
+
const { total, failures } = run(process.argv[2] ?? "vectors");
|
|
77
|
+
for (const f of failures)
|
|
78
|
+
console.log("FAIL", f);
|
|
79
|
+
console.log(`oac conformance (typescript): ${total - failures.length}/${total} passed`);
|
|
80
|
+
process.exit(failures.length ? 1 : 0);
|
|
81
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { KeySet, type SigningKey } from "./jws.ts";
|
|
2
|
+
export declare const TYP = "oac-consent+jws";
|
|
3
|
+
export declare const MAX_S = 900;
|
|
4
|
+
export declare const SKEW_S = 60;
|
|
5
|
+
/** Record jti until exp and return true, or return false if already recorded. Must be atomic (insert-if-absent). */
|
|
6
|
+
export interface NonceStore {
|
|
7
|
+
useOnce(jti: string, exp: number): boolean;
|
|
8
|
+
}
|
|
9
|
+
export declare class MemoryNonces implements NonceStore {
|
|
10
|
+
private s;
|
|
11
|
+
constructor(seen?: Iterable<string>);
|
|
12
|
+
useOnce(jti: string, exp: number): boolean;
|
|
13
|
+
}
|
|
14
|
+
export declare const newNonce: () => string;
|
|
15
|
+
export declare function makeConsent(o: {
|
|
16
|
+
key: SigningKey;
|
|
17
|
+
iss: string;
|
|
18
|
+
sub: string;
|
|
19
|
+
aud: string;
|
|
20
|
+
previews: Record<string, any>[];
|
|
21
|
+
now: number;
|
|
22
|
+
ttl?: number;
|
|
23
|
+
jti?: string;
|
|
24
|
+
signerKind?: string;
|
|
25
|
+
payment?: Record<string, any>;
|
|
26
|
+
}): string;
|
|
27
|
+
export declare function checkTime(p: Record<string, any>, now: number, maxS: number): void;
|
|
28
|
+
export declare function verifyConsent(token: string, o: {
|
|
29
|
+
keys: KeySet;
|
|
30
|
+
executor: string;
|
|
31
|
+
sub: string;
|
|
32
|
+
previews: Record<string, any>[];
|
|
33
|
+
actions: Record<string, any>[];
|
|
34
|
+
profiles: Record<string, Record<string, any>>;
|
|
35
|
+
now: number;
|
|
36
|
+
nonces: NonceStore;
|
|
37
|
+
policyFloor?: string;
|
|
38
|
+
tainted?: boolean;
|
|
39
|
+
}): Record<string, any>;
|
package/dist/consent.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// Consent (spec section 6): the person's signed yes to one or more Previews. Checks run in the spec's order; the
|
|
2
|
+
// first failure is the one reported. Mirrors consent.py.
|
|
3
|
+
import * as crypto from "node:crypto";
|
|
4
|
+
import * as shapes from "./shapes.js";
|
|
5
|
+
import { OACError } from "./errors.js";
|
|
6
|
+
import { digest, KeySet, sign, verify } from "./jws.js";
|
|
7
|
+
import { actionDigest, checkBinding } from "./preview.js";
|
|
8
|
+
import { tierFor } from "./tiers.js";
|
|
9
|
+
export const TYP = "oac-consent+jws";
|
|
10
|
+
export const MAX_S = 900;
|
|
11
|
+
export const SKEW_S = 60;
|
|
12
|
+
export class MemoryNonces {
|
|
13
|
+
s = new Map();
|
|
14
|
+
constructor(seen = []) { for (const j of seen)
|
|
15
|
+
this.s.set(j, 0); }
|
|
16
|
+
useOnce(jti, exp) {
|
|
17
|
+
if (this.s.has(jti))
|
|
18
|
+
return false;
|
|
19
|
+
this.s.set(jti, exp);
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export const newNonce = () => crypto.randomBytes(18).toString("base64url");
|
|
24
|
+
export function makeConsent(o) {
|
|
25
|
+
const payload = {
|
|
26
|
+
oac: shapes.VERSION, type: "consent", iss: o.iss, sub: o.sub, aud: o.aud, jti: o.jti ?? newNonce(), iat: o.now,
|
|
27
|
+
exp: o.now + (o.ttl ?? 300), items: o.previews.map((p) => ({ preview: digest(p), action: p.action })),
|
|
28
|
+
signer: { kind: o.signerKind ?? "custodial" },
|
|
29
|
+
};
|
|
30
|
+
if (o.payment)
|
|
31
|
+
payload.payment = { ...o.payment };
|
|
32
|
+
return sign(shapes.consent(payload), o.key, TYP);
|
|
33
|
+
}
|
|
34
|
+
export function checkTime(p, now, maxS) {
|
|
35
|
+
if (p.iat > now + SKEW_S)
|
|
36
|
+
throw new OACError("not_yet_valid");
|
|
37
|
+
if (now >= p.exp)
|
|
38
|
+
throw new OACError("expired");
|
|
39
|
+
if (p.exp - p.iat > maxS || p.exp <= p.iat)
|
|
40
|
+
throw new OACError("lifetime_too_long");
|
|
41
|
+
}
|
|
42
|
+
function payment(p, preview) {
|
|
43
|
+
const pay = p.payment;
|
|
44
|
+
if (pay === undefined)
|
|
45
|
+
throw new OACError("payment_missing", "a pay action needs consent.payment");
|
|
46
|
+
for (const name of ["amount_minor", "currency", "payee"]) {
|
|
47
|
+
if (preview.fields[name] !== pay[name])
|
|
48
|
+
throw new OACError("payment_mismatch", `payment ${name} differs from the preview`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export function verifyConsent(token, o) {
|
|
52
|
+
const p = verify(token, o.keys, TYP);
|
|
53
|
+
shapes.consent(p);
|
|
54
|
+
if (p.aud !== o.executor)
|
|
55
|
+
throw new OACError("wrong_audience");
|
|
56
|
+
if (p.sub !== o.sub)
|
|
57
|
+
throw new OACError("subject_mismatch", "the consent is another person's");
|
|
58
|
+
checkTime(p, o.now, MAX_S);
|
|
59
|
+
if (p.items.length !== o.previews.length || o.previews.length !== o.actions.length) {
|
|
60
|
+
throw new OACError("preview_mismatch", "one preview and one action per item");
|
|
61
|
+
}
|
|
62
|
+
let pays = 0;
|
|
63
|
+
p.items.forEach((item, i) => {
|
|
64
|
+
const preview = o.previews[i], action = o.actions[i];
|
|
65
|
+
shapes.preview(preview);
|
|
66
|
+
if (digest(preview) !== item.preview)
|
|
67
|
+
throw new OACError("preview_mismatch");
|
|
68
|
+
if (preview.action !== item.action || actionDigest(action) !== item.action)
|
|
69
|
+
throw new OACError("action_mismatch");
|
|
70
|
+
if (preview.sub !== p.sub)
|
|
71
|
+
throw new OACError("subject_mismatch");
|
|
72
|
+
if (preview.executor !== o.executor)
|
|
73
|
+
throw new OACError("executor_mismatch");
|
|
74
|
+
if (o.now >= preview.exp)
|
|
75
|
+
throw new OACError("preview_expired");
|
|
76
|
+
const profile = Object.hasOwn(o.profiles, action.tool) ? o.profiles[action.tool] : undefined;
|
|
77
|
+
if (profile === undefined)
|
|
78
|
+
throw new OACError("unknown_tool");
|
|
79
|
+
shapes.profile(profile);
|
|
80
|
+
checkBinding(preview, action, profile);
|
|
81
|
+
if (tierFor(profile, { policyFloor: o.policyFloor, tainted: o.tainted }) === "D")
|
|
82
|
+
throw new OACError("tier_d");
|
|
83
|
+
if (profile.effect === "pay") {
|
|
84
|
+
pays += 1;
|
|
85
|
+
if (pays > 1)
|
|
86
|
+
throw new OACError("malformed", "one payment per consent");
|
|
87
|
+
payment(p, preview);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
if (pays === 0 && Object.hasOwn(p, "payment"))
|
|
91
|
+
throw new OACError("payment_mismatch", "payment given but no item pays");
|
|
92
|
+
if (!o.nonces.useOnce(p.jti, p.exp))
|
|
93
|
+
throw new OACError("replay");
|
|
94
|
+
return p;
|
|
95
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { KeySet } from "./jws.ts";
|
|
2
|
+
export declare const MAX_DOC: number;
|
|
3
|
+
export declare function metadataUrl(iss: string): string;
|
|
4
|
+
export declare function keysetFrom(docs: [string, Record<string, any>][]): KeySet;
|
|
5
|
+
export declare function fetchMetadata(iss: string, get?: (url: string) => Promise<Uint8Array>): Promise<Record<string, any>>;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// Issuer metadata (spec section 3.5): for issuer https://host/path the document is at
|
|
2
|
+
// https://host/.well-known/oac-issuer/path (the RFC 8414 pattern) and its `issuer` must equal the issuer exactly.
|
|
3
|
+
// `keysetFrom` turns documents you already have into a KeySet; `fetchMetadata` is a small, strict HTTPS fetcher.
|
|
4
|
+
import * as shapes from "./shapes.js";
|
|
5
|
+
import { OACError } from "./errors.js";
|
|
6
|
+
import { parse } from "./jcs.js";
|
|
7
|
+
import { KeySet } from "./jws.js";
|
|
8
|
+
export const MAX_DOC = 64 * 1024;
|
|
9
|
+
// Matched as text, same pattern as discovery.py, so every language agrees (no URL parser normalizing ports or IDNs).
|
|
10
|
+
const LABEL = "[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?";
|
|
11
|
+
const ISSUER = new RegExp(`^https://(${LABEL}(\\.${LABEL})*)(:[0-9]{1,5})?((/(?!\\.\\.?(/|$))[A-Za-z0-9._~-]+)*)/?$`);
|
|
12
|
+
export function metadataUrl(iss) {
|
|
13
|
+
const m = typeof iss === "string" ? ISSUER.exec(iss) : null;
|
|
14
|
+
if (m === null)
|
|
15
|
+
throw new OACError("malformed", "an issuer is https://host[:port][/path] in lowercase ASCII, without query or fragment");
|
|
16
|
+
return `https://${m[1]}${m[5] ?? ""}/.well-known/oac-issuer${m[6]}`;
|
|
17
|
+
}
|
|
18
|
+
export function keysetFrom(docs) {
|
|
19
|
+
const grouped = {};
|
|
20
|
+
for (const [iss, doc] of docs) {
|
|
21
|
+
metadataUrl(iss);
|
|
22
|
+
shapes.issuerMetadata(doc);
|
|
23
|
+
if (doc.issuer !== iss)
|
|
24
|
+
throw new OACError("wrong_issuer", "the metadata names another issuer");
|
|
25
|
+
(grouped[iss] ??= []).push(...doc.keys);
|
|
26
|
+
}
|
|
27
|
+
return new KeySet(grouped);
|
|
28
|
+
}
|
|
29
|
+
export async function fetchMetadata(iss, get) {
|
|
30
|
+
const fetcher = get ?? (async (url) => {
|
|
31
|
+
const r = await fetch(url, { redirect: "error", headers: { accept: "application/json" }, signal: AbortSignal.timeout(5000) });
|
|
32
|
+
if (!r.ok)
|
|
33
|
+
throw new OACError("malformed", `issuer metadata: HTTP ${r.status}`);
|
|
34
|
+
const body = new Uint8Array(await r.arrayBuffer());
|
|
35
|
+
if (body.length > MAX_DOC)
|
|
36
|
+
throw new OACError("malformed", "issuer metadata too large");
|
|
37
|
+
return body;
|
|
38
|
+
});
|
|
39
|
+
const doc = parse(await fetcher(metadataUrl(iss)));
|
|
40
|
+
shapes.issuerMetadata(doc);
|
|
41
|
+
if (doc.issuer !== iss)
|
|
42
|
+
throw new OACError("wrong_issuer", "the metadata names another issuer");
|
|
43
|
+
return doc;
|
|
44
|
+
}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const CODES: Set<string>;
|
|
2
|
+
/** A refusal. `code` is one of CODES; `detail` is for logs, never for deciding anything. */
|
|
3
|
+
export declare class OACError extends Error {
|
|
4
|
+
readonly code: string;
|
|
5
|
+
readonly detail: string;
|
|
6
|
+
constructor(code: string, detail?: string);
|
|
7
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Error codes shared by every OAC implementation (spec section 9). A verifier reports exactly one code: the first
|
|
2
|
+
// check that fails, in the order the spec lists them.
|
|
3
|
+
export const CODES = new Set([
|
|
4
|
+
"malformed", "not_canonical", "bad_type", "bad_alg", "unknown_key", "bad_signature",
|
|
5
|
+
"wrong_audience", "not_yet_valid", "expired", "lifetime_too_long", "replay",
|
|
6
|
+
"preview_mismatch", "action_mismatch", "binding_mismatch", "subject_mismatch", "executor_mismatch",
|
|
7
|
+
"preview_expired", "unknown_tool", "tier_d", "payment_missing", "payment_mismatch",
|
|
8
|
+
"holder_mismatch", "tool_not_covered", "tainted", "pay_without_caps", "recipient_not_allowed",
|
|
9
|
+
"payee_not_allowed", "site_not_allowed", "currency_mismatch", "over_amount", "over_daily_amount", "over_calls",
|
|
10
|
+
"chain_broken", "chain_seq", "chain_issuer", "receipt_invalid", "receipt_mismatch", "not_reversible",
|
|
11
|
+
"undo_mode_mismatch", "undo_window_closed",
|
|
12
|
+
// added after the 2026-11-04 review
|
|
13
|
+
"wrong_issuer", "undeclared_argument", "revoked", "already_undone", "compensation_mismatch",
|
|
14
|
+
// 0.1 gap closers: status lists and seals
|
|
15
|
+
"status_unknown", "seal_mismatch",
|
|
16
|
+
]);
|
|
17
|
+
/** A refusal. `code` is one of CODES; `detail` is for logs, never for deciding anything. */
|
|
18
|
+
export class OACError extends Error {
|
|
19
|
+
code;
|
|
20
|
+
detail;
|
|
21
|
+
constructor(code, detail = "") {
|
|
22
|
+
if (!CODES.has(code))
|
|
23
|
+
throw new Error(`unknown OAC error code ${code}`);
|
|
24
|
+
super(detail ? `${code}: ${detail}` : code);
|
|
25
|
+
this.code = code;
|
|
26
|
+
this.detail = detail;
|
|
27
|
+
}
|
|
28
|
+
}
|
package/dist/gate.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { KeySet, type SigningKey } from "./jws.ts";
|
|
2
|
+
import { type NonceStore } from "./consent.ts";
|
|
3
|
+
export declare class NeedsConsent extends Error {
|
|
4
|
+
readonly preview: Record<string, any>;
|
|
5
|
+
constructor(preview: Record<string, any>);
|
|
6
|
+
}
|
|
7
|
+
export type ProfileSpec = {
|
|
8
|
+
effect: string;
|
|
9
|
+
undo?: string;
|
|
10
|
+
windowS?: number;
|
|
11
|
+
tierFloor?: string;
|
|
12
|
+
binds?: Record<string, string>;
|
|
13
|
+
otherArgs?: string[];
|
|
14
|
+
};
|
|
15
|
+
export declare class Gate {
|
|
16
|
+
readonly executor: string;
|
|
17
|
+
readonly key: SigningKey;
|
|
18
|
+
readonly signers: KeySet;
|
|
19
|
+
readonly nonces: NonceStore;
|
|
20
|
+
readonly policyFloor: string;
|
|
21
|
+
readonly profiles: Record<string, Record<string, any>>;
|
|
22
|
+
readonly pending: Map<string, Record<string, any>>;
|
|
23
|
+
readonly consents: Map<string, string>;
|
|
24
|
+
readonly receipts: Map<string, string[]>;
|
|
25
|
+
private readonly clock;
|
|
26
|
+
private readonly summarize;
|
|
27
|
+
constructor(o: {
|
|
28
|
+
executor: string;
|
|
29
|
+
key: SigningKey;
|
|
30
|
+
signers: KeySet;
|
|
31
|
+
nonces?: NonceStore;
|
|
32
|
+
policyFloor?: string;
|
|
33
|
+
summarize?: (a: Record<string, any>) => string;
|
|
34
|
+
clock?: () => number;
|
|
35
|
+
});
|
|
36
|
+
profile(tool: string, p: ProfileSpec): Record<string, any>;
|
|
37
|
+
private prof;
|
|
38
|
+
tier(tool: string, args: Record<string, unknown>, tainted?: boolean): string;
|
|
39
|
+
preview(tool: string, args: Record<string, unknown>, person: string, evidence?: Record<string, any>): Record<string, any>;
|
|
40
|
+
previewById(id: string): Record<string, any> | undefined;
|
|
41
|
+
acceptConsent(token: string): boolean;
|
|
42
|
+
private authorize;
|
|
43
|
+
run<T>(tool: string, args: Record<string, unknown>, fn: (args: Record<string, unknown>) => T | Promise<T>, o: {
|
|
44
|
+
person: string;
|
|
45
|
+
tainted?: boolean;
|
|
46
|
+
}): Promise<[T, string]>;
|
|
47
|
+
private receipt;
|
|
48
|
+
}
|