@t3foundry/tasra-sdk 0.0.0-stage → 0.3.1-next.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/CHANGELOG.md +148 -0
- package/LICENSE +202 -0
- package/NOTICE +94 -0
- package/README.md +137 -2
- package/SECURITY.md +115 -0
- package/dist/app/approvals.d.ts +50 -0
- package/dist/app/approvals.js +90 -0
- package/dist/app/authorization.d.ts +19 -0
- package/dist/app/authorization.js +20 -0
- package/dist/app/client.d.ts +231 -0
- package/dist/app/client.js +272 -0
- package/dist/app/creation.d.ts +73 -0
- package/dist/app/creation.js +77 -0
- package/dist/app/deployment.d.ts +36 -0
- package/dist/app/deployment.js +18 -0
- package/dist/app/identity.d.ts +211 -0
- package/dist/app/identity.js +287 -0
- package/dist/app/index.d.ts +22 -0
- package/dist/app/index.js +12 -0
- package/dist/app/manifest.d.ts +74 -0
- package/dist/app/manifest.js +120 -0
- package/dist/app/node.d.ts +30 -0
- package/dist/app/node.js +159 -0
- package/dist/app/ready-slot.d.ts +157 -0
- package/dist/app/ready-slot.js +199 -0
- package/dist/app/tasra-client.d.ts +125 -0
- package/dist/app/tasra-client.js +79 -0
- package/dist/app/viem.d.ts +10 -0
- package/dist/app/viem.js +32 -0
- package/dist/app/wallet.d.ts +126 -0
- package/dist/app/wallet.js +137 -0
- package/dist/auth/dpop.d.ts +86 -0
- package/dist/auth/dpop.js +142 -0
- package/dist/auth/holderProof.d.ts +102 -0
- package/dist/auth/holderProof.js +161 -0
- package/dist/auth/identityScope.d.ts +19 -0
- package/dist/auth/identityScope.js +82 -0
- package/dist/auth/index.d.ts +11 -0
- package/dist/auth/index.js +13 -0
- package/dist/auth/oid4vp.d.ts +230 -0
- package/dist/auth/oid4vp.js +780 -0
- package/dist/auth/rego/wasm/PINS.json +11 -0
- package/dist/auth/rego/wasm/keykeeper_rego_wasm.d.ts +76 -0
- package/dist/auth/rego/wasm/keykeeper_rego_wasm.js +379 -0
- package/dist/auth/rego/wasm/keykeeper_rego_wasm_bg.wasm +0 -0
- package/dist/auth/rego.d.ts +192 -0
- package/dist/auth/rego.js +492 -0
- package/dist/auth/verifier.d.ts +166 -0
- package/dist/auth/verifier.js +211 -0
- package/dist/chain/abis/accountantSetRegistry.d.ts +441 -0
- package/dist/chain/abis/accountantSetRegistry.js +574 -0
- package/dist/chain/abis/accountantSlashing.d.ts +2071 -0
- package/dist/chain/abis/accountantSlashing.js +2670 -0
- package/dist/chain/abis/bondingCurve.d.ts +398 -0
- package/dist/chain/abis/bondingCurve.js +521 -0
- package/dist/chain/abis/equivocationSlasher.d.ts +292 -0
- package/dist/chain/abis/equivocationSlasher.js +379 -0
- package/dist/chain/abis/fixedTasraPriceOracle.d.ts +52 -0
- package/dist/chain/abis/fixedTasraPriceOracle.js +72 -0
- package/dist/chain/abis/index.d.ts +12801 -0
- package/dist/chain/abis/index.js +64 -0
- package/dist/chain/abis/keeperShareRegistry.d.ts +519 -0
- package/dist/chain/abis/keeperShareRegistry.js +675 -0
- package/dist/chain/abis/keyRegistry.d.ts +2421 -0
- package/dist/chain/abis/keyRegistry.js +3114 -0
- package/dist/chain/abis/livenessRegistry.d.ts +402 -0
- package/dist/chain/abis/livenessRegistry.js +524 -0
- package/dist/chain/abis/mockEurc.d.ts +210 -0
- package/dist/chain/abis/mockEurc.js +276 -0
- package/dist/chain/abis/nodeRegistry.d.ts +1630 -0
- package/dist/chain/abis/nodeRegistry.js +2113 -0
- package/dist/chain/abis/platformExecutor.d.ts +328 -0
- package/dist/chain/abis/platformExecutor.js +429 -0
- package/dist/chain/abis/prevrandaoSaltBeacon.d.ts +16 -0
- package/dist/chain/abis/prevrandaoSaltBeacon.js +24 -0
- package/dist/chain/abis/serviceRegistry.d.ts +556 -0
- package/dist/chain/abis/serviceRegistry.js +719 -0
- package/dist/chain/abis/settlement.d.ts +1624 -0
- package/dist/chain/abis/settlement.js +2096 -0
- package/dist/chain/abis/tasraSwapRouter.d.ts +92 -0
- package/dist/chain/abis/tasraSwapRouter.js +122 -0
- package/dist/chain/abis/tasraToken.d.ts +455 -0
- package/dist/chain/abis/tasraToken.js +599 -0
- package/dist/chain/abis/tasraVestingVault.d.ts +165 -0
- package/dist/chain/abis/tasraVestingVault.js +215 -0
- package/dist/chain/abis/thresholdRandomBeacon.d.ts +378 -0
- package/dist/chain/abis/thresholdRandomBeacon.js +494 -0
- package/dist/chain/abis/treasury.d.ts +301 -0
- package/dist/chain/abis/treasury.js +390 -0
- package/dist/chain/abis/verifierSetRegistry.d.ts +445 -0
- package/dist/chain/abis/verifierSetRegistry.js +579 -0
- package/dist/chain/client.d.ts +212 -0
- package/dist/chain/client.js +576 -0
- package/dist/chain/commitmentRecovery.d.ts +21 -0
- package/dist/chain/commitmentRecovery.js +44 -0
- package/dist/chain/committeeClient.d.ts +102 -0
- package/dist/chain/committeeClient.js +137 -0
- package/dist/chain/deployments.d.ts +56 -0
- package/dist/chain/deployments.js +269 -0
- package/dist/chain/discovery.d.ts +49 -0
- package/dist/chain/discovery.js +107 -0
- package/dist/chain/events.d.ts +58 -0
- package/dist/chain/events.js +125 -0
- package/dist/chain/format.d.ts +25 -0
- package/dist/chain/format.js +51 -0
- package/dist/chain/index.d.ts +41 -0
- package/dist/chain/index.js +56 -0
- package/dist/chain/manifest.d.ts +165 -0
- package/dist/chain/manifest.js +132 -0
- package/dist/chain/network-profiles.json +75 -0
- package/dist/chain/networks.d.ts +73 -0
- package/dist/chain/networks.js +62 -0
- package/dist/chain/node.d.ts +44 -0
- package/dist/chain/node.js +176 -0
- package/dist/chain/offchain.d.ts +149 -0
- package/dist/chain/offchain.js +73 -0
- package/dist/chain/provisionRule.d.ts +164 -0
- package/dist/chain/provisionRule.js +212 -0
- package/dist/chain/registeredAgent.d.ts +64 -0
- package/dist/chain/registeredAgent.js +140 -0
- package/dist/chain/registeredOperation.d.ts +49 -0
- package/dist/chain/registeredOperation.js +136 -0
- package/dist/chain/registeredRelay.d.ts +206 -0
- package/dist/chain/registeredRelay.js +312 -0
- package/dist/chain/revertReason.d.ts +14 -0
- package/dist/chain/revertReason.js +62 -0
- package/dist/chain/ruleAmendment.d.ts +80 -0
- package/dist/chain/ruleAmendment.js +195 -0
- package/dist/chain/ruleAmendmentCore.d.ts +129 -0
- package/dist/chain/ruleAmendmentCore.js +152 -0
- package/dist/chain/ruleInput.d.ts +10 -0
- package/dist/chain/ruleInput.js +13 -0
- package/dist/chain/serviceIdentity.d.ts +176 -0
- package/dist/chain/serviceIdentity.js +237 -0
- package/dist/chain/serviceProfiles.d.ts +39 -0
- package/dist/chain/serviceProfiles.js +74 -0
- package/dist/chain/services.d.ts +141 -0
- package/dist/chain/services.js +79 -0
- package/dist/chain/slotClient.d.ts +55 -0
- package/dist/chain/slotClient.js +62 -0
- package/dist/chain/slotSeed.d.ts +31 -0
- package/dist/chain/slotSeed.js +98 -0
- package/dist/chain/write.d.ts +431 -0
- package/dist/chain/write.js +958 -0
- package/dist/client/client.d.ts +112 -0
- package/dist/client/client.js +111 -0
- package/dist/client/index.d.ts +3 -0
- package/dist/client/index.js +2 -0
- package/dist/client/session.d.ts +67 -0
- package/dist/client/session.js +238 -0
- package/dist/committee/client.d.ts +284 -0
- package/dist/committee/client.js +351 -0
- package/dist/committee/dual-sign.d.ts +110 -0
- package/dist/committee/dual-sign.js +202 -0
- package/dist/committee/ecdsa.d.ts +36 -0
- package/dist/committee/ecdsa.js +45 -0
- package/dist/committee/extraction.d.ts +82 -0
- package/dist/committee/extraction.js +96 -0
- package/dist/committee/index.d.ts +19 -0
- package/dist/committee/index.js +13 -0
- package/dist/committee/oauth.d.ts +51 -0
- package/dist/committee/oauth.js +90 -0
- package/dist/committee/receipts.d.ts +51 -0
- package/dist/committee/receipts.js +59 -0
- package/dist/committee/request.d.ts +264 -0
- package/dist/committee/request.js +305 -0
- package/dist/committee/token.d.ts +274 -0
- package/dist/committee/token.js +418 -0
- package/dist/crypto/constants.d.ts +10 -0
- package/dist/crypto/constants.js +32 -0
- package/dist/crypto/detect.d.ts +20 -0
- package/dist/crypto/detect.js +44 -0
- package/dist/crypto/envelope.d.ts +60 -0
- package/dist/crypto/envelope.js +243 -0
- package/dist/crypto/frost.d.ts +49 -0
- package/dist/crypto/frost.js +152 -0
- package/dist/crypto/hex.d.ts +7 -0
- package/dist/crypto/hex.js +24 -0
- package/dist/crypto/ibe-blob.d.ts +114 -0
- package/dist/crypto/ibe-blob.js +208 -0
- package/dist/crypto/ibe.d.ts +88 -0
- package/dist/crypto/ibe.js +262 -0
- package/dist/crypto/index.d.ts +12 -0
- package/dist/crypto/index.js +12 -0
- package/dist/crypto/kem.d.ts +55 -0
- package/dist/crypto/kem.js +241 -0
- package/dist/decryption/client.d.ts +64 -0
- package/dist/decryption/client.js +82 -0
- package/dist/decryption/index.d.ts +2 -0
- package/dist/decryption/index.js +2 -0
- package/dist/errors.d.ts +124 -0
- package/dist/errors.js +167 -0
- package/dist/index.d.ts +43 -0
- package/dist/index.js +98 -0
- package/dist/keys/index.d.ts +1 -0
- package/dist/keys/index.js +4 -0
- package/dist/keys/node-client.d.ts +27 -0
- package/dist/keys/node-client.js +97 -0
- package/dist/oid4vp/binding.d.ts +76 -0
- package/dist/oid4vp/binding.js +118 -0
- package/dist/oid4vp/did-web.d.ts +49 -0
- package/dist/oid4vp/did-web.js +57 -0
- package/dist/oid4vp/index.d.ts +9 -0
- package/dist/oid4vp/index.js +10 -0
- package/dist/oid4vp/jose.d.ts +166 -0
- package/dist/oid4vp/jose.js +242 -0
- package/dist/oid4vp/jwe.d.ts +30 -0
- package/dist/oid4vp/jwe.js +93 -0
- package/dist/oid4vp/oid4vci.d.ts +84 -0
- package/dist/oid4vp/oid4vci.js +85 -0
- package/dist/oid4vp/request-object.d.ts +109 -0
- package/dist/oid4vp/request-object.js +96 -0
- package/dist/oid4vp/sd-jwt.d.ts +177 -0
- package/dist/oid4vp/sd-jwt.js +208 -0
- package/dist/oid4vp/verifier-agent.d.ts +161 -0
- package/dist/oid4vp/verifier-agent.js +132 -0
- package/dist/oid4vp/wallet.d.ts +116 -0
- package/dist/oid4vp/wallet.js +108 -0
- package/dist/recipient/store.d.ts +67 -0
- package/dist/recipient/store.js +98 -0
- package/dist/signing/ecdsa.d.ts +49 -0
- package/dist/signing/ecdsa.js +79 -0
- package/dist/signing/frost.d.ts +88 -0
- package/dist/signing/frost.js +145 -0
- package/dist/signing/index.d.ts +4 -0
- package/dist/signing/index.js +3 -0
- package/dist/slots/faucet.d.ts +23 -0
- package/dist/slots/faucet.js +35 -0
- package/dist/slots/index.d.ts +2 -0
- package/dist/slots/index.js +4 -0
- package/dist/verifier-agent/index.d.ts +241 -0
- package/dist/verifier-agent/index.js +411 -0
- package/docs/DEVELOPER-EXPERIENCE.md +62 -0
- package/docs/README.md +33 -0
- package/docs/access-control.md +76 -0
- package/docs/ai-development.md +292 -0
- package/docs/api.md +338 -0
- package/docs/application-api.md +335 -0
- package/docs/architecture.md +65 -0
- package/docs/basics.md +48 -0
- package/docs/capabilities.md +86 -0
- package/docs/chain.md +153 -0
- package/docs/compatibility.md +36 -0
- package/docs/consumer-migrations.md +80 -0
- package/docs/create-slot.md +104 -0
- package/docs/document-signing.md +243 -0
- package/docs/encrypted-notes.md +129 -0
- package/docs/encryption.md +100 -0
- package/docs/errors.md +76 -0
- package/docs/flows.md +289 -0
- package/docs/fuji.md +127 -0
- package/docs/funding.md +126 -0
- package/docs/getting-started.md +94 -0
- package/docs/glossary.md +44 -0
- package/docs/installation.md +132 -0
- package/docs/manage-slot.md +95 -0
- package/docs/native-approvals.md +124 -0
- package/docs/prerequisites.md +137 -0
- package/docs/read-slot.md +41 -0
- package/docs/reference/README.md +30 -0
- package/docs/reference/app-node.md +102 -0
- package/docs/reference/app.md +1737 -0
- package/docs/reference/chain-node.md +159 -0
- package/docs/reference/chain.md +3609 -0
- package/docs/reference/committee.md +2176 -0
- package/docs/reference/main.md +4068 -0
- package/docs/reference/oid4vp.md +2547 -0
- package/docs/reference/verifier-agent.md +498 -0
- package/docs/shared-account.md +168 -0
- package/docs/signing-and-encryption.md +52 -0
- package/docs/signing.md +172 -0
- package/examples/committee-slot.ts +16 -0
- package/examples/connect-fuji.ts +41 -0
- package/examples/connect-network.ts +14 -0
- package/examples/credential-approvals.ts +46 -0
- package/examples/document-signing/README.md +59 -0
- package/examples/document-signing/backend.ts +132 -0
- package/examples/document-signing/build.mjs +19 -0
- package/examples/document-signing/config.ts +34 -0
- package/examples/document-signing/gitignore.template +9 -0
- package/examples/document-signing/model.ts +140 -0
- package/examples/document-signing/network.ts +58 -0
- package/examples/document-signing/package.json +25 -0
- package/examples/document-signing/public/index.html +27 -0
- package/examples/document-signing/public/sample.pdf +31 -0
- package/examples/document-signing/public/styles.css +531 -0
- package/examples/document-signing/server.ts +358 -0
- package/examples/document-signing/setup.ts +111 -0
- package/examples/document-signing/store.ts +105 -0
- package/examples/document-signing/tsconfig.json +24 -0
- package/examples/document-signing/verify.ts +24 -0
- package/examples/document-signing/web/app.ts +312 -0
- package/examples/document-signing/web/preview.ts +67 -0
- package/examples/document-signing/web/wallet.ts +146 -0
- package/examples/document-signing/workflow.test.ts +228 -0
- package/examples/document-signing/workflow.ts +241 -0
- package/examples/document-signing.ts +96 -0
- package/examples/encrypted-notes.ts +86 -0
- package/examples/ethers-signer.ts +204 -0
- package/examples/fund-slot.ts +73 -0
- package/examples/getting-started.ts +25 -0
- package/examples/learning-authorize.ts +23 -0
- package/examples/learning-cancel-slot.ts +11 -0
- package/examples/learning-context.ts +28 -0
- package/examples/learning-create-slot.ts +25 -0
- package/examples/learning-read-slot.ts +8 -0
- package/examples/learning-renew-slot.ts +12 -0
- package/examples/learning-sign.ts +8 -0
- package/examples/live-config.ts +71 -0
- package/examples/minimal.ts +6 -0
- package/examples/native-approvals.ts +57 -0
- package/examples/network.ts +58 -0
- package/examples/offline.ts +16 -0
- package/examples/personal-vault.ts +20 -0
- package/examples/provision-slot.ts +47 -0
- package/examples/shared-account.ts +46 -0
- package/examples/slot-address.ts +13 -0
- package/examples/slot-creation.ts +9 -0
- package/examples/tutorial-negative.ts +26 -0
- package/examples/tutorial-support.ts +76 -0
- package/examples/viem-account.ts +203 -0
- package/package.json +303 -4
|
@@ -0,0 +1,780 @@
|
|
|
1
|
+
// OID4VP-DCQL - the OpenID Foundation's Digital Credentials Query Language.
|
|
2
|
+
//
|
|
3
|
+
// The rule IS the wallet query. A third-party credential wallet can only be asked for
|
|
4
|
+
// a presentation via a standard request, so making the policy language BE the standard
|
|
5
|
+
// deletes the compiler between policy and wallet request.
|
|
6
|
+
//
|
|
7
|
+
// THIS FILE MUST AGREE WITH THE RUST CRATE EXACTLY. Both gate the same slots: the
|
|
8
|
+
// keeper and accountant run the reference evaluator, recipients and this SDK run this.
|
|
9
|
+
// The guard is the shared corpus: test/oid4vp-vectors.json, a byte-identical copy of
|
|
10
|
+
// the vendored vector file, run by test/oid4vp.conformance.ts on every `npm test`.
|
|
11
|
+
import { TasraError } from '../errors.js';
|
|
12
|
+
import { MAX_IDENTITY_LEN, scopeCovers } from './identityScope.js';
|
|
13
|
+
/**
|
|
14
|
+
* The rule is not a well-formed OID4VP-DCQL query (or exceeds `MAX_RULE_LEN`).
|
|
15
|
+
* Never retryable - the same rule fails identically. Extends
|
|
16
|
+
* {@link TasraError} so one `instanceof` catches every SDK error.
|
|
17
|
+
*/
|
|
18
|
+
export class DcqlMalformedError extends TasraError {
|
|
19
|
+
constructor(message) {
|
|
20
|
+
super(message, { retryable: false });
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/** W3C JWT-VC JSON credential format. */
|
|
24
|
+
export const FORMAT_JWT_VC_JSON = 'jwt_vc_json';
|
|
25
|
+
/** IETF SD-JWT VC credential format (selective disclosure) - the Hovi / EUDI wallet format. */
|
|
26
|
+
export const FORMAT_DC_SD_JWT = 'dc+sd-jwt';
|
|
27
|
+
/**
|
|
28
|
+
* An OAuth 2.0 access token from the slot owner's own IdP, sender-constrained
|
|
29
|
+
* with DPoP (RFC 9449). Not a credential format in the OID4VP sense - a wallet never
|
|
30
|
+
* presents one - but it reaches the evaluator as one claim set, so it is one `format`.
|
|
31
|
+
*/
|
|
32
|
+
export const FORMAT_OAUTH_AT_DPOP = 'oauth+access-token+dpop';
|
|
33
|
+
/**
|
|
34
|
+
* Prefix of the OAuth family. Written as a prefix so a later tier (a bearer
|
|
35
|
+
* artifact, deliberately NOT built) is one constant and one row, never a second set of
|
|
36
|
+
* validator rules that could disagree with this one.
|
|
37
|
+
*/
|
|
38
|
+
export const OAUTH_FORMAT_PREFIX = 'oauth+';
|
|
39
|
+
/** Mirrors the reference evaluator's `ACCEPTED_FORMATS` . */
|
|
40
|
+
export const ACCEPTED_FORMATS = [
|
|
41
|
+
FORMAT_JWT_VC_JSON,
|
|
42
|
+
FORMAT_DC_SD_JWT,
|
|
43
|
+
FORMAT_OAUTH_AT_DPOP,
|
|
44
|
+
];
|
|
45
|
+
/** Whether `format` is in the OAuth family. */
|
|
46
|
+
export function isOauthFormat(format) {
|
|
47
|
+
return format.startsWith(OAUTH_FORMAT_PREFIX);
|
|
48
|
+
}
|
|
49
|
+
/** Bounds on `meta.max_age_secs`, mirroring the reference implementation `MAX_AGE_SECS_RANGE`. */
|
|
50
|
+
export const MAX_AGE_SECS_MIN = 1;
|
|
51
|
+
export const MAX_AGE_SECS_MAX = 86_400;
|
|
52
|
+
/**
|
|
53
|
+
* Cap on a rule, in BYTES, before parsing.
|
|
54
|
+
*
|
|
55
|
+
* Deliberately the same 4096 as the legacy grammar for now, and it is not yet a
|
|
56
|
+
* justified number: 4096 was chosen against a terse five-clause language and an
|
|
57
|
+
* OID4VP-DCQL query expressing the same policy is several times larger. The keeper
|
|
58
|
+
* re-parses the rule on every request, so this bounds real per-request work.
|
|
59
|
+
*/
|
|
60
|
+
export const MAX_RULE_LEN = 4096;
|
|
61
|
+
/** Whether `path` selects across an array anywhere (a `null` segment). */
|
|
62
|
+
function selectsMany(path) {
|
|
63
|
+
return path.some((s) => s === null);
|
|
64
|
+
}
|
|
65
|
+
const ABSENT = { found: false };
|
|
66
|
+
/**
|
|
67
|
+
* A {@link CredentialView} over a parsed JSON credential body.
|
|
68
|
+
*
|
|
69
|
+
* Path resolution walks object keys, plus `null` for "every element of this array". A path
|
|
70
|
+
* that runs into the wrong shape is ABSENT rather than an error, which is what makes the
|
|
71
|
+
* evaluator fail closed.
|
|
72
|
+
*
|
|
73
|
+
* @param args - Format, credential types and parsed JSON body exposed to the evaluator.
|
|
74
|
+
*/
|
|
75
|
+
export function jsonCredential(args) {
|
|
76
|
+
const { format, types, body } = args;
|
|
77
|
+
return {
|
|
78
|
+
format,
|
|
79
|
+
types,
|
|
80
|
+
claim(path) {
|
|
81
|
+
return resolvePath(body, path);
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/** Resolve object keys and null array wildcards in a JSON value. Return absent for incompatible shapes; collapse a single result after a wildcard to that value. */
|
|
86
|
+
export function resolvePath(value, path) {
|
|
87
|
+
const segment = path[0];
|
|
88
|
+
if (segment === undefined)
|
|
89
|
+
return { found: true, value };
|
|
90
|
+
const rest = path.slice(1);
|
|
91
|
+
if (segment === null) {
|
|
92
|
+
if (!Array.isArray(value))
|
|
93
|
+
return ABSENT;
|
|
94
|
+
if (rest.length === 0)
|
|
95
|
+
return { found: true, value };
|
|
96
|
+
const results = [];
|
|
97
|
+
for (const elem of value) {
|
|
98
|
+
const r = resolvePath(elem, rest);
|
|
99
|
+
if (r.found)
|
|
100
|
+
results.push(r.value);
|
|
101
|
+
}
|
|
102
|
+
if (results.length === 0)
|
|
103
|
+
return ABSENT;
|
|
104
|
+
if (results.length === 1)
|
|
105
|
+
return { found: true, value: results[0] };
|
|
106
|
+
return { found: true, value: results };
|
|
107
|
+
}
|
|
108
|
+
if (!isPlainObject(value) || !Object.hasOwn(value, segment))
|
|
109
|
+
return ABSENT;
|
|
110
|
+
return resolvePath(value[segment], rest);
|
|
111
|
+
}
|
|
112
|
+
// parse / validate
|
|
113
|
+
const encoder = new TextEncoder();
|
|
114
|
+
/** UTF-8 byte length - the cap is defined in bytes, and JS `.length` counts UTF-16
|
|
115
|
+
* code units, which diverges for any non-ASCII rule. */
|
|
116
|
+
function byteLen(s) {
|
|
117
|
+
return encoder.encode(s).length;
|
|
118
|
+
}
|
|
119
|
+
function isPlainObject(v) {
|
|
120
|
+
return typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
121
|
+
}
|
|
122
|
+
function malformed(msg) {
|
|
123
|
+
return new DcqlMalformedError(msg);
|
|
124
|
+
}
|
|
125
|
+
/** Refuse any key outside the allow-list, naming the offender. */
|
|
126
|
+
function denyUnknownFields(what, o, allowed) {
|
|
127
|
+
for (const k of Object.keys(o)) {
|
|
128
|
+
if (!allowed.includes(k)) {
|
|
129
|
+
throw malformed(`not a supported DCQL query: ${what} has unknown field ${JSON.stringify(k)} ` +
|
|
130
|
+
`(supported: ${allowed.join(', ')})`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function asObject(what, v) {
|
|
135
|
+
if (!isPlainObject(v))
|
|
136
|
+
throw malformed(`not a supported DCQL query: ${what} must be an object`);
|
|
137
|
+
return v;
|
|
138
|
+
}
|
|
139
|
+
function asArray(what, v) {
|
|
140
|
+
if (!Array.isArray(v))
|
|
141
|
+
throw malformed(`not a supported DCQL query: ${what} must be an array`);
|
|
142
|
+
return v;
|
|
143
|
+
}
|
|
144
|
+
function asString(what, v) {
|
|
145
|
+
if (typeof v !== 'string')
|
|
146
|
+
throw malformed(`not a supported DCQL query: ${what} must be a string`);
|
|
147
|
+
return v;
|
|
148
|
+
}
|
|
149
|
+
function asStringArray(what, v) {
|
|
150
|
+
return asArray(what, v).map((x, i) => asString(`${what}[${i}]`, x));
|
|
151
|
+
}
|
|
152
|
+
/** A claim path: object keys and `null` wildcards. Anything else is refused by name. */
|
|
153
|
+
function asClaimPath(what, v) {
|
|
154
|
+
return asArray(what, v).map((x, i) => {
|
|
155
|
+
if (typeof x === 'string' || x === null)
|
|
156
|
+
return x;
|
|
157
|
+
throw malformed(`not a supported DCQL query: ${what}[${i}] must be a string key or null`);
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
/** Exactly the top-level `iss` claim - a nested path does not count. */
|
|
161
|
+
function isIssuerPath(q) {
|
|
162
|
+
return q.path.length === 1 && q.path[0] === 'iss';
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* The extra rules an `oauth+*` credential query must satisfy (Decision 3).
|
|
166
|
+
*
|
|
167
|
+
* All three exist because an OAuth access token is a bearer-shaped artifact from a tenant's
|
|
168
|
+
* OWN IdP, with none of the trust scaffolding a credential brings: no issuer registry, no
|
|
169
|
+
* status list, and an audience the IdP chooses. The slot's rule is therefore the ONLY place
|
|
170
|
+
* those can be pinned, so the validator makes pinning mandatory rather than optional.
|
|
171
|
+
*/
|
|
172
|
+
function validateOauthQuery(id, claims, meta) {
|
|
173
|
+
// 1. The issuer set must be PINNED. the "explicitly open" form is sound for
|
|
174
|
+
// credentials; for OAuth it would mean "any IdP on the internet that will mint a token
|
|
175
|
+
// naming our audience".
|
|
176
|
+
const issuerEntry = (claims ?? []).find(isIssuerPath);
|
|
177
|
+
const issuerValues = issuerEntry?.values;
|
|
178
|
+
if (issuerValues === undefined) {
|
|
179
|
+
throw malformed(`credential ${JSON.stringify(id)} is an oauth+* query with an open issuer set; the ` +
|
|
180
|
+
`accepted issuers must be pinned (add \`values\` to the ["iss"] entry)`);
|
|
181
|
+
}
|
|
182
|
+
for (const v of issuerValues) {
|
|
183
|
+
if (typeof v !== 'string') {
|
|
184
|
+
throw malformed(`credential ${JSON.stringify(id)} pins a non-string issuer; an OIDC issuer is a URL`);
|
|
185
|
+
}
|
|
186
|
+
if (!v.startsWith('https://')) {
|
|
187
|
+
throw malformed(`credential ${JSON.stringify(id)} pins issuer ${JSON.stringify(v)}, which is not https://`);
|
|
188
|
+
}
|
|
189
|
+
// The issuer is concatenated with `/.well-known/openid-configuration` at discovery and
|
|
190
|
+
// compared BYTE-FOR-BYTE with the document's own `issuer`; a `?`/`#` makes both meaningless.
|
|
191
|
+
if (v.includes('?') || v.includes('#')) {
|
|
192
|
+
throw malformed(`credential ${JSON.stringify(id)} pins issuer ${JSON.stringify(v)}, which carries a ` +
|
|
193
|
+
`query or fragment; an OIDC issuer identifier has neither`);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
// 2. The audience must be constrained. Without it a token minted for ANOTHER relying
|
|
197
|
+
// party of the same IdP authorizes here - the classic confused deputy.
|
|
198
|
+
const audEntry = (claims ?? []).find((q) => q.path.length === 2 && q.path[0] === 'aud' && q.path[1] === null);
|
|
199
|
+
if (audEntry === undefined) {
|
|
200
|
+
throw malformed(`credential ${JSON.stringify(id)} is an oauth+* query with no audience constraint: add ` +
|
|
201
|
+
`a claims entry with path ["aud", null] and the platform audience in \`values\``);
|
|
202
|
+
}
|
|
203
|
+
if (audEntry.values === undefined || audEntry.values.length === 0) {
|
|
204
|
+
throw malformed(`credential ${JSON.stringify(id)} has an ["aud", null] entry with no \`values\`; the ` +
|
|
205
|
+
`accepted audience must be named`);
|
|
206
|
+
}
|
|
207
|
+
// 3. Freshness IS the revocation signal (no status list exists for an access token).
|
|
208
|
+
const maxAge = meta?.max_age_secs;
|
|
209
|
+
if (maxAge === undefined) {
|
|
210
|
+
throw malformed(`credential ${JSON.stringify(id)} is an oauth+* query without meta.max_age_secs; an ` +
|
|
211
|
+
`access token has no status list, so the rule must bound how stale an ` +
|
|
212
|
+
`authentication may be`);
|
|
213
|
+
}
|
|
214
|
+
if (maxAge < MAX_AGE_SECS_MIN || maxAge > MAX_AGE_SECS_MAX) {
|
|
215
|
+
throw malformed(`credential ${JSON.stringify(id)} has meta.max_age_secs ${maxAge}, outside ` +
|
|
216
|
+
`${MAX_AGE_SECS_MIN}..=${MAX_AGE_SECS_MAX}`);
|
|
217
|
+
}
|
|
218
|
+
// `type_values`/`vct_values` describe credential typing an access token does not have;
|
|
219
|
+
// accepting them silently would let a rule look constrained while constraining nothing.
|
|
220
|
+
if (meta?.type_values !== undefined || meta?.vct_values !== undefined) {
|
|
221
|
+
throw malformed(`credential ${JSON.stringify(id)} is an oauth+* query carrying type_values/vct_values; ` +
|
|
222
|
+
`an access token has no credential type`);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Parse and validate the supported DCQL rule grammar. Reject unknown fields, unsupported constraints and rules that exceed the byte limit.
|
|
227
|
+
*
|
|
228
|
+
* @param rule - DCQL policy encoded as JSON text.
|
|
229
|
+
* @param opts - Whether each credential query must explicitly constrain its issuer.
|
|
230
|
+
*/
|
|
231
|
+
export function validate(rule, opts = {}) {
|
|
232
|
+
const n = byteLen(rule);
|
|
233
|
+
if (n > MAX_RULE_LEN) {
|
|
234
|
+
throw malformed(`rule is ${n} bytes (max ${MAX_RULE_LEN})`);
|
|
235
|
+
}
|
|
236
|
+
let parsed;
|
|
237
|
+
try {
|
|
238
|
+
parsed = JSON.parse(rule);
|
|
239
|
+
}
|
|
240
|
+
catch (e) {
|
|
241
|
+
throw malformed(`not a supported DCQL query: ${e instanceof Error ? e.message : String(e)}`);
|
|
242
|
+
}
|
|
243
|
+
const root = asObject('query', parsed);
|
|
244
|
+
denyUnknownFields('query', root, ['credentials', 'credential_sets']);
|
|
245
|
+
const rawCreds = asArray('credentials', root['credentials']);
|
|
246
|
+
if (rawCreds.length === 0) {
|
|
247
|
+
throw malformed('query has no credentials; an empty query would authorize everyone');
|
|
248
|
+
}
|
|
249
|
+
const ids = new Set();
|
|
250
|
+
const credentials = rawCreds.map((rc, i) => {
|
|
251
|
+
const c = asObject(`credentials[${i}]`, rc);
|
|
252
|
+
denyUnknownFields(`credentials[${i}]`, c, [
|
|
253
|
+
'id',
|
|
254
|
+
'format',
|
|
255
|
+
'meta',
|
|
256
|
+
'claims',
|
|
257
|
+
'kk_identity_scope_claim',
|
|
258
|
+
'kk_scope_namespace',
|
|
259
|
+
]);
|
|
260
|
+
const id = asString(`credentials[${i}].id`, c['id']);
|
|
261
|
+
if (id === '')
|
|
262
|
+
throw malformed('credential query has an empty id');
|
|
263
|
+
if (ids.has(id))
|
|
264
|
+
throw malformed(`duplicate credential id ${JSON.stringify(id)}`);
|
|
265
|
+
ids.add(id);
|
|
266
|
+
const format = asString(`credentials[${i}].format`, c['format']);
|
|
267
|
+
if (!ACCEPTED_FORMATS.includes(format)) {
|
|
268
|
+
throw malformed(`unsupported credential format ${JSON.stringify(format)} ` +
|
|
269
|
+
`(supported: ${ACCEPTED_FORMATS.join(', ')})`);
|
|
270
|
+
}
|
|
271
|
+
let meta;
|
|
272
|
+
if (c['meta'] !== undefined) {
|
|
273
|
+
const m = asObject(`credentials[${i}].meta`, c['meta']);
|
|
274
|
+
denyUnknownFields(`credentials[${i}].meta`, m, [
|
|
275
|
+
'type_values',
|
|
276
|
+
'vct_values',
|
|
277
|
+
'max_age_secs',
|
|
278
|
+
]);
|
|
279
|
+
meta = {};
|
|
280
|
+
if (m['type_values'] !== undefined) {
|
|
281
|
+
meta.type_values = asArray(`credentials[${i}].meta.type_values`, m['type_values']).map((alt, j) => asStringArray(`credentials[${i}].meta.type_values[${j}]`, alt));
|
|
282
|
+
}
|
|
283
|
+
if (m['vct_values'] !== undefined) {
|
|
284
|
+
meta.vct_values = asStringArray(`credentials[${i}].meta.vct_values`, m['vct_values']);
|
|
285
|
+
}
|
|
286
|
+
if (m['max_age_secs'] !== undefined) {
|
|
287
|
+
const v = m['max_age_secs'];
|
|
288
|
+
if (typeof v !== 'number' || !Number.isInteger(v) || v < 0) {
|
|
289
|
+
throw malformed(`not a supported DCQL query: credentials[${i}].meta.max_age_secs must be a ` +
|
|
290
|
+
`non-negative integer`);
|
|
291
|
+
}
|
|
292
|
+
meta.max_age_secs = v;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
let claims;
|
|
296
|
+
if (c['claims'] !== undefined) {
|
|
297
|
+
claims = asArray(`credentials[${i}].claims`, c['claims']).map((rq, j) => {
|
|
298
|
+
const q = asObject(`credentials[${i}].claims[${j}]`, rq);
|
|
299
|
+
denyUnknownFields(`credentials[${i}].claims[${j}]`, q, ['path', 'values']);
|
|
300
|
+
const path = asClaimPath(`credentials[${i}].claims[${j}].path`, q['path']);
|
|
301
|
+
if (path.length === 0) {
|
|
302
|
+
throw malformed(`credential ${JSON.stringify(id)} has a claim query with an empty path`);
|
|
303
|
+
}
|
|
304
|
+
// An integer index addresses a POSITION in a list. No format we admit fixes the
|
|
305
|
+
// order of an array claim, so a rule that matched at issuance could silently stop
|
|
306
|
+
// matching on the next one - fail at provisioning instead. Enforced by hand:
|
|
307
|
+
// `JSON.parse` admits the number and nothing else would notice.
|
|
308
|
+
if (q['path'].some((seg) => typeof seg === 'number')) {
|
|
309
|
+
throw malformed(`credential ${JSON.stringify(id)} has an integer index in a claim path; array ` +
|
|
310
|
+
`positions are not addressable (use null to select every element)`);
|
|
311
|
+
}
|
|
312
|
+
// A leading `null` would select over the credential body, which is an object.
|
|
313
|
+
if (path[0] === null) {
|
|
314
|
+
throw malformed(`credential ${JSON.stringify(id)} has a claim path starting with null; the ` +
|
|
315
|
+
`credential body is an object, so a leading wildcard can never select anything`);
|
|
316
|
+
}
|
|
317
|
+
let values;
|
|
318
|
+
if (q['values'] !== undefined) {
|
|
319
|
+
values = asArray(`credentials[${i}].claims[${j}].values`, q['values']);
|
|
320
|
+
// An explicit empty `values` can never match - almost certainly a mistake
|
|
321
|
+
// rather than an intentional deny-all, and far better refused at
|
|
322
|
+
// provisioning than at 3am.
|
|
323
|
+
if (values.length === 0) {
|
|
324
|
+
throw malformed(`credential ${JSON.stringify(id)} has a claim query with an empty \`values\` ` +
|
|
325
|
+
`list, which no credential can satisfy`);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return values === undefined ? { path } : { path, values };
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
// Issuer acceptance is slot policy, so the constraint is MANDATORY -
|
|
332
|
+
// `{"path":["iss"],"values":[...]}` pins the accepted issuers; the same entry
|
|
333
|
+
// without `values` is the explicitly-open form (any verifiable issuer, stated in
|
|
334
|
+
// the committed rule rather than implied by silence).
|
|
335
|
+
const hasIssuerEntry = (claims ?? []).some(isIssuerPath);
|
|
336
|
+
if (!hasIssuerEntry && opts.requireIssuer !== false) {
|
|
337
|
+
throw malformed(`credential ${JSON.stringify(id)} has no issuer constraint: add a claims entry ` +
|
|
338
|
+
`with path ["iss"] — with \`values\` to pin the accepted issuers, or without ` +
|
|
339
|
+
`\`values\` to accept any verifiable issuer (explicitly)`);
|
|
340
|
+
}
|
|
341
|
+
// the oauth+* family carries its own mandatory constraints
|
|
342
|
+
if (isOauthFormat(format)) {
|
|
343
|
+
validateOauthQuery(id, claims, meta);
|
|
344
|
+
}
|
|
345
|
+
else if (meta?.max_age_secs !== undefined) {
|
|
346
|
+
throw malformed(`credential ${JSON.stringify(id)} sets meta.max_age_secs, which only the oauth+* ` +
|
|
347
|
+
`formats accept — a credential format carries its own revocation signal`);
|
|
348
|
+
}
|
|
349
|
+
// identity-scope binding validation (mirrors the reference order)
|
|
350
|
+
// The namespace governs a scope claim, so it is meaningless alone.
|
|
351
|
+
if (c['kk_scope_namespace'] !== undefined && c['kk_identity_scope_claim'] === undefined) {
|
|
352
|
+
throw malformed(`credential ${JSON.stringify(id)} has kk_scope_namespace without kk_identity_scope_claim`);
|
|
353
|
+
}
|
|
354
|
+
let scopeClaim;
|
|
355
|
+
let scopeNamespace;
|
|
356
|
+
if (c['kk_identity_scope_claim'] !== undefined) {
|
|
357
|
+
scopeClaim = asStringArray(`credentials[${i}].kk_identity_scope_claim`, c['kk_identity_scope_claim']);
|
|
358
|
+
if (scopeClaim.length === 0) {
|
|
359
|
+
throw malformed(`credential ${JSON.stringify(id)} has an empty kk_identity_scope_claim path`);
|
|
360
|
+
}
|
|
361
|
+
// The scope claim must be part of the credential match itself, so a query cannot
|
|
362
|
+
// be satisfied without the grant being present to check.
|
|
363
|
+
const path = scopeClaim;
|
|
364
|
+
const scopeClaimPresent = (claims ?? []).some((q) => q.path.length === path.length && q.path.every((p, k) => p === path[k]));
|
|
365
|
+
if (!scopeClaimPresent) {
|
|
366
|
+
throw malformed(`credential ${JSON.stringify(id)} names kk_identity_scope_claim ` +
|
|
367
|
+
`${JSON.stringify(scopeClaim)} but has no matching claims entry for that path`);
|
|
368
|
+
}
|
|
369
|
+
// The namespace is a required companion - it says whose grant power the claim
|
|
370
|
+
// carries. (The reference implementation refuses a value outside the enum at parse time; the verdict class
|
|
371
|
+
// is the same malformed either way.)
|
|
372
|
+
const ns = c['kk_scope_namespace'];
|
|
373
|
+
if (ns === undefined) {
|
|
374
|
+
throw malformed(`credential ${JSON.stringify(id)} has kk_identity_scope_claim but no ` +
|
|
375
|
+
`kk_scope_namespace (required — "issuer" or "any")`);
|
|
376
|
+
}
|
|
377
|
+
if (ns !== 'issuer' && ns !== 'any') {
|
|
378
|
+
throw malformed(`not a supported DCQL query: kk_scope_namespace must be "issuer" or "any"`);
|
|
379
|
+
}
|
|
380
|
+
// `"any"` (grant over any namespace) with an OPEN issuer set would let anyone
|
|
381
|
+
// self-issue a credential granting scope over anyone. Refuse the pairing.
|
|
382
|
+
if (ns === 'any') {
|
|
383
|
+
const issuerPinned = (claims ?? []).some((q) => isIssuerPath(q) && q.values !== undefined);
|
|
384
|
+
if (!issuerPinned) {
|
|
385
|
+
throw malformed(`credential ${JSON.stringify(id)} pairs kk_scope_namespace "any" with an open ` +
|
|
386
|
+
`issuer set; unscoped grant power must come from pinned issuers (add ` +
|
|
387
|
+
`\`values\` to the ["issuer"] entry)`);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
scopeNamespace = ns;
|
|
391
|
+
}
|
|
392
|
+
const out = { id, format };
|
|
393
|
+
if (meta !== undefined)
|
|
394
|
+
out.meta = meta;
|
|
395
|
+
if (claims !== undefined)
|
|
396
|
+
out.claims = claims;
|
|
397
|
+
if (scopeClaim !== undefined)
|
|
398
|
+
out.kk_identity_scope_claim = scopeClaim;
|
|
399
|
+
if (scopeNamespace !== undefined)
|
|
400
|
+
out.kk_scope_namespace = scopeNamespace;
|
|
401
|
+
return out;
|
|
402
|
+
});
|
|
403
|
+
let credential_sets;
|
|
404
|
+
if (root['credential_sets'] !== undefined) {
|
|
405
|
+
credential_sets = asArray('credential_sets', root['credential_sets']).map((rs, i) => {
|
|
406
|
+
const s = asObject(`credential_sets[${i}]`, rs);
|
|
407
|
+
denyUnknownFields(`credential_sets[${i}]`, s, ['options', 'required', 'purpose']);
|
|
408
|
+
const options = asArray(`credential_sets[${i}].options`, s['options']).map((o, j) => asStringArray(`credential_sets[${i}].options[${j}]`, o));
|
|
409
|
+
if (options.length === 0) {
|
|
410
|
+
throw malformed('credential_set has no options; it could never be satisfied');
|
|
411
|
+
}
|
|
412
|
+
for (const opt of options) {
|
|
413
|
+
if (opt.length === 0) {
|
|
414
|
+
throw malformed('credential_set option is empty, which would match vacuously');
|
|
415
|
+
}
|
|
416
|
+
for (const id of opt) {
|
|
417
|
+
if (!ids.has(id)) {
|
|
418
|
+
throw malformed(`credential_set references unknown credential id ${JSON.stringify(id)}`);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
const out = { options };
|
|
423
|
+
if (s['required'] !== undefined) {
|
|
424
|
+
if (typeof s['required'] !== 'boolean') {
|
|
425
|
+
throw malformed(`not a supported DCQL query: credential_sets[${i}].required must be a boolean`);
|
|
426
|
+
}
|
|
427
|
+
out.required = s['required'];
|
|
428
|
+
}
|
|
429
|
+
if (s['purpose'] !== undefined)
|
|
430
|
+
out.purpose = s['purpose'];
|
|
431
|
+
return out;
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
return credential_sets === undefined ? { credentials } : { credentials, credential_sets };
|
|
435
|
+
}
|
|
436
|
+
// evaluate
|
|
437
|
+
/**
|
|
438
|
+
* Does `credentials` satisfy `rule`?
|
|
439
|
+
*
|
|
440
|
+
* Returns `true` to grant and `false` to deny; throws {@link DcqlMalformedError} when
|
|
441
|
+
* the rule itself is broken. Takes NO holder identity - DCQL constrains credentials,
|
|
442
|
+
* and holder identity is established by the presentation's holder binding. That is why
|
|
443
|
+
* the legacy `required_sub_in` clause has no encoding here.
|
|
444
|
+
*
|
|
445
|
+
* @param rule - DCQL policy encoded as JSON text.
|
|
446
|
+
* @param credentials - Credential views whose signatures and trust were already validated.
|
|
447
|
+
*/
|
|
448
|
+
export function evaluate(rule, credentials) {
|
|
449
|
+
const q = validate(rule);
|
|
450
|
+
// an identity-scoped rule constrains WHICH identity may be operated on,
|
|
451
|
+
// so authorizing it here - with no identity to check - would silently ignore that
|
|
452
|
+
// constraint. Denied, mirroring the reference implementation; use evaluateIdentityScoped.
|
|
453
|
+
if (q.credentials.some(c => c.kk_identity_scope_claim !== undefined))
|
|
454
|
+
return false;
|
|
455
|
+
return evaluateWho(q, credentials) !== null;
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* The WHO gate shared by {@link evaluate} and {@link evaluateIdentityScoped}, so the
|
|
459
|
+
* two can never disagree about which credential queries are satisfied. Returns the
|
|
460
|
+
* satisfied-query id set, or `null` when the rule as a whole is unsatisfied (the reference implementation
|
|
461
|
+
* side's `Err(Reject)`).
|
|
462
|
+
*/
|
|
463
|
+
function evaluateWho(q, credentials) {
|
|
464
|
+
// Which credential queries are satisfied by at least one presented credential.
|
|
465
|
+
const satisfied = new Set();
|
|
466
|
+
for (const cq of q.credentials) {
|
|
467
|
+
if (credentials.some(c => matches(cq, c)))
|
|
468
|
+
satisfied.add(cq.id);
|
|
469
|
+
}
|
|
470
|
+
if (q.credential_sets === undefined) {
|
|
471
|
+
// Absent means every credential query is required.
|
|
472
|
+
return q.credentials.every(c => satisfied.has(c.id)) ? satisfied : null;
|
|
473
|
+
}
|
|
474
|
+
// Present means ONLY the sets decide. A credential query not named by any satisfied
|
|
475
|
+
// option is not independently required - that is what makes `options` alternatives
|
|
476
|
+
// rather than extra conditions.
|
|
477
|
+
for (const set of q.credential_sets) {
|
|
478
|
+
if (set.required === false)
|
|
479
|
+
continue;
|
|
480
|
+
const ok = set.options.some(opt => opt.every(id => satisfied.has(id)));
|
|
481
|
+
if (!ok)
|
|
482
|
+
return null;
|
|
483
|
+
}
|
|
484
|
+
return satisfied;
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* Select the minimal set of credentials that satisfy `rule` - the inverse of
|
|
488
|
+
* {@link evaluate}. Given a rule and held credentials, returns which to present.
|
|
489
|
+
*
|
|
490
|
+
* When `credential_sets` are present, picks the cheapest satisfying option
|
|
491
|
+
* (fewest credential queries). When absent, every credential query must be
|
|
492
|
+
* satisfied.
|
|
493
|
+
*
|
|
494
|
+
* @param rule - DCQL policy encoded as JSON text.
|
|
495
|
+
* @param credentials - Credential views available for local matching.
|
|
496
|
+
* @param opts - Issuer-constraint validation options.
|
|
497
|
+
*/
|
|
498
|
+
export function select(rule, credentials, opts = {}) {
|
|
499
|
+
const q = validate(rule, opts);
|
|
500
|
+
// Map each credential query to its matching credentials.
|
|
501
|
+
const matchMap = new Map();
|
|
502
|
+
for (const cq of q.credentials) {
|
|
503
|
+
const matching = credentials.filter(c => matches(cq, c));
|
|
504
|
+
matchMap.set(cq.id, matching);
|
|
505
|
+
}
|
|
506
|
+
const satisfied = new Set();
|
|
507
|
+
for (const [id, creds] of matchMap) {
|
|
508
|
+
if (creds.length > 0)
|
|
509
|
+
satisfied.add(id);
|
|
510
|
+
}
|
|
511
|
+
// Determine which credential query ids are needed.
|
|
512
|
+
let neededIds;
|
|
513
|
+
if (q.credential_sets === undefined) {
|
|
514
|
+
// No sets means every credential query is required.
|
|
515
|
+
neededIds = q.credentials.map(c => c.id);
|
|
516
|
+
}
|
|
517
|
+
else {
|
|
518
|
+
// Pick the cheapest satisfying option per required set.
|
|
519
|
+
const picked = new Set();
|
|
520
|
+
const unsatisfiable = new Set();
|
|
521
|
+
for (const set of q.credential_sets) {
|
|
522
|
+
if (set.required === false)
|
|
523
|
+
continue;
|
|
524
|
+
// Find the satisfying option with the fewest queries.
|
|
525
|
+
let bestOption = null;
|
|
526
|
+
for (const opt of set.options) {
|
|
527
|
+
if (opt.every(id => satisfied.has(id))) {
|
|
528
|
+
if (bestOption === null || opt.length < bestOption.length) {
|
|
529
|
+
bestOption = opt;
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
if (bestOption !== null) {
|
|
534
|
+
for (const id of bestOption)
|
|
535
|
+
picked.add(id);
|
|
536
|
+
}
|
|
537
|
+
else {
|
|
538
|
+
// A required set with NO satisfiable option denies (as `evaluate` does); name every
|
|
539
|
+
// query the set could have been answered with so the caller can say what is missing.
|
|
540
|
+
for (const opt of set.options)
|
|
541
|
+
for (const id of opt)
|
|
542
|
+
if (!satisfied.has(id))
|
|
543
|
+
unsatisfiable.add(id);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
if (unsatisfiable.size > 0) {
|
|
547
|
+
return { satisfied: false, credentials: [], unsatisfied: [...unsatisfiable] };
|
|
548
|
+
}
|
|
549
|
+
neededIds = [...picked];
|
|
550
|
+
}
|
|
551
|
+
const unsatisfied = neededIds.filter(id => !satisfied.has(id));
|
|
552
|
+
if (unsatisfied.length > 0) {
|
|
553
|
+
return { satisfied: false, credentials: [], unsatisfied };
|
|
554
|
+
}
|
|
555
|
+
// Collect one matching credential per needed query (deduplicated).
|
|
556
|
+
const selected = new Set();
|
|
557
|
+
for (const id of neededIds) {
|
|
558
|
+
const creds = matchMap.get(id);
|
|
559
|
+
if (creds && creds.length > 0)
|
|
560
|
+
selected.add(creds[0]);
|
|
561
|
+
}
|
|
562
|
+
return { satisfied: true, credentials: [...selected], unsatisfied: [] };
|
|
563
|
+
}
|
|
564
|
+
/**
|
|
565
|
+
* Does `credentials` authorize an identity-scoped operation on `identity`?
|
|
566
|
+
*
|
|
567
|
+
* The {@link evaluate} WHO gate PLUS the WHICH gate: a satisfied credential query
|
|
568
|
+
* carrying `kk_identity_scope_claim` must have a matching credential whose scope grant
|
|
569
|
+
* (a string or array at that path) covers `identity` ({@link scopeCovers}) and - under
|
|
570
|
+
* `kk_scope_namespace: "issuer"` - whose verified issuer owns the identity's namespace
|
|
571
|
+
* (its first `/`-segment must byte-equal the issuer DID, the self-grant-over-others
|
|
572
|
+
* gate). A rule with no scope binding on any satisfied query denies: an unscoped rule
|
|
573
|
+
* can never authorize a scoped operation.
|
|
574
|
+
*
|
|
575
|
+
* Local evaluation is ADVISORY here as everywhere in this SDK - the verifier
|
|
576
|
+
* committee runs the authoritative check; a wrong local answer costs a wasted request,
|
|
577
|
+
* never access.
|
|
578
|
+
*
|
|
579
|
+
* @param rule - DCQL policy containing identity-scope constraints.
|
|
580
|
+
* @param credentials - Authenticated credential views to evaluate.
|
|
581
|
+
* @param identity - Requested identity string whose scope must be authorized.
|
|
582
|
+
*/
|
|
583
|
+
export function evaluateIdentityScoped(rule, credentials, identity) {
|
|
584
|
+
// Mirrors the reference order: the identity's own shape is checked BEFORE the rule parses
|
|
585
|
+
// - an oversize or NUL-bearing identity is a DENY, not a malformed rule.
|
|
586
|
+
if (byteLen(identity) > MAX_IDENTITY_LEN || identity.includes('\0'))
|
|
587
|
+
return false;
|
|
588
|
+
const q = validate(rule);
|
|
589
|
+
const satisfied = evaluateWho(q, credentials);
|
|
590
|
+
if (satisfied === null)
|
|
591
|
+
return false;
|
|
592
|
+
for (const cq of q.credentials) {
|
|
593
|
+
if (!satisfied.has(cq.id))
|
|
594
|
+
continue;
|
|
595
|
+
const scopePath = cq.kk_identity_scope_claim;
|
|
596
|
+
if (scopePath === undefined)
|
|
597
|
+
continue;
|
|
598
|
+
// validate() guarantees a namespace whenever the scope claim is present.
|
|
599
|
+
const namespace = cq.kk_scope_namespace;
|
|
600
|
+
if (namespace === undefined)
|
|
601
|
+
continue;
|
|
602
|
+
// Every credential MATCHING this query is a candidate grantor.
|
|
603
|
+
for (const c of credentials) {
|
|
604
|
+
if (matches(cq, c) && credentialGrantCovers(c, scopePath, namespace, identity)) {
|
|
605
|
+
return true;
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
// "No scope binding on any satisfied query" and "no covering grant" are both
|
|
610
|
+
// denials - the reference implementation distinguishes them only in the Reject message.
|
|
611
|
+
return false;
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* Whether credential `c`'s scope claim at `scopePath` covers `identity`, honoring the
|
|
615
|
+
* namespace binding. Under `"issuer"` the identity's first `/`-segment must byte-equal
|
|
616
|
+
* `c`'s verified issuer DID; under `"any"` no such gate (the validator already required
|
|
617
|
+
* a pinned issuer set for `"any"`).
|
|
618
|
+
*/
|
|
619
|
+
function credentialGrantCovers(c, scopePath, namespace, identity) {
|
|
620
|
+
if (namespace === 'issuer') {
|
|
621
|
+
const issuer = c.claim(['iss']);
|
|
622
|
+
if (!issuer.found || typeof issuer.value !== 'string')
|
|
623
|
+
return false;
|
|
624
|
+
const firstSegment = identity.split('/')[0];
|
|
625
|
+
if (firstSegment !== issuer.value)
|
|
626
|
+
return false;
|
|
627
|
+
}
|
|
628
|
+
const got = c.claim(scopePath);
|
|
629
|
+
if (!got.found)
|
|
630
|
+
return false;
|
|
631
|
+
if (typeof got.value === 'string')
|
|
632
|
+
return scopeCovers(got.value, identity);
|
|
633
|
+
if (Array.isArray(got.value)) {
|
|
634
|
+
return got.value.some(g => typeof g === 'string' && scopeCovers(g, identity));
|
|
635
|
+
}
|
|
636
|
+
return false;
|
|
637
|
+
}
|
|
638
|
+
/** One credential query against one credential. */
|
|
639
|
+
/** One credential query against one credential - exported for the wallet's per-query planning. */
|
|
640
|
+
export function credentialMatches(q, c) {
|
|
641
|
+
return matches(q, c);
|
|
642
|
+
}
|
|
643
|
+
function matches(q, c) {
|
|
644
|
+
if (c.format !== q.format)
|
|
645
|
+
return false;
|
|
646
|
+
const typeValues = q.meta?.type_values;
|
|
647
|
+
if (typeValues !== undefined) {
|
|
648
|
+
// Outer = alternatives (any), inner = all-required.
|
|
649
|
+
const ok = typeValues.some(alt => alt.every(t => c.types.includes(t)));
|
|
650
|
+
if (!ok)
|
|
651
|
+
return false;
|
|
652
|
+
}
|
|
653
|
+
// `dc+sd-jwt`: the view's `types` carry the credential's `vct`; any listed value matches.
|
|
654
|
+
const vctValues = q.meta?.vct_values;
|
|
655
|
+
if (vctValues !== undefined && !c.types.some(t => vctValues.includes(t)))
|
|
656
|
+
return false;
|
|
657
|
+
for (const claim of q.claims ?? []) {
|
|
658
|
+
const got = c.claim(claim.path);
|
|
659
|
+
if (!got.found)
|
|
660
|
+
return false;
|
|
661
|
+
if (!claimValueMatches(claim.path, claim.values, got.value))
|
|
662
|
+
return false;
|
|
663
|
+
}
|
|
664
|
+
return true;
|
|
665
|
+
}
|
|
666
|
+
/**
|
|
667
|
+
* Whether a resolved claim value satisfies a claim query's `values`.
|
|
668
|
+
*
|
|
669
|
+
* Without a `null` segment this is whole-value equality, unchanged. WITH one, the resolved
|
|
670
|
+
* value is the SELECTION, so the question becomes "does ANY selected element match ANY
|
|
671
|
+
* listed value" - the only reading under which a rule can say "this role is among the
|
|
672
|
+
* holder's roles".
|
|
673
|
+
*/
|
|
674
|
+
function claimValueMatches(path, values, actual) {
|
|
675
|
+
const wildcard = selectsMany(path);
|
|
676
|
+
// A wildcard that selected nothing is ABSENT, not present-and-empty: `resolvePath`
|
|
677
|
+
// answers `[]` for `null` over an empty array, and treating that as "present" would let a
|
|
678
|
+
// values-less entry match a holder with no roles at all.
|
|
679
|
+
if (wildcard && Array.isArray(actual) && actual.length === 0)
|
|
680
|
+
return false;
|
|
681
|
+
if (values === undefined)
|
|
682
|
+
return true;
|
|
683
|
+
if (wildcard && Array.isArray(actual)) {
|
|
684
|
+
return actual.some((elem) => values.some((v) => jsonEqual(v, elem)));
|
|
685
|
+
}
|
|
686
|
+
return values.some((v) => jsonEqual(v, actual));
|
|
687
|
+
}
|
|
688
|
+
/**
|
|
689
|
+
* Structural JSON equality, via the canonical form.
|
|
690
|
+
*
|
|
691
|
+
* Reusing JCS rather than hand-rolling a deep-equal keeps ONE notion of "the same JSON
|
|
692
|
+
* value" shared with the commitment, and it is key-order insensitive by construction.
|
|
693
|
+
*
|
|
694
|
+
* A KNOWN, UNFIXABLE CROSS-LANGUAGE EDGE: the reference JSON parser distinguishes the
|
|
695
|
+
* integer `3` from the float `3.0`, so a rule allowing `3.0` does not match a
|
|
696
|
+
* credential carrying `3`. JavaScript has ONE number type - `JSON.parse('3')` and
|
|
697
|
+
* `JSON.parse('3.0')` are indistinguishable - so this file cannot reproduce that
|
|
698
|
+
* distinction, and would grant where the reference implementation denies. Do not write a rule whose
|
|
699
|
+
* correctness depends on it. (`"3"` vs `3` - string vs number - IS distinguished on
|
|
700
|
+
* both sides, and the corpus pins it.)
|
|
701
|
+
*/
|
|
702
|
+
function jsonEqual(a, b) {
|
|
703
|
+
return jcs(a) === jcs(b);
|
|
704
|
+
}
|
|
705
|
+
// canonicalisation + commitment
|
|
706
|
+
/**
|
|
707
|
+
* RFC 8785 (JCS) canonical form: sorted keys, no insignificant whitespace, ECMAScript
|
|
708
|
+
* number formatting, UTF-8.
|
|
709
|
+
*
|
|
710
|
+
* Why the commitment needs this at all: the rule stops being an opaque string the
|
|
711
|
+
* moment it becomes the `dcql_query` inside a signed OID4VP request object. It must be
|
|
712
|
+
* parsed and re-serialised, and any JSON library may reorder keys or restyle
|
|
713
|
+
* whitespace. Hashing raw bytes would break the commitment at exactly the point the
|
|
714
|
+
* rule is used for its new purpose.
|
|
715
|
+
*
|
|
716
|
+
* @param rule - DCQL policy encoded as JSON text.
|
|
717
|
+
*/
|
|
718
|
+
export function canonicalize(rule) {
|
|
719
|
+
let v;
|
|
720
|
+
try {
|
|
721
|
+
v = JSON.parse(rule);
|
|
722
|
+
}
|
|
723
|
+
catch (e) {
|
|
724
|
+
throw malformed(`rule is not valid JSON: ${e instanceof Error ? e.message : String(e)}`);
|
|
725
|
+
}
|
|
726
|
+
return jcs(v);
|
|
727
|
+
}
|
|
728
|
+
/**
|
|
729
|
+
* RFC 8785 serialisation of an already-parsed JSON value.
|
|
730
|
+
*
|
|
731
|
+
* Three details do the work, and each is a place a hand-rolled canonicaliser goes
|
|
732
|
+
* wrong:
|
|
733
|
+
* - **Key order** is by UTF-16 code unit. JS's default `Array.prototype.sort()` on
|
|
734
|
+
* strings compares exactly that, so a bare `.sort()` is correct - but only because
|
|
735
|
+
* it is the default comparator; a locale-aware one would NOT be.
|
|
736
|
+
* - **Numbers** use ECMAScript `Number::toString`, which is what `JSON.stringify`
|
|
737
|
+
* emits for a number. RFC 8785 chose that algorithm precisely so JS needs no
|
|
738
|
+
* special casing.
|
|
739
|
+
* - **Strings** use JSON escaping with the shortest form, and lone surrogates escaped
|
|
740
|
+
* as `\uXXXX` - which is well-formed `JSON.stringify` (ES2019) exactly.
|
|
741
|
+
*/
|
|
742
|
+
function jcs(v) {
|
|
743
|
+
if (v === null)
|
|
744
|
+
return 'null';
|
|
745
|
+
switch (typeof v) {
|
|
746
|
+
case 'boolean':
|
|
747
|
+
return v ? 'true' : 'false';
|
|
748
|
+
case 'number':
|
|
749
|
+
if (!Number.isFinite(v)) {
|
|
750
|
+
throw malformed(`rule could not be canonicalised: ${String(v)} is not a JSON number`);
|
|
751
|
+
}
|
|
752
|
+
return JSON.stringify(v);
|
|
753
|
+
case 'string':
|
|
754
|
+
return JSON.stringify(v);
|
|
755
|
+
}
|
|
756
|
+
if (Array.isArray(v))
|
|
757
|
+
return `[${v.map(jcs).join(',')}]`;
|
|
758
|
+
if (isPlainObject(v)) {
|
|
759
|
+
const keys = Object.keys(v).sort();
|
|
760
|
+
return `{${keys.map(k => `${JSON.stringify(k)}:${jcs(v[k])}`).join(',')}}`;
|
|
761
|
+
}
|
|
762
|
+
throw malformed(`rule could not be canonicalised: unsupported value of type ${typeof v}`);
|
|
763
|
+
}
|
|
764
|
+
/**
|
|
765
|
+
* True when `rule` parses as a supported OID4VP-DCQL query.
|
|
766
|
+
*
|
|
767
|
+
* This is the grammar dispatch used by the commitment. It must stay a TOTAL function -
|
|
768
|
+
* a legacy kk-DCQL rule is not an error here, it is simply "not OID4VP".
|
|
769
|
+
*
|
|
770
|
+
* @param rule - Policy text to check for a supported DCQL shape.
|
|
771
|
+
*/
|
|
772
|
+
export function isOid4vpRule(rule) {
|
|
773
|
+
try {
|
|
774
|
+
validate(rule);
|
|
775
|
+
return true;
|
|
776
|
+
}
|
|
777
|
+
catch {
|
|
778
|
+
return false;
|
|
779
|
+
}
|
|
780
|
+
}
|