@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.js
CHANGED
|
@@ -24,6 +24,9 @@ __export(index_exports, {
|
|
|
24
24
|
AAuthClient: () => AAuthClient,
|
|
25
25
|
AAuthError: () => AAuthError,
|
|
26
26
|
AccessTokenError: () => AccessTokenError,
|
|
27
|
+
CLIENT_ASSERTION_TYP: () => CLIENT_ASSERTION_TYP,
|
|
28
|
+
CLIENT_ASSERTION_TYPE: () => CLIENT_ASSERTION_TYPE,
|
|
29
|
+
ClientAssertionSigner: () => ClientAssertionSigner,
|
|
27
30
|
DPOP_NONCE_HEADER: () => import_shared7.DPOP_NONCE_HEADER,
|
|
28
31
|
DPOP_SCHEME: () => import_shared7.DPOP_SCHEME,
|
|
29
32
|
DpopProofSigner: () => DpopProofSigner,
|
|
@@ -43,6 +46,7 @@ __export(index_exports, {
|
|
|
43
46
|
LumoAuthPermissionDeniedError: () => import_shared8.LumoAuthPermissionDeniedError,
|
|
44
47
|
LumoAuthRateLimitError: () => import_shared8.LumoAuthRateLimitError,
|
|
45
48
|
LumoAuthValidationError: () => import_shared8.LumoAuthValidationError,
|
|
49
|
+
MAX_ASSERTION_LIFETIME_SECONDS: () => MAX_ASSERTION_LIFETIME_SECONDS,
|
|
46
50
|
USE_DPOP_NONCE: () => import_shared7.USE_DPOP_NONCE,
|
|
47
51
|
accessTokenScopes: () => accessTokenScopes,
|
|
48
52
|
attestWorkload: () => attestWorkload,
|
|
@@ -56,6 +60,7 @@ __export(index_exports, {
|
|
|
56
60
|
fetchWithDpop: () => import_shared7.fetchWithDpop,
|
|
57
61
|
generateKeypair: () => generateKeypair,
|
|
58
62
|
generateNonce: () => generateNonce,
|
|
63
|
+
issuerFor: () => issuerFor,
|
|
59
64
|
jwkThumbprint: () => jwkThumbprint,
|
|
60
65
|
parseAuthorizationHeader: () => parseAuthorizationHeader,
|
|
61
66
|
publicJwkFromPrivateKey: () => publicJwkFromPrivateKey,
|
|
@@ -71,6 +76,157 @@ module.exports = __toCommonJS(index_exports);
|
|
|
71
76
|
|
|
72
77
|
// src/agent.ts
|
|
73
78
|
var import_shared = require("@lumoauth/shared");
|
|
79
|
+
|
|
80
|
+
// src/client-assertion.ts
|
|
81
|
+
var import_node_crypto = require("crypto");
|
|
82
|
+
var CLIENT_ASSERTION_TYPE = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer";
|
|
83
|
+
var CLIENT_ASSERTION_TYP = "client-authentication+jwt";
|
|
84
|
+
var MAX_ASSERTION_LIFETIME_SECONDS = 600;
|
|
85
|
+
var RSA_ALGORITHMS = ["RS256", "RS384", "RS512", "PS256", "PS384", "PS512"];
|
|
86
|
+
var EC_ALGORITHM_BY_CURVE = {
|
|
87
|
+
prime256v1: "ES256",
|
|
88
|
+
"P-256": "ES256",
|
|
89
|
+
secp384r1: "ES384",
|
|
90
|
+
"P-384": "ES384",
|
|
91
|
+
secp521r1: "ES512",
|
|
92
|
+
"P-521": "ES512"
|
|
93
|
+
};
|
|
94
|
+
var ClientAssertionSigner = class _ClientAssertionSigner {
|
|
95
|
+
/** Generate a new key: ES256 (default), ES384, ES512, EdDSA, or an RSA algorithm (2048-bit key). */
|
|
96
|
+
static generate(algorithm = "ES256", keyId) {
|
|
97
|
+
let privateKey;
|
|
98
|
+
if (algorithm === "ES256" || algorithm === "ES384" || algorithm === "ES512") {
|
|
99
|
+
const namedCurve = { ES256: "P-256", ES384: "P-384", ES512: "P-521" }[algorithm];
|
|
100
|
+
privateKey = (0, import_node_crypto.generateKeyPairSync)("ec", { namedCurve }).privateKey;
|
|
101
|
+
} else if (algorithm === "EdDSA") {
|
|
102
|
+
privateKey = (0, import_node_crypto.generateKeyPairSync)("ed25519").privateKey;
|
|
103
|
+
} else if (RSA_ALGORITHMS.includes(algorithm)) {
|
|
104
|
+
privateKey = (0, import_node_crypto.generateKeyPairSync)("rsa", { modulusLength: 2048 }).privateKey;
|
|
105
|
+
} else {
|
|
106
|
+
throw new Error(`Unsupported client assertion algorithm: ${String(algorithm)}`);
|
|
107
|
+
}
|
|
108
|
+
return new _ClientAssertionSigner({ privateKey, keyId, algorithm });
|
|
109
|
+
}
|
|
110
|
+
/** Load a PEM private key. */
|
|
111
|
+
static fromPem(pem, options = {}) {
|
|
112
|
+
return new _ClientAssertionSigner({ ...options, privateKeyPem: pem });
|
|
113
|
+
}
|
|
114
|
+
constructor(options) {
|
|
115
|
+
if (options.privateKey) {
|
|
116
|
+
this.key = options.privateKey;
|
|
117
|
+
} else if (options.privateKeyPem) {
|
|
118
|
+
this.key = (0, import_node_crypto.createPrivateKey)(options.privateKeyPem);
|
|
119
|
+
} else {
|
|
120
|
+
throw new Error("ClientAssertionSigner needs privateKeyPem or privateKey");
|
|
121
|
+
}
|
|
122
|
+
if (this.key.type !== "private") {
|
|
123
|
+
throw new Error("ClientAssertionSigner needs a private key");
|
|
124
|
+
}
|
|
125
|
+
const natural = naturalAlgorithm(this.key);
|
|
126
|
+
const chosen = options.algorithm ?? natural;
|
|
127
|
+
if (this.key.asymmetricKeyType === "rsa") {
|
|
128
|
+
if (!RSA_ALGORITHMS.includes(chosen)) {
|
|
129
|
+
throw new Error(`${chosen} does not fit an RSA key; use one of ${RSA_ALGORITHMS.join(", ")}`);
|
|
130
|
+
}
|
|
131
|
+
const bits = this.key.asymmetricKeyDetails?.modulusLength ?? 0;
|
|
132
|
+
if (bits < 2048) throw new Error("RSA keys must be at least 2048 bits");
|
|
133
|
+
} else if (chosen !== natural) {
|
|
134
|
+
throw new Error(`${chosen} does not fit this key; it signs with ${natural}`);
|
|
135
|
+
}
|
|
136
|
+
const lifetime = options.lifetimeSeconds ?? 60;
|
|
137
|
+
if (!Number.isInteger(lifetime) || lifetime < 1 || lifetime > MAX_ASSERTION_LIFETIME_SECONDS) {
|
|
138
|
+
throw new Error(`lifetimeSeconds must be an integer from 1 to ${MAX_ASSERTION_LIFETIME_SECONDS}`);
|
|
139
|
+
}
|
|
140
|
+
this.algorithm = chosen;
|
|
141
|
+
this.keyId = options.keyId;
|
|
142
|
+
this.lifetimeSeconds = lifetime;
|
|
143
|
+
this.now = options.now ?? (() => Math.floor(Date.now() / 1e3));
|
|
144
|
+
}
|
|
145
|
+
/** Sign one assertion for `clientId` (the agent ID) and `audience` (the organization's issuer). */
|
|
146
|
+
create(clientId, audience) {
|
|
147
|
+
if (!clientId || !audience) {
|
|
148
|
+
throw new Error("clientId and audience are required for a client assertion");
|
|
149
|
+
}
|
|
150
|
+
const iat = this.now();
|
|
151
|
+
const header = { alg: this.algorithm, typ: CLIENT_ASSERTION_TYP };
|
|
152
|
+
if (this.keyId) header.kid = this.keyId;
|
|
153
|
+
const claims = {
|
|
154
|
+
iss: clientId,
|
|
155
|
+
sub: clientId,
|
|
156
|
+
aud: audience,
|
|
157
|
+
iat,
|
|
158
|
+
exp: iat + this.lifetimeSeconds,
|
|
159
|
+
jti: (0, import_node_crypto.randomBytes)(24).toString("base64url")
|
|
160
|
+
};
|
|
161
|
+
const signingInput = `${b64url(JSON.stringify(header))}.${b64url(JSON.stringify(claims))}`;
|
|
162
|
+
return `${signingInput}.${signAssertion(this.algorithm, Buffer.from(signingInput, "utf8"), this.key).toString("base64url")}`;
|
|
163
|
+
}
|
|
164
|
+
/** The public key as a JWK with `kid` (when set), `alg` and `use: "sig"`. */
|
|
165
|
+
get jwk() {
|
|
166
|
+
const pub = (0, import_node_crypto.createPublicKey)(this.key).export({ format: "jwk" });
|
|
167
|
+
const out = { kty: pub.kty };
|
|
168
|
+
for (const member of ["crv", "x", "y", "n", "e"]) {
|
|
169
|
+
if (typeof pub[member] === "string") out[member] = pub[member];
|
|
170
|
+
}
|
|
171
|
+
if (this.keyId) out.kid = this.keyId;
|
|
172
|
+
out.alg = this.algorithm;
|
|
173
|
+
out.use = "sig";
|
|
174
|
+
return out;
|
|
175
|
+
}
|
|
176
|
+
/** `{ keys: [jwk] }`: what to register on the agent. */
|
|
177
|
+
get jwks() {
|
|
178
|
+
return { keys: [this.jwk] };
|
|
179
|
+
}
|
|
180
|
+
/** The private key as PKCS#8 PEM, for your secret store. */
|
|
181
|
+
get privateKeyPem() {
|
|
182
|
+
return this.key.export({ type: "pkcs8", format: "pem" }).toString();
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
function issuerFor(baseUrl, orgId) {
|
|
186
|
+
return `${baseUrl.replace(/\/+$/, "")}/orgs/${orgId}/api/v1`;
|
|
187
|
+
}
|
|
188
|
+
function naturalAlgorithm(key) {
|
|
189
|
+
switch (key.asymmetricKeyType) {
|
|
190
|
+
case "ec": {
|
|
191
|
+
const curve = String(key.asymmetricKeyDetails?.namedCurve ?? "");
|
|
192
|
+
const alg = EC_ALGORITHM_BY_CURVE[curve];
|
|
193
|
+
if (!alg) throw new Error(`EC keys must use P-256, P-384 or P-521 (got ${curve})`);
|
|
194
|
+
return alg;
|
|
195
|
+
}
|
|
196
|
+
case "ed25519":
|
|
197
|
+
return "EdDSA";
|
|
198
|
+
case "rsa":
|
|
199
|
+
return "RS256";
|
|
200
|
+
default:
|
|
201
|
+
throw new Error(`Unsupported key type: ${String(key.asymmetricKeyType)}`);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
function signAssertion(alg, data, key) {
|
|
205
|
+
const hash = `sha${alg.slice(2)}`;
|
|
206
|
+
switch (alg) {
|
|
207
|
+
case "ES256":
|
|
208
|
+
case "ES384":
|
|
209
|
+
case "ES512":
|
|
210
|
+
return (0, import_node_crypto.sign)(hash, data, { key, dsaEncoding: "ieee-p1363" });
|
|
211
|
+
case "EdDSA":
|
|
212
|
+
return (0, import_node_crypto.sign)(null, data, key);
|
|
213
|
+
case "PS256":
|
|
214
|
+
case "PS384":
|
|
215
|
+
case "PS512":
|
|
216
|
+
return (0, import_node_crypto.sign)(hash, data, {
|
|
217
|
+
key,
|
|
218
|
+
padding: import_node_crypto.constants.RSA_PKCS1_PSS_PADDING,
|
|
219
|
+
saltLength: import_node_crypto.constants.RSA_PSS_SALTLEN_DIGEST
|
|
220
|
+
});
|
|
221
|
+
default:
|
|
222
|
+
return (0, import_node_crypto.sign)(hash, data, key);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
function b64url(text) {
|
|
226
|
+
return Buffer.from(text, "utf8").toString("base64url");
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// src/agent.ts
|
|
74
230
|
function envVar(name) {
|
|
75
231
|
return typeof process !== "undefined" && process.env ? process.env[name] : void 0;
|
|
76
232
|
}
|
|
@@ -82,13 +238,22 @@ var LumoAgent = class {
|
|
|
82
238
|
this.baseUrl = (options.baseUrl ?? envVar("LUMOAUTH_URL") ?? "https://app.lumoauth.dev").replace(/\/+$/, "");
|
|
83
239
|
this.orgId = options.orgId ?? envVar("LUMOAUTH_ORG_ID") ?? "";
|
|
84
240
|
this.clientId = options.clientId ?? envVar("AGENT_CLIENT_ID") ?? "";
|
|
85
|
-
|
|
241
|
+
const privateKey = options.privateKey ?? envVar("AGENT_PRIVATE_KEY");
|
|
242
|
+
const keyId = options.keyId ?? envVar("AGENT_KEY_ID");
|
|
243
|
+
this.assertionSigner = privateKey === void 0 || privateKey === "" ? void 0 : privateKey instanceof ClientAssertionSigner ? privateKey : ClientAssertionSigner.fromPem(privateKey, { keyId });
|
|
244
|
+
this.clientSecret = options.clientSecret ?? (this.assertionSigner ? "" : envVar("AGENT_CLIENT_SECRET") ?? "");
|
|
245
|
+
this.issuer = options.issuer;
|
|
86
246
|
this.scopes = options.scopes;
|
|
87
247
|
this.fetchFn = options.fetch ?? globalThis.fetch.bind(globalThis);
|
|
88
248
|
this.dpop = options.dpop;
|
|
89
|
-
if (!this.clientId || !this.clientSecret) {
|
|
249
|
+
if (!this.clientId || !this.clientSecret && !this.assertionSigner) {
|
|
90
250
|
throw new import_shared.LumoAuthConfigError(
|
|
91
|
-
"Agent credentials required. Set AGENT_CLIENT_ID and AGENT_CLIENT_SECRET
|
|
251
|
+
"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."
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
if (this.clientSecret && this.assertionSigner) {
|
|
255
|
+
throw new import_shared.LumoAuthConfigError(
|
|
256
|
+
"Pass clientSecret or privateKey, not both: an agent authenticates with one method."
|
|
92
257
|
);
|
|
93
258
|
}
|
|
94
259
|
const tokenProvider = () => this.getAccessToken();
|
|
@@ -130,9 +295,14 @@ var LumoAgent = class {
|
|
|
130
295
|
async authenticate(scopes) {
|
|
131
296
|
const form = {
|
|
132
297
|
grant_type: "client_credentials",
|
|
133
|
-
client_id: this.clientId
|
|
134
|
-
client_secret: this.clientSecret
|
|
298
|
+
client_id: this.clientId
|
|
135
299
|
};
|
|
300
|
+
if (this.assertionSigner) {
|
|
301
|
+
form.client_assertion_type = CLIENT_ASSERTION_TYPE;
|
|
302
|
+
form.client_assertion = this.createClientAssertion();
|
|
303
|
+
} else {
|
|
304
|
+
form.client_secret = this.clientSecret;
|
|
305
|
+
}
|
|
136
306
|
const requested = scopes ?? this.scopes;
|
|
137
307
|
if (requested?.length) form.scope = requested.join(" ");
|
|
138
308
|
const url = `${this.baseUrl}${(0, import_shared.buildPath)(import_shared.ROUTES["oauth.token"].path, { orgId: this.requireOrgId() })}`;
|
|
@@ -176,6 +346,21 @@ var LumoAgent = class {
|
|
|
176
346
|
get tokenType() {
|
|
177
347
|
return this.token?.tokenType ?? null;
|
|
178
348
|
}
|
|
349
|
+
/**
|
|
350
|
+
* A new `private_key_jwt` client assertion for this agent (single-use),
|
|
351
|
+
* addressed to `issuer` or `{baseUrl}/orgs/{orgId}/api/v1`.
|
|
352
|
+
*/
|
|
353
|
+
createClientAssertion() {
|
|
354
|
+
if (!this.assertionSigner) {
|
|
355
|
+
throw new import_shared.LumoAuthConfigError("This agent signs no client assertions: create it with privateKey.");
|
|
356
|
+
}
|
|
357
|
+
const audience = this.issuer ?? issuerFor(this.baseUrl, this.requireOrgId());
|
|
358
|
+
return this.assertionSigner.create(this.clientId, audience);
|
|
359
|
+
}
|
|
360
|
+
/** The client assertion signer this agent was configured with, if any. */
|
|
361
|
+
get clientAssertionSigner() {
|
|
362
|
+
return this.assertionSigner;
|
|
363
|
+
}
|
|
179
364
|
/** The DPoP proof signer this agent was configured with, if any. */
|
|
180
365
|
get dpopSigner() {
|
|
181
366
|
return this.dpop;
|
|
@@ -228,6 +413,22 @@ var LumoAgent = class {
|
|
|
228
413
|
async budget() {
|
|
229
414
|
return this.agents.budget();
|
|
230
415
|
}
|
|
416
|
+
/**
|
|
417
|
+
* Report the model tokens and cost that the agent just used, for example
|
|
418
|
+
* the `usage` that the model provider returned. LumoAuth does not see
|
|
419
|
+
* model calls, so token and cost limits count only reported usage. The
|
|
420
|
+
* result's `budget.exhausted` says whether the next metered call (Ask,
|
|
421
|
+
* token or JIT request) will get 429.
|
|
422
|
+
*
|
|
423
|
+
* ```ts
|
|
424
|
+
* const resp = await llm.chat(...);
|
|
425
|
+
* const { budget } = await agent.reportUsage({ tokens: resp.usage.total_tokens, costUsd: 0.0042 });
|
|
426
|
+
* if (budget.exhausted) throw new Error('budget spent');
|
|
427
|
+
* ```
|
|
428
|
+
*/
|
|
429
|
+
async reportUsage(report) {
|
|
430
|
+
return this.agents.reportUsage(report);
|
|
431
|
+
}
|
|
231
432
|
// ── Internal ──────────────────────────────────────────────────────
|
|
232
433
|
requireOrgId() {
|
|
233
434
|
if (!this.orgId) {
|
|
@@ -240,9 +441,9 @@ var LumoAgent = class {
|
|
|
240
441
|
};
|
|
241
442
|
|
|
242
443
|
// src/keys.ts
|
|
243
|
-
var
|
|
444
|
+
var import_node_crypto2 = require("crypto");
|
|
244
445
|
function generateKeypair(kid = "key-1") {
|
|
245
|
-
const { privateKey, publicKey } = (0,
|
|
446
|
+
const { privateKey, publicKey } = (0, import_node_crypto2.generateKeyPairSync)("ed25519");
|
|
246
447
|
const privateKeyPem = privateKey.export({ type: "pkcs8", format: "pem" }).toString();
|
|
247
448
|
const publicKeyPem = publicKey.export({ type: "spki", format: "pem" }).toString();
|
|
248
449
|
const rawJwk = publicKey.export({ format: "jwk" });
|
|
@@ -270,10 +471,10 @@ function jwkThumbprint(jwk) {
|
|
|
270
471
|
default:
|
|
271
472
|
throw new Error(`Unsupported key type for thumbprint: ${jwk.kty}`);
|
|
272
473
|
}
|
|
273
|
-
return (0,
|
|
474
|
+
return (0, import_node_crypto2.createHash)("sha256").update(canonical, "utf8").digest("base64url");
|
|
274
475
|
}
|
|
275
476
|
function loadPrivateKey(pem) {
|
|
276
|
-
const key = (0,
|
|
477
|
+
const key = (0, import_node_crypto2.createPrivateKey)(pem);
|
|
277
478
|
if (key.asymmetricKeyType === "ed25519") {
|
|
278
479
|
return { key, type: "ed25519" };
|
|
279
480
|
}
|
|
@@ -285,15 +486,15 @@ function loadPrivateKey(pem) {
|
|
|
285
486
|
);
|
|
286
487
|
}
|
|
287
488
|
function publicJwkFromPrivateKey(pem, kid) {
|
|
288
|
-
const priv = (0,
|
|
289
|
-
const pub = (0,
|
|
489
|
+
const priv = (0, import_node_crypto2.createPrivateKey)(pem);
|
|
490
|
+
const pub = (0, import_node_crypto2.createPublicKey)(priv);
|
|
290
491
|
const jwk = pub.export({ format: "jwk" });
|
|
291
492
|
if (kid) jwk.kid = kid;
|
|
292
493
|
return jwk;
|
|
293
494
|
}
|
|
294
495
|
|
|
295
496
|
// src/signing.ts
|
|
296
|
-
var
|
|
497
|
+
var import_node_crypto3 = require("crypto");
|
|
297
498
|
var AAUTH_COVERED_COMPONENTS = [
|
|
298
499
|
"@method",
|
|
299
500
|
"@authority",
|
|
@@ -305,11 +506,11 @@ var AAUTH_COVERED_COMPONENTS = [
|
|
|
305
506
|
];
|
|
306
507
|
var SIGNATURE_LABEL = "sig1";
|
|
307
508
|
function contentDigestSha256(body) {
|
|
308
|
-
const digest = (0,
|
|
509
|
+
const digest = (0, import_node_crypto3.createHash)("sha256").update(body).digest("base64");
|
|
309
510
|
return `sha-256=:${digest}:`;
|
|
310
511
|
}
|
|
311
512
|
function generateNonce() {
|
|
312
|
-
return (0,
|
|
513
|
+
return (0, import_node_crypto3.randomBytes)(16).toString("base64url");
|
|
313
514
|
}
|
|
314
515
|
function buildSignatureBase(components, values, created, nonce) {
|
|
315
516
|
const lines = components.map((c) => `"${c}": ${values[c] ?? ""}`);
|
|
@@ -324,12 +525,12 @@ function signSignatureBase(signatureBase, privateKey) {
|
|
|
324
525
|
const data = Buffer.from(signatureBase, "utf8");
|
|
325
526
|
let signature;
|
|
326
527
|
if (privateKey.asymmetricKeyType === "ed25519") {
|
|
327
|
-
signature = (0,
|
|
528
|
+
signature = (0, import_node_crypto3.sign)(null, data, privateKey);
|
|
328
529
|
} else {
|
|
329
|
-
signature = (0,
|
|
530
|
+
signature = (0, import_node_crypto3.sign)("sha512", data, {
|
|
330
531
|
key: privateKey,
|
|
331
|
-
padding:
|
|
332
|
-
saltLength:
|
|
532
|
+
padding: import_node_crypto3.constants.RSA_PKCS1_PSS_PADDING,
|
|
533
|
+
saltLength: import_node_crypto3.constants.RSA_PSS_SALTLEN_DIGEST
|
|
333
534
|
// 64 bytes for SHA-512
|
|
334
535
|
});
|
|
335
536
|
}
|
|
@@ -339,12 +540,12 @@ function verifySignatureBase(signatureBase, signatureB64url, publicKey) {
|
|
|
339
540
|
const data = Buffer.from(signatureBase, "utf8");
|
|
340
541
|
const sig = Buffer.from(signatureB64url, "base64url");
|
|
341
542
|
if (publicKey.asymmetricKeyType === "ed25519") {
|
|
342
|
-
return (0,
|
|
543
|
+
return (0, import_node_crypto3.verify)(null, data, publicKey, sig);
|
|
343
544
|
}
|
|
344
|
-
return (0,
|
|
545
|
+
return (0, import_node_crypto3.verify)("sha512", data, {
|
|
345
546
|
key: publicKey,
|
|
346
|
-
padding:
|
|
347
|
-
saltLength:
|
|
547
|
+
padding: import_node_crypto3.constants.RSA_PKCS1_PSS_PADDING,
|
|
548
|
+
saltLength: import_node_crypto3.constants.RSA_PSS_SALTLEN_DIGEST
|
|
348
549
|
}, sig);
|
|
349
550
|
}
|
|
350
551
|
function signRequest(privateKeyPem, method, url, options = {}) {
|
|
@@ -389,7 +590,7 @@ function signRequest(privateKeyPem, method, url, options = {}) {
|
|
|
389
590
|
}
|
|
390
591
|
|
|
391
592
|
// src/verify.ts
|
|
392
|
-
var
|
|
593
|
+
var import_node_crypto4 = require("crypto");
|
|
393
594
|
|
|
394
595
|
// src/errors.ts
|
|
395
596
|
var import_shared2 = require("@lumoauth/shared");
|
|
@@ -403,19 +604,19 @@ var AAuthError = class extends import_shared2.LumoAuthError {
|
|
|
403
604
|
|
|
404
605
|
// src/verify.ts
|
|
405
606
|
var JWS_VERIFIERS = {
|
|
406
|
-
RS256: (d, k, s) => (0,
|
|
407
|
-
RS384: (d, k, s) => (0,
|
|
408
|
-
RS512: (d, k, s) => (0,
|
|
409
|
-
PS256: (d, k, s) => (0,
|
|
410
|
-
PS384: (d, k, s) => (0,
|
|
411
|
-
PS512: (d, k, s) => (0,
|
|
412
|
-
ES256: (d, k, s) => (0,
|
|
413
|
-
ES384: (d, k, s) => (0,
|
|
414
|
-
ES512: (d, k, s) => (0,
|
|
415
|
-
EdDSA: (d, k, s) => (0,
|
|
607
|
+
RS256: (d, k, s) => (0, import_node_crypto4.verify)("sha256", d, k, s),
|
|
608
|
+
RS384: (d, k, s) => (0, import_node_crypto4.verify)("sha384", d, k, s),
|
|
609
|
+
RS512: (d, k, s) => (0, import_node_crypto4.verify)("sha512", d, k, s),
|
|
610
|
+
PS256: (d, k, s) => (0, import_node_crypto4.verify)("sha256", d, pss(k), s),
|
|
611
|
+
PS384: (d, k, s) => (0, import_node_crypto4.verify)("sha384", d, pss(k), s),
|
|
612
|
+
PS512: (d, k, s) => (0, import_node_crypto4.verify)("sha512", d, pss(k), s),
|
|
613
|
+
ES256: (d, k, s) => (0, import_node_crypto4.verify)("sha256", d, p1363(k), s),
|
|
614
|
+
ES384: (d, k, s) => (0, import_node_crypto4.verify)("sha384", d, p1363(k), s),
|
|
615
|
+
ES512: (d, k, s) => (0, import_node_crypto4.verify)("sha512", d, p1363(k), s),
|
|
616
|
+
EdDSA: (d, k, s) => (0, import_node_crypto4.verify)(null, d, k, s)
|
|
416
617
|
};
|
|
417
618
|
function pss(key) {
|
|
418
|
-
return { key, padding:
|
|
619
|
+
return { key, padding: import_node_crypto4.constants.RSA_PKCS1_PSS_PADDING, saltLength: import_node_crypto4.constants.RSA_PSS_SALTLEN_DIGEST };
|
|
419
620
|
}
|
|
420
621
|
function p1363(key) {
|
|
421
622
|
return { key, dsaEncoding: "ieee-p1363" };
|
|
@@ -461,7 +662,7 @@ async function verifyAuthToken(token, options) {
|
|
|
461
662
|
throw new AAuthError(`Key not found: kid=${kid}`, "invalid_token");
|
|
462
663
|
}
|
|
463
664
|
assertAlgMatchesKey(alg, jwk);
|
|
464
|
-
const publicKey = (0,
|
|
665
|
+
const publicKey = (0, import_node_crypto4.createPublicKey)({ key: jwk, format: "jwk" });
|
|
465
666
|
const data = Buffer.from(`${parts[0]}.${parts[1]}`, "utf8");
|
|
466
667
|
const signature = Buffer.from(parts[2], "base64url");
|
|
467
668
|
if (!verifier(data, publicKey, signature)) {
|
|
@@ -777,7 +978,7 @@ var AAuthClient = class {
|
|
|
777
978
|
AAuthClient.generateKeypair = generateKeypair;
|
|
778
979
|
|
|
779
980
|
// src/dpop.ts
|
|
780
|
-
var
|
|
981
|
+
var import_node_crypto5 = require("crypto");
|
|
781
982
|
var import_shared4 = require("@lumoauth/shared");
|
|
782
983
|
var DpopProofSigner = class _DpopProofSigner {
|
|
783
984
|
constructor(options) {
|
|
@@ -785,7 +986,7 @@ var DpopProofSigner = class _DpopProofSigner {
|
|
|
785
986
|
if (options.privateKey) {
|
|
786
987
|
this.key = options.privateKey;
|
|
787
988
|
} else if (options.privateKeyPem) {
|
|
788
|
-
this.key = (0,
|
|
989
|
+
this.key = (0, import_node_crypto5.createPrivateKey)(options.privateKeyPem);
|
|
789
990
|
} else {
|
|
790
991
|
throw new Error("DpopProofSigner needs privateKeyPem or privateKey");
|
|
791
992
|
}
|
|
@@ -794,7 +995,7 @@ var DpopProofSigner = class _DpopProofSigner {
|
|
|
794
995
|
}
|
|
795
996
|
this.algorithm = algorithmFor(this.key);
|
|
796
997
|
this.now = options.now ?? (() => Math.floor(Date.now() / 1e3));
|
|
797
|
-
const pub = (0,
|
|
998
|
+
const pub = (0, import_node_crypto5.createPublicKey)(this.key).export({ format: "jwk" });
|
|
798
999
|
this.jwk = publicMembers(pub);
|
|
799
1000
|
this.thumbprint = jwkThumbprint(this.jwk);
|
|
800
1001
|
}
|
|
@@ -806,13 +1007,13 @@ var DpopProofSigner = class _DpopProofSigner {
|
|
|
806
1007
|
let privateKey;
|
|
807
1008
|
switch (algorithm) {
|
|
808
1009
|
case "ES256":
|
|
809
|
-
privateKey = (0,
|
|
1010
|
+
privateKey = (0, import_node_crypto5.generateKeyPairSync)("ec", { namedCurve: "P-256" }).privateKey;
|
|
810
1011
|
break;
|
|
811
1012
|
case "EdDSA":
|
|
812
|
-
privateKey = (0,
|
|
1013
|
+
privateKey = (0, import_node_crypto5.generateKeyPairSync)("ed25519").privateKey;
|
|
813
1014
|
break;
|
|
814
1015
|
case "RS256":
|
|
815
|
-
privateKey = (0,
|
|
1016
|
+
privateKey = (0, import_node_crypto5.generateKeyPairSync)("rsa", { modulusLength: 2048 }).privateKey;
|
|
816
1017
|
break;
|
|
817
1018
|
default:
|
|
818
1019
|
throw new Error(`Unsupported DPoP algorithm: ${String(algorithm)}`);
|
|
@@ -830,14 +1031,14 @@ var DpopProofSigner = class _DpopProofSigner {
|
|
|
830
1031
|
const nonce = input.nonce ?? this.getNonce(input.url);
|
|
831
1032
|
const header = { typ: "dpop+jwt", alg: this.algorithm, jwk: this.jwk };
|
|
832
1033
|
const claims = {
|
|
833
|
-
jti: (0,
|
|
1034
|
+
jti: (0, import_node_crypto5.randomBytes)(24).toString("base64url"),
|
|
834
1035
|
htm: method,
|
|
835
1036
|
htu,
|
|
836
1037
|
iat: this.now()
|
|
837
1038
|
};
|
|
838
1039
|
if (input.accessToken) claims.ath = dpopAth(input.accessToken);
|
|
839
1040
|
if (nonce) claims.nonce = nonce;
|
|
840
|
-
const signingInput = `${
|
|
1041
|
+
const signingInput = `${b64url2(JSON.stringify(header))}.${b64url2(JSON.stringify(claims))}`;
|
|
841
1042
|
const signature = signJws(this.algorithm, Buffer.from(signingInput, "utf8"), this.key);
|
|
842
1043
|
return `${signingInput}.${signature.toString("base64url")}`;
|
|
843
1044
|
}
|
|
@@ -855,16 +1056,16 @@ var DpopProofSigner = class _DpopProofSigner {
|
|
|
855
1056
|
}
|
|
856
1057
|
};
|
|
857
1058
|
function dpopAth(accessToken) {
|
|
858
|
-
return (0,
|
|
1059
|
+
return (0, import_node_crypto5.createHash)("sha256").update(accessToken, "ascii").digest("base64url");
|
|
859
1060
|
}
|
|
860
1061
|
function signJws(alg, data, key) {
|
|
861
1062
|
switch (alg) {
|
|
862
1063
|
case "ES256":
|
|
863
|
-
return (0,
|
|
1064
|
+
return (0, import_node_crypto5.sign)("sha256", data, { key, dsaEncoding: "ieee-p1363" });
|
|
864
1065
|
case "EdDSA":
|
|
865
|
-
return (0,
|
|
1066
|
+
return (0, import_node_crypto5.sign)(null, data, key);
|
|
866
1067
|
case "RS256":
|
|
867
|
-
return (0,
|
|
1068
|
+
return (0, import_node_crypto5.sign)("sha256", data, key);
|
|
868
1069
|
default:
|
|
869
1070
|
throw new Error(`Unsupported DPoP algorithm: ${String(alg)}`);
|
|
870
1071
|
}
|
|
@@ -896,7 +1097,7 @@ function publicMembers(jwk) {
|
|
|
896
1097
|
function originOf(url) {
|
|
897
1098
|
return new URL(url).origin;
|
|
898
1099
|
}
|
|
899
|
-
function
|
|
1100
|
+
function b64url2(text) {
|
|
900
1101
|
return Buffer.from(text, "utf8").toString("base64url");
|
|
901
1102
|
}
|
|
902
1103
|
|
|
@@ -952,7 +1153,7 @@ async function attestWorkload(options) {
|
|
|
952
1153
|
}
|
|
953
1154
|
|
|
954
1155
|
// src/access-token.ts
|
|
955
|
-
var
|
|
1156
|
+
var import_node_crypto6 = require("crypto");
|
|
956
1157
|
var import_shared6 = require("@lumoauth/shared");
|
|
957
1158
|
var AccessTokenError = class extends import_shared6.LumoAuthError {
|
|
958
1159
|
constructor(message, code, statusCode = 401) {
|
|
@@ -968,16 +1169,16 @@ var DPOP_JTI_TTL_SECONDS = 300;
|
|
|
968
1169
|
var DPOP_JTI_MIN_LENGTH = 16;
|
|
969
1170
|
var JWKS_REFETCH_INTERVAL_MS = 3e4;
|
|
970
1171
|
var JWS_VERIFIERS2 = {
|
|
971
|
-
RS256: (d, k, s) => (0,
|
|
972
|
-
RS384: (d, k, s) => (0,
|
|
973
|
-
RS512: (d, k, s) => (0,
|
|
974
|
-
PS256: (d, k, s) => (0,
|
|
975
|
-
PS384: (d, k, s) => (0,
|
|
976
|
-
PS512: (d, k, s) => (0,
|
|
977
|
-
ES256: (d, k, s) => (0,
|
|
978
|
-
ES384: (d, k, s) => (0,
|
|
979
|
-
ES512: (d, k, s) => (0,
|
|
980
|
-
EdDSA: (d, k, s) => (0,
|
|
1172
|
+
RS256: (d, k, s) => (0, import_node_crypto6.verify)("sha256", d, k, s),
|
|
1173
|
+
RS384: (d, k, s) => (0, import_node_crypto6.verify)("sha384", d, k, s),
|
|
1174
|
+
RS512: (d, k, s) => (0, import_node_crypto6.verify)("sha512", d, k, s),
|
|
1175
|
+
PS256: (d, k, s) => (0, import_node_crypto6.verify)("sha256", d, pss2(k), s),
|
|
1176
|
+
PS384: (d, k, s) => (0, import_node_crypto6.verify)("sha384", d, pss2(k), s),
|
|
1177
|
+
PS512: (d, k, s) => (0, import_node_crypto6.verify)("sha512", d, pss2(k), s),
|
|
1178
|
+
ES256: (d, k, s) => (0, import_node_crypto6.verify)("sha256", d, p13632(k), s),
|
|
1179
|
+
ES384: (d, k, s) => (0, import_node_crypto6.verify)("sha384", d, p13632(k), s),
|
|
1180
|
+
ES512: (d, k, s) => (0, import_node_crypto6.verify)("sha512", d, p13632(k), s),
|
|
1181
|
+
EdDSA: (d, k, s) => (0, import_node_crypto6.verify)(null, d, k, s)
|
|
981
1182
|
};
|
|
982
1183
|
var DPOP_ALG_KEYS = {
|
|
983
1184
|
ES256: { kty: "EC", crv: "P-256" },
|
|
@@ -992,7 +1193,7 @@ var DPOP_ALG_KEYS = {
|
|
|
992
1193
|
EdDSA: { kty: "OKP", crv: "Ed25519" }
|
|
993
1194
|
};
|
|
994
1195
|
function pss2(key) {
|
|
995
|
-
return { key, padding:
|
|
1196
|
+
return { key, padding: import_node_crypto6.constants.RSA_PKCS1_PSS_PADDING, saltLength: import_node_crypto6.constants.RSA_PSS_SALTLEN_DIGEST };
|
|
996
1197
|
}
|
|
997
1198
|
function p13632(key) {
|
|
998
1199
|
return { key, dsaEncoding: "ieee-p1363" };
|
|
@@ -1126,7 +1327,7 @@ async function verifyAccessToken(token, options) {
|
|
|
1126
1327
|
throw new AccessTokenError(`Key not found: kid=${kid}`, "invalid_token");
|
|
1127
1328
|
}
|
|
1128
1329
|
assertAlgMatchesKey2(alg, jwk);
|
|
1129
|
-
const publicKey = (0,
|
|
1330
|
+
const publicKey = (0, import_node_crypto6.createPublicKey)({ key: jwk, format: "jwk" });
|
|
1130
1331
|
const data = Buffer.from(`${parts[0]}.${parts[1]}`, "utf8");
|
|
1131
1332
|
const signature = Buffer.from(parts[2], "base64url");
|
|
1132
1333
|
if (!verifier(data, publicKey, signature)) {
|
|
@@ -1235,7 +1436,7 @@ async function verifyDpopProof(proof, options) {
|
|
|
1235
1436
|
}
|
|
1236
1437
|
let publicKey;
|
|
1237
1438
|
try {
|
|
1238
|
-
publicKey = (0,
|
|
1439
|
+
publicKey = (0, import_node_crypto6.createPublicKey)({ key: jwk, format: "jwk" });
|
|
1239
1440
|
} catch {
|
|
1240
1441
|
throw new AccessTokenError("DPoP proof jwk is not a valid public key", "invalid_dpop_proof");
|
|
1241
1442
|
}
|
|
@@ -1267,7 +1468,7 @@ async function verifyDpopProof(proof, options) {
|
|
|
1267
1468
|
throw new AccessTokenError("DPoP proof nonce does not match", "invalid_dpop_proof");
|
|
1268
1469
|
}
|
|
1269
1470
|
if (options.accessToken !== void 0) {
|
|
1270
|
-
const expectedAth = (0,
|
|
1471
|
+
const expectedAth = (0, import_node_crypto6.createHash)("sha256").update(options.accessToken, "ascii").digest("base64url");
|
|
1271
1472
|
if (claims.ath !== expectedAth) {
|
|
1272
1473
|
throw new AccessTokenError("DPoP proof ath does not match the access token", "invalid_dpop_proof");
|
|
1273
1474
|
}
|
|
@@ -1335,6 +1536,9 @@ var import_shared8 = require("@lumoauth/shared");
|
|
|
1335
1536
|
AAuthClient,
|
|
1336
1537
|
AAuthError,
|
|
1337
1538
|
AccessTokenError,
|
|
1539
|
+
CLIENT_ASSERTION_TYP,
|
|
1540
|
+
CLIENT_ASSERTION_TYPE,
|
|
1541
|
+
ClientAssertionSigner,
|
|
1338
1542
|
DPOP_NONCE_HEADER,
|
|
1339
1543
|
DPOP_SCHEME,
|
|
1340
1544
|
DpopProofSigner,
|
|
@@ -1354,6 +1558,7 @@ var import_shared8 = require("@lumoauth/shared");
|
|
|
1354
1558
|
LumoAuthPermissionDeniedError,
|
|
1355
1559
|
LumoAuthRateLimitError,
|
|
1356
1560
|
LumoAuthValidationError,
|
|
1561
|
+
MAX_ASSERTION_LIFETIME_SECONDS,
|
|
1357
1562
|
USE_DPOP_NONCE,
|
|
1358
1563
|
accessTokenScopes,
|
|
1359
1564
|
attestWorkload,
|
|
@@ -1367,6 +1572,7 @@ var import_shared8 = require("@lumoauth/shared");
|
|
|
1367
1572
|
fetchWithDpop,
|
|
1368
1573
|
generateKeypair,
|
|
1369
1574
|
generateNonce,
|
|
1575
|
+
issuerFor,
|
|
1370
1576
|
jwkThumbprint,
|
|
1371
1577
|
parseAuthorizationHeader,
|
|
1372
1578
|
publicJwkFromPrivateKey,
|