@flashyid/sdk 0.1.1
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/README.md +171 -0
- package/dist/cjs/enforce/gate.js +81 -0
- package/dist/cjs/grants/fromCharter.js +36 -0
- package/dist/cjs/grants/grants.js +227 -0
- package/dist/cjs/grants/index.js +37 -0
- package/dist/cjs/grants/subject.js +107 -0
- package/dist/cjs/grants/types.js +24 -0
- package/dist/cjs/index.js +49 -0
- package/dist/cjs/init/charterGrants.js +83 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/sdk/index.js +27 -0
- package/dist/cjs/sdk/sign.js +22 -0
- package/dist/cjs/sdk/verify.js +138 -0
- package/dist/enforce/gate.d.ts +88 -0
- package/dist/enforce/gate.js +74 -0
- package/dist/grants/fromCharter.d.ts +43 -0
- package/dist/grants/fromCharter.js +33 -0
- package/dist/grants/grants.d.ts +93 -0
- package/dist/grants/grants.js +220 -0
- package/dist/grants/index.d.ts +16 -0
- package/dist/grants/index.js +14 -0
- package/dist/grants/subject.d.ts +78 -0
- package/dist/grants/subject.js +99 -0
- package/dist/grants/types.d.ts +83 -0
- package/dist/grants/types.js +20 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +18 -0
- package/dist/init/charterGrants.d.ts +96 -0
- package/dist/init/charterGrants.js +78 -0
- package/dist/init/cli.d.ts +23 -0
- package/dist/init/cli.js +102 -0
- package/dist/sdk/index.d.ts +16 -0
- package/dist/sdk/index.js +13 -0
- package/dist/sdk/sign.d.ts +42 -0
- package/dist/sdk/sign.js +19 -0
- package/dist/sdk/verify.d.ts +85 -0
- package/dist/sdk/verify.js +134 -0
- package/docs/enforcement-gate.md +56 -0
- package/package.json +63 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://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 2026 Flashy Group
|
|
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
|
+
http://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/README.md
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
# @flashyid/sdk
|
|
2
|
+
|
|
3
|
+
Verify a Flashy ID assertion and the delegated authority it carries — from one
|
|
4
|
+
import, holding no secret. A relying party asks two questions of an assertion,
|
|
5
|
+
and this answers both:
|
|
6
|
+
|
|
7
|
+
1. **Is it genuine?** — the signature against Flashy ID's published JWKS
|
|
8
|
+
(EdDSA), the issuer, the audience, the expiry.
|
|
9
|
+
2. **Does its delegation permit *this* action?** — the grant chain the assertion
|
|
10
|
+
carries, verified and checked against what the action needs.
|
|
11
|
+
|
|
12
|
+
The verify path fetches only the **public** JWKS, so a relying party can never
|
|
13
|
+
mint an assertion — only check one. That is the whole point of the asymmetric
|
|
14
|
+
design.
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
npm install @flashyid/sdk
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Node 18+, ESM. One runtime dependency (`jose`).
|
|
23
|
+
|
|
24
|
+
## Authorize a request
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { authorize } from '@flashyid/sdk';
|
|
28
|
+
|
|
29
|
+
const out = await authorize(jws, { scope: 'payment.execute', amount: 1240 }, {
|
|
30
|
+
issuer: 'https://id.flashyid.com',
|
|
31
|
+
audience: yourClientId,
|
|
32
|
+
nowSec: Math.floor(Date.now() / 1000),
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
if (out === null) {
|
|
36
|
+
// the assertion is not genuinely from Flashy ID → 401
|
|
37
|
+
} else if (out.result.ok) {
|
|
38
|
+
// authorized — out.result.scp / .lim / .exp
|
|
39
|
+
} else {
|
|
40
|
+
// genuine but refused → 403; out.result.code says why
|
|
41
|
+
// 'out_of_mandate' | 'approval_required' | 'untrusted_root' | 'expired' | 'revoked' | …
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Sign in with Flashy ID, across the mesh
|
|
46
|
+
|
|
47
|
+
The example above is single-issuer: it trusts any chain a genuine Flashy ID
|
|
48
|
+
assertion carries. On the mesh that is not enough. An agent from org A signs in
|
|
49
|
+
at org B; org B verifies the assertion is real, but a real assertion can carry a
|
|
50
|
+
chain rooted at *any* accountable human anywhere — so "verified" would quietly
|
|
51
|
+
mean "belongs to someone", not "belongs to someone we trust".
|
|
52
|
+
|
|
53
|
+
Pin the roots you accept. Every AAO charter names the one human all of its
|
|
54
|
+
chains root at (`accountableTo`), so a relying party derives the set from the
|
|
55
|
+
charters it federates with:
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
import { authorize, trustedRootsFromCharters } from '@flashyid/sdk';
|
|
59
|
+
|
|
60
|
+
const trustedRoots = trustedRootsFromCharters([ourCharter, partnerCharter]);
|
|
61
|
+
|
|
62
|
+
const out = await authorize(jws, demand, {
|
|
63
|
+
issuer: 'https://id.flashyid.com',
|
|
64
|
+
audience: yourClientId,
|
|
65
|
+
nowSec: Math.floor(Date.now() / 1000),
|
|
66
|
+
trustedRoots, // a sound chain rooted elsewhere is refused with `untrusted_root`
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Omit `trustedRoots` and behaviour is unchanged — any root is accepted, which is
|
|
71
|
+
right for a single issuer and wrong for a mesh. The check runs before the
|
|
72
|
+
mandate check, so an untrusted root is never masked as a missing scope. The
|
|
73
|
+
underlying `rootsWithin(effective, roots)` is exported for a party that verifies
|
|
74
|
+
a chain itself.
|
|
75
|
+
|
|
76
|
+
## What's in it
|
|
77
|
+
|
|
78
|
+
- **`signAssertion(input)`** — the issuer half: mint an EdDSA assertion (JWS)
|
|
79
|
+
carrying a subject and a delegation chain. The private key never leaves the
|
|
80
|
+
issuer; anything it signs, `verifyAssertion` accepts.
|
|
81
|
+
- **`verifyAssertion(token, opts)`** — verify signature + iss/aud/exp; returns
|
|
82
|
+
the identity and any delegation, or `null`. Never throws.
|
|
83
|
+
- **`authorize(token, demand, opts)`** — verify the assertion, then verify the
|
|
84
|
+
delegation authorizes a specific action.
|
|
85
|
+
- **The grant kernel** — `issueRoot`, `attenuate`, `verifyChain`, `permits`.
|
|
86
|
+
Pure functions over a delegation chain; the one rule is that a chain only ever
|
|
87
|
+
narrows (three invariants: never widens, effective expiry is the minimum
|
|
88
|
+
across the chain, revocation walks down).
|
|
89
|
+
- **Charter → grants** — `grantsFromCharter`, `grantFromCharterRole`. An AAO
|
|
90
|
+
chartered role already carries the fields a grant needs
|
|
91
|
+
(`capabilities → scp`, `worksIn → res`, `humanApprovalAtOrAbove → lim`,
|
|
92
|
+
`accountableTo → root`), so no new policy language.
|
|
93
|
+
|
|
94
|
+
## The enforcement gate
|
|
95
|
+
|
|
96
|
+
The kernel answers "does the grant permit this?"; the gate maps that answer
|
|
97
|
+
onto the three outcomes a caller can act on — `ALLOW`, `ESCALATE` (permitted,
|
|
98
|
+
but at or above the chain's human-approval bar a named human must co-sign), and
|
|
99
|
+
`DENY` (with the kernel's refusal code, so the "why" is auditable).
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
import { evaluateGrant, enforce, grantAdapter, recordOnly } from '@flashyid/sdk';
|
|
103
|
+
|
|
104
|
+
// Pure decision — no clock, no I/O. Same answer for issuer, RP, and auditor.
|
|
105
|
+
const r = evaluateGrant(chain, { scope: 'seal', impact: 'CRITICAL' }, { nowSec });
|
|
106
|
+
// → { action: 'ESCALATE', escalateAtOrAbove: 'HIGH', ... }
|
|
107
|
+
|
|
108
|
+
// Operational wrapper: adapter within a timeout, record-only fallback + alert.
|
|
109
|
+
const decision = await enforce({ chain, demand, nowSec }, grantAdapter(), {
|
|
110
|
+
onUnavailable: (err) => alertOps(err),
|
|
111
|
+
});
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
- **`evaluateGrant(chain, demand, opts)`** — the pure core.
|
|
115
|
+
- **`grantAdapter()`** — an adapter backed by `evaluateGrant` (the real gate).
|
|
116
|
+
- **`recordOnly`** — the default adapter: always `ALLOW`, never block.
|
|
117
|
+
- **`enforce(decision, adapter, opts)`** — run an adapter with a timeout and
|
|
118
|
+
fall back to record-only (raising `onUnavailable`) when it is unreachable.
|
|
119
|
+
|
|
120
|
+
The approval bar is enforced in `permits` itself: a demand whose `impact` is at
|
|
121
|
+
or above the chain's effective `approval_at_or_above` refuses with
|
|
122
|
+
`approval_required`, so `authorize` never silently allows an action a human was
|
|
123
|
+
meant to see. The gate is the one caller that turns that specific refusal into
|
|
124
|
+
`ESCALATE`. Full contract: [docs/enforcement-gate.md](docs/enforcement-gate.md).
|
|
125
|
+
|
|
126
|
+
## The `flashyid init` command
|
|
127
|
+
|
|
128
|
+
Read an organization's charter and issue the root grants its roles hold — the
|
|
129
|
+
charter is already the grant, so this is a file read and a mapping, not a
|
|
130
|
+
policy language to learn.
|
|
131
|
+
|
|
132
|
+
```sh
|
|
133
|
+
npx flashyid init # reads ./flashyos.roles.json, prints a summary
|
|
134
|
+
npx flashyid init my-charter.json # a specific charter file
|
|
135
|
+
npx flashyid init --json # emit the grants as JSON
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Flags: a positional file (or `--file`/`-f`), and `--json`. Exits non-zero when
|
|
139
|
+
the file is missing/malformed or any role's capability maps to no registered
|
|
140
|
+
scope — an init that would silently drop authority fails loudly instead.
|
|
141
|
+
|
|
142
|
+
## Refusal codes
|
|
143
|
+
|
|
144
|
+
A refusal always carries a specific reason — it is part of the published
|
|
145
|
+
contract, for relying parties and auditors alike.
|
|
146
|
+
|
|
147
|
+
| Code | Meaning |
|
|
148
|
+
|---|---|
|
|
149
|
+
| `chain_widened` | a link grants more scope, resource, or limit than its parent |
|
|
150
|
+
| `broken_chain` | a link's issuer is not the holder above it, the chain's leaf holder is not the assertion's subject, or the `del` claim is malformed |
|
|
151
|
+
| `expired` | a link is past its expiry at the evaluated time |
|
|
152
|
+
| `revoked` | a link (or one above it) has been revoked |
|
|
153
|
+
| `out_of_mandate` | the action exceeds the chain's effective scope, resource, or spend limit |
|
|
154
|
+
| `approval_required` | in mandate, but at/above the human-approval bar — a named human must co-sign (the gate maps this to `ESCALATE`) |
|
|
155
|
+
| `scope_unmapped` | a charter capability maps to no registered scope |
|
|
156
|
+
| `untrusted_root` | the chain is sound but roots at a principal this relying party has not agreed to accept (only when `trustedRoots` is set) |
|
|
157
|
+
| `empty_chain` | no links to evaluate |
|
|
158
|
+
|
|
159
|
+
## Design line
|
|
160
|
+
|
|
161
|
+
`contract.sign` is deliberately absent from the vocabulary. An agent moves value
|
|
162
|
+
within a mandate; a **named human signs** what binds the company. The kernel
|
|
163
|
+
enforces the mandate — it never issues authority to sign.
|
|
164
|
+
|
|
165
|
+
## Standards
|
|
166
|
+
|
|
167
|
+
OIDC ID tokens (EdDSA / JWS) for the assertion; a delegation-chain profile
|
|
168
|
+
(Flashy ID spec v0.3) for the authority. Verification is pure and deterministic
|
|
169
|
+
— the same code runs in a relying party and in a stranger's audit script.
|
|
170
|
+
|
|
171
|
+
Spec and registries: <https://flashyid.com/spec>.
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.recordOnly = void 0;
|
|
4
|
+
exports.evaluateGrant = evaluateGrant;
|
|
5
|
+
exports.grantAdapter = grantAdapter;
|
|
6
|
+
exports.enforce = enforce;
|
|
7
|
+
exports.withTimeout = withTimeout;
|
|
8
|
+
const index_js_1 = require("../grants/index.js");
|
|
9
|
+
/**
|
|
10
|
+
* The gate decision for a signed grant chain against a demanded action. Pure.
|
|
11
|
+
*
|
|
12
|
+
* The human-approval bar is enforced inside `permits` itself (refusal code
|
|
13
|
+
* `approval_required`), so no call path can silently allow a CRITICAL action a
|
|
14
|
+
* human was meant to see. The gate is the one caller that can route to a
|
|
15
|
+
* human, so it maps that specific refusal — and only that one — to ESCALATE:
|
|
16
|
+
* the action *is* within mandate, it just needs a named human's co-signature.
|
|
17
|
+
* Every other refusal (unsound chain, out of mandate) stays a DENY.
|
|
18
|
+
*/
|
|
19
|
+
function evaluateGrant(chain, demand, opts) {
|
|
20
|
+
const verified = (0, index_js_1.verifyChain)(chain, opts.nowSec, opts.revokedJtis ?? new Set());
|
|
21
|
+
if (!verified.ok) {
|
|
22
|
+
return { action: 'DENY', code: verified.code, reason: verified.detail ?? verified.code };
|
|
23
|
+
}
|
|
24
|
+
const permitted = (0, index_js_1.permits)(verified, demand);
|
|
25
|
+
if (permitted !== true) {
|
|
26
|
+
if (permitted.code === 'approval_required') {
|
|
27
|
+
// Permitted, but at or above the bar a named human must co-sign —
|
|
28
|
+
// escalate rather than deny (or silently allow).
|
|
29
|
+
return {
|
|
30
|
+
action: 'ESCALATE',
|
|
31
|
+
escalateAtOrAbove: verified.lim.approval_at_or_above,
|
|
32
|
+
reason: permitted.detail ?? permitted.code,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
return { action: 'DENY', code: permitted.code, reason: permitted.detail ?? permitted.code };
|
|
36
|
+
}
|
|
37
|
+
return { action: 'ALLOW' };
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The default: record only, never block. An org with no adapter configured, or
|
|
41
|
+
* whose adapter is unreachable, must not have its agents halted — the mesh is
|
|
42
|
+
* an audit trail, not a single point of failure. Always ALLOW.
|
|
43
|
+
*/
|
|
44
|
+
exports.recordOnly = {
|
|
45
|
+
evaluate: () => Promise.resolve({ action: 'ALLOW', reason: 'record-only: no enforcement adapter' }),
|
|
46
|
+
};
|
|
47
|
+
/** An adapter backed by the grant kernel — the real gate. */
|
|
48
|
+
function grantAdapter() {
|
|
49
|
+
return {
|
|
50
|
+
evaluate: (d) => Promise.resolve(evaluateGrant(d.chain, d.demand, { nowSec: d.nowSec, revokedJtis: d.revokedJtis })),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Run a decision through an adapter with the operational contract: await the
|
|
55
|
+
* adapter up to a timeout; if it throws or times out, fall back to record-only
|
|
56
|
+
* ALLOW and raise `onUnavailable`. Preserves "never block" for everyone, while
|
|
57
|
+
* giving an adapter-configured org a real gate.
|
|
58
|
+
*/
|
|
59
|
+
async function enforce(decision, adapter, opts = {}) {
|
|
60
|
+
const timeoutMs = opts.timeoutMs ?? 5000;
|
|
61
|
+
try {
|
|
62
|
+
return await withTimeout(adapter.evaluate(decision), timeoutMs);
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
opts.onUnavailable?.(error);
|
|
66
|
+
return { action: 'ALLOW', reason: 'enforcement adapter unavailable — recorded only' };
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/** Reject if the promise does not settle within `ms`. */
|
|
70
|
+
function withTimeout(promise, ms) {
|
|
71
|
+
return new Promise((resolve, reject) => {
|
|
72
|
+
const timer = setTimeout(() => reject(new Error(`enforcement adapter timed out after ${ms}ms`)), ms);
|
|
73
|
+
promise.then((value) => {
|
|
74
|
+
clearTimeout(timer);
|
|
75
|
+
resolve(value);
|
|
76
|
+
}, (error) => {
|
|
77
|
+
clearTimeout(timer);
|
|
78
|
+
reject(error instanceof Error ? error : new Error(String(error)));
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.grantFromCharterRole = grantFromCharterRole;
|
|
4
|
+
const grants_js_1 = require("./grants.js");
|
|
5
|
+
/** Map a chartered role to a root grant, or refuse if a capability is unmapped. */
|
|
6
|
+
function grantFromCharterRole(input) {
|
|
7
|
+
const { role, scopeRegistry } = input;
|
|
8
|
+
let scp;
|
|
9
|
+
if (scopeRegistry) {
|
|
10
|
+
const mapped = [];
|
|
11
|
+
for (const cap of role.capabilities) {
|
|
12
|
+
const scope = scopeRegistry[cap];
|
|
13
|
+
if (scope === undefined) {
|
|
14
|
+
return { ok: false, code: 'scope_unmapped', detail: `capability "${cap}" maps to no registered scope` };
|
|
15
|
+
}
|
|
16
|
+
mapped.push(scope);
|
|
17
|
+
}
|
|
18
|
+
scp = [...new Set(mapped)];
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
scp = [...new Set(role.capabilities)];
|
|
22
|
+
}
|
|
23
|
+
const lim = {};
|
|
24
|
+
if (role.humanApprovalAtOrAbove !== undefined)
|
|
25
|
+
lim.approval_at_or_above = role.humanApprovalAtOrAbove;
|
|
26
|
+
return (0, grants_js_1.issueRoot)({
|
|
27
|
+
rootHuman: input.accountableTo,
|
|
28
|
+
holder: input.holder,
|
|
29
|
+
scp,
|
|
30
|
+
res: role.worksIn ? [...role.worksIn] : [],
|
|
31
|
+
lim,
|
|
32
|
+
iat: input.iat,
|
|
33
|
+
exp: input.exp,
|
|
34
|
+
jti: input.jti,
|
|
35
|
+
});
|
|
36
|
+
}
|