@prson/login-verifier 0.0.0-stage → 0.1.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/README.md +246 -2
- package/dist/index.d.ts +172 -0
- package/dist/index.js +602 -0
- package/dist/notify.d.ts +48 -0
- package/dist/notify.js +46 -0
- package/dist/status.d.ts +50 -0
- package/dist/status.js +105 -0
- package/dist/verdict.d.ts +84 -0
- package/dist/verdict.js +117 -0
- package/package.json +53 -4
package/dist/notify.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// Website notifications, server side (documentation/SITE_NOTIFICATIONS.md).
|
|
2
|
+
//
|
|
3
|
+
// When the site signed in with `signIn({ notifications: true })` and the
|
|
4
|
+
// person ticked "Let {site} send you notifications", the browser SDK's
|
|
5
|
+
// `SignInResult.notifications.handle` carries a handle for that person at the
|
|
6
|
+
// site. Post it to your backend beside the presentation; read it there with
|
|
7
|
+
// `readNotifyHandle`, keep it with the account, and send with
|
|
8
|
+
// `sendNotification` (an API key with notify:write, for an app whose
|
|
9
|
+
// registered domains include the sign-in domain). The handle isn't inside the
|
|
10
|
+
// signed presentation: accept it only on the request that also carried a
|
|
11
|
+
// presentation you verified, and store it on that account.
|
|
12
|
+
/** `nh_` and 32 random bytes, base64url. */
|
|
13
|
+
export const NOTIFY_HANDLE = /^nh_[A-Za-z0-9_-]{43}$/;
|
|
14
|
+
/**
|
|
15
|
+
* The handle from what your page posted: the SignInResult (or its
|
|
16
|
+
* `notifications`), or the handle itself. null when there's none or it is
|
|
17
|
+
* malformed.
|
|
18
|
+
*/
|
|
19
|
+
export function readNotifyHandle(value) {
|
|
20
|
+
let v = value;
|
|
21
|
+
if (v && typeof v === "object" && "notifications" in v)
|
|
22
|
+
v = v.notifications;
|
|
23
|
+
if (v && typeof v === "object" && "handle" in v)
|
|
24
|
+
v = v.handle;
|
|
25
|
+
return typeof v === "string" && NOTIFY_HANDLE.test(v) ? v : null;
|
|
26
|
+
}
|
|
27
|
+
/** POST /v1/notify. Never throws for an API refusal; network errors propagate. */
|
|
28
|
+
export async function sendNotification(opts) {
|
|
29
|
+
const doFetch = opts.fetch ?? globalThis.fetch;
|
|
30
|
+
const base = (opts.apiBase ?? "https://api.prson.net").replace(/\/+$/, "");
|
|
31
|
+
const res = await doFetch(`${base}/v1/notify`, {
|
|
32
|
+
method: "POST",
|
|
33
|
+
headers: { Authorization: `Bearer ${opts.apiKey}`, "Content-Type": "application/json" },
|
|
34
|
+
body: JSON.stringify({ handle: opts.handle, title: opts.title, body: opts.body, ...(opts.url ? { url: opts.url } : {}) }),
|
|
35
|
+
});
|
|
36
|
+
const json = (await res.json().catch(() => ({})));
|
|
37
|
+
if (res.status === 202 && typeof json.id === "string")
|
|
38
|
+
return { ok: true, id: json.id, status: res.status };
|
|
39
|
+
return {
|
|
40
|
+
ok: false,
|
|
41
|
+
status: res.status,
|
|
42
|
+
error: typeof json.error === "string" ? json.error : `http_${res.status}`,
|
|
43
|
+
...(typeof json.message === "string" ? { message: json.message } : {}),
|
|
44
|
+
revoked: res.status === 410,
|
|
45
|
+
};
|
|
46
|
+
}
|
package/dist/status.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Revocation check: IETF Token Status List (draft-ietf-oauth-status-list).
|
|
3
|
+
*
|
|
4
|
+
* Every credential Prson issues carries
|
|
5
|
+
* status: { status_list: { idx, uri } }
|
|
6
|
+
* and `uri` serves a Status List Token: a JWT (typ statuslist+jwt) signed by
|
|
7
|
+
* the same issuer key as the credential, whose status_list.lst is a
|
|
8
|
+
* base64url, DEFLATE (ZLIB) compressed bitstring (`bits` bits per entry,
|
|
9
|
+
* index 0 in the lowest bits of byte 0). 0 = VALID; anything else (1
|
|
10
|
+
* INVALID, 2 SUSPENDED) means the credential must not be accepted.
|
|
11
|
+
*
|
|
12
|
+
* Lists are cached briefly (default 60 s, never longer than the token's own
|
|
13
|
+
* ttl). When the list can't be fetched or checked, `onUnavailable` decides:
|
|
14
|
+
* "fail-closed" rejects, "fail-open" accepts. The default is closed for
|
|
15
|
+
* verified humans (verification_level 5 or more, or verified_human: true)
|
|
16
|
+
* and open below that.
|
|
17
|
+
*/
|
|
18
|
+
export type StatusPolicy = "fail-open" | "fail-closed";
|
|
19
|
+
type JsonWebKey = import("node:crypto").webcrypto.JsonWebKey;
|
|
20
|
+
export type StatusCheckOptions = {
|
|
21
|
+
/** What to do when the status list can't be fetched or verified. Default: closed at level 5+, open below. */
|
|
22
|
+
onUnavailable?: StatusPolicy;
|
|
23
|
+
/** How long a fetched list is reused (seconds, default 60; capped by the token's ttl). */
|
|
24
|
+
cacheSeconds?: number;
|
|
25
|
+
/** The issuer's key for a kid (the same keys that sign credentials). */
|
|
26
|
+
resolveKey: (kid: string) => Promise<JsonWebKey | null>;
|
|
27
|
+
/** Override fetch (tests, proxies). */
|
|
28
|
+
fetch?: (uri: string) => Promise<{
|
|
29
|
+
ok: boolean;
|
|
30
|
+
status: number;
|
|
31
|
+
text(): Promise<string>;
|
|
32
|
+
}>;
|
|
33
|
+
/** Clock override (ms). */
|
|
34
|
+
now?: () => number;
|
|
35
|
+
};
|
|
36
|
+
export type StatusCheckResult = {
|
|
37
|
+
ok: true;
|
|
38
|
+
checked: boolean;
|
|
39
|
+
unavailable?: string;
|
|
40
|
+
} | {
|
|
41
|
+
ok: false;
|
|
42
|
+
reason: "vc_revoked" | "status_unavailable";
|
|
43
|
+
detail?: string;
|
|
44
|
+
};
|
|
45
|
+
/** Tests: forget cached lists. */
|
|
46
|
+
export declare function clearStatusCache(): void;
|
|
47
|
+
/** Whether the credential's claims describe a verified human (where an unknown status fails closed by default). */
|
|
48
|
+
export declare function isStrongCredential(payload: Record<string, unknown>): boolean;
|
|
49
|
+
export declare function checkCredentialStatus(payload: Record<string, unknown>, opts: StatusCheckOptions): Promise<StatusCheckResult>;
|
|
50
|
+
export {};
|
package/dist/status.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Revocation check: IETF Token Status List (draft-ietf-oauth-status-list).
|
|
3
|
+
*
|
|
4
|
+
* Every credential Prson issues carries
|
|
5
|
+
* status: { status_list: { idx, uri } }
|
|
6
|
+
* and `uri` serves a Status List Token: a JWT (typ statuslist+jwt) signed by
|
|
7
|
+
* the same issuer key as the credential, whose status_list.lst is a
|
|
8
|
+
* base64url, DEFLATE (ZLIB) compressed bitstring (`bits` bits per entry,
|
|
9
|
+
* index 0 in the lowest bits of byte 0). 0 = VALID; anything else (1
|
|
10
|
+
* INVALID, 2 SUSPENDED) means the credential must not be accepted.
|
|
11
|
+
*
|
|
12
|
+
* Lists are cached briefly (default 60 s, never longer than the token's own
|
|
13
|
+
* ttl). When the list can't be fetched or checked, `onUnavailable` decides:
|
|
14
|
+
* "fail-closed" rejects, "fail-open" accepts. The default is closed for
|
|
15
|
+
* verified humans (verification_level 5 or more, or verified_human: true)
|
|
16
|
+
* and open below that.
|
|
17
|
+
*/
|
|
18
|
+
const cache = new Map();
|
|
19
|
+
/** Tests: forget cached lists. */
|
|
20
|
+
export function clearStatusCache() {
|
|
21
|
+
cache.clear();
|
|
22
|
+
}
|
|
23
|
+
/** Whether the credential's claims describe a verified human (where an unknown status fails closed by default). */
|
|
24
|
+
export function isStrongCredential(payload) {
|
|
25
|
+
return payload.verified_human === true || (typeof payload.verification_level === "number" && payload.verification_level >= 5);
|
|
26
|
+
}
|
|
27
|
+
export async function checkCredentialStatus(payload, opts) {
|
|
28
|
+
const ref = payload.status?.status_list;
|
|
29
|
+
if (!ref)
|
|
30
|
+
return { ok: true, checked: false };
|
|
31
|
+
const policy = opts.onUnavailable ?? (isStrongCredential(payload) ? "fail-closed" : "fail-open");
|
|
32
|
+
const unavailable = (detail) => policy === "fail-open" ? { ok: true, checked: false, unavailable: detail } : { ok: false, reason: "status_unavailable", detail };
|
|
33
|
+
const idx = ref.idx;
|
|
34
|
+
const uri = ref.uri;
|
|
35
|
+
if (typeof idx !== "number" || !Number.isInteger(idx) || idx < 0 || typeof uri !== "string" || !/^https:\/\//.test(uri)) {
|
|
36
|
+
return { ok: false, reason: "status_unavailable", detail: "malformed status reference" };
|
|
37
|
+
}
|
|
38
|
+
let entry;
|
|
39
|
+
try {
|
|
40
|
+
entry = await statusList(uri, opts);
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
return unavailable(e.message);
|
|
44
|
+
}
|
|
45
|
+
const bitOffset = idx * entry.bits;
|
|
46
|
+
const byte = entry.list[bitOffset >> 3];
|
|
47
|
+
if (byte === undefined)
|
|
48
|
+
return unavailable("index outside the list");
|
|
49
|
+
const value = (byte >> (bitOffset & 7)) & ((1 << entry.bits) - 1);
|
|
50
|
+
if (value !== 0)
|
|
51
|
+
return { ok: false, reason: "vc_revoked", detail: value === 2 ? "suspended" : "invalid" };
|
|
52
|
+
return { ok: true, checked: true };
|
|
53
|
+
}
|
|
54
|
+
async function statusList(uri, opts) {
|
|
55
|
+
const now = opts.now?.() ?? Date.now();
|
|
56
|
+
const cached = cache.get(uri);
|
|
57
|
+
if (cached && now - cached.fetchedAt < cached.ttlMs && (cached.exp === null || cached.exp * 1000 > now))
|
|
58
|
+
return cached;
|
|
59
|
+
const doFetch = opts.fetch ?? ((u) => fetch(u, { headers: { Accept: "application/statuslist+jwt" } }));
|
|
60
|
+
const res = await doFetch(uri);
|
|
61
|
+
if (!res.ok)
|
|
62
|
+
throw new Error(`status list HTTP ${res.status}`);
|
|
63
|
+
const jwt = (await res.text()).trim();
|
|
64
|
+
const [h, p, s] = jwt.split(".");
|
|
65
|
+
if (!h || !p || !s)
|
|
66
|
+
throw new Error("status list token malformed");
|
|
67
|
+
const header = JSON.parse(utf8(b64uBytes(h)));
|
|
68
|
+
const payload = JSON.parse(utf8(b64uBytes(p)));
|
|
69
|
+
if (header.typ !== "statuslist+jwt" || header.alg !== "RS256" || !header.kid)
|
|
70
|
+
throw new Error("status list token header");
|
|
71
|
+
const jwk = await opts.resolveKey(header.kid);
|
|
72
|
+
if (!jwk)
|
|
73
|
+
throw new Error("status list key unknown");
|
|
74
|
+
const key = await crypto.subtle.importKey("jwk", jwk, { name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" }, false, ["verify"]);
|
|
75
|
+
if (!(await crypto.subtle.verify("RSASSA-PKCS1-v1_5", key, b64uBytes(s), new TextEncoder().encode(`${h}.${p}`))))
|
|
76
|
+
throw new Error("status list signature");
|
|
77
|
+
if (payload.sub !== uri)
|
|
78
|
+
throw new Error("status list subject");
|
|
79
|
+
if (typeof payload.exp === "number" && payload.exp * 1000 <= now)
|
|
80
|
+
throw new Error("status list expired");
|
|
81
|
+
const bits = payload.status_list?.bits;
|
|
82
|
+
if (bits !== 1 && bits !== 2 && bits !== 4 && bits !== 8)
|
|
83
|
+
throw new Error("status list bits");
|
|
84
|
+
if (typeof payload.status_list?.lst !== "string")
|
|
85
|
+
throw new Error("status list lst");
|
|
86
|
+
const list = await inflate(b64uBytes(payload.status_list.lst));
|
|
87
|
+
const want = (opts.cacheSeconds ?? 60) * 1000;
|
|
88
|
+
const ttlMs = typeof payload.ttl === "number" ? Math.min(want, payload.ttl * 1000) : want;
|
|
89
|
+
const entry = { fetchedAt: now, ttlMs, exp: typeof payload.exp === "number" ? payload.exp : null, bits, list };
|
|
90
|
+
cache.set(uri, entry);
|
|
91
|
+
return entry;
|
|
92
|
+
}
|
|
93
|
+
async function inflate(bytes) {
|
|
94
|
+
const stream = new Blob([bytes]).stream().pipeThrough(new DecompressionStream("deflate"));
|
|
95
|
+
return new Uint8Array(await new Response(stream).arrayBuffer());
|
|
96
|
+
}
|
|
97
|
+
const utf8 = (b) => new TextDecoder().decode(b);
|
|
98
|
+
function b64uBytes(s) {
|
|
99
|
+
const pad = (4 - (s.length % 4)) % 4;
|
|
100
|
+
const bin = atob((s + "=".repeat(pad)).replace(/-/g, "+").replace(/_/g, "/"));
|
|
101
|
+
const out = new Uint8Array(new ArrayBuffer(bin.length));
|
|
102
|
+
for (let i = 0; i < bin.length; i++)
|
|
103
|
+
out[i] = bin.charCodeAt(i);
|
|
104
|
+
return out;
|
|
105
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prson's signed proof verdicts ("prson-truth-verdict/2").
|
|
3
|
+
*
|
|
4
|
+
* Prson countersigns each proof's verdict as a canonical JSON envelope
|
|
5
|
+
* (RS256: RSASSA-PKCS1-v1_5 with SHA-256, base64 signature over the UTF-8
|
|
6
|
+
* bytes of the sorted-keys JSON). From v2 the envelope also commits to what
|
|
7
|
+
* was disclosed: one `{kind, id, digest}` per disclosed fact, property value
|
|
8
|
+
* or document field, where
|
|
9
|
+
*
|
|
10
|
+
* digest = base64url(SHA-256(canonicalJson([salt, kind, id, value])))
|
|
11
|
+
*
|
|
12
|
+
* The recipient gets each `[salt, kind, id, value]` beside the envelope
|
|
13
|
+
* (SD-JWT style), so whoever later holds those values can prove offline
|
|
14
|
+
* that Prson vouched for exactly them. The salt is 16 random bytes, so the
|
|
15
|
+
* envelope alone reveals nothing guessable.
|
|
16
|
+
*
|
|
17
|
+
* node:crypto only (no `jose`), so this module runs on its own.
|
|
18
|
+
*/
|
|
19
|
+
import { type JsonWebKey, type KeyObject } from "node:crypto";
|
|
20
|
+
export declare const VERDICT_SCOPE_V1 = "prson-truth-verdict/1";
|
|
21
|
+
export declare const VERDICT_SCOPE_V2 = "prson-truth-verdict/2";
|
|
22
|
+
export type VerdictDisclosureKind = "fact" | "property" | "document_field";
|
|
23
|
+
/** `[salt, kind, id, value]`, as the API returns it beside the envelope. */
|
|
24
|
+
export type VerdictDisclosure = [salt: string, kind: VerdictDisclosureKind | string, id: string, value: unknown];
|
|
25
|
+
export type VerdictEnvelope = {
|
|
26
|
+
scope: string;
|
|
27
|
+
proofId: string;
|
|
28
|
+
type?: string | null;
|
|
29
|
+
captureHash?: string | null;
|
|
30
|
+
artefactHashes?: Record<string, string> | null;
|
|
31
|
+
challengeId?: string | null;
|
|
32
|
+
slotId?: string | null;
|
|
33
|
+
signerIdentityId?: string | null;
|
|
34
|
+
level: string;
|
|
35
|
+
verifiedAt: string | null;
|
|
36
|
+
/** v2 only: one commitment per disclosed item. */
|
|
37
|
+
disclosures?: Array<{
|
|
38
|
+
kind: string;
|
|
39
|
+
id: string;
|
|
40
|
+
digest: string;
|
|
41
|
+
}>;
|
|
42
|
+
[k: string]: unknown;
|
|
43
|
+
};
|
|
44
|
+
/** A public key: SPKI PEM, a JWK or a node KeyObject. */
|
|
45
|
+
export type VerdictPublicKey = string | JsonWebKey | KeyObject;
|
|
46
|
+
export type VerifyVerdictOptions = {
|
|
47
|
+
/** One key to check against. */
|
|
48
|
+
publicKey?: VerdictPublicKey;
|
|
49
|
+
/** Prson's keyring (GET /v1/company/public-key `verificationKeys`, or a JWKS's `keys`); `kid` is tried first. */
|
|
50
|
+
keys?: Array<{
|
|
51
|
+
kid?: string | null;
|
|
52
|
+
publicKey: VerdictPublicKey;
|
|
53
|
+
}>;
|
|
54
|
+
/** The seal's `kid` (pins the matching key first). */
|
|
55
|
+
kid?: string | null;
|
|
56
|
+
};
|
|
57
|
+
export type VerifiedDisclosure = {
|
|
58
|
+
kind: string;
|
|
59
|
+
id: string;
|
|
60
|
+
value: unknown;
|
|
61
|
+
verified: boolean;
|
|
62
|
+
reason?: "malformed" | "not-covered" | "digest-mismatch" | "signature-invalid";
|
|
63
|
+
};
|
|
64
|
+
export type VerifyVerdictResult = {
|
|
65
|
+
/** The signature checks and every disclosure is covered by the envelope. */
|
|
66
|
+
ok: boolean;
|
|
67
|
+
signatureValid: boolean;
|
|
68
|
+
version: 1 | 2 | null;
|
|
69
|
+
disclosures: VerifiedDisclosure[];
|
|
70
|
+
reason?: "malformed" | "unsupported-scope" | "no-key" | "signature-invalid" | "disclosure-not-verified";
|
|
71
|
+
};
|
|
72
|
+
/** Sorted-keys JSON, byte-identical to the server's `canonicalJson`. */
|
|
73
|
+
export declare function canonicalJson(value: unknown): string;
|
|
74
|
+
/** The digest a v2 envelope carries for `[salt, kind, id, value]`. */
|
|
75
|
+
export declare function verdictDisclosureDigest(disclosure: VerdictDisclosure): string;
|
|
76
|
+
/**
|
|
77
|
+
* Check Prson's signature on a verdict envelope and each disclosed value
|
|
78
|
+
* against it. A disclosure verifies only when the envelope is v2, the
|
|
79
|
+
* signature checks and the envelope carries an entry with the same kind, id
|
|
80
|
+
* and recomputed digest. A v1 envelope (made before verdicts covered values)
|
|
81
|
+
* can still have a valid signature, but no disclosure verifies against it
|
|
82
|
+
* (`reason: "not-covered"`), so `ok` is false whenever values were given.
|
|
83
|
+
*/
|
|
84
|
+
export declare function verifyVerdict(envelope: VerdictEnvelope | unknown, signature: string, disclosures: VerdictDisclosure[] | unknown[] | null | undefined, opts: VerifyVerdictOptions): VerifyVerdictResult;
|
package/dist/verdict.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prson's signed proof verdicts ("prson-truth-verdict/2").
|
|
3
|
+
*
|
|
4
|
+
* Prson countersigns each proof's verdict as a canonical JSON envelope
|
|
5
|
+
* (RS256: RSASSA-PKCS1-v1_5 with SHA-256, base64 signature over the UTF-8
|
|
6
|
+
* bytes of the sorted-keys JSON). From v2 the envelope also commits to what
|
|
7
|
+
* was disclosed: one `{kind, id, digest}` per disclosed fact, property value
|
|
8
|
+
* or document field, where
|
|
9
|
+
*
|
|
10
|
+
* digest = base64url(SHA-256(canonicalJson([salt, kind, id, value])))
|
|
11
|
+
*
|
|
12
|
+
* The recipient gets each `[salt, kind, id, value]` beside the envelope
|
|
13
|
+
* (SD-JWT style), so whoever later holds those values can prove offline
|
|
14
|
+
* that Prson vouched for exactly them. The salt is 16 random bytes, so the
|
|
15
|
+
* envelope alone reveals nothing guessable.
|
|
16
|
+
*
|
|
17
|
+
* node:crypto only (no `jose`), so this module runs on its own.
|
|
18
|
+
*/
|
|
19
|
+
import { createHash, createPublicKey, createVerify } from "node:crypto";
|
|
20
|
+
export const VERDICT_SCOPE_V1 = "prson-truth-verdict/1";
|
|
21
|
+
export const VERDICT_SCOPE_V2 = "prson-truth-verdict/2";
|
|
22
|
+
/** Sorted-keys JSON, byte-identical to the server's `canonicalJson`. */
|
|
23
|
+
export function canonicalJson(value) {
|
|
24
|
+
if (value === null || value === undefined)
|
|
25
|
+
return "null";
|
|
26
|
+
if (Array.isArray(value))
|
|
27
|
+
return "[" + value.map(canonicalJson).join(",") + "]";
|
|
28
|
+
if (typeof value === "object") {
|
|
29
|
+
const rec = value;
|
|
30
|
+
return "{" + Object.keys(rec).sort().map((k) => JSON.stringify(k) + ":" + canonicalJson(rec[k])).join(",") + "}";
|
|
31
|
+
}
|
|
32
|
+
return JSON.stringify(value);
|
|
33
|
+
}
|
|
34
|
+
/** The digest a v2 envelope carries for `[salt, kind, id, value]`. */
|
|
35
|
+
export function verdictDisclosureDigest(disclosure) {
|
|
36
|
+
return createHash("sha256").update(canonicalJson(disclosure), "utf8").digest("base64url");
|
|
37
|
+
}
|
|
38
|
+
function toKey(k) {
|
|
39
|
+
if (typeof k === "string")
|
|
40
|
+
return createPublicKey(k);
|
|
41
|
+
if ("type" in k && k.type === "public" && typeof k.export === "function")
|
|
42
|
+
return k;
|
|
43
|
+
return createPublicKey({ key: k, format: "jwk" });
|
|
44
|
+
}
|
|
45
|
+
function signatureChecks(envelope, signature, opts) {
|
|
46
|
+
const ring = [...(opts.publicKey ? [{ kid: opts.kid ?? null, publicKey: opts.publicKey }] : []), ...(opts.keys ?? [])];
|
|
47
|
+
if (ring.length === 0)
|
|
48
|
+
return null;
|
|
49
|
+
const ordered = [...ring.filter((k) => opts.kid && k.kid === opts.kid), ...ring.filter((k) => !opts.kid || k.kid !== opts.kid)];
|
|
50
|
+
const sig = Buffer.from(signature, "base64");
|
|
51
|
+
const message = canonicalJson(envelope);
|
|
52
|
+
for (const k of ordered) {
|
|
53
|
+
try {
|
|
54
|
+
const v = createVerify("RSA-SHA256");
|
|
55
|
+
v.update(message, "utf8");
|
|
56
|
+
v.end();
|
|
57
|
+
if (v.verify(toKey(k.publicKey), sig))
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
// An unreadable key or a signature of the wrong size: try the next key.
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Check Prson's signature on a verdict envelope and each disclosed value
|
|
68
|
+
* against it. A disclosure verifies only when the envelope is v2, the
|
|
69
|
+
* signature checks and the envelope carries an entry with the same kind, id
|
|
70
|
+
* and recomputed digest. A v1 envelope (made before verdicts covered values)
|
|
71
|
+
* can still have a valid signature, but no disclosure verifies against it
|
|
72
|
+
* (`reason: "not-covered"`), so `ok` is false whenever values were given.
|
|
73
|
+
*/
|
|
74
|
+
export function verifyVerdict(envelope, signature, disclosures, opts) {
|
|
75
|
+
const list = Array.isArray(disclosures) ? disclosures : [];
|
|
76
|
+
const shaped = list.map((d) => {
|
|
77
|
+
if (!Array.isArray(d) || d.length !== 4 || typeof d[0] !== "string" || typeof d[1] !== "string" || typeof d[2] !== "string") {
|
|
78
|
+
return { kind: "", id: "", value: null, verified: false, reason: "malformed", raw: d };
|
|
79
|
+
}
|
|
80
|
+
return { kind: d[1], id: d[2], value: d[3], verified: false, raw: d };
|
|
81
|
+
});
|
|
82
|
+
const strip = (xs) => xs.map(({ raw: _raw, ...rest }) => rest);
|
|
83
|
+
const env = envelope;
|
|
84
|
+
if (!env || typeof env !== "object" || typeof signature !== "string" || !signature) {
|
|
85
|
+
return { ok: false, signatureValid: false, version: null, disclosures: strip(shaped), reason: "malformed" };
|
|
86
|
+
}
|
|
87
|
+
const version = env.scope === VERDICT_SCOPE_V2 ? 2 : env.scope === VERDICT_SCOPE_V1 ? 1 : null;
|
|
88
|
+
if (version === null)
|
|
89
|
+
return { ok: false, signatureValid: false, version, disclosures: strip(shaped), reason: "unsupported-scope" };
|
|
90
|
+
const sigOk = signatureChecks(env, signature, opts);
|
|
91
|
+
if (sigOk === null)
|
|
92
|
+
return { ok: false, signatureValid: false, version, disclosures: strip(shaped), reason: "no-key" };
|
|
93
|
+
const entries = version === 2 && Array.isArray(env.disclosures) ? env.disclosures : [];
|
|
94
|
+
for (const d of shaped) {
|
|
95
|
+
if (d.reason)
|
|
96
|
+
continue;
|
|
97
|
+
if (!sigOk) {
|
|
98
|
+
d.reason = "signature-invalid";
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
if (version !== 2) {
|
|
102
|
+
d.reason = "not-covered";
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
const digest = verdictDisclosureDigest(d.raw);
|
|
106
|
+
const sameItem = entries.filter((e) => e && e.kind === d.kind && e.id === d.id);
|
|
107
|
+
if (sameItem.some((e) => e.digest === digest))
|
|
108
|
+
d.verified = true;
|
|
109
|
+
else
|
|
110
|
+
d.reason = sameItem.length ? "digest-mismatch" : "not-covered";
|
|
111
|
+
}
|
|
112
|
+
const out = strip(shaped);
|
|
113
|
+
if (!sigOk)
|
|
114
|
+
return { ok: false, signatureValid: false, version, disclosures: out, reason: "signature-invalid" };
|
|
115
|
+
const allOk = out.every((d) => d.verified);
|
|
116
|
+
return { ok: allOk, signatureValid: true, version, disclosures: out, ...(allOk ? {} : { reason: "disclosure-not-verified" }) };
|
|
117
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,55 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prson/login-verifier",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Node.js verifier for SD-JWT-VC presentations issued by the Prson notary and delivered via @prson/login-sdk. Pure-function; no Prson account or API key required.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"!dist/**/*.test.*",
|
|
12
|
+
"!dist/test",
|
|
13
|
+
"README.md",
|
|
14
|
+
"LICENSE"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "tsc",
|
|
18
|
+
"typecheck": "tsc --noEmit",
|
|
19
|
+
"test": "tsc && node --test dist/status.test.js && node --experimental-strip-types --no-warnings --test test/*.test.ts",
|
|
20
|
+
"prepublishOnly": "npm test && npm run build"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"jose": "^6.2.12"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/node": "^20.0.0",
|
|
27
|
+
"typescript": "^5.4.0"
|
|
28
|
+
},
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=20"
|
|
31
|
+
},
|
|
32
|
+
"keywords": [
|
|
33
|
+
"sd-jwt-vc",
|
|
34
|
+
"verifiable-credentials",
|
|
35
|
+
"prson",
|
|
36
|
+
"did-web",
|
|
37
|
+
"verifier"
|
|
38
|
+
],
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "git+https://github.com/Yumankind/prson-sdk.git",
|
|
42
|
+
"directory": "packages/verifier-node"
|
|
43
|
+
},
|
|
44
|
+
"license": "Apache-2.0",
|
|
45
|
+
"author": "Yumankind OÜ <support@prson.net>",
|
|
46
|
+
"homepage": "https://prson.net/developers",
|
|
47
|
+
"bugs": {
|
|
48
|
+
"url": "https://github.com/Yumankind/prson-sdk/issues",
|
|
49
|
+
"email": "security@yumankind.com"
|
|
50
|
+
},
|
|
51
|
+
"publishConfig": {
|
|
52
|
+
"access": "public",
|
|
53
|
+
"provenance": true
|
|
54
|
+
}
|
|
55
|
+
}
|