@impz/client 6.11.5 → 6.12.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/dist/identity.d.ts +26 -0
- package/dist/identity.js +148 -0
- package/dist/index-BE17J_Lz.d.ts +23016 -0
- package/dist/index.d.ts +2 -22596
- package/dist/index.js +10 -3
- package/package.json +5 -1
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { v as IdentityTrustDocument } from "./index-BE17J_Lz.js";
|
|
2
|
+
interface VerifiedImp {
|
|
3
|
+
readonly issuer: string;
|
|
4
|
+
readonly id: string;
|
|
5
|
+
readonly name: string;
|
|
6
|
+
}
|
|
7
|
+
interface VerifyOptions {
|
|
8
|
+
readonly issuers: readonly IdentityTrustDocument[];
|
|
9
|
+
readonly audience: string;
|
|
10
|
+
readonly nowSeconds: number;
|
|
11
|
+
}
|
|
12
|
+
interface SessionHost {
|
|
13
|
+
readonly impID: string | null;
|
|
14
|
+
readonly impIssuer: string | null;
|
|
15
|
+
readonly impName: string | null;
|
|
16
|
+
}
|
|
17
|
+
type SessionCheck = {
|
|
18
|
+
readonly kind: 'allowed';
|
|
19
|
+
} | {
|
|
20
|
+
readonly kind: 'refused';
|
|
21
|
+
readonly reason: 'no-identity' | 'unknown-session' | 'not-in-an-imp' | 'imp-id-unknown' | 'imp-issuer-unknown' | 'other-host' | 'other-imp';
|
|
22
|
+
};
|
|
23
|
+
export declare function checkSessionImp(caller: Readonly<VerifiedImp> | null, session: Readonly<SessionHost> | null): SessionCheck;
|
|
24
|
+
export declare function loadIdentityIssuers(documents: readonly unknown[]): IdentityTrustDocument[];
|
|
25
|
+
export declare function verifyImpIdentity(header: string | null, options: Readonly<VerifyOptions>): Promise<VerifiedImp | null>;
|
|
26
|
+
export type { IdentityTrustDocument, SessionCheck, SessionHost, VerifiedImp, VerifyOptions };
|
package/dist/identity.js
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
// ../api/src/schemas/identity-schema.ts
|
|
3
|
+
const IdentityIssuerSchema = z.string().regex(/^[A-Za-z0-9._-]{1,128}$/u, "must be 1 to 128 letters, digits, \".\", \"_\" or \"-\"");
|
|
4
|
+
const Base64UrlSchema = z.string().regex(/^[A-Za-z0-9_-]+$/u);
|
|
5
|
+
const IdentityJwkSchema = z.object({
|
|
6
|
+
kty: z.literal("EC"),
|
|
7
|
+
crv: z.literal("P-256"),
|
|
8
|
+
x: Base64UrlSchema,
|
|
9
|
+
y: Base64UrlSchema,
|
|
10
|
+
kid: z.string().min(1).max(128),
|
|
11
|
+
alg: z.literal("ES256"),
|
|
12
|
+
use: z.literal("sig")
|
|
13
|
+
});
|
|
14
|
+
const RetireEntrySchema = z.object({
|
|
15
|
+
kid: z.string().min(1).max(128),
|
|
16
|
+
at: z.int().nonnegative()
|
|
17
|
+
});
|
|
18
|
+
const IdentityTrustDocumentSchema = z.object({
|
|
19
|
+
issuer: IdentityIssuerSchema,
|
|
20
|
+
keys: z.array(IdentityJwkSchema).min(1).max(2).readonly(),
|
|
21
|
+
retire: z.array(RetireEntrySchema).max(1).readonly()
|
|
22
|
+
});
|
|
23
|
+
z.object({ at: z.date() });
|
|
24
|
+
// src/identity/check-session-imp.ts
|
|
25
|
+
function checkSessionImp(caller, session) {
|
|
26
|
+
if (caller === null) return {
|
|
27
|
+
kind: "refused",
|
|
28
|
+
reason: "no-identity"
|
|
29
|
+
};
|
|
30
|
+
if (session === null) return {
|
|
31
|
+
kind: "refused",
|
|
32
|
+
reason: "unknown-session"
|
|
33
|
+
};
|
|
34
|
+
if (session.impName === null) return {
|
|
35
|
+
kind: "refused",
|
|
36
|
+
reason: "not-in-an-imp"
|
|
37
|
+
};
|
|
38
|
+
if (session.impID === null) return {
|
|
39
|
+
kind: "refused",
|
|
40
|
+
reason: "imp-id-unknown"
|
|
41
|
+
};
|
|
42
|
+
if (session.impIssuer === null) return {
|
|
43
|
+
kind: "refused",
|
|
44
|
+
reason: "imp-issuer-unknown"
|
|
45
|
+
};
|
|
46
|
+
if (session.impIssuer !== caller.issuer) return {
|
|
47
|
+
kind: "refused",
|
|
48
|
+
reason: "other-host"
|
|
49
|
+
};
|
|
50
|
+
return session.impID === caller.id ? { kind: "allowed" } : {
|
|
51
|
+
kind: "refused",
|
|
52
|
+
reason: "other-imp"
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
// src/identity/load-identity-issuers.ts
|
|
56
|
+
function loadIdentityIssuers(documents) {
|
|
57
|
+
const issuers = documents.map((document) => IdentityTrustDocumentSchema.parse(document));
|
|
58
|
+
checkIdentityIssuers(issuers);
|
|
59
|
+
return issuers;
|
|
60
|
+
}
|
|
61
|
+
function checkIdentityIssuers(issuers) {
|
|
62
|
+
if (issuers.length === 0) throw new Error("no identity key document is configured");
|
|
63
|
+
const names = issuers.map((document) => document.issuer);
|
|
64
|
+
const repeated = names.find((name, index) => names.indexOf(name) !== index);
|
|
65
|
+
if (repeated !== void 0) throw new Error(`two identity key documents name the issuer ${repeated}`);
|
|
66
|
+
}
|
|
67
|
+
// src/identity/decode-base64url.ts
|
|
68
|
+
function decodeBase64Url(text) {
|
|
69
|
+
try {
|
|
70
|
+
const padded = text.replaceAll("-", "+").replaceAll("_", "/").padEnd(Math.ceil(text.length / 4) * 4, "=");
|
|
71
|
+
return Uint8Array.from(atob(padded), (character) => character.codePointAt(0) ?? 0);
|
|
72
|
+
} catch {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
// src/identity/verify-imp-identity.ts
|
|
77
|
+
const COMPACT_JWT = /^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/u;
|
|
78
|
+
const TokenHeaderSchema = z.object({
|
|
79
|
+
alg: z.literal("ES256"),
|
|
80
|
+
kid: z.string().min(1)
|
|
81
|
+
});
|
|
82
|
+
const TokenClaimsSchema = z.object({
|
|
83
|
+
iss: z.string().min(1),
|
|
84
|
+
sub: z.string().min(1),
|
|
85
|
+
aud: z.string(),
|
|
86
|
+
iat: z.int(),
|
|
87
|
+
exp: z.int(),
|
|
88
|
+
imp_name: z.string()
|
|
89
|
+
});
|
|
90
|
+
async function verifyImpIdentity(header, options) {
|
|
91
|
+
checkIdentityIssuers(options.issuers);
|
|
92
|
+
if (header === null || !COMPACT_JWT.test(header)) return null;
|
|
93
|
+
const [headerPart = "", claimsPart = "", signaturePart = ""] = header.split(".");
|
|
94
|
+
const token = {
|
|
95
|
+
header: parseSegment(headerPart, TokenHeaderSchema),
|
|
96
|
+
claims: parseSegment(claimsPart, TokenClaimsSchema)
|
|
97
|
+
};
|
|
98
|
+
if (token.header === null || token.claims === null) return null;
|
|
99
|
+
const claims = token.claims;
|
|
100
|
+
const document = options.issuers.find((candidate) => candidate.issuer === claims.iss);
|
|
101
|
+
const jwk = document?.keys.find((candidate) => candidate.kid === token.header?.kid);
|
|
102
|
+
if (document === void 0 || jwk === void 0 || claims.aud !== options.audience || !isInTime(claims, options.nowSeconds) || isRetired(document, jwk.kid, claims.iat, options.nowSeconds) || !await probeSignature(jwk, `${headerPart}.${claimsPart}`, signaturePart)) return null;
|
|
103
|
+
return {
|
|
104
|
+
issuer: claims.iss,
|
|
105
|
+
id: claims.sub,
|
|
106
|
+
name: claims.imp_name
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function parseSegment(segment, schema) {
|
|
110
|
+
try {
|
|
111
|
+
const bytes = decodeBase64Url(segment);
|
|
112
|
+
if (bytes === null) return null;
|
|
113
|
+
const parsed = schema.safeParse(JSON.parse(new TextDecoder().decode(bytes)));
|
|
114
|
+
return parsed.success ? parsed.data : null;
|
|
115
|
+
} catch {
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function isInTime(claims, nowSeconds) {
|
|
120
|
+
return claims.exp - claims.iat <= 55 && nowSeconds <= claims.exp + 5 && claims.iat <= nowSeconds + 5;
|
|
121
|
+
}
|
|
122
|
+
function isRetired(document, kid, iat, nowSeconds) {
|
|
123
|
+
const entry = document.retire.find((candidate) => candidate.kid === kid);
|
|
124
|
+
return entry !== void 0 && (iat >= entry.at || nowSeconds >= entry.at + 55);
|
|
125
|
+
}
|
|
126
|
+
const ES256_SIGNATURE_BYTES = 64;
|
|
127
|
+
async function probeSignature(jwk, signingInput, signaturePart) {
|
|
128
|
+
const signature = decodeBase64Url(signaturePart);
|
|
129
|
+
if (signature === null || signature.byteLength !== ES256_SIGNATURE_BYTES) return false;
|
|
130
|
+
try {
|
|
131
|
+
const key = await crypto.subtle.importKey("jwk", {
|
|
132
|
+
kty: jwk.kty,
|
|
133
|
+
crv: jwk.crv,
|
|
134
|
+
x: jwk.x,
|
|
135
|
+
y: jwk.y
|
|
136
|
+
}, {
|
|
137
|
+
name: "ECDSA",
|
|
138
|
+
namedCurve: "P-256"
|
|
139
|
+
}, false, ["verify"]);
|
|
140
|
+
return await crypto.subtle.verify({
|
|
141
|
+
name: "ECDSA",
|
|
142
|
+
hash: "SHA-256"
|
|
143
|
+
}, key, signature, new TextEncoder().encode(signingInput));
|
|
144
|
+
} catch {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
export { checkSessionImp, loadIdentityIssuers, verifyImpIdentity };
|