@lumoauth/agent 1.2.0 → 1.3.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 +33 -3
- package/dist/index.d.mts +114 -1
- package/dist/index.d.ts +114 -1
- package/dist/index.js +253 -63
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +228 -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) {
|
|
194
|
+
throw new LumoAuthConfigError(
|
|
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) {
|
|
32
199
|
throw new LumoAuthConfigError(
|
|
33
|
-
"
|
|
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;
|
|
@@ -182,9 +369,9 @@ var LumoAgent = class {
|
|
|
182
369
|
};
|
|
183
370
|
|
|
184
371
|
// src/keys.ts
|
|
185
|
-
import { createHash, createPublicKey, createPrivateKey, generateKeyPairSync } from "crypto";
|
|
372
|
+
import { createHash, createPublicKey as createPublicKey2, createPrivateKey as createPrivateKey2, generateKeyPairSync as generateKeyPairSync2 } from "crypto";
|
|
186
373
|
function generateKeypair(kid = "key-1") {
|
|
187
|
-
const { privateKey, publicKey } =
|
|
374
|
+
const { privateKey, publicKey } = generateKeyPairSync2("ed25519");
|
|
188
375
|
const privateKeyPem = privateKey.export({ type: "pkcs8", format: "pem" }).toString();
|
|
189
376
|
const publicKeyPem = publicKey.export({ type: "spki", format: "pem" }).toString();
|
|
190
377
|
const rawJwk = publicKey.export({ format: "jwk" });
|
|
@@ -215,7 +402,7 @@ function jwkThumbprint(jwk) {
|
|
|
215
402
|
return createHash("sha256").update(canonical, "utf8").digest("base64url");
|
|
216
403
|
}
|
|
217
404
|
function loadPrivateKey(pem) {
|
|
218
|
-
const key =
|
|
405
|
+
const key = createPrivateKey2(pem);
|
|
219
406
|
if (key.asymmetricKeyType === "ed25519") {
|
|
220
407
|
return { key, type: "ed25519" };
|
|
221
408
|
}
|
|
@@ -227,8 +414,8 @@ function loadPrivateKey(pem) {
|
|
|
227
414
|
);
|
|
228
415
|
}
|
|
229
416
|
function publicJwkFromPrivateKey(pem, kid) {
|
|
230
|
-
const priv =
|
|
231
|
-
const pub =
|
|
417
|
+
const priv = createPrivateKey2(pem);
|
|
418
|
+
const pub = createPublicKey2(priv);
|
|
232
419
|
const jwk = pub.export({ format: "jwk" });
|
|
233
420
|
if (kid) jwk.kid = kid;
|
|
234
421
|
return jwk;
|
|
@@ -238,8 +425,8 @@ function publicJwkFromPrivateKey(pem, kid) {
|
|
|
238
425
|
import {
|
|
239
426
|
constants,
|
|
240
427
|
createHash as createHash2,
|
|
241
|
-
randomBytes,
|
|
242
|
-
sign as
|
|
428
|
+
randomBytes as randomBytes2,
|
|
429
|
+
sign as cryptoSign2,
|
|
243
430
|
verify as cryptoVerify
|
|
244
431
|
} from "crypto";
|
|
245
432
|
var AAUTH_COVERED_COMPONENTS = [
|
|
@@ -257,7 +444,7 @@ function contentDigestSha256(body) {
|
|
|
257
444
|
return `sha-256=:${digest}:`;
|
|
258
445
|
}
|
|
259
446
|
function generateNonce() {
|
|
260
|
-
return
|
|
447
|
+
return randomBytes2(16).toString("base64url");
|
|
261
448
|
}
|
|
262
449
|
function buildSignatureBase(components, values, created, nonce) {
|
|
263
450
|
const lines = components.map((c) => `"${c}": ${values[c] ?? ""}`);
|
|
@@ -272,9 +459,9 @@ function signSignatureBase(signatureBase, privateKey) {
|
|
|
272
459
|
const data = Buffer.from(signatureBase, "utf8");
|
|
273
460
|
let signature;
|
|
274
461
|
if (privateKey.asymmetricKeyType === "ed25519") {
|
|
275
|
-
signature =
|
|
462
|
+
signature = cryptoSign2(null, data, privateKey);
|
|
276
463
|
} else {
|
|
277
|
-
signature =
|
|
464
|
+
signature = cryptoSign2("sha512", data, {
|
|
278
465
|
key: privateKey,
|
|
279
466
|
padding: constants.RSA_PKCS1_PSS_PADDING,
|
|
280
467
|
saltLength: constants.RSA_PSS_SALTLEN_DIGEST
|
|
@@ -337,7 +524,7 @@ function signRequest(privateKeyPem, method, url, options = {}) {
|
|
|
337
524
|
}
|
|
338
525
|
|
|
339
526
|
// src/verify.ts
|
|
340
|
-
import { constants as constants2, createPublicKey as
|
|
527
|
+
import { constants as constants2, createPublicKey as createPublicKey3, verify as cryptoVerify2 } from "crypto";
|
|
341
528
|
|
|
342
529
|
// src/errors.ts
|
|
343
530
|
import { LumoAuthError } from "@lumoauth/shared";
|
|
@@ -409,7 +596,7 @@ async function verifyAuthToken(token, options) {
|
|
|
409
596
|
throw new AAuthError(`Key not found: kid=${kid}`, "invalid_token");
|
|
410
597
|
}
|
|
411
598
|
assertAlgMatchesKey(alg, jwk);
|
|
412
|
-
const publicKey =
|
|
599
|
+
const publicKey = createPublicKey3({ key: jwk, format: "jwk" });
|
|
413
600
|
const data = Buffer.from(`${parts[0]}.${parts[1]}`, "utf8");
|
|
414
601
|
const signature = Buffer.from(parts[2], "base64url");
|
|
415
602
|
if (!verifier(data, publicKey, signature)) {
|
|
@@ -727,11 +914,11 @@ AAuthClient.generateKeypair = generateKeypair;
|
|
|
727
914
|
// src/dpop.ts
|
|
728
915
|
import {
|
|
729
916
|
createHash as createHash3,
|
|
730
|
-
createPrivateKey as
|
|
731
|
-
createPublicKey as
|
|
732
|
-
generateKeyPairSync as
|
|
733
|
-
randomBytes as
|
|
734
|
-
sign as
|
|
917
|
+
createPrivateKey as createPrivateKey3,
|
|
918
|
+
createPublicKey as createPublicKey4,
|
|
919
|
+
generateKeyPairSync as generateKeyPairSync3,
|
|
920
|
+
randomBytes as randomBytes3,
|
|
921
|
+
sign as cryptoSign3
|
|
735
922
|
} from "crypto";
|
|
736
923
|
import { dpopHtu } from "@lumoauth/shared";
|
|
737
924
|
var DpopProofSigner = class _DpopProofSigner {
|
|
@@ -740,7 +927,7 @@ var DpopProofSigner = class _DpopProofSigner {
|
|
|
740
927
|
if (options.privateKey) {
|
|
741
928
|
this.key = options.privateKey;
|
|
742
929
|
} else if (options.privateKeyPem) {
|
|
743
|
-
this.key =
|
|
930
|
+
this.key = createPrivateKey3(options.privateKeyPem);
|
|
744
931
|
} else {
|
|
745
932
|
throw new Error("DpopProofSigner needs privateKeyPem or privateKey");
|
|
746
933
|
}
|
|
@@ -749,7 +936,7 @@ var DpopProofSigner = class _DpopProofSigner {
|
|
|
749
936
|
}
|
|
750
937
|
this.algorithm = algorithmFor(this.key);
|
|
751
938
|
this.now = options.now ?? (() => Math.floor(Date.now() / 1e3));
|
|
752
|
-
const pub =
|
|
939
|
+
const pub = createPublicKey4(this.key).export({ format: "jwk" });
|
|
753
940
|
this.jwk = publicMembers(pub);
|
|
754
941
|
this.thumbprint = jwkThumbprint(this.jwk);
|
|
755
942
|
}
|
|
@@ -761,13 +948,13 @@ var DpopProofSigner = class _DpopProofSigner {
|
|
|
761
948
|
let privateKey;
|
|
762
949
|
switch (algorithm) {
|
|
763
950
|
case "ES256":
|
|
764
|
-
privateKey =
|
|
951
|
+
privateKey = generateKeyPairSync3("ec", { namedCurve: "P-256" }).privateKey;
|
|
765
952
|
break;
|
|
766
953
|
case "EdDSA":
|
|
767
|
-
privateKey =
|
|
954
|
+
privateKey = generateKeyPairSync3("ed25519").privateKey;
|
|
768
955
|
break;
|
|
769
956
|
case "RS256":
|
|
770
|
-
privateKey =
|
|
957
|
+
privateKey = generateKeyPairSync3("rsa", { modulusLength: 2048 }).privateKey;
|
|
771
958
|
break;
|
|
772
959
|
default:
|
|
773
960
|
throw new Error(`Unsupported DPoP algorithm: ${String(algorithm)}`);
|
|
@@ -785,14 +972,14 @@ var DpopProofSigner = class _DpopProofSigner {
|
|
|
785
972
|
const nonce = input.nonce ?? this.getNonce(input.url);
|
|
786
973
|
const header = { typ: "dpop+jwt", alg: this.algorithm, jwk: this.jwk };
|
|
787
974
|
const claims = {
|
|
788
|
-
jti:
|
|
975
|
+
jti: randomBytes3(24).toString("base64url"),
|
|
789
976
|
htm: method,
|
|
790
977
|
htu,
|
|
791
978
|
iat: this.now()
|
|
792
979
|
};
|
|
793
980
|
if (input.accessToken) claims.ath = dpopAth(input.accessToken);
|
|
794
981
|
if (nonce) claims.nonce = nonce;
|
|
795
|
-
const signingInput = `${
|
|
982
|
+
const signingInput = `${b64url2(JSON.stringify(header))}.${b64url2(JSON.stringify(claims))}`;
|
|
796
983
|
const signature = signJws(this.algorithm, Buffer.from(signingInput, "utf8"), this.key);
|
|
797
984
|
return `${signingInput}.${signature.toString("base64url")}`;
|
|
798
985
|
}
|
|
@@ -815,11 +1002,11 @@ function dpopAth(accessToken) {
|
|
|
815
1002
|
function signJws(alg, data, key) {
|
|
816
1003
|
switch (alg) {
|
|
817
1004
|
case "ES256":
|
|
818
|
-
return
|
|
1005
|
+
return cryptoSign3("sha256", data, { key, dsaEncoding: "ieee-p1363" });
|
|
819
1006
|
case "EdDSA":
|
|
820
|
-
return
|
|
1007
|
+
return cryptoSign3(null, data, key);
|
|
821
1008
|
case "RS256":
|
|
822
|
-
return
|
|
1009
|
+
return cryptoSign3("sha256", data, key);
|
|
823
1010
|
default:
|
|
824
1011
|
throw new Error(`Unsupported DPoP algorithm: ${String(alg)}`);
|
|
825
1012
|
}
|
|
@@ -851,7 +1038,7 @@ function publicMembers(jwk) {
|
|
|
851
1038
|
function originOf(url) {
|
|
852
1039
|
return new URL(url).origin;
|
|
853
1040
|
}
|
|
854
|
-
function
|
|
1041
|
+
function b64url2(text) {
|
|
855
1042
|
return Buffer.from(text, "utf8").toString("base64url");
|
|
856
1043
|
}
|
|
857
1044
|
|
|
@@ -919,7 +1106,7 @@ async function attestWorkload(options) {
|
|
|
919
1106
|
}
|
|
920
1107
|
|
|
921
1108
|
// src/access-token.ts
|
|
922
|
-
import { constants as constants3, createHash as createHash4, createPublicKey as
|
|
1109
|
+
import { constants as constants3, createHash as createHash4, createPublicKey as createPublicKey5, verify as cryptoVerify3 } from "crypto";
|
|
923
1110
|
import { DPOP_SCHEME, LumoAuthError as LumoAuthError2, dpopHtu as dpopHtu2 } from "@lumoauth/shared";
|
|
924
1111
|
var AccessTokenError = class extends LumoAuthError2 {
|
|
925
1112
|
constructor(message, code, statusCode = 401) {
|
|
@@ -1093,7 +1280,7 @@ async function verifyAccessToken(token, options) {
|
|
|
1093
1280
|
throw new AccessTokenError(`Key not found: kid=${kid}`, "invalid_token");
|
|
1094
1281
|
}
|
|
1095
1282
|
assertAlgMatchesKey2(alg, jwk);
|
|
1096
|
-
const publicKey =
|
|
1283
|
+
const publicKey = createPublicKey5({ key: jwk, format: "jwk" });
|
|
1097
1284
|
const data = Buffer.from(`${parts[0]}.${parts[1]}`, "utf8");
|
|
1098
1285
|
const signature = Buffer.from(parts[2], "base64url");
|
|
1099
1286
|
if (!verifier(data, publicKey, signature)) {
|
|
@@ -1202,7 +1389,7 @@ async function verifyDpopProof(proof, options) {
|
|
|
1202
1389
|
}
|
|
1203
1390
|
let publicKey;
|
|
1204
1391
|
try {
|
|
1205
|
-
publicKey =
|
|
1392
|
+
publicKey = createPublicKey5({ key: jwk, format: "jwk" });
|
|
1206
1393
|
} catch {
|
|
1207
1394
|
throw new AccessTokenError("DPoP proof jwk is not a valid public key", "invalid_dpop_proof");
|
|
1208
1395
|
}
|
|
@@ -1315,6 +1502,9 @@ export {
|
|
|
1315
1502
|
AAuthClient,
|
|
1316
1503
|
AAuthError,
|
|
1317
1504
|
AccessTokenError,
|
|
1505
|
+
CLIENT_ASSERTION_TYP,
|
|
1506
|
+
CLIENT_ASSERTION_TYPE,
|
|
1507
|
+
ClientAssertionSigner,
|
|
1318
1508
|
DPOP_NONCE_HEADER,
|
|
1319
1509
|
DPOP_SCHEME2 as DPOP_SCHEME,
|
|
1320
1510
|
DpopProofSigner,
|
|
@@ -1334,6 +1524,7 @@ export {
|
|
|
1334
1524
|
LumoAuthPermissionDeniedError,
|
|
1335
1525
|
LumoAuthRateLimitError,
|
|
1336
1526
|
LumoAuthValidationError,
|
|
1527
|
+
MAX_ASSERTION_LIFETIME_SECONDS,
|
|
1337
1528
|
USE_DPOP_NONCE,
|
|
1338
1529
|
accessTokenScopes,
|
|
1339
1530
|
attestWorkload,
|
|
@@ -1347,6 +1538,7 @@ export {
|
|
|
1347
1538
|
fetchWithDpop3 as fetchWithDpop,
|
|
1348
1539
|
generateKeypair,
|
|
1349
1540
|
generateNonce,
|
|
1541
|
+
issuerFor,
|
|
1350
1542
|
jwkThumbprint,
|
|
1351
1543
|
parseAuthorizationHeader,
|
|
1352
1544
|
publicJwkFromPrivateKey,
|