@lumoauth/agent 1.2.0 → 1.4.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/README.md +35 -4
- package/dist/index.d.mts +130 -3
- package/dist/index.d.ts +130 -3
- package/dist/index.js +269 -63
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +244 -36
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -13,6 +13,164 @@ import {
|
|
|
13
13
|
buildPath,
|
|
14
14
|
fetchWithDpop
|
|
15
15
|
} from "@lumoauth/shared";
|
|
16
|
+
|
|
17
|
+
// src/client-assertion.ts
|
|
18
|
+
import {
|
|
19
|
+
createPrivateKey,
|
|
20
|
+
createPublicKey,
|
|
21
|
+
generateKeyPairSync,
|
|
22
|
+
randomBytes,
|
|
23
|
+
sign as cryptoSign,
|
|
24
|
+
constants as cryptoConstants
|
|
25
|
+
} from "crypto";
|
|
26
|
+
var CLIENT_ASSERTION_TYPE = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer";
|
|
27
|
+
var CLIENT_ASSERTION_TYP = "client-authentication+jwt";
|
|
28
|
+
var MAX_ASSERTION_LIFETIME_SECONDS = 600;
|
|
29
|
+
var RSA_ALGORITHMS = ["RS256", "RS384", "RS512", "PS256", "PS384", "PS512"];
|
|
30
|
+
var EC_ALGORITHM_BY_CURVE = {
|
|
31
|
+
prime256v1: "ES256",
|
|
32
|
+
"P-256": "ES256",
|
|
33
|
+
secp384r1: "ES384",
|
|
34
|
+
"P-384": "ES384",
|
|
35
|
+
secp521r1: "ES512",
|
|
36
|
+
"P-521": "ES512"
|
|
37
|
+
};
|
|
38
|
+
var ClientAssertionSigner = class _ClientAssertionSigner {
|
|
39
|
+
/** Generate a new key: ES256 (default), ES384, ES512, EdDSA, or an RSA algorithm (2048-bit key). */
|
|
40
|
+
static generate(algorithm = "ES256", keyId) {
|
|
41
|
+
let privateKey;
|
|
42
|
+
if (algorithm === "ES256" || algorithm === "ES384" || algorithm === "ES512") {
|
|
43
|
+
const namedCurve = { ES256: "P-256", ES384: "P-384", ES512: "P-521" }[algorithm];
|
|
44
|
+
privateKey = generateKeyPairSync("ec", { namedCurve }).privateKey;
|
|
45
|
+
} else if (algorithm === "EdDSA") {
|
|
46
|
+
privateKey = generateKeyPairSync("ed25519").privateKey;
|
|
47
|
+
} else if (RSA_ALGORITHMS.includes(algorithm)) {
|
|
48
|
+
privateKey = generateKeyPairSync("rsa", { modulusLength: 2048 }).privateKey;
|
|
49
|
+
} else {
|
|
50
|
+
throw new Error(`Unsupported client assertion algorithm: ${String(algorithm)}`);
|
|
51
|
+
}
|
|
52
|
+
return new _ClientAssertionSigner({ privateKey, keyId, algorithm });
|
|
53
|
+
}
|
|
54
|
+
/** Load a PEM private key. */
|
|
55
|
+
static fromPem(pem, options = {}) {
|
|
56
|
+
return new _ClientAssertionSigner({ ...options, privateKeyPem: pem });
|
|
57
|
+
}
|
|
58
|
+
constructor(options) {
|
|
59
|
+
if (options.privateKey) {
|
|
60
|
+
this.key = options.privateKey;
|
|
61
|
+
} else if (options.privateKeyPem) {
|
|
62
|
+
this.key = createPrivateKey(options.privateKeyPem);
|
|
63
|
+
} else {
|
|
64
|
+
throw new Error("ClientAssertionSigner needs privateKeyPem or privateKey");
|
|
65
|
+
}
|
|
66
|
+
if (this.key.type !== "private") {
|
|
67
|
+
throw new Error("ClientAssertionSigner needs a private key");
|
|
68
|
+
}
|
|
69
|
+
const natural = naturalAlgorithm(this.key);
|
|
70
|
+
const chosen = options.algorithm ?? natural;
|
|
71
|
+
if (this.key.asymmetricKeyType === "rsa") {
|
|
72
|
+
if (!RSA_ALGORITHMS.includes(chosen)) {
|
|
73
|
+
throw new Error(`${chosen} does not fit an RSA key; use one of ${RSA_ALGORITHMS.join(", ")}`);
|
|
74
|
+
}
|
|
75
|
+
const bits = this.key.asymmetricKeyDetails?.modulusLength ?? 0;
|
|
76
|
+
if (bits < 2048) throw new Error("RSA keys must be at least 2048 bits");
|
|
77
|
+
} else if (chosen !== natural) {
|
|
78
|
+
throw new Error(`${chosen} does not fit this key; it signs with ${natural}`);
|
|
79
|
+
}
|
|
80
|
+
const lifetime = options.lifetimeSeconds ?? 60;
|
|
81
|
+
if (!Number.isInteger(lifetime) || lifetime < 1 || lifetime > MAX_ASSERTION_LIFETIME_SECONDS) {
|
|
82
|
+
throw new Error(`lifetimeSeconds must be an integer from 1 to ${MAX_ASSERTION_LIFETIME_SECONDS}`);
|
|
83
|
+
}
|
|
84
|
+
this.algorithm = chosen;
|
|
85
|
+
this.keyId = options.keyId;
|
|
86
|
+
this.lifetimeSeconds = lifetime;
|
|
87
|
+
this.now = options.now ?? (() => Math.floor(Date.now() / 1e3));
|
|
88
|
+
}
|
|
89
|
+
/** Sign one assertion for `clientId` (the agent ID) and `audience` (the organization's issuer). */
|
|
90
|
+
create(clientId, audience) {
|
|
91
|
+
if (!clientId || !audience) {
|
|
92
|
+
throw new Error("clientId and audience are required for a client assertion");
|
|
93
|
+
}
|
|
94
|
+
const iat = this.now();
|
|
95
|
+
const header = { alg: this.algorithm, typ: CLIENT_ASSERTION_TYP };
|
|
96
|
+
if (this.keyId) header.kid = this.keyId;
|
|
97
|
+
const claims = {
|
|
98
|
+
iss: clientId,
|
|
99
|
+
sub: clientId,
|
|
100
|
+
aud: audience,
|
|
101
|
+
iat,
|
|
102
|
+
exp: iat + this.lifetimeSeconds,
|
|
103
|
+
jti: randomBytes(24).toString("base64url")
|
|
104
|
+
};
|
|
105
|
+
const signingInput = `${b64url(JSON.stringify(header))}.${b64url(JSON.stringify(claims))}`;
|
|
106
|
+
return `${signingInput}.${signAssertion(this.algorithm, Buffer.from(signingInput, "utf8"), this.key).toString("base64url")}`;
|
|
107
|
+
}
|
|
108
|
+
/** The public key as a JWK with `kid` (when set), `alg` and `use: "sig"`. */
|
|
109
|
+
get jwk() {
|
|
110
|
+
const pub = createPublicKey(this.key).export({ format: "jwk" });
|
|
111
|
+
const out = { kty: pub.kty };
|
|
112
|
+
for (const member of ["crv", "x", "y", "n", "e"]) {
|
|
113
|
+
if (typeof pub[member] === "string") out[member] = pub[member];
|
|
114
|
+
}
|
|
115
|
+
if (this.keyId) out.kid = this.keyId;
|
|
116
|
+
out.alg = this.algorithm;
|
|
117
|
+
out.use = "sig";
|
|
118
|
+
return out;
|
|
119
|
+
}
|
|
120
|
+
/** `{ keys: [jwk] }`: what to register on the agent. */
|
|
121
|
+
get jwks() {
|
|
122
|
+
return { keys: [this.jwk] };
|
|
123
|
+
}
|
|
124
|
+
/** The private key as PKCS#8 PEM, for your secret store. */
|
|
125
|
+
get privateKeyPem() {
|
|
126
|
+
return this.key.export({ type: "pkcs8", format: "pem" }).toString();
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
function issuerFor(baseUrl, orgId) {
|
|
130
|
+
return `${baseUrl.replace(/\/+$/, "")}/orgs/${orgId}/api/v1`;
|
|
131
|
+
}
|
|
132
|
+
function naturalAlgorithm(key) {
|
|
133
|
+
switch (key.asymmetricKeyType) {
|
|
134
|
+
case "ec": {
|
|
135
|
+
const curve = String(key.asymmetricKeyDetails?.namedCurve ?? "");
|
|
136
|
+
const alg = EC_ALGORITHM_BY_CURVE[curve];
|
|
137
|
+
if (!alg) throw new Error(`EC keys must use P-256, P-384 or P-521 (got ${curve})`);
|
|
138
|
+
return alg;
|
|
139
|
+
}
|
|
140
|
+
case "ed25519":
|
|
141
|
+
return "EdDSA";
|
|
142
|
+
case "rsa":
|
|
143
|
+
return "RS256";
|
|
144
|
+
default:
|
|
145
|
+
throw new Error(`Unsupported key type: ${String(key.asymmetricKeyType)}`);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
function signAssertion(alg, data, key) {
|
|
149
|
+
const hash = `sha${alg.slice(2)}`;
|
|
150
|
+
switch (alg) {
|
|
151
|
+
case "ES256":
|
|
152
|
+
case "ES384":
|
|
153
|
+
case "ES512":
|
|
154
|
+
return cryptoSign(hash, data, { key, dsaEncoding: "ieee-p1363" });
|
|
155
|
+
case "EdDSA":
|
|
156
|
+
return cryptoSign(null, data, key);
|
|
157
|
+
case "PS256":
|
|
158
|
+
case "PS384":
|
|
159
|
+
case "PS512":
|
|
160
|
+
return cryptoSign(hash, data, {
|
|
161
|
+
key,
|
|
162
|
+
padding: cryptoConstants.RSA_PKCS1_PSS_PADDING,
|
|
163
|
+
saltLength: cryptoConstants.RSA_PSS_SALTLEN_DIGEST
|
|
164
|
+
});
|
|
165
|
+
default:
|
|
166
|
+
return cryptoSign(hash, data, key);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
function b64url(text) {
|
|
170
|
+
return Buffer.from(text, "utf8").toString("base64url");
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// src/agent.ts
|
|
16
174
|
function envVar(name) {
|
|
17
175
|
return typeof process !== "undefined" && process.env ? process.env[name] : void 0;
|
|
18
176
|
}
|
|
@@ -24,13 +182,22 @@ var LumoAgent = class {
|
|
|
24
182
|
this.baseUrl = (options.baseUrl ?? envVar("LUMOAUTH_URL") ?? "https://app.lumoauth.dev").replace(/\/+$/, "");
|
|
25
183
|
this.orgId = options.orgId ?? envVar("LUMOAUTH_ORG_ID") ?? "";
|
|
26
184
|
this.clientId = options.clientId ?? envVar("AGENT_CLIENT_ID") ?? "";
|
|
27
|
-
|
|
185
|
+
const privateKey = options.privateKey ?? envVar("AGENT_PRIVATE_KEY");
|
|
186
|
+
const keyId = options.keyId ?? envVar("AGENT_KEY_ID");
|
|
187
|
+
this.assertionSigner = privateKey === void 0 || privateKey === "" ? void 0 : privateKey instanceof ClientAssertionSigner ? privateKey : ClientAssertionSigner.fromPem(privateKey, { keyId });
|
|
188
|
+
this.clientSecret = options.clientSecret ?? (this.assertionSigner ? "" : envVar("AGENT_CLIENT_SECRET") ?? "");
|
|
189
|
+
this.issuer = options.issuer;
|
|
28
190
|
this.scopes = options.scopes;
|
|
29
191
|
this.fetchFn = options.fetch ?? globalThis.fetch.bind(globalThis);
|
|
30
192
|
this.dpop = options.dpop;
|
|
31
|
-
if (!this.clientId || !this.clientSecret) {
|
|
193
|
+
if (!this.clientId || !this.clientSecret && !this.assertionSigner) {
|
|
32
194
|
throw new LumoAuthConfigError(
|
|
33
|
-
"Agent credentials required. Set AGENT_CLIENT_ID and AGENT_CLIENT_SECRET
|
|
195
|
+
"Agent credentials required. Set AGENT_CLIENT_ID and AGENT_CLIENT_SECRET (or AGENT_PRIVATE_KEY for a private key JWT agent), or pass clientId with clientSecret or privateKey to the constructor."
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
if (this.clientSecret && this.assertionSigner) {
|
|
199
|
+
throw new LumoAuthConfigError(
|
|
200
|
+
"Pass clientSecret or privateKey, not both: an agent authenticates with one method."
|
|
34
201
|
);
|
|
35
202
|
}
|
|
36
203
|
const tokenProvider = () => this.getAccessToken();
|
|
@@ -72,9 +239,14 @@ var LumoAgent = class {
|
|
|
72
239
|
async authenticate(scopes) {
|
|
73
240
|
const form = {
|
|
74
241
|
grant_type: "client_credentials",
|
|
75
|
-
client_id: this.clientId
|
|
76
|
-
client_secret: this.clientSecret
|
|
242
|
+
client_id: this.clientId
|
|
77
243
|
};
|
|
244
|
+
if (this.assertionSigner) {
|
|
245
|
+
form.client_assertion_type = CLIENT_ASSERTION_TYPE;
|
|
246
|
+
form.client_assertion = this.createClientAssertion();
|
|
247
|
+
} else {
|
|
248
|
+
form.client_secret = this.clientSecret;
|
|
249
|
+
}
|
|
78
250
|
const requested = scopes ?? this.scopes;
|
|
79
251
|
if (requested?.length) form.scope = requested.join(" ");
|
|
80
252
|
const url = `${this.baseUrl}${buildPath(ROUTES["oauth.token"].path, { orgId: this.requireOrgId() })}`;
|
|
@@ -118,6 +290,21 @@ var LumoAgent = class {
|
|
|
118
290
|
get tokenType() {
|
|
119
291
|
return this.token?.tokenType ?? null;
|
|
120
292
|
}
|
|
293
|
+
/**
|
|
294
|
+
* A new `private_key_jwt` client assertion for this agent (single-use),
|
|
295
|
+
* addressed to `issuer` or `{baseUrl}/orgs/{orgId}/api/v1`.
|
|
296
|
+
*/
|
|
297
|
+
createClientAssertion() {
|
|
298
|
+
if (!this.assertionSigner) {
|
|
299
|
+
throw new LumoAuthConfigError("This agent signs no client assertions: create it with privateKey.");
|
|
300
|
+
}
|
|
301
|
+
const audience = this.issuer ?? issuerFor(this.baseUrl, this.requireOrgId());
|
|
302
|
+
return this.assertionSigner.create(this.clientId, audience);
|
|
303
|
+
}
|
|
304
|
+
/** The client assertion signer this agent was configured with, if any. */
|
|
305
|
+
get clientAssertionSigner() {
|
|
306
|
+
return this.assertionSigner;
|
|
307
|
+
}
|
|
121
308
|
/** The DPoP proof signer this agent was configured with, if any. */
|
|
122
309
|
get dpopSigner() {
|
|
123
310
|
return this.dpop;
|
|
@@ -170,6 +357,22 @@ var LumoAgent = class {
|
|
|
170
357
|
async budget() {
|
|
171
358
|
return this.agents.budget();
|
|
172
359
|
}
|
|
360
|
+
/**
|
|
361
|
+
* Report the model tokens and cost that the agent just used, for example
|
|
362
|
+
* the `usage` that the model provider returned. LumoAuth does not see
|
|
363
|
+
* model calls, so token and cost limits count only reported usage. The
|
|
364
|
+
* result's `budget.exhausted` says whether the next metered call (Ask,
|
|
365
|
+
* token or JIT request) will get 429.
|
|
366
|
+
*
|
|
367
|
+
* ```ts
|
|
368
|
+
* const resp = await llm.chat(...);
|
|
369
|
+
* const { budget } = await agent.reportUsage({ tokens: resp.usage.total_tokens, costUsd: 0.0042 });
|
|
370
|
+
* if (budget.exhausted) throw new Error('budget spent');
|
|
371
|
+
* ```
|
|
372
|
+
*/
|
|
373
|
+
async reportUsage(report) {
|
|
374
|
+
return this.agents.reportUsage(report);
|
|
375
|
+
}
|
|
173
376
|
// ── Internal ──────────────────────────────────────────────────────
|
|
174
377
|
requireOrgId() {
|
|
175
378
|
if (!this.orgId) {
|
|
@@ -182,9 +385,9 @@ var LumoAgent = class {
|
|
|
182
385
|
};
|
|
183
386
|
|
|
184
387
|
// src/keys.ts
|
|
185
|
-
import { createHash, createPublicKey, createPrivateKey, generateKeyPairSync } from "crypto";
|
|
388
|
+
import { createHash, createPublicKey as createPublicKey2, createPrivateKey as createPrivateKey2, generateKeyPairSync as generateKeyPairSync2 } from "crypto";
|
|
186
389
|
function generateKeypair(kid = "key-1") {
|
|
187
|
-
const { privateKey, publicKey } =
|
|
390
|
+
const { privateKey, publicKey } = generateKeyPairSync2("ed25519");
|
|
188
391
|
const privateKeyPem = privateKey.export({ type: "pkcs8", format: "pem" }).toString();
|
|
189
392
|
const publicKeyPem = publicKey.export({ type: "spki", format: "pem" }).toString();
|
|
190
393
|
const rawJwk = publicKey.export({ format: "jwk" });
|
|
@@ -215,7 +418,7 @@ function jwkThumbprint(jwk) {
|
|
|
215
418
|
return createHash("sha256").update(canonical, "utf8").digest("base64url");
|
|
216
419
|
}
|
|
217
420
|
function loadPrivateKey(pem) {
|
|
218
|
-
const key =
|
|
421
|
+
const key = createPrivateKey2(pem);
|
|
219
422
|
if (key.asymmetricKeyType === "ed25519") {
|
|
220
423
|
return { key, type: "ed25519" };
|
|
221
424
|
}
|
|
@@ -227,8 +430,8 @@ function loadPrivateKey(pem) {
|
|
|
227
430
|
);
|
|
228
431
|
}
|
|
229
432
|
function publicJwkFromPrivateKey(pem, kid) {
|
|
230
|
-
const priv =
|
|
231
|
-
const pub =
|
|
433
|
+
const priv = createPrivateKey2(pem);
|
|
434
|
+
const pub = createPublicKey2(priv);
|
|
232
435
|
const jwk = pub.export({ format: "jwk" });
|
|
233
436
|
if (kid) jwk.kid = kid;
|
|
234
437
|
return jwk;
|
|
@@ -238,8 +441,8 @@ function publicJwkFromPrivateKey(pem, kid) {
|
|
|
238
441
|
import {
|
|
239
442
|
constants,
|
|
240
443
|
createHash as createHash2,
|
|
241
|
-
randomBytes,
|
|
242
|
-
sign as
|
|
444
|
+
randomBytes as randomBytes2,
|
|
445
|
+
sign as cryptoSign2,
|
|
243
446
|
verify as cryptoVerify
|
|
244
447
|
} from "crypto";
|
|
245
448
|
var AAUTH_COVERED_COMPONENTS = [
|
|
@@ -257,7 +460,7 @@ function contentDigestSha256(body) {
|
|
|
257
460
|
return `sha-256=:${digest}:`;
|
|
258
461
|
}
|
|
259
462
|
function generateNonce() {
|
|
260
|
-
return
|
|
463
|
+
return randomBytes2(16).toString("base64url");
|
|
261
464
|
}
|
|
262
465
|
function buildSignatureBase(components, values, created, nonce) {
|
|
263
466
|
const lines = components.map((c) => `"${c}": ${values[c] ?? ""}`);
|
|
@@ -272,9 +475,9 @@ function signSignatureBase(signatureBase, privateKey) {
|
|
|
272
475
|
const data = Buffer.from(signatureBase, "utf8");
|
|
273
476
|
let signature;
|
|
274
477
|
if (privateKey.asymmetricKeyType === "ed25519") {
|
|
275
|
-
signature =
|
|
478
|
+
signature = cryptoSign2(null, data, privateKey);
|
|
276
479
|
} else {
|
|
277
|
-
signature =
|
|
480
|
+
signature = cryptoSign2("sha512", data, {
|
|
278
481
|
key: privateKey,
|
|
279
482
|
padding: constants.RSA_PKCS1_PSS_PADDING,
|
|
280
483
|
saltLength: constants.RSA_PSS_SALTLEN_DIGEST
|
|
@@ -337,7 +540,7 @@ function signRequest(privateKeyPem, method, url, options = {}) {
|
|
|
337
540
|
}
|
|
338
541
|
|
|
339
542
|
// src/verify.ts
|
|
340
|
-
import { constants as constants2, createPublicKey as
|
|
543
|
+
import { constants as constants2, createPublicKey as createPublicKey3, verify as cryptoVerify2 } from "crypto";
|
|
341
544
|
|
|
342
545
|
// src/errors.ts
|
|
343
546
|
import { LumoAuthError } from "@lumoauth/shared";
|
|
@@ -409,7 +612,7 @@ async function verifyAuthToken(token, options) {
|
|
|
409
612
|
throw new AAuthError(`Key not found: kid=${kid}`, "invalid_token");
|
|
410
613
|
}
|
|
411
614
|
assertAlgMatchesKey(alg, jwk);
|
|
412
|
-
const publicKey =
|
|
615
|
+
const publicKey = createPublicKey3({ key: jwk, format: "jwk" });
|
|
413
616
|
const data = Buffer.from(`${parts[0]}.${parts[1]}`, "utf8");
|
|
414
617
|
const signature = Buffer.from(parts[2], "base64url");
|
|
415
618
|
if (!verifier(data, publicKey, signature)) {
|
|
@@ -727,11 +930,11 @@ AAuthClient.generateKeypair = generateKeypair;
|
|
|
727
930
|
// src/dpop.ts
|
|
728
931
|
import {
|
|
729
932
|
createHash as createHash3,
|
|
730
|
-
createPrivateKey as
|
|
731
|
-
createPublicKey as
|
|
732
|
-
generateKeyPairSync as
|
|
733
|
-
randomBytes as
|
|
734
|
-
sign as
|
|
933
|
+
createPrivateKey as createPrivateKey3,
|
|
934
|
+
createPublicKey as createPublicKey4,
|
|
935
|
+
generateKeyPairSync as generateKeyPairSync3,
|
|
936
|
+
randomBytes as randomBytes3,
|
|
937
|
+
sign as cryptoSign3
|
|
735
938
|
} from "crypto";
|
|
736
939
|
import { dpopHtu } from "@lumoauth/shared";
|
|
737
940
|
var DpopProofSigner = class _DpopProofSigner {
|
|
@@ -740,7 +943,7 @@ var DpopProofSigner = class _DpopProofSigner {
|
|
|
740
943
|
if (options.privateKey) {
|
|
741
944
|
this.key = options.privateKey;
|
|
742
945
|
} else if (options.privateKeyPem) {
|
|
743
|
-
this.key =
|
|
946
|
+
this.key = createPrivateKey3(options.privateKeyPem);
|
|
744
947
|
} else {
|
|
745
948
|
throw new Error("DpopProofSigner needs privateKeyPem or privateKey");
|
|
746
949
|
}
|
|
@@ -749,7 +952,7 @@ var DpopProofSigner = class _DpopProofSigner {
|
|
|
749
952
|
}
|
|
750
953
|
this.algorithm = algorithmFor(this.key);
|
|
751
954
|
this.now = options.now ?? (() => Math.floor(Date.now() / 1e3));
|
|
752
|
-
const pub =
|
|
955
|
+
const pub = createPublicKey4(this.key).export({ format: "jwk" });
|
|
753
956
|
this.jwk = publicMembers(pub);
|
|
754
957
|
this.thumbprint = jwkThumbprint(this.jwk);
|
|
755
958
|
}
|
|
@@ -761,13 +964,13 @@ var DpopProofSigner = class _DpopProofSigner {
|
|
|
761
964
|
let privateKey;
|
|
762
965
|
switch (algorithm) {
|
|
763
966
|
case "ES256":
|
|
764
|
-
privateKey =
|
|
967
|
+
privateKey = generateKeyPairSync3("ec", { namedCurve: "P-256" }).privateKey;
|
|
765
968
|
break;
|
|
766
969
|
case "EdDSA":
|
|
767
|
-
privateKey =
|
|
970
|
+
privateKey = generateKeyPairSync3("ed25519").privateKey;
|
|
768
971
|
break;
|
|
769
972
|
case "RS256":
|
|
770
|
-
privateKey =
|
|
973
|
+
privateKey = generateKeyPairSync3("rsa", { modulusLength: 2048 }).privateKey;
|
|
771
974
|
break;
|
|
772
975
|
default:
|
|
773
976
|
throw new Error(`Unsupported DPoP algorithm: ${String(algorithm)}`);
|
|
@@ -785,14 +988,14 @@ var DpopProofSigner = class _DpopProofSigner {
|
|
|
785
988
|
const nonce = input.nonce ?? this.getNonce(input.url);
|
|
786
989
|
const header = { typ: "dpop+jwt", alg: this.algorithm, jwk: this.jwk };
|
|
787
990
|
const claims = {
|
|
788
|
-
jti:
|
|
991
|
+
jti: randomBytes3(24).toString("base64url"),
|
|
789
992
|
htm: method,
|
|
790
993
|
htu,
|
|
791
994
|
iat: this.now()
|
|
792
995
|
};
|
|
793
996
|
if (input.accessToken) claims.ath = dpopAth(input.accessToken);
|
|
794
997
|
if (nonce) claims.nonce = nonce;
|
|
795
|
-
const signingInput = `${
|
|
998
|
+
const signingInput = `${b64url2(JSON.stringify(header))}.${b64url2(JSON.stringify(claims))}`;
|
|
796
999
|
const signature = signJws(this.algorithm, Buffer.from(signingInput, "utf8"), this.key);
|
|
797
1000
|
return `${signingInput}.${signature.toString("base64url")}`;
|
|
798
1001
|
}
|
|
@@ -815,11 +1018,11 @@ function dpopAth(accessToken) {
|
|
|
815
1018
|
function signJws(alg, data, key) {
|
|
816
1019
|
switch (alg) {
|
|
817
1020
|
case "ES256":
|
|
818
|
-
return
|
|
1021
|
+
return cryptoSign3("sha256", data, { key, dsaEncoding: "ieee-p1363" });
|
|
819
1022
|
case "EdDSA":
|
|
820
|
-
return
|
|
1023
|
+
return cryptoSign3(null, data, key);
|
|
821
1024
|
case "RS256":
|
|
822
|
-
return
|
|
1025
|
+
return cryptoSign3("sha256", data, key);
|
|
823
1026
|
default:
|
|
824
1027
|
throw new Error(`Unsupported DPoP algorithm: ${String(alg)}`);
|
|
825
1028
|
}
|
|
@@ -851,7 +1054,7 @@ function publicMembers(jwk) {
|
|
|
851
1054
|
function originOf(url) {
|
|
852
1055
|
return new URL(url).origin;
|
|
853
1056
|
}
|
|
854
|
-
function
|
|
1057
|
+
function b64url2(text) {
|
|
855
1058
|
return Buffer.from(text, "utf8").toString("base64url");
|
|
856
1059
|
}
|
|
857
1060
|
|
|
@@ -919,7 +1122,7 @@ async function attestWorkload(options) {
|
|
|
919
1122
|
}
|
|
920
1123
|
|
|
921
1124
|
// src/access-token.ts
|
|
922
|
-
import { constants as constants3, createHash as createHash4, createPublicKey as
|
|
1125
|
+
import { constants as constants3, createHash as createHash4, createPublicKey as createPublicKey5, verify as cryptoVerify3 } from "crypto";
|
|
923
1126
|
import { DPOP_SCHEME, LumoAuthError as LumoAuthError2, dpopHtu as dpopHtu2 } from "@lumoauth/shared";
|
|
924
1127
|
var AccessTokenError = class extends LumoAuthError2 {
|
|
925
1128
|
constructor(message, code, statusCode = 401) {
|
|
@@ -1093,7 +1296,7 @@ async function verifyAccessToken(token, options) {
|
|
|
1093
1296
|
throw new AccessTokenError(`Key not found: kid=${kid}`, "invalid_token");
|
|
1094
1297
|
}
|
|
1095
1298
|
assertAlgMatchesKey2(alg, jwk);
|
|
1096
|
-
const publicKey =
|
|
1299
|
+
const publicKey = createPublicKey5({ key: jwk, format: "jwk" });
|
|
1097
1300
|
const data = Buffer.from(`${parts[0]}.${parts[1]}`, "utf8");
|
|
1098
1301
|
const signature = Buffer.from(parts[2], "base64url");
|
|
1099
1302
|
if (!verifier(data, publicKey, signature)) {
|
|
@@ -1202,7 +1405,7 @@ async function verifyDpopProof(proof, options) {
|
|
|
1202
1405
|
}
|
|
1203
1406
|
let publicKey;
|
|
1204
1407
|
try {
|
|
1205
|
-
publicKey =
|
|
1408
|
+
publicKey = createPublicKey5({ key: jwk, format: "jwk" });
|
|
1206
1409
|
} catch {
|
|
1207
1410
|
throw new AccessTokenError("DPoP proof jwk is not a valid public key", "invalid_dpop_proof");
|
|
1208
1411
|
}
|
|
@@ -1315,6 +1518,9 @@ export {
|
|
|
1315
1518
|
AAuthClient,
|
|
1316
1519
|
AAuthError,
|
|
1317
1520
|
AccessTokenError,
|
|
1521
|
+
CLIENT_ASSERTION_TYP,
|
|
1522
|
+
CLIENT_ASSERTION_TYPE,
|
|
1523
|
+
ClientAssertionSigner,
|
|
1318
1524
|
DPOP_NONCE_HEADER,
|
|
1319
1525
|
DPOP_SCHEME2 as DPOP_SCHEME,
|
|
1320
1526
|
DpopProofSigner,
|
|
@@ -1334,6 +1540,7 @@ export {
|
|
|
1334
1540
|
LumoAuthPermissionDeniedError,
|
|
1335
1541
|
LumoAuthRateLimitError,
|
|
1336
1542
|
LumoAuthValidationError,
|
|
1543
|
+
MAX_ASSERTION_LIFETIME_SECONDS,
|
|
1337
1544
|
USE_DPOP_NONCE,
|
|
1338
1545
|
accessTokenScopes,
|
|
1339
1546
|
attestWorkload,
|
|
@@ -1347,6 +1554,7 @@ export {
|
|
|
1347
1554
|
fetchWithDpop3 as fetchWithDpop,
|
|
1348
1555
|
generateKeypair,
|
|
1349
1556
|
generateNonce,
|
|
1557
|
+
issuerFor,
|
|
1350
1558
|
jwkThumbprint,
|
|
1351
1559
|
parseAuthorizationHeader,
|
|
1352
1560
|
publicJwkFromPrivateKey,
|