@acarmisc/backstage-plugin-litellm-backend 0.2.2 → 0.3.1
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/config.d.ts +29 -0
- package/dist/bridge.d.ts +69 -0
- package/dist/bridge.js +144 -0
- package/dist/index.cjs.js +1575 -9
- package/dist/index.cjs.js.map +4 -4
- package/dist/index.d.ts +2 -0
- package/dist/index.js +9 -1
- package/dist/plugin.js +14 -0
- package/dist/router.d.ts +6 -0
- package/dist/router.js +75 -1
- package/package.json +6 -4
- package/dist/index.esm.js +0 -323
- package/dist/index.esm.js.map +0 -7
package/dist/index.cjs.js
CHANGED
|
@@ -30,10 +30,17 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
|
+
KeycloakJWTVerifier: () => KeycloakJWTVerifier,
|
|
33
34
|
LiteLLMClient: () => LiteLLMClient,
|
|
35
|
+
bridgeGenerateKey: () => bridgeGenerateKey,
|
|
36
|
+
bridgeListKeys: () => bridgeListKeys,
|
|
34
37
|
createRouter: () => createRouter,
|
|
35
38
|
default: () => litellmPlugin,
|
|
36
|
-
|
|
39
|
+
getOrProvisionUserFromClaims: () => getOrProvisionUserFromClaims,
|
|
40
|
+
litellmPlugin: () => litellmPlugin,
|
|
41
|
+
newDefaultVerifier: () => newDefaultVerifier,
|
|
42
|
+
readBridgeConfig: () => readBridgeConfig,
|
|
43
|
+
resolveBridgeUserId: () => resolveBridgeUserId
|
|
37
44
|
});
|
|
38
45
|
module.exports = __toCommonJS(index_exports);
|
|
39
46
|
|
|
@@ -540,15 +547,15 @@ async function provisionUser(client, userId, defaults, profile, backstageEntity,
|
|
|
540
547
|
}
|
|
541
548
|
}
|
|
542
549
|
var provisioningInFlight = /* @__PURE__ */ new Map();
|
|
543
|
-
function sanitizeUpstreamMessage(
|
|
544
|
-
if (!
|
|
545
|
-
return
|
|
550
|
+
function sanitizeUpstreamMessage(message2) {
|
|
551
|
+
if (!message2) return "unknown error";
|
|
552
|
+
return message2.replace(/Bearer\s+[A-Za-z0-9._\-+/=]+/g, "Bearer [redacted]").replace(/sk-[A-Za-z0-9_\-]{8,}/g, "sk-[redacted]").slice(0, 500);
|
|
546
553
|
}
|
|
547
554
|
var ProvisioningError = class extends Error {
|
|
548
|
-
constructor(
|
|
549
|
-
super(
|
|
555
|
+
constructor(message2, hint, provisioning, status = 404) {
|
|
556
|
+
super(message2);
|
|
550
557
|
this.status = status;
|
|
551
|
-
this.body = { error:
|
|
558
|
+
this.body = { error: message2, hint, provisioning };
|
|
552
559
|
}
|
|
553
560
|
};
|
|
554
561
|
async function getOrProvisionUser(client, tokenEntityRef, userId, provisioningEnabled, provisioningDefaults, roleConfigs, catalogClient, auth, logger) {
|
|
@@ -654,13 +661,1478 @@ async function resolveUserRole(userEntityRef, roleConfigs, catalogClient, auth,
|
|
|
654
661
|
}
|
|
655
662
|
}
|
|
656
663
|
|
|
664
|
+
// node_modules/jose/dist/node/esm/runtime/base64url.js
|
|
665
|
+
var import_node_buffer = require("node:buffer");
|
|
666
|
+
|
|
667
|
+
// node_modules/jose/dist/node/esm/lib/buffer_utils.js
|
|
668
|
+
var encoder = new TextEncoder();
|
|
669
|
+
var decoder = new TextDecoder();
|
|
670
|
+
var MAX_INT32 = 2 ** 32;
|
|
671
|
+
function concat(...buffers) {
|
|
672
|
+
const size = buffers.reduce((acc, { length }) => acc + length, 0);
|
|
673
|
+
const buf = new Uint8Array(size);
|
|
674
|
+
let i = 0;
|
|
675
|
+
for (const buffer of buffers) {
|
|
676
|
+
buf.set(buffer, i);
|
|
677
|
+
i += buffer.length;
|
|
678
|
+
}
|
|
679
|
+
return buf;
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
// node_modules/jose/dist/node/esm/runtime/base64url.js
|
|
683
|
+
function normalize(input) {
|
|
684
|
+
let encoded = input;
|
|
685
|
+
if (encoded instanceof Uint8Array) {
|
|
686
|
+
encoded = decoder.decode(encoded);
|
|
687
|
+
}
|
|
688
|
+
return encoded;
|
|
689
|
+
}
|
|
690
|
+
var decode = (input) => new Uint8Array(import_node_buffer.Buffer.from(normalize(input), "base64url"));
|
|
691
|
+
|
|
692
|
+
// node_modules/jose/dist/node/esm/util/errors.js
|
|
693
|
+
var JOSEError = class extends Error {
|
|
694
|
+
static code = "ERR_JOSE_GENERIC";
|
|
695
|
+
code = "ERR_JOSE_GENERIC";
|
|
696
|
+
constructor(message2, options) {
|
|
697
|
+
super(message2, options);
|
|
698
|
+
this.name = this.constructor.name;
|
|
699
|
+
Error.captureStackTrace?.(this, this.constructor);
|
|
700
|
+
}
|
|
701
|
+
};
|
|
702
|
+
var JWTClaimValidationFailed = class extends JOSEError {
|
|
703
|
+
static code = "ERR_JWT_CLAIM_VALIDATION_FAILED";
|
|
704
|
+
code = "ERR_JWT_CLAIM_VALIDATION_FAILED";
|
|
705
|
+
claim;
|
|
706
|
+
reason;
|
|
707
|
+
payload;
|
|
708
|
+
constructor(message2, payload, claim = "unspecified", reason = "unspecified") {
|
|
709
|
+
super(message2, { cause: { claim, reason, payload } });
|
|
710
|
+
this.claim = claim;
|
|
711
|
+
this.reason = reason;
|
|
712
|
+
this.payload = payload;
|
|
713
|
+
}
|
|
714
|
+
};
|
|
715
|
+
var JWTExpired = class extends JOSEError {
|
|
716
|
+
static code = "ERR_JWT_EXPIRED";
|
|
717
|
+
code = "ERR_JWT_EXPIRED";
|
|
718
|
+
claim;
|
|
719
|
+
reason;
|
|
720
|
+
payload;
|
|
721
|
+
constructor(message2, payload, claim = "unspecified", reason = "unspecified") {
|
|
722
|
+
super(message2, { cause: { claim, reason, payload } });
|
|
723
|
+
this.claim = claim;
|
|
724
|
+
this.reason = reason;
|
|
725
|
+
this.payload = payload;
|
|
726
|
+
}
|
|
727
|
+
};
|
|
728
|
+
var JOSEAlgNotAllowed = class extends JOSEError {
|
|
729
|
+
static code = "ERR_JOSE_ALG_NOT_ALLOWED";
|
|
730
|
+
code = "ERR_JOSE_ALG_NOT_ALLOWED";
|
|
731
|
+
};
|
|
732
|
+
var JOSENotSupported = class extends JOSEError {
|
|
733
|
+
static code = "ERR_JOSE_NOT_SUPPORTED";
|
|
734
|
+
code = "ERR_JOSE_NOT_SUPPORTED";
|
|
735
|
+
};
|
|
736
|
+
var JWSInvalid = class extends JOSEError {
|
|
737
|
+
static code = "ERR_JWS_INVALID";
|
|
738
|
+
code = "ERR_JWS_INVALID";
|
|
739
|
+
};
|
|
740
|
+
var JWTInvalid = class extends JOSEError {
|
|
741
|
+
static code = "ERR_JWT_INVALID";
|
|
742
|
+
code = "ERR_JWT_INVALID";
|
|
743
|
+
};
|
|
744
|
+
var JWKSInvalid = class extends JOSEError {
|
|
745
|
+
static code = "ERR_JWKS_INVALID";
|
|
746
|
+
code = "ERR_JWKS_INVALID";
|
|
747
|
+
};
|
|
748
|
+
var JWKSNoMatchingKey = class extends JOSEError {
|
|
749
|
+
static code = "ERR_JWKS_NO_MATCHING_KEY";
|
|
750
|
+
code = "ERR_JWKS_NO_MATCHING_KEY";
|
|
751
|
+
constructor(message2 = "no applicable key found in the JSON Web Key Set", options) {
|
|
752
|
+
super(message2, options);
|
|
753
|
+
}
|
|
754
|
+
};
|
|
755
|
+
var JWKSMultipleMatchingKeys = class extends JOSEError {
|
|
756
|
+
[Symbol.asyncIterator];
|
|
757
|
+
static code = "ERR_JWKS_MULTIPLE_MATCHING_KEYS";
|
|
758
|
+
code = "ERR_JWKS_MULTIPLE_MATCHING_KEYS";
|
|
759
|
+
constructor(message2 = "multiple matching keys found in the JSON Web Key Set", options) {
|
|
760
|
+
super(message2, options);
|
|
761
|
+
}
|
|
762
|
+
};
|
|
763
|
+
var JWKSTimeout = class extends JOSEError {
|
|
764
|
+
static code = "ERR_JWKS_TIMEOUT";
|
|
765
|
+
code = "ERR_JWKS_TIMEOUT";
|
|
766
|
+
constructor(message2 = "request timed out", options) {
|
|
767
|
+
super(message2, options);
|
|
768
|
+
}
|
|
769
|
+
};
|
|
770
|
+
var JWSSignatureVerificationFailed = class extends JOSEError {
|
|
771
|
+
static code = "ERR_JWS_SIGNATURE_VERIFICATION_FAILED";
|
|
772
|
+
code = "ERR_JWS_SIGNATURE_VERIFICATION_FAILED";
|
|
773
|
+
constructor(message2 = "signature verification failed", options) {
|
|
774
|
+
super(message2, options);
|
|
775
|
+
}
|
|
776
|
+
};
|
|
777
|
+
|
|
778
|
+
// node_modules/jose/dist/node/esm/runtime/is_key_object.js
|
|
779
|
+
var util = __toESM(require("node:util"), 1);
|
|
780
|
+
var is_key_object_default = (obj) => util.types.isKeyObject(obj);
|
|
781
|
+
|
|
782
|
+
// node_modules/jose/dist/node/esm/runtime/webcrypto.js
|
|
783
|
+
var crypto = __toESM(require("node:crypto"), 1);
|
|
784
|
+
var util2 = __toESM(require("node:util"), 1);
|
|
785
|
+
var webcrypto2 = crypto.webcrypto;
|
|
786
|
+
var webcrypto_default = webcrypto2;
|
|
787
|
+
var isCryptoKey = (key) => util2.types.isCryptoKey(key);
|
|
788
|
+
|
|
789
|
+
// node_modules/jose/dist/node/esm/lib/crypto_key.js
|
|
790
|
+
function unusable(name, prop = "algorithm.name") {
|
|
791
|
+
return new TypeError(`CryptoKey does not support this operation, its ${prop} must be ${name}`);
|
|
792
|
+
}
|
|
793
|
+
function isAlgorithm(algorithm, name) {
|
|
794
|
+
return algorithm.name === name;
|
|
795
|
+
}
|
|
796
|
+
function getHashLength(hash) {
|
|
797
|
+
return parseInt(hash.name.slice(4), 10);
|
|
798
|
+
}
|
|
799
|
+
function getNamedCurve(alg) {
|
|
800
|
+
switch (alg) {
|
|
801
|
+
case "ES256":
|
|
802
|
+
return "P-256";
|
|
803
|
+
case "ES384":
|
|
804
|
+
return "P-384";
|
|
805
|
+
case "ES512":
|
|
806
|
+
return "P-521";
|
|
807
|
+
default:
|
|
808
|
+
throw new Error("unreachable");
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
function checkUsage(key, usages) {
|
|
812
|
+
if (usages.length && !usages.some((expected) => key.usages.includes(expected))) {
|
|
813
|
+
let msg = "CryptoKey does not support this operation, its usages must include ";
|
|
814
|
+
if (usages.length > 2) {
|
|
815
|
+
const last = usages.pop();
|
|
816
|
+
msg += `one of ${usages.join(", ")}, or ${last}.`;
|
|
817
|
+
} else if (usages.length === 2) {
|
|
818
|
+
msg += `one of ${usages[0]} or ${usages[1]}.`;
|
|
819
|
+
} else {
|
|
820
|
+
msg += `${usages[0]}.`;
|
|
821
|
+
}
|
|
822
|
+
throw new TypeError(msg);
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
function checkSigCryptoKey(key, alg, ...usages) {
|
|
826
|
+
switch (alg) {
|
|
827
|
+
case "HS256":
|
|
828
|
+
case "HS384":
|
|
829
|
+
case "HS512": {
|
|
830
|
+
if (!isAlgorithm(key.algorithm, "HMAC"))
|
|
831
|
+
throw unusable("HMAC");
|
|
832
|
+
const expected = parseInt(alg.slice(2), 10);
|
|
833
|
+
const actual = getHashLength(key.algorithm.hash);
|
|
834
|
+
if (actual !== expected)
|
|
835
|
+
throw unusable(`SHA-${expected}`, "algorithm.hash");
|
|
836
|
+
break;
|
|
837
|
+
}
|
|
838
|
+
case "RS256":
|
|
839
|
+
case "RS384":
|
|
840
|
+
case "RS512": {
|
|
841
|
+
if (!isAlgorithm(key.algorithm, "RSASSA-PKCS1-v1_5"))
|
|
842
|
+
throw unusable("RSASSA-PKCS1-v1_5");
|
|
843
|
+
const expected = parseInt(alg.slice(2), 10);
|
|
844
|
+
const actual = getHashLength(key.algorithm.hash);
|
|
845
|
+
if (actual !== expected)
|
|
846
|
+
throw unusable(`SHA-${expected}`, "algorithm.hash");
|
|
847
|
+
break;
|
|
848
|
+
}
|
|
849
|
+
case "PS256":
|
|
850
|
+
case "PS384":
|
|
851
|
+
case "PS512": {
|
|
852
|
+
if (!isAlgorithm(key.algorithm, "RSA-PSS"))
|
|
853
|
+
throw unusable("RSA-PSS");
|
|
854
|
+
const expected = parseInt(alg.slice(2), 10);
|
|
855
|
+
const actual = getHashLength(key.algorithm.hash);
|
|
856
|
+
if (actual !== expected)
|
|
857
|
+
throw unusable(`SHA-${expected}`, "algorithm.hash");
|
|
858
|
+
break;
|
|
859
|
+
}
|
|
860
|
+
case "EdDSA": {
|
|
861
|
+
if (key.algorithm.name !== "Ed25519" && key.algorithm.name !== "Ed448") {
|
|
862
|
+
throw unusable("Ed25519 or Ed448");
|
|
863
|
+
}
|
|
864
|
+
break;
|
|
865
|
+
}
|
|
866
|
+
case "Ed25519": {
|
|
867
|
+
if (!isAlgorithm(key.algorithm, "Ed25519"))
|
|
868
|
+
throw unusable("Ed25519");
|
|
869
|
+
break;
|
|
870
|
+
}
|
|
871
|
+
case "ES256":
|
|
872
|
+
case "ES384":
|
|
873
|
+
case "ES512": {
|
|
874
|
+
if (!isAlgorithm(key.algorithm, "ECDSA"))
|
|
875
|
+
throw unusable("ECDSA");
|
|
876
|
+
const expected = getNamedCurve(alg);
|
|
877
|
+
const actual = key.algorithm.namedCurve;
|
|
878
|
+
if (actual !== expected)
|
|
879
|
+
throw unusable(expected, "algorithm.namedCurve");
|
|
880
|
+
break;
|
|
881
|
+
}
|
|
882
|
+
default:
|
|
883
|
+
throw new TypeError("CryptoKey does not support this operation");
|
|
884
|
+
}
|
|
885
|
+
checkUsage(key, usages);
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
// node_modules/jose/dist/node/esm/lib/invalid_key_input.js
|
|
889
|
+
function message(msg, actual, ...types4) {
|
|
890
|
+
types4 = types4.filter(Boolean);
|
|
891
|
+
if (types4.length > 2) {
|
|
892
|
+
const last = types4.pop();
|
|
893
|
+
msg += `one of type ${types4.join(", ")}, or ${last}.`;
|
|
894
|
+
} else if (types4.length === 2) {
|
|
895
|
+
msg += `one of type ${types4[0]} or ${types4[1]}.`;
|
|
896
|
+
} else {
|
|
897
|
+
msg += `of type ${types4[0]}.`;
|
|
898
|
+
}
|
|
899
|
+
if (actual == null) {
|
|
900
|
+
msg += ` Received ${actual}`;
|
|
901
|
+
} else if (typeof actual === "function" && actual.name) {
|
|
902
|
+
msg += ` Received function ${actual.name}`;
|
|
903
|
+
} else if (typeof actual === "object" && actual != null) {
|
|
904
|
+
if (actual.constructor?.name) {
|
|
905
|
+
msg += ` Received an instance of ${actual.constructor.name}`;
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
return msg;
|
|
909
|
+
}
|
|
910
|
+
var invalid_key_input_default = (actual, ...types4) => {
|
|
911
|
+
return message("Key must be ", actual, ...types4);
|
|
912
|
+
};
|
|
913
|
+
function withAlg(alg, actual, ...types4) {
|
|
914
|
+
return message(`Key for the ${alg} algorithm must be `, actual, ...types4);
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
// node_modules/jose/dist/node/esm/runtime/is_key_like.js
|
|
918
|
+
var is_key_like_default = (key) => is_key_object_default(key) || isCryptoKey(key);
|
|
919
|
+
var types3 = ["KeyObject"];
|
|
920
|
+
if (globalThis.CryptoKey || webcrypto_default?.CryptoKey) {
|
|
921
|
+
types3.push("CryptoKey");
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
// node_modules/jose/dist/node/esm/lib/is_disjoint.js
|
|
925
|
+
var isDisjoint = (...headers) => {
|
|
926
|
+
const sources = headers.filter(Boolean);
|
|
927
|
+
if (sources.length === 0 || sources.length === 1) {
|
|
928
|
+
return true;
|
|
929
|
+
}
|
|
930
|
+
let acc;
|
|
931
|
+
for (const header of sources) {
|
|
932
|
+
const parameters = Object.keys(header);
|
|
933
|
+
if (!acc || acc.size === 0) {
|
|
934
|
+
acc = new Set(parameters);
|
|
935
|
+
continue;
|
|
936
|
+
}
|
|
937
|
+
for (const parameter of parameters) {
|
|
938
|
+
if (acc.has(parameter)) {
|
|
939
|
+
return false;
|
|
940
|
+
}
|
|
941
|
+
acc.add(parameter);
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
return true;
|
|
945
|
+
};
|
|
946
|
+
var is_disjoint_default = isDisjoint;
|
|
947
|
+
|
|
948
|
+
// node_modules/jose/dist/node/esm/lib/is_object.js
|
|
949
|
+
function isObjectLike(value) {
|
|
950
|
+
return typeof value === "object" && value !== null;
|
|
951
|
+
}
|
|
952
|
+
function isObject(input) {
|
|
953
|
+
if (!isObjectLike(input) || Object.prototype.toString.call(input) !== "[object Object]") {
|
|
954
|
+
return false;
|
|
955
|
+
}
|
|
956
|
+
if (Object.getPrototypeOf(input) === null) {
|
|
957
|
+
return true;
|
|
958
|
+
}
|
|
959
|
+
let proto = input;
|
|
960
|
+
while (Object.getPrototypeOf(proto) !== null) {
|
|
961
|
+
proto = Object.getPrototypeOf(proto);
|
|
962
|
+
}
|
|
963
|
+
return Object.getPrototypeOf(input) === proto;
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
// node_modules/jose/dist/node/esm/runtime/get_named_curve.js
|
|
967
|
+
var import_node_crypto = require("node:crypto");
|
|
968
|
+
|
|
969
|
+
// node_modules/jose/dist/node/esm/lib/is_jwk.js
|
|
970
|
+
function isJWK(key) {
|
|
971
|
+
return isObject(key) && typeof key.kty === "string";
|
|
972
|
+
}
|
|
973
|
+
function isPrivateJWK(key) {
|
|
974
|
+
return key.kty !== "oct" && typeof key.d === "string";
|
|
975
|
+
}
|
|
976
|
+
function isPublicJWK(key) {
|
|
977
|
+
return key.kty !== "oct" && typeof key.d === "undefined";
|
|
978
|
+
}
|
|
979
|
+
function isSecretJWK(key) {
|
|
980
|
+
return isJWK(key) && key.kty === "oct" && typeof key.k === "string";
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
// node_modules/jose/dist/node/esm/runtime/get_named_curve.js
|
|
984
|
+
var namedCurveToJOSE = (namedCurve) => {
|
|
985
|
+
switch (namedCurve) {
|
|
986
|
+
case "prime256v1":
|
|
987
|
+
return "P-256";
|
|
988
|
+
case "secp384r1":
|
|
989
|
+
return "P-384";
|
|
990
|
+
case "secp521r1":
|
|
991
|
+
return "P-521";
|
|
992
|
+
case "secp256k1":
|
|
993
|
+
return "secp256k1";
|
|
994
|
+
default:
|
|
995
|
+
throw new JOSENotSupported("Unsupported key curve for this operation");
|
|
996
|
+
}
|
|
997
|
+
};
|
|
998
|
+
var getNamedCurve2 = (kee, raw) => {
|
|
999
|
+
let key;
|
|
1000
|
+
if (isCryptoKey(kee)) {
|
|
1001
|
+
key = import_node_crypto.KeyObject.from(kee);
|
|
1002
|
+
} else if (is_key_object_default(kee)) {
|
|
1003
|
+
key = kee;
|
|
1004
|
+
} else if (isJWK(kee)) {
|
|
1005
|
+
return kee.crv;
|
|
1006
|
+
} else {
|
|
1007
|
+
throw new TypeError(invalid_key_input_default(kee, ...types3));
|
|
1008
|
+
}
|
|
1009
|
+
if (key.type === "secret") {
|
|
1010
|
+
throw new TypeError('only "private" or "public" type keys can be used for this operation');
|
|
1011
|
+
}
|
|
1012
|
+
switch (key.asymmetricKeyType) {
|
|
1013
|
+
case "ed25519":
|
|
1014
|
+
case "ed448":
|
|
1015
|
+
return `Ed${key.asymmetricKeyType.slice(2)}`;
|
|
1016
|
+
case "x25519":
|
|
1017
|
+
case "x448":
|
|
1018
|
+
return `X${key.asymmetricKeyType.slice(1)}`;
|
|
1019
|
+
case "ec": {
|
|
1020
|
+
const namedCurve = key.asymmetricKeyDetails.namedCurve;
|
|
1021
|
+
if (raw) {
|
|
1022
|
+
return namedCurve;
|
|
1023
|
+
}
|
|
1024
|
+
return namedCurveToJOSE(namedCurve);
|
|
1025
|
+
}
|
|
1026
|
+
default:
|
|
1027
|
+
throw new TypeError("Invalid asymmetric key type for this operation");
|
|
1028
|
+
}
|
|
1029
|
+
};
|
|
1030
|
+
var get_named_curve_default = getNamedCurve2;
|
|
1031
|
+
|
|
1032
|
+
// node_modules/jose/dist/node/esm/runtime/check_key_length.js
|
|
1033
|
+
var import_node_crypto2 = require("node:crypto");
|
|
1034
|
+
var check_key_length_default = (key, alg) => {
|
|
1035
|
+
let modulusLength;
|
|
1036
|
+
try {
|
|
1037
|
+
if (key instanceof import_node_crypto2.KeyObject) {
|
|
1038
|
+
modulusLength = key.asymmetricKeyDetails?.modulusLength;
|
|
1039
|
+
} else {
|
|
1040
|
+
modulusLength = Buffer.from(key.n, "base64url").byteLength << 3;
|
|
1041
|
+
}
|
|
1042
|
+
} catch {
|
|
1043
|
+
}
|
|
1044
|
+
if (typeof modulusLength !== "number" || modulusLength < 2048) {
|
|
1045
|
+
throw new TypeError(`${alg} requires key modulusLength to be 2048 bits or larger`);
|
|
1046
|
+
}
|
|
1047
|
+
};
|
|
1048
|
+
|
|
1049
|
+
// node_modules/jose/dist/node/esm/runtime/jwk_to_key.js
|
|
1050
|
+
var import_node_crypto3 = require("node:crypto");
|
|
1051
|
+
var parse = (key) => {
|
|
1052
|
+
if (key.d) {
|
|
1053
|
+
return (0, import_node_crypto3.createPrivateKey)({ format: "jwk", key });
|
|
1054
|
+
}
|
|
1055
|
+
return (0, import_node_crypto3.createPublicKey)({ format: "jwk", key });
|
|
1056
|
+
};
|
|
1057
|
+
var jwk_to_key_default = parse;
|
|
1058
|
+
|
|
1059
|
+
// node_modules/jose/dist/node/esm/key/import.js
|
|
1060
|
+
async function importJWK(jwk, alg) {
|
|
1061
|
+
if (!isObject(jwk)) {
|
|
1062
|
+
throw new TypeError("JWK must be an object");
|
|
1063
|
+
}
|
|
1064
|
+
alg ||= jwk.alg;
|
|
1065
|
+
switch (jwk.kty) {
|
|
1066
|
+
case "oct":
|
|
1067
|
+
if (typeof jwk.k !== "string" || !jwk.k) {
|
|
1068
|
+
throw new TypeError('missing "k" (Key Value) Parameter value');
|
|
1069
|
+
}
|
|
1070
|
+
return decode(jwk.k);
|
|
1071
|
+
case "RSA":
|
|
1072
|
+
if ("oth" in jwk && jwk.oth !== void 0) {
|
|
1073
|
+
throw new JOSENotSupported('RSA JWK "oth" (Other Primes Info) Parameter value is not supported');
|
|
1074
|
+
}
|
|
1075
|
+
case "EC":
|
|
1076
|
+
case "OKP":
|
|
1077
|
+
return jwk_to_key_default({ ...jwk, alg });
|
|
1078
|
+
default:
|
|
1079
|
+
throw new JOSENotSupported('Unsupported "kty" (Key Type) Parameter value');
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
// node_modules/jose/dist/node/esm/lib/check_key_type.js
|
|
1084
|
+
var tag = (key) => key?.[Symbol.toStringTag];
|
|
1085
|
+
var jwkMatchesOp = (alg, key, usage) => {
|
|
1086
|
+
if (key.use !== void 0 && key.use !== "sig") {
|
|
1087
|
+
throw new TypeError("Invalid key for this operation, when present its use must be sig");
|
|
1088
|
+
}
|
|
1089
|
+
if (key.key_ops !== void 0 && key.key_ops.includes?.(usage) !== true) {
|
|
1090
|
+
throw new TypeError(`Invalid key for this operation, when present its key_ops must include ${usage}`);
|
|
1091
|
+
}
|
|
1092
|
+
if (key.alg !== void 0 && key.alg !== alg) {
|
|
1093
|
+
throw new TypeError(`Invalid key for this operation, when present its alg must be ${alg}`);
|
|
1094
|
+
}
|
|
1095
|
+
return true;
|
|
1096
|
+
};
|
|
1097
|
+
var symmetricTypeCheck = (alg, key, usage, allowJwk) => {
|
|
1098
|
+
if (key instanceof Uint8Array)
|
|
1099
|
+
return;
|
|
1100
|
+
if (allowJwk && isJWK(key)) {
|
|
1101
|
+
if (isSecretJWK(key) && jwkMatchesOp(alg, key, usage))
|
|
1102
|
+
return;
|
|
1103
|
+
throw new TypeError(`JSON Web Key for symmetric algorithms must have JWK "kty" (Key Type) equal to "oct" and the JWK "k" (Key Value) present`);
|
|
1104
|
+
}
|
|
1105
|
+
if (!is_key_like_default(key)) {
|
|
1106
|
+
throw new TypeError(withAlg(alg, key, ...types3, "Uint8Array", allowJwk ? "JSON Web Key" : null));
|
|
1107
|
+
}
|
|
1108
|
+
if (key.type !== "secret") {
|
|
1109
|
+
throw new TypeError(`${tag(key)} instances for symmetric algorithms must be of type "secret"`);
|
|
1110
|
+
}
|
|
1111
|
+
};
|
|
1112
|
+
var asymmetricTypeCheck = (alg, key, usage, allowJwk) => {
|
|
1113
|
+
if (allowJwk && isJWK(key)) {
|
|
1114
|
+
switch (usage) {
|
|
1115
|
+
case "sign":
|
|
1116
|
+
if (isPrivateJWK(key) && jwkMatchesOp(alg, key, usage))
|
|
1117
|
+
return;
|
|
1118
|
+
throw new TypeError(`JSON Web Key for this operation be a private JWK`);
|
|
1119
|
+
case "verify":
|
|
1120
|
+
if (isPublicJWK(key) && jwkMatchesOp(alg, key, usage))
|
|
1121
|
+
return;
|
|
1122
|
+
throw new TypeError(`JSON Web Key for this operation be a public JWK`);
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
if (!is_key_like_default(key)) {
|
|
1126
|
+
throw new TypeError(withAlg(alg, key, ...types3, allowJwk ? "JSON Web Key" : null));
|
|
1127
|
+
}
|
|
1128
|
+
if (key.type === "secret") {
|
|
1129
|
+
throw new TypeError(`${tag(key)} instances for asymmetric algorithms must not be of type "secret"`);
|
|
1130
|
+
}
|
|
1131
|
+
if (usage === "sign" && key.type === "public") {
|
|
1132
|
+
throw new TypeError(`${tag(key)} instances for asymmetric algorithm signing must be of type "private"`);
|
|
1133
|
+
}
|
|
1134
|
+
if (usage === "decrypt" && key.type === "public") {
|
|
1135
|
+
throw new TypeError(`${tag(key)} instances for asymmetric algorithm decryption must be of type "private"`);
|
|
1136
|
+
}
|
|
1137
|
+
if (key.algorithm && usage === "verify" && key.type === "private") {
|
|
1138
|
+
throw new TypeError(`${tag(key)} instances for asymmetric algorithm verifying must be of type "public"`);
|
|
1139
|
+
}
|
|
1140
|
+
if (key.algorithm && usage === "encrypt" && key.type === "private") {
|
|
1141
|
+
throw new TypeError(`${tag(key)} instances for asymmetric algorithm encryption must be of type "public"`);
|
|
1142
|
+
}
|
|
1143
|
+
};
|
|
1144
|
+
function checkKeyType(allowJwk, alg, key, usage) {
|
|
1145
|
+
const symmetric = alg.startsWith("HS") || alg === "dir" || alg.startsWith("PBES2") || /^A\d{3}(?:GCM)?KW$/.test(alg);
|
|
1146
|
+
if (symmetric) {
|
|
1147
|
+
symmetricTypeCheck(alg, key, usage, allowJwk);
|
|
1148
|
+
} else {
|
|
1149
|
+
asymmetricTypeCheck(alg, key, usage, allowJwk);
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
var check_key_type_default = checkKeyType.bind(void 0, false);
|
|
1153
|
+
var checkKeyTypeWithJwk = checkKeyType.bind(void 0, true);
|
|
1154
|
+
|
|
1155
|
+
// node_modules/jose/dist/node/esm/lib/validate_crit.js
|
|
1156
|
+
function validateCrit(Err, recognizedDefault, recognizedOption, protectedHeader, joseHeader) {
|
|
1157
|
+
if (joseHeader.crit !== void 0 && protectedHeader?.crit === void 0) {
|
|
1158
|
+
throw new Err('"crit" (Critical) Header Parameter MUST be integrity protected');
|
|
1159
|
+
}
|
|
1160
|
+
if (!protectedHeader || protectedHeader.crit === void 0) {
|
|
1161
|
+
return /* @__PURE__ */ new Set();
|
|
1162
|
+
}
|
|
1163
|
+
if (!Array.isArray(protectedHeader.crit) || protectedHeader.crit.length === 0 || protectedHeader.crit.some((input) => typeof input !== "string" || input.length === 0)) {
|
|
1164
|
+
throw new Err('"crit" (Critical) Header Parameter MUST be an array of non-empty strings when present');
|
|
1165
|
+
}
|
|
1166
|
+
let recognized;
|
|
1167
|
+
if (recognizedOption !== void 0) {
|
|
1168
|
+
recognized = new Map([...Object.entries(recognizedOption), ...recognizedDefault.entries()]);
|
|
1169
|
+
} else {
|
|
1170
|
+
recognized = recognizedDefault;
|
|
1171
|
+
}
|
|
1172
|
+
for (const parameter of protectedHeader.crit) {
|
|
1173
|
+
if (!recognized.has(parameter)) {
|
|
1174
|
+
throw new JOSENotSupported(`Extension Header Parameter "${parameter}" is not recognized`);
|
|
1175
|
+
}
|
|
1176
|
+
if (joseHeader[parameter] === void 0) {
|
|
1177
|
+
throw new Err(`Extension Header Parameter "${parameter}" is missing`);
|
|
1178
|
+
}
|
|
1179
|
+
if (recognized.get(parameter) && protectedHeader[parameter] === void 0) {
|
|
1180
|
+
throw new Err(`Extension Header Parameter "${parameter}" MUST be integrity protected`);
|
|
1181
|
+
}
|
|
1182
|
+
}
|
|
1183
|
+
return new Set(protectedHeader.crit);
|
|
1184
|
+
}
|
|
1185
|
+
var validate_crit_default = validateCrit;
|
|
1186
|
+
|
|
1187
|
+
// node_modules/jose/dist/node/esm/lib/validate_algorithms.js
|
|
1188
|
+
var validateAlgorithms = (option, algorithms) => {
|
|
1189
|
+
if (algorithms !== void 0 && (!Array.isArray(algorithms) || algorithms.some((s) => typeof s !== "string"))) {
|
|
1190
|
+
throw new TypeError(`"${option}" option must be an array of strings`);
|
|
1191
|
+
}
|
|
1192
|
+
if (!algorithms) {
|
|
1193
|
+
return void 0;
|
|
1194
|
+
}
|
|
1195
|
+
return new Set(algorithms);
|
|
1196
|
+
};
|
|
1197
|
+
var validate_algorithms_default = validateAlgorithms;
|
|
1198
|
+
|
|
1199
|
+
// node_modules/jose/dist/node/esm/runtime/verify.js
|
|
1200
|
+
var crypto3 = __toESM(require("node:crypto"), 1);
|
|
1201
|
+
var import_node_util2 = require("node:util");
|
|
1202
|
+
|
|
1203
|
+
// node_modules/jose/dist/node/esm/runtime/dsa_digest.js
|
|
1204
|
+
function dsaDigest(alg) {
|
|
1205
|
+
switch (alg) {
|
|
1206
|
+
case "PS256":
|
|
1207
|
+
case "RS256":
|
|
1208
|
+
case "ES256":
|
|
1209
|
+
case "ES256K":
|
|
1210
|
+
return "sha256";
|
|
1211
|
+
case "PS384":
|
|
1212
|
+
case "RS384":
|
|
1213
|
+
case "ES384":
|
|
1214
|
+
return "sha384";
|
|
1215
|
+
case "PS512":
|
|
1216
|
+
case "RS512":
|
|
1217
|
+
case "ES512":
|
|
1218
|
+
return "sha512";
|
|
1219
|
+
case "Ed25519":
|
|
1220
|
+
case "EdDSA":
|
|
1221
|
+
return void 0;
|
|
1222
|
+
default:
|
|
1223
|
+
throw new JOSENotSupported(`alg ${alg} is not supported either by JOSE or your javascript runtime`);
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
// node_modules/jose/dist/node/esm/runtime/node_key.js
|
|
1228
|
+
var import_node_crypto4 = require("node:crypto");
|
|
1229
|
+
var ecCurveAlgMap = /* @__PURE__ */ new Map([
|
|
1230
|
+
["ES256", "P-256"],
|
|
1231
|
+
["ES256K", "secp256k1"],
|
|
1232
|
+
["ES384", "P-384"],
|
|
1233
|
+
["ES512", "P-521"]
|
|
1234
|
+
]);
|
|
1235
|
+
function keyForCrypto(alg, key) {
|
|
1236
|
+
let asymmetricKeyType;
|
|
1237
|
+
let asymmetricKeyDetails;
|
|
1238
|
+
let isJWK2;
|
|
1239
|
+
if (key instanceof import_node_crypto4.KeyObject) {
|
|
1240
|
+
asymmetricKeyType = key.asymmetricKeyType;
|
|
1241
|
+
asymmetricKeyDetails = key.asymmetricKeyDetails;
|
|
1242
|
+
} else {
|
|
1243
|
+
isJWK2 = true;
|
|
1244
|
+
switch (key.kty) {
|
|
1245
|
+
case "RSA":
|
|
1246
|
+
asymmetricKeyType = "rsa";
|
|
1247
|
+
break;
|
|
1248
|
+
case "EC":
|
|
1249
|
+
asymmetricKeyType = "ec";
|
|
1250
|
+
break;
|
|
1251
|
+
case "OKP": {
|
|
1252
|
+
if (key.crv === "Ed25519") {
|
|
1253
|
+
asymmetricKeyType = "ed25519";
|
|
1254
|
+
break;
|
|
1255
|
+
}
|
|
1256
|
+
if (key.crv === "Ed448") {
|
|
1257
|
+
asymmetricKeyType = "ed448";
|
|
1258
|
+
break;
|
|
1259
|
+
}
|
|
1260
|
+
throw new TypeError("Invalid key for this operation, its crv must be Ed25519 or Ed448");
|
|
1261
|
+
}
|
|
1262
|
+
default:
|
|
1263
|
+
throw new TypeError("Invalid key for this operation, its kty must be RSA, OKP, or EC");
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
let options;
|
|
1267
|
+
switch (alg) {
|
|
1268
|
+
case "Ed25519":
|
|
1269
|
+
if (asymmetricKeyType !== "ed25519") {
|
|
1270
|
+
throw new TypeError(`Invalid key for this operation, its asymmetricKeyType must be ed25519`);
|
|
1271
|
+
}
|
|
1272
|
+
break;
|
|
1273
|
+
case "EdDSA":
|
|
1274
|
+
if (!["ed25519", "ed448"].includes(asymmetricKeyType)) {
|
|
1275
|
+
throw new TypeError("Invalid key for this operation, its asymmetricKeyType must be ed25519 or ed448");
|
|
1276
|
+
}
|
|
1277
|
+
break;
|
|
1278
|
+
case "RS256":
|
|
1279
|
+
case "RS384":
|
|
1280
|
+
case "RS512":
|
|
1281
|
+
if (asymmetricKeyType !== "rsa") {
|
|
1282
|
+
throw new TypeError("Invalid key for this operation, its asymmetricKeyType must be rsa");
|
|
1283
|
+
}
|
|
1284
|
+
check_key_length_default(key, alg);
|
|
1285
|
+
break;
|
|
1286
|
+
case "PS256":
|
|
1287
|
+
case "PS384":
|
|
1288
|
+
case "PS512":
|
|
1289
|
+
if (asymmetricKeyType === "rsa-pss") {
|
|
1290
|
+
const { hashAlgorithm, mgf1HashAlgorithm, saltLength } = asymmetricKeyDetails;
|
|
1291
|
+
const length = parseInt(alg.slice(-3), 10);
|
|
1292
|
+
if (hashAlgorithm !== void 0 && (hashAlgorithm !== `sha${length}` || mgf1HashAlgorithm !== hashAlgorithm)) {
|
|
1293
|
+
throw new TypeError(`Invalid key for this operation, its RSA-PSS parameters do not meet the requirements of "alg" ${alg}`);
|
|
1294
|
+
}
|
|
1295
|
+
if (saltLength !== void 0 && saltLength > length >> 3) {
|
|
1296
|
+
throw new TypeError(`Invalid key for this operation, its RSA-PSS parameter saltLength does not meet the requirements of "alg" ${alg}`);
|
|
1297
|
+
}
|
|
1298
|
+
} else if (asymmetricKeyType !== "rsa") {
|
|
1299
|
+
throw new TypeError("Invalid key for this operation, its asymmetricKeyType must be rsa or rsa-pss");
|
|
1300
|
+
}
|
|
1301
|
+
check_key_length_default(key, alg);
|
|
1302
|
+
options = {
|
|
1303
|
+
padding: import_node_crypto4.constants.RSA_PKCS1_PSS_PADDING,
|
|
1304
|
+
saltLength: import_node_crypto4.constants.RSA_PSS_SALTLEN_DIGEST
|
|
1305
|
+
};
|
|
1306
|
+
break;
|
|
1307
|
+
case "ES256":
|
|
1308
|
+
case "ES256K":
|
|
1309
|
+
case "ES384":
|
|
1310
|
+
case "ES512": {
|
|
1311
|
+
if (asymmetricKeyType !== "ec") {
|
|
1312
|
+
throw new TypeError("Invalid key for this operation, its asymmetricKeyType must be ec");
|
|
1313
|
+
}
|
|
1314
|
+
const actual = get_named_curve_default(key);
|
|
1315
|
+
const expected = ecCurveAlgMap.get(alg);
|
|
1316
|
+
if (actual !== expected) {
|
|
1317
|
+
throw new TypeError(`Invalid key curve for the algorithm, its curve must be ${expected}, got ${actual}`);
|
|
1318
|
+
}
|
|
1319
|
+
options = { dsaEncoding: "ieee-p1363" };
|
|
1320
|
+
break;
|
|
1321
|
+
}
|
|
1322
|
+
default:
|
|
1323
|
+
throw new JOSENotSupported(`alg ${alg} is not supported either by JOSE or your javascript runtime`);
|
|
1324
|
+
}
|
|
1325
|
+
if (isJWK2) {
|
|
1326
|
+
return { format: "jwk", key, ...options };
|
|
1327
|
+
}
|
|
1328
|
+
return options ? { ...options, key } : key;
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
// node_modules/jose/dist/node/esm/runtime/sign.js
|
|
1332
|
+
var crypto2 = __toESM(require("node:crypto"), 1);
|
|
1333
|
+
var import_node_util = require("node:util");
|
|
1334
|
+
|
|
1335
|
+
// node_modules/jose/dist/node/esm/runtime/hmac_digest.js
|
|
1336
|
+
function hmacDigest(alg) {
|
|
1337
|
+
switch (alg) {
|
|
1338
|
+
case "HS256":
|
|
1339
|
+
return "sha256";
|
|
1340
|
+
case "HS384":
|
|
1341
|
+
return "sha384";
|
|
1342
|
+
case "HS512":
|
|
1343
|
+
return "sha512";
|
|
1344
|
+
default:
|
|
1345
|
+
throw new JOSENotSupported(`alg ${alg} is not supported either by JOSE or your javascript runtime`);
|
|
1346
|
+
}
|
|
1347
|
+
}
|
|
1348
|
+
|
|
1349
|
+
// node_modules/jose/dist/node/esm/runtime/get_sign_verify_key.js
|
|
1350
|
+
var import_node_crypto5 = require("node:crypto");
|
|
1351
|
+
function getSignVerifyKey(alg, key, usage) {
|
|
1352
|
+
if (key instanceof Uint8Array) {
|
|
1353
|
+
if (!alg.startsWith("HS")) {
|
|
1354
|
+
throw new TypeError(invalid_key_input_default(key, ...types3));
|
|
1355
|
+
}
|
|
1356
|
+
return (0, import_node_crypto5.createSecretKey)(key);
|
|
1357
|
+
}
|
|
1358
|
+
if (key instanceof import_node_crypto5.KeyObject) {
|
|
1359
|
+
return key;
|
|
1360
|
+
}
|
|
1361
|
+
if (isCryptoKey(key)) {
|
|
1362
|
+
checkSigCryptoKey(key, alg, usage);
|
|
1363
|
+
return import_node_crypto5.KeyObject.from(key);
|
|
1364
|
+
}
|
|
1365
|
+
if (isJWK(key)) {
|
|
1366
|
+
if (alg.startsWith("HS")) {
|
|
1367
|
+
return (0, import_node_crypto5.createSecretKey)(Buffer.from(key.k, "base64url"));
|
|
1368
|
+
}
|
|
1369
|
+
return key;
|
|
1370
|
+
}
|
|
1371
|
+
throw new TypeError(invalid_key_input_default(key, ...types3, "Uint8Array", "JSON Web Key"));
|
|
1372
|
+
}
|
|
1373
|
+
|
|
1374
|
+
// node_modules/jose/dist/node/esm/runtime/sign.js
|
|
1375
|
+
var oneShotSign = (0, import_node_util.promisify)(crypto2.sign);
|
|
1376
|
+
var sign2 = async (alg, key, data) => {
|
|
1377
|
+
const k = getSignVerifyKey(alg, key, "sign");
|
|
1378
|
+
if (alg.startsWith("HS")) {
|
|
1379
|
+
const hmac = crypto2.createHmac(hmacDigest(alg), k);
|
|
1380
|
+
hmac.update(data);
|
|
1381
|
+
return hmac.digest();
|
|
1382
|
+
}
|
|
1383
|
+
return oneShotSign(dsaDigest(alg), data, keyForCrypto(alg, k));
|
|
1384
|
+
};
|
|
1385
|
+
var sign_default = sign2;
|
|
1386
|
+
|
|
1387
|
+
// node_modules/jose/dist/node/esm/runtime/verify.js
|
|
1388
|
+
var oneShotVerify = (0, import_node_util2.promisify)(crypto3.verify);
|
|
1389
|
+
var verify2 = async (alg, key, signature, data) => {
|
|
1390
|
+
const k = getSignVerifyKey(alg, key, "verify");
|
|
1391
|
+
if (alg.startsWith("HS")) {
|
|
1392
|
+
const expected = await sign_default(alg, k, data);
|
|
1393
|
+
const actual = signature;
|
|
1394
|
+
try {
|
|
1395
|
+
return crypto3.timingSafeEqual(actual, expected);
|
|
1396
|
+
} catch {
|
|
1397
|
+
return false;
|
|
1398
|
+
}
|
|
1399
|
+
}
|
|
1400
|
+
const algorithm = dsaDigest(alg);
|
|
1401
|
+
const keyInput = keyForCrypto(alg, k);
|
|
1402
|
+
try {
|
|
1403
|
+
return await oneShotVerify(algorithm, data, keyInput, signature);
|
|
1404
|
+
} catch {
|
|
1405
|
+
return false;
|
|
1406
|
+
}
|
|
1407
|
+
};
|
|
1408
|
+
var verify_default = verify2;
|
|
1409
|
+
|
|
1410
|
+
// node_modules/jose/dist/node/esm/jws/flattened/verify.js
|
|
1411
|
+
async function flattenedVerify(jws, key, options) {
|
|
1412
|
+
if (!isObject(jws)) {
|
|
1413
|
+
throw new JWSInvalid("Flattened JWS must be an object");
|
|
1414
|
+
}
|
|
1415
|
+
if (jws.protected === void 0 && jws.header === void 0) {
|
|
1416
|
+
throw new JWSInvalid('Flattened JWS must have either of the "protected" or "header" members');
|
|
1417
|
+
}
|
|
1418
|
+
if (jws.protected !== void 0 && typeof jws.protected !== "string") {
|
|
1419
|
+
throw new JWSInvalid("JWS Protected Header incorrect type");
|
|
1420
|
+
}
|
|
1421
|
+
if (jws.payload === void 0) {
|
|
1422
|
+
throw new JWSInvalid("JWS Payload missing");
|
|
1423
|
+
}
|
|
1424
|
+
if (typeof jws.signature !== "string") {
|
|
1425
|
+
throw new JWSInvalid("JWS Signature missing or incorrect type");
|
|
1426
|
+
}
|
|
1427
|
+
if (jws.header !== void 0 && !isObject(jws.header)) {
|
|
1428
|
+
throw new JWSInvalid("JWS Unprotected Header incorrect type");
|
|
1429
|
+
}
|
|
1430
|
+
let parsedProt = {};
|
|
1431
|
+
if (jws.protected) {
|
|
1432
|
+
try {
|
|
1433
|
+
const protectedHeader = decode(jws.protected);
|
|
1434
|
+
parsedProt = JSON.parse(decoder.decode(protectedHeader));
|
|
1435
|
+
} catch {
|
|
1436
|
+
throw new JWSInvalid("JWS Protected Header is invalid");
|
|
1437
|
+
}
|
|
1438
|
+
}
|
|
1439
|
+
if (!is_disjoint_default(parsedProt, jws.header)) {
|
|
1440
|
+
throw new JWSInvalid("JWS Protected and JWS Unprotected Header Parameter names must be disjoint");
|
|
1441
|
+
}
|
|
1442
|
+
const joseHeader = {
|
|
1443
|
+
...parsedProt,
|
|
1444
|
+
...jws.header
|
|
1445
|
+
};
|
|
1446
|
+
const extensions = validate_crit_default(JWSInvalid, /* @__PURE__ */ new Map([["b64", true]]), options?.crit, parsedProt, joseHeader);
|
|
1447
|
+
let b64 = true;
|
|
1448
|
+
if (extensions.has("b64")) {
|
|
1449
|
+
b64 = parsedProt.b64;
|
|
1450
|
+
if (typeof b64 !== "boolean") {
|
|
1451
|
+
throw new JWSInvalid('The "b64" (base64url-encode payload) Header Parameter must be a boolean');
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
const { alg } = joseHeader;
|
|
1455
|
+
if (typeof alg !== "string" || !alg) {
|
|
1456
|
+
throw new JWSInvalid('JWS "alg" (Algorithm) Header Parameter missing or invalid');
|
|
1457
|
+
}
|
|
1458
|
+
const algorithms = options && validate_algorithms_default("algorithms", options.algorithms);
|
|
1459
|
+
if (algorithms && !algorithms.has(alg)) {
|
|
1460
|
+
throw new JOSEAlgNotAllowed('"alg" (Algorithm) Header Parameter value not allowed');
|
|
1461
|
+
}
|
|
1462
|
+
if (b64) {
|
|
1463
|
+
if (typeof jws.payload !== "string") {
|
|
1464
|
+
throw new JWSInvalid("JWS Payload must be a string");
|
|
1465
|
+
}
|
|
1466
|
+
} else if (typeof jws.payload !== "string" && !(jws.payload instanceof Uint8Array)) {
|
|
1467
|
+
throw new JWSInvalid("JWS Payload must be a string or an Uint8Array instance");
|
|
1468
|
+
}
|
|
1469
|
+
let resolvedKey = false;
|
|
1470
|
+
if (typeof key === "function") {
|
|
1471
|
+
key = await key(parsedProt, jws);
|
|
1472
|
+
resolvedKey = true;
|
|
1473
|
+
checkKeyTypeWithJwk(alg, key, "verify");
|
|
1474
|
+
if (isJWK(key)) {
|
|
1475
|
+
key = await importJWK(key, alg);
|
|
1476
|
+
}
|
|
1477
|
+
} else {
|
|
1478
|
+
checkKeyTypeWithJwk(alg, key, "verify");
|
|
1479
|
+
}
|
|
1480
|
+
const data = concat(encoder.encode(jws.protected ?? ""), encoder.encode("."), typeof jws.payload === "string" ? encoder.encode(jws.payload) : jws.payload);
|
|
1481
|
+
let signature;
|
|
1482
|
+
try {
|
|
1483
|
+
signature = decode(jws.signature);
|
|
1484
|
+
} catch {
|
|
1485
|
+
throw new JWSInvalid("Failed to base64url decode the signature");
|
|
1486
|
+
}
|
|
1487
|
+
const verified = await verify_default(alg, key, signature, data);
|
|
1488
|
+
if (!verified) {
|
|
1489
|
+
throw new JWSSignatureVerificationFailed();
|
|
1490
|
+
}
|
|
1491
|
+
let payload;
|
|
1492
|
+
if (b64) {
|
|
1493
|
+
try {
|
|
1494
|
+
payload = decode(jws.payload);
|
|
1495
|
+
} catch {
|
|
1496
|
+
throw new JWSInvalid("Failed to base64url decode the payload");
|
|
1497
|
+
}
|
|
1498
|
+
} else if (typeof jws.payload === "string") {
|
|
1499
|
+
payload = encoder.encode(jws.payload);
|
|
1500
|
+
} else {
|
|
1501
|
+
payload = jws.payload;
|
|
1502
|
+
}
|
|
1503
|
+
const result = { payload };
|
|
1504
|
+
if (jws.protected !== void 0) {
|
|
1505
|
+
result.protectedHeader = parsedProt;
|
|
1506
|
+
}
|
|
1507
|
+
if (jws.header !== void 0) {
|
|
1508
|
+
result.unprotectedHeader = jws.header;
|
|
1509
|
+
}
|
|
1510
|
+
if (resolvedKey) {
|
|
1511
|
+
return { ...result, key };
|
|
1512
|
+
}
|
|
1513
|
+
return result;
|
|
1514
|
+
}
|
|
1515
|
+
|
|
1516
|
+
// node_modules/jose/dist/node/esm/jws/compact/verify.js
|
|
1517
|
+
async function compactVerify(jws, key, options) {
|
|
1518
|
+
if (jws instanceof Uint8Array) {
|
|
1519
|
+
jws = decoder.decode(jws);
|
|
1520
|
+
}
|
|
1521
|
+
if (typeof jws !== "string") {
|
|
1522
|
+
throw new JWSInvalid("Compact JWS must be a string or Uint8Array");
|
|
1523
|
+
}
|
|
1524
|
+
const { 0: protectedHeader, 1: payload, 2: signature, length } = jws.split(".");
|
|
1525
|
+
if (length !== 3) {
|
|
1526
|
+
throw new JWSInvalid("Invalid Compact JWS");
|
|
1527
|
+
}
|
|
1528
|
+
const verified = await flattenedVerify({ payload, protected: protectedHeader, signature }, key, options);
|
|
1529
|
+
const result = { payload: verified.payload, protectedHeader: verified.protectedHeader };
|
|
1530
|
+
if (typeof key === "function") {
|
|
1531
|
+
return { ...result, key: verified.key };
|
|
1532
|
+
}
|
|
1533
|
+
return result;
|
|
1534
|
+
}
|
|
1535
|
+
|
|
1536
|
+
// node_modules/jose/dist/node/esm/lib/epoch.js
|
|
1537
|
+
var epoch_default = (date) => Math.floor(date.getTime() / 1e3);
|
|
1538
|
+
|
|
1539
|
+
// node_modules/jose/dist/node/esm/lib/secs.js
|
|
1540
|
+
var minute = 60;
|
|
1541
|
+
var hour = minute * 60;
|
|
1542
|
+
var day = hour * 24;
|
|
1543
|
+
var week = day * 7;
|
|
1544
|
+
var year = day * 365.25;
|
|
1545
|
+
var REGEX = /^(\+|\-)? ?(\d+|\d+\.\d+) ?(seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)(?: (ago|from now))?$/i;
|
|
1546
|
+
var secs_default = (str) => {
|
|
1547
|
+
const matched = REGEX.exec(str);
|
|
1548
|
+
if (!matched || matched[4] && matched[1]) {
|
|
1549
|
+
throw new TypeError("Invalid time period format");
|
|
1550
|
+
}
|
|
1551
|
+
const value = parseFloat(matched[2]);
|
|
1552
|
+
const unit = matched[3].toLowerCase();
|
|
1553
|
+
let numericDate;
|
|
1554
|
+
switch (unit) {
|
|
1555
|
+
case "sec":
|
|
1556
|
+
case "secs":
|
|
1557
|
+
case "second":
|
|
1558
|
+
case "seconds":
|
|
1559
|
+
case "s":
|
|
1560
|
+
numericDate = Math.round(value);
|
|
1561
|
+
break;
|
|
1562
|
+
case "minute":
|
|
1563
|
+
case "minutes":
|
|
1564
|
+
case "min":
|
|
1565
|
+
case "mins":
|
|
1566
|
+
case "m":
|
|
1567
|
+
numericDate = Math.round(value * minute);
|
|
1568
|
+
break;
|
|
1569
|
+
case "hour":
|
|
1570
|
+
case "hours":
|
|
1571
|
+
case "hr":
|
|
1572
|
+
case "hrs":
|
|
1573
|
+
case "h":
|
|
1574
|
+
numericDate = Math.round(value * hour);
|
|
1575
|
+
break;
|
|
1576
|
+
case "day":
|
|
1577
|
+
case "days":
|
|
1578
|
+
case "d":
|
|
1579
|
+
numericDate = Math.round(value * day);
|
|
1580
|
+
break;
|
|
1581
|
+
case "week":
|
|
1582
|
+
case "weeks":
|
|
1583
|
+
case "w":
|
|
1584
|
+
numericDate = Math.round(value * week);
|
|
1585
|
+
break;
|
|
1586
|
+
default:
|
|
1587
|
+
numericDate = Math.round(value * year);
|
|
1588
|
+
break;
|
|
1589
|
+
}
|
|
1590
|
+
if (matched[1] === "-" || matched[4] === "ago") {
|
|
1591
|
+
return -numericDate;
|
|
1592
|
+
}
|
|
1593
|
+
return numericDate;
|
|
1594
|
+
};
|
|
1595
|
+
|
|
1596
|
+
// node_modules/jose/dist/node/esm/lib/jwt_claims_set.js
|
|
1597
|
+
var normalizeTyp = (value) => value.toLowerCase().replace(/^application\//, "");
|
|
1598
|
+
var checkAudiencePresence = (audPayload, audOption) => {
|
|
1599
|
+
if (typeof audPayload === "string") {
|
|
1600
|
+
return audOption.includes(audPayload);
|
|
1601
|
+
}
|
|
1602
|
+
if (Array.isArray(audPayload)) {
|
|
1603
|
+
return audOption.some(Set.prototype.has.bind(new Set(audPayload)));
|
|
1604
|
+
}
|
|
1605
|
+
return false;
|
|
1606
|
+
};
|
|
1607
|
+
var jwt_claims_set_default = (protectedHeader, encodedPayload, options = {}) => {
|
|
1608
|
+
let payload;
|
|
1609
|
+
try {
|
|
1610
|
+
payload = JSON.parse(decoder.decode(encodedPayload));
|
|
1611
|
+
} catch {
|
|
1612
|
+
}
|
|
1613
|
+
if (!isObject(payload)) {
|
|
1614
|
+
throw new JWTInvalid("JWT Claims Set must be a top-level JSON object");
|
|
1615
|
+
}
|
|
1616
|
+
const { typ } = options;
|
|
1617
|
+
if (typ && (typeof protectedHeader.typ !== "string" || normalizeTyp(protectedHeader.typ) !== normalizeTyp(typ))) {
|
|
1618
|
+
throw new JWTClaimValidationFailed('unexpected "typ" JWT header value', payload, "typ", "check_failed");
|
|
1619
|
+
}
|
|
1620
|
+
const { requiredClaims = [], issuer, subject, audience, maxTokenAge } = options;
|
|
1621
|
+
const presenceCheck = [...requiredClaims];
|
|
1622
|
+
if (maxTokenAge !== void 0)
|
|
1623
|
+
presenceCheck.push("iat");
|
|
1624
|
+
if (audience !== void 0)
|
|
1625
|
+
presenceCheck.push("aud");
|
|
1626
|
+
if (subject !== void 0)
|
|
1627
|
+
presenceCheck.push("sub");
|
|
1628
|
+
if (issuer !== void 0)
|
|
1629
|
+
presenceCheck.push("iss");
|
|
1630
|
+
for (const claim of new Set(presenceCheck.reverse())) {
|
|
1631
|
+
if (!(claim in payload)) {
|
|
1632
|
+
throw new JWTClaimValidationFailed(`missing required "${claim}" claim`, payload, claim, "missing");
|
|
1633
|
+
}
|
|
1634
|
+
}
|
|
1635
|
+
if (issuer && !(Array.isArray(issuer) ? issuer : [issuer]).includes(payload.iss)) {
|
|
1636
|
+
throw new JWTClaimValidationFailed('unexpected "iss" claim value', payload, "iss", "check_failed");
|
|
1637
|
+
}
|
|
1638
|
+
if (subject && payload.sub !== subject) {
|
|
1639
|
+
throw new JWTClaimValidationFailed('unexpected "sub" claim value', payload, "sub", "check_failed");
|
|
1640
|
+
}
|
|
1641
|
+
if (audience && !checkAudiencePresence(payload.aud, typeof audience === "string" ? [audience] : audience)) {
|
|
1642
|
+
throw new JWTClaimValidationFailed('unexpected "aud" claim value', payload, "aud", "check_failed");
|
|
1643
|
+
}
|
|
1644
|
+
let tolerance;
|
|
1645
|
+
switch (typeof options.clockTolerance) {
|
|
1646
|
+
case "string":
|
|
1647
|
+
tolerance = secs_default(options.clockTolerance);
|
|
1648
|
+
break;
|
|
1649
|
+
case "number":
|
|
1650
|
+
tolerance = options.clockTolerance;
|
|
1651
|
+
break;
|
|
1652
|
+
case "undefined":
|
|
1653
|
+
tolerance = 0;
|
|
1654
|
+
break;
|
|
1655
|
+
default:
|
|
1656
|
+
throw new TypeError("Invalid clockTolerance option type");
|
|
1657
|
+
}
|
|
1658
|
+
const { currentDate } = options;
|
|
1659
|
+
const now = epoch_default(currentDate || /* @__PURE__ */ new Date());
|
|
1660
|
+
if ((payload.iat !== void 0 || maxTokenAge) && typeof payload.iat !== "number") {
|
|
1661
|
+
throw new JWTClaimValidationFailed('"iat" claim must be a number', payload, "iat", "invalid");
|
|
1662
|
+
}
|
|
1663
|
+
if (payload.nbf !== void 0) {
|
|
1664
|
+
if (typeof payload.nbf !== "number") {
|
|
1665
|
+
throw new JWTClaimValidationFailed('"nbf" claim must be a number', payload, "nbf", "invalid");
|
|
1666
|
+
}
|
|
1667
|
+
if (payload.nbf > now + tolerance) {
|
|
1668
|
+
throw new JWTClaimValidationFailed('"nbf" claim timestamp check failed', payload, "nbf", "check_failed");
|
|
1669
|
+
}
|
|
1670
|
+
}
|
|
1671
|
+
if (payload.exp !== void 0) {
|
|
1672
|
+
if (typeof payload.exp !== "number") {
|
|
1673
|
+
throw new JWTClaimValidationFailed('"exp" claim must be a number', payload, "exp", "invalid");
|
|
1674
|
+
}
|
|
1675
|
+
if (payload.exp <= now - tolerance) {
|
|
1676
|
+
throw new JWTExpired('"exp" claim timestamp check failed', payload, "exp", "check_failed");
|
|
1677
|
+
}
|
|
1678
|
+
}
|
|
1679
|
+
if (maxTokenAge) {
|
|
1680
|
+
const age = now - payload.iat;
|
|
1681
|
+
const max = typeof maxTokenAge === "number" ? maxTokenAge : secs_default(maxTokenAge);
|
|
1682
|
+
if (age - tolerance > max) {
|
|
1683
|
+
throw new JWTExpired('"iat" claim timestamp check failed (too far in the past)', payload, "iat", "check_failed");
|
|
1684
|
+
}
|
|
1685
|
+
if (age < 0 - tolerance) {
|
|
1686
|
+
throw new JWTClaimValidationFailed('"iat" claim timestamp check failed (it should be in the past)', payload, "iat", "check_failed");
|
|
1687
|
+
}
|
|
1688
|
+
}
|
|
1689
|
+
return payload;
|
|
1690
|
+
};
|
|
1691
|
+
|
|
1692
|
+
// node_modules/jose/dist/node/esm/jwt/verify.js
|
|
1693
|
+
async function jwtVerify(jwt, key, options) {
|
|
1694
|
+
const verified = await compactVerify(jwt, key, options);
|
|
1695
|
+
if (verified.protectedHeader.crit?.includes("b64") && verified.protectedHeader.b64 === false) {
|
|
1696
|
+
throw new JWTInvalid("JWTs MUST NOT use unencoded payload");
|
|
1697
|
+
}
|
|
1698
|
+
const payload = jwt_claims_set_default(verified.protectedHeader, verified.payload, options);
|
|
1699
|
+
const result = { payload, protectedHeader: verified.protectedHeader };
|
|
1700
|
+
if (typeof key === "function") {
|
|
1701
|
+
return { ...result, key: verified.key };
|
|
1702
|
+
}
|
|
1703
|
+
return result;
|
|
1704
|
+
}
|
|
1705
|
+
|
|
1706
|
+
// node_modules/jose/dist/node/esm/jwks/local.js
|
|
1707
|
+
function getKtyFromAlg(alg) {
|
|
1708
|
+
switch (typeof alg === "string" && alg.slice(0, 2)) {
|
|
1709
|
+
case "RS":
|
|
1710
|
+
case "PS":
|
|
1711
|
+
return "RSA";
|
|
1712
|
+
case "ES":
|
|
1713
|
+
return "EC";
|
|
1714
|
+
case "Ed":
|
|
1715
|
+
return "OKP";
|
|
1716
|
+
default:
|
|
1717
|
+
throw new JOSENotSupported('Unsupported "alg" value for a JSON Web Key Set');
|
|
1718
|
+
}
|
|
1719
|
+
}
|
|
1720
|
+
function isJWKSLike(jwks) {
|
|
1721
|
+
return jwks && typeof jwks === "object" && Array.isArray(jwks.keys) && jwks.keys.every(isJWKLike);
|
|
1722
|
+
}
|
|
1723
|
+
function isJWKLike(key) {
|
|
1724
|
+
return isObject(key);
|
|
1725
|
+
}
|
|
1726
|
+
function clone(obj) {
|
|
1727
|
+
if (typeof structuredClone === "function") {
|
|
1728
|
+
return structuredClone(obj);
|
|
1729
|
+
}
|
|
1730
|
+
return JSON.parse(JSON.stringify(obj));
|
|
1731
|
+
}
|
|
1732
|
+
var LocalJWKSet = class {
|
|
1733
|
+
_jwks;
|
|
1734
|
+
_cached = /* @__PURE__ */ new WeakMap();
|
|
1735
|
+
constructor(jwks) {
|
|
1736
|
+
if (!isJWKSLike(jwks)) {
|
|
1737
|
+
throw new JWKSInvalid("JSON Web Key Set malformed");
|
|
1738
|
+
}
|
|
1739
|
+
this._jwks = clone(jwks);
|
|
1740
|
+
}
|
|
1741
|
+
async getKey(protectedHeader, token) {
|
|
1742
|
+
const { alg, kid } = { ...protectedHeader, ...token?.header };
|
|
1743
|
+
const kty = getKtyFromAlg(alg);
|
|
1744
|
+
const candidates = this._jwks.keys.filter((jwk2) => {
|
|
1745
|
+
let candidate = kty === jwk2.kty;
|
|
1746
|
+
if (candidate && typeof kid === "string") {
|
|
1747
|
+
candidate = kid === jwk2.kid;
|
|
1748
|
+
}
|
|
1749
|
+
if (candidate && typeof jwk2.alg === "string") {
|
|
1750
|
+
candidate = alg === jwk2.alg;
|
|
1751
|
+
}
|
|
1752
|
+
if (candidate && typeof jwk2.use === "string") {
|
|
1753
|
+
candidate = jwk2.use === "sig";
|
|
1754
|
+
}
|
|
1755
|
+
if (candidate && Array.isArray(jwk2.key_ops)) {
|
|
1756
|
+
candidate = jwk2.key_ops.includes("verify");
|
|
1757
|
+
}
|
|
1758
|
+
if (candidate) {
|
|
1759
|
+
switch (alg) {
|
|
1760
|
+
case "ES256":
|
|
1761
|
+
candidate = jwk2.crv === "P-256";
|
|
1762
|
+
break;
|
|
1763
|
+
case "ES256K":
|
|
1764
|
+
candidate = jwk2.crv === "secp256k1";
|
|
1765
|
+
break;
|
|
1766
|
+
case "ES384":
|
|
1767
|
+
candidate = jwk2.crv === "P-384";
|
|
1768
|
+
break;
|
|
1769
|
+
case "ES512":
|
|
1770
|
+
candidate = jwk2.crv === "P-521";
|
|
1771
|
+
break;
|
|
1772
|
+
case "Ed25519":
|
|
1773
|
+
candidate = jwk2.crv === "Ed25519";
|
|
1774
|
+
break;
|
|
1775
|
+
case "EdDSA":
|
|
1776
|
+
candidate = jwk2.crv === "Ed25519" || jwk2.crv === "Ed448";
|
|
1777
|
+
break;
|
|
1778
|
+
}
|
|
1779
|
+
}
|
|
1780
|
+
return candidate;
|
|
1781
|
+
});
|
|
1782
|
+
const { 0: jwk, length } = candidates;
|
|
1783
|
+
if (length === 0) {
|
|
1784
|
+
throw new JWKSNoMatchingKey();
|
|
1785
|
+
}
|
|
1786
|
+
if (length !== 1) {
|
|
1787
|
+
const error = new JWKSMultipleMatchingKeys();
|
|
1788
|
+
const { _cached } = this;
|
|
1789
|
+
error[Symbol.asyncIterator] = async function* () {
|
|
1790
|
+
for (const jwk2 of candidates) {
|
|
1791
|
+
try {
|
|
1792
|
+
yield await importWithAlgCache(_cached, jwk2, alg);
|
|
1793
|
+
} catch {
|
|
1794
|
+
}
|
|
1795
|
+
}
|
|
1796
|
+
};
|
|
1797
|
+
throw error;
|
|
1798
|
+
}
|
|
1799
|
+
return importWithAlgCache(this._cached, jwk, alg);
|
|
1800
|
+
}
|
|
1801
|
+
};
|
|
1802
|
+
async function importWithAlgCache(cache, jwk, alg) {
|
|
1803
|
+
const cached = cache.get(jwk) || cache.set(jwk, {}).get(jwk);
|
|
1804
|
+
if (cached[alg] === void 0) {
|
|
1805
|
+
const key = await importJWK({ ...jwk, ext: true }, alg);
|
|
1806
|
+
if (key instanceof Uint8Array || key.type !== "public") {
|
|
1807
|
+
throw new JWKSInvalid("JSON Web Key Set members must be public keys");
|
|
1808
|
+
}
|
|
1809
|
+
cached[alg] = key;
|
|
1810
|
+
}
|
|
1811
|
+
return cached[alg];
|
|
1812
|
+
}
|
|
1813
|
+
function createLocalJWKSet(jwks) {
|
|
1814
|
+
const set = new LocalJWKSet(jwks);
|
|
1815
|
+
const localJWKSet = async (protectedHeader, token) => set.getKey(protectedHeader, token);
|
|
1816
|
+
Object.defineProperties(localJWKSet, {
|
|
1817
|
+
jwks: {
|
|
1818
|
+
value: () => clone(set._jwks),
|
|
1819
|
+
enumerable: true,
|
|
1820
|
+
configurable: false,
|
|
1821
|
+
writable: false
|
|
1822
|
+
}
|
|
1823
|
+
});
|
|
1824
|
+
return localJWKSet;
|
|
1825
|
+
}
|
|
1826
|
+
|
|
1827
|
+
// node_modules/jose/dist/node/esm/runtime/fetch_jwks.js
|
|
1828
|
+
var http = __toESM(require("node:http"), 1);
|
|
1829
|
+
var https = __toESM(require("node:https"), 1);
|
|
1830
|
+
var import_node_events = require("node:events");
|
|
1831
|
+
var fetchJwks = async (url, timeout, options) => {
|
|
1832
|
+
let get3;
|
|
1833
|
+
switch (url.protocol) {
|
|
1834
|
+
case "https:":
|
|
1835
|
+
get3 = https.get;
|
|
1836
|
+
break;
|
|
1837
|
+
case "http:":
|
|
1838
|
+
get3 = http.get;
|
|
1839
|
+
break;
|
|
1840
|
+
default:
|
|
1841
|
+
throw new TypeError("Unsupported URL protocol.");
|
|
1842
|
+
}
|
|
1843
|
+
const { agent, headers } = options;
|
|
1844
|
+
const req = get3(url.href, {
|
|
1845
|
+
agent,
|
|
1846
|
+
timeout,
|
|
1847
|
+
headers
|
|
1848
|
+
});
|
|
1849
|
+
const [response] = await Promise.race([(0, import_node_events.once)(req, "response"), (0, import_node_events.once)(req, "timeout")]);
|
|
1850
|
+
if (!response) {
|
|
1851
|
+
req.destroy();
|
|
1852
|
+
throw new JWKSTimeout();
|
|
1853
|
+
}
|
|
1854
|
+
if (response.statusCode !== 200) {
|
|
1855
|
+
throw new JOSEError("Expected 200 OK from the JSON Web Key Set HTTP response");
|
|
1856
|
+
}
|
|
1857
|
+
const parts = [];
|
|
1858
|
+
for await (const part of response) {
|
|
1859
|
+
parts.push(part);
|
|
1860
|
+
}
|
|
1861
|
+
try {
|
|
1862
|
+
return JSON.parse(decoder.decode(concat(...parts)));
|
|
1863
|
+
} catch {
|
|
1864
|
+
throw new JOSEError("Failed to parse the JSON Web Key Set HTTP response as JSON");
|
|
1865
|
+
}
|
|
1866
|
+
};
|
|
1867
|
+
var fetch_jwks_default = fetchJwks;
|
|
1868
|
+
|
|
1869
|
+
// node_modules/jose/dist/node/esm/jwks/remote.js
|
|
1870
|
+
function isCloudflareWorkers() {
|
|
1871
|
+
return typeof WebSocketPair !== "undefined" || typeof navigator !== "undefined" && navigator.userAgent === "Cloudflare-Workers" || typeof EdgeRuntime !== "undefined" && EdgeRuntime === "vercel";
|
|
1872
|
+
}
|
|
1873
|
+
var USER_AGENT;
|
|
1874
|
+
if (typeof navigator === "undefined" || !navigator.userAgent?.startsWith?.("Mozilla/5.0 ")) {
|
|
1875
|
+
const NAME = "jose";
|
|
1876
|
+
const VERSION = "v5.10.0";
|
|
1877
|
+
USER_AGENT = `${NAME}/${VERSION}`;
|
|
1878
|
+
}
|
|
1879
|
+
var jwksCache = /* @__PURE__ */ Symbol();
|
|
1880
|
+
function isFreshJwksCache(input, cacheMaxAge) {
|
|
1881
|
+
if (typeof input !== "object" || input === null) {
|
|
1882
|
+
return false;
|
|
1883
|
+
}
|
|
1884
|
+
if (!("uat" in input) || typeof input.uat !== "number" || Date.now() - input.uat >= cacheMaxAge) {
|
|
1885
|
+
return false;
|
|
1886
|
+
}
|
|
1887
|
+
if (!("jwks" in input) || !isObject(input.jwks) || !Array.isArray(input.jwks.keys) || !Array.prototype.every.call(input.jwks.keys, isObject)) {
|
|
1888
|
+
return false;
|
|
1889
|
+
}
|
|
1890
|
+
return true;
|
|
1891
|
+
}
|
|
1892
|
+
var RemoteJWKSet = class {
|
|
1893
|
+
_url;
|
|
1894
|
+
_timeoutDuration;
|
|
1895
|
+
_cooldownDuration;
|
|
1896
|
+
_cacheMaxAge;
|
|
1897
|
+
_jwksTimestamp;
|
|
1898
|
+
_pendingFetch;
|
|
1899
|
+
_options;
|
|
1900
|
+
_local;
|
|
1901
|
+
_cache;
|
|
1902
|
+
constructor(url, options) {
|
|
1903
|
+
if (!(url instanceof URL)) {
|
|
1904
|
+
throw new TypeError("url must be an instance of URL");
|
|
1905
|
+
}
|
|
1906
|
+
this._url = new URL(url.href);
|
|
1907
|
+
this._options = { agent: options?.agent, headers: options?.headers };
|
|
1908
|
+
this._timeoutDuration = typeof options?.timeoutDuration === "number" ? options?.timeoutDuration : 5e3;
|
|
1909
|
+
this._cooldownDuration = typeof options?.cooldownDuration === "number" ? options?.cooldownDuration : 3e4;
|
|
1910
|
+
this._cacheMaxAge = typeof options?.cacheMaxAge === "number" ? options?.cacheMaxAge : 6e5;
|
|
1911
|
+
if (options?.[jwksCache] !== void 0) {
|
|
1912
|
+
this._cache = options?.[jwksCache];
|
|
1913
|
+
if (isFreshJwksCache(options?.[jwksCache], this._cacheMaxAge)) {
|
|
1914
|
+
this._jwksTimestamp = this._cache.uat;
|
|
1915
|
+
this._local = createLocalJWKSet(this._cache.jwks);
|
|
1916
|
+
}
|
|
1917
|
+
}
|
|
1918
|
+
}
|
|
1919
|
+
coolingDown() {
|
|
1920
|
+
return typeof this._jwksTimestamp === "number" ? Date.now() < this._jwksTimestamp + this._cooldownDuration : false;
|
|
1921
|
+
}
|
|
1922
|
+
fresh() {
|
|
1923
|
+
return typeof this._jwksTimestamp === "number" ? Date.now() < this._jwksTimestamp + this._cacheMaxAge : false;
|
|
1924
|
+
}
|
|
1925
|
+
async getKey(protectedHeader, token) {
|
|
1926
|
+
if (!this._local || !this.fresh()) {
|
|
1927
|
+
await this.reload();
|
|
1928
|
+
}
|
|
1929
|
+
try {
|
|
1930
|
+
return await this._local(protectedHeader, token);
|
|
1931
|
+
} catch (err) {
|
|
1932
|
+
if (err instanceof JWKSNoMatchingKey) {
|
|
1933
|
+
if (this.coolingDown() === false) {
|
|
1934
|
+
await this.reload();
|
|
1935
|
+
return this._local(protectedHeader, token);
|
|
1936
|
+
}
|
|
1937
|
+
}
|
|
1938
|
+
throw err;
|
|
1939
|
+
}
|
|
1940
|
+
}
|
|
1941
|
+
async reload() {
|
|
1942
|
+
if (this._pendingFetch && isCloudflareWorkers()) {
|
|
1943
|
+
this._pendingFetch = void 0;
|
|
1944
|
+
}
|
|
1945
|
+
const headers = new Headers(this._options.headers);
|
|
1946
|
+
if (USER_AGENT && !headers.has("User-Agent")) {
|
|
1947
|
+
headers.set("User-Agent", USER_AGENT);
|
|
1948
|
+
this._options.headers = Object.fromEntries(headers.entries());
|
|
1949
|
+
}
|
|
1950
|
+
this._pendingFetch ||= fetch_jwks_default(this._url, this._timeoutDuration, this._options).then((json) => {
|
|
1951
|
+
this._local = createLocalJWKSet(json);
|
|
1952
|
+
if (this._cache) {
|
|
1953
|
+
this._cache.uat = Date.now();
|
|
1954
|
+
this._cache.jwks = json;
|
|
1955
|
+
}
|
|
1956
|
+
this._jwksTimestamp = Date.now();
|
|
1957
|
+
this._pendingFetch = void 0;
|
|
1958
|
+
}).catch((err) => {
|
|
1959
|
+
this._pendingFetch = void 0;
|
|
1960
|
+
throw err;
|
|
1961
|
+
});
|
|
1962
|
+
await this._pendingFetch;
|
|
1963
|
+
}
|
|
1964
|
+
};
|
|
1965
|
+
function createRemoteJWKSet(url, options) {
|
|
1966
|
+
const set = new RemoteJWKSet(url, options);
|
|
1967
|
+
const remoteJWKSet = async (protectedHeader, token) => set.getKey(protectedHeader, token);
|
|
1968
|
+
Object.defineProperties(remoteJWKSet, {
|
|
1969
|
+
coolingDown: {
|
|
1970
|
+
get: () => set.coolingDown(),
|
|
1971
|
+
enumerable: true,
|
|
1972
|
+
configurable: false
|
|
1973
|
+
},
|
|
1974
|
+
fresh: {
|
|
1975
|
+
get: () => set.fresh(),
|
|
1976
|
+
enumerable: true,
|
|
1977
|
+
configurable: false
|
|
1978
|
+
},
|
|
1979
|
+
reload: {
|
|
1980
|
+
value: () => set.reload(),
|
|
1981
|
+
enumerable: true,
|
|
1982
|
+
configurable: false,
|
|
1983
|
+
writable: false
|
|
1984
|
+
},
|
|
1985
|
+
reloading: {
|
|
1986
|
+
get: () => !!set._pendingFetch,
|
|
1987
|
+
enumerable: true,
|
|
1988
|
+
configurable: false
|
|
1989
|
+
},
|
|
1990
|
+
jwks: {
|
|
1991
|
+
value: () => set._local?.jwks(),
|
|
1992
|
+
enumerable: true,
|
|
1993
|
+
configurable: false,
|
|
1994
|
+
writable: false
|
|
1995
|
+
}
|
|
1996
|
+
});
|
|
1997
|
+
return remoteJWKSet;
|
|
1998
|
+
}
|
|
1999
|
+
|
|
2000
|
+
// src/bridge.ts
|
|
2001
|
+
function readBridgeConfig(config) {
|
|
2002
|
+
const enabled = config.getOptionalBoolean("litellm.bridge.enabled") ?? false;
|
|
2003
|
+
const issuer = config.getOptionalString("litellm.bridge.issuer");
|
|
2004
|
+
const clientId = config.getOptionalString("litellm.bridge.clientId") ?? "abby-cli";
|
|
2005
|
+
return { enabled, issuer, clientId };
|
|
2006
|
+
}
|
|
2007
|
+
var BridgeConfigError = class extends Error {
|
|
2008
|
+
};
|
|
2009
|
+
var BridgeAuthError = class extends Error {
|
|
2010
|
+
constructor() {
|
|
2011
|
+
super(...arguments);
|
|
2012
|
+
this.status = 401;
|
|
2013
|
+
}
|
|
2014
|
+
};
|
|
2015
|
+
var KeycloakJWTVerifier = class {
|
|
2016
|
+
constructor(opts) {
|
|
2017
|
+
this.issuer = opts.issuer.replace(/\/$/, "");
|
|
2018
|
+
this.clientId = opts.clientId;
|
|
2019
|
+
this.jwks = createRemoteJWKSet(
|
|
2020
|
+
new URL(`${this.issuer}/protocol/openid-connect/certs`)
|
|
2021
|
+
);
|
|
2022
|
+
}
|
|
2023
|
+
async verify(token) {
|
|
2024
|
+
let payload;
|
|
2025
|
+
try {
|
|
2026
|
+
const result = await jwtVerify(token, this.jwks, {
|
|
2027
|
+
issuer: this.issuer
|
|
2028
|
+
});
|
|
2029
|
+
payload = result.payload;
|
|
2030
|
+
} catch (err) {
|
|
2031
|
+
throw new BridgeAuthError(
|
|
2032
|
+
`invalid keycloak token: ${err.message ?? err}`
|
|
2033
|
+
);
|
|
2034
|
+
}
|
|
2035
|
+
const azp = payload.azp;
|
|
2036
|
+
const aud = payload.aud;
|
|
2037
|
+
const audMatches = Array.isArray(aud) ? aud.includes(this.clientId) : aud === this.clientId;
|
|
2038
|
+
if (azp !== this.clientId && !audMatches) {
|
|
2039
|
+
throw new BridgeAuthError(
|
|
2040
|
+
`token not issued for client "${this.clientId}" (azp=${azp ?? "none"})`
|
|
2041
|
+
);
|
|
2042
|
+
}
|
|
2043
|
+
return {
|
|
2044
|
+
sub: payload.sub ?? "",
|
|
2045
|
+
email: payload.email,
|
|
2046
|
+
preferred_username: payload.preferred_username,
|
|
2047
|
+
name: payload.name,
|
|
2048
|
+
azp,
|
|
2049
|
+
aud
|
|
2050
|
+
};
|
|
2051
|
+
}
|
|
2052
|
+
};
|
|
2053
|
+
function newDefaultVerifier(cfg) {
|
|
2054
|
+
if (!cfg.issuer) {
|
|
2055
|
+
throw new BridgeConfigError(
|
|
2056
|
+
"litellm.bridge.issuer is required when litellm.bridge.enabled is true (e.g. https://auth.ces.abssrv.it/realms/solution-innovation)"
|
|
2057
|
+
);
|
|
2058
|
+
}
|
|
2059
|
+
return new KeycloakJWTVerifier({ issuer: cfg.issuer, clientId: cfg.clientId });
|
|
2060
|
+
}
|
|
2061
|
+
function resolveBridgeUserId(claims) {
|
|
2062
|
+
return claims.email ?? claims.preferred_username ?? claims.sub;
|
|
2063
|
+
}
|
|
2064
|
+
async function getOrProvisionUserFromClaims(client, claims, provisioningEnabled, provisioningDefaults, logger) {
|
|
2065
|
+
const userId = resolveBridgeUserId(claims);
|
|
2066
|
+
const existing = await client.getUserInfo(userId);
|
|
2067
|
+
if (existing) return existing;
|
|
2068
|
+
if (!provisioningEnabled) {
|
|
2069
|
+
throw new ProvisioningError(
|
|
2070
|
+
"User not found in LiteLLM",
|
|
2071
|
+
"No LiteLLM user for this identity. Log in to Backstage once to be provisioned, or enable litellm.provisioning.enabled.",
|
|
2072
|
+
false
|
|
2073
|
+
);
|
|
2074
|
+
}
|
|
2075
|
+
const profile = {
|
|
2076
|
+
email: claims.email ?? claims.preferred_username,
|
|
2077
|
+
displayName: claims.name ?? claims.preferred_username
|
|
2078
|
+
};
|
|
2079
|
+
const created = await provisionUser(
|
|
2080
|
+
client,
|
|
2081
|
+
userId,
|
|
2082
|
+
provisioningDefaults,
|
|
2083
|
+
profile,
|
|
2084
|
+
void 0,
|
|
2085
|
+
logger
|
|
2086
|
+
);
|
|
2087
|
+
if (!created) {
|
|
2088
|
+
throw new ProvisioningError(
|
|
2089
|
+
"User not found in LiteLLM",
|
|
2090
|
+
"Provisioning attempted but returned no user \u2014 check LiteLLM logs",
|
|
2091
|
+
true,
|
|
2092
|
+
500
|
|
2093
|
+
);
|
|
2094
|
+
}
|
|
2095
|
+
return created;
|
|
2096
|
+
}
|
|
2097
|
+
async function bridgeListKeys(client, claims, provisioningEnabled, provisioningDefaults, logger) {
|
|
2098
|
+
await getOrProvisionUserFromClaims(
|
|
2099
|
+
client,
|
|
2100
|
+
claims,
|
|
2101
|
+
provisioningEnabled,
|
|
2102
|
+
provisioningDefaults,
|
|
2103
|
+
logger
|
|
2104
|
+
);
|
|
2105
|
+
return client.listKeys(resolveBridgeUserId(claims));
|
|
2106
|
+
}
|
|
2107
|
+
async function bridgeGenerateKey(client, claims, provisioningEnabled, provisioningDefaults, logger, request) {
|
|
2108
|
+
await getOrProvisionUserFromClaims(
|
|
2109
|
+
client,
|
|
2110
|
+
claims,
|
|
2111
|
+
provisioningEnabled,
|
|
2112
|
+
provisioningDefaults,
|
|
2113
|
+
logger
|
|
2114
|
+
);
|
|
2115
|
+
const userId = resolveBridgeUserId(claims);
|
|
2116
|
+
const enriched = {
|
|
2117
|
+
...request,
|
|
2118
|
+
user_id: userId,
|
|
2119
|
+
metadata: {
|
|
2120
|
+
...request.metadata ?? {},
|
|
2121
|
+
created_via: "abby-cli",
|
|
2122
|
+
created_by: userId,
|
|
2123
|
+
created_at_iso: (/* @__PURE__ */ new Date()).toISOString()
|
|
2124
|
+
}
|
|
2125
|
+
};
|
|
2126
|
+
return client.generateKey(enriched);
|
|
2127
|
+
}
|
|
2128
|
+
|
|
657
2129
|
// src/router.ts
|
|
658
2130
|
async function createRouter(options) {
|
|
659
2131
|
const { config, logger, auth, discovery } = options;
|
|
660
2132
|
const baseUrl = config.getString("litellm.baseUrl");
|
|
661
2133
|
const masterKey = config.getString("litellm.masterKey");
|
|
662
2134
|
const userIdDomain = config.getOptionalString("litellm.userIdDomain");
|
|
663
|
-
const client = new LiteLLMClient({ baseUrl, masterKey });
|
|
2135
|
+
const client = options.client ?? new LiteLLMClient({ baseUrl, masterKey });
|
|
664
2136
|
const { enabled: provisioningEnabled, defaults: provisioningDefaults } = readProvisioningDefaults(config);
|
|
665
2137
|
const roleConfigs = readRoleConfigs(config);
|
|
666
2138
|
const catalogClient = new import_catalog_client.CatalogClient({ discoveryApi: discovery });
|
|
@@ -914,6 +2386,84 @@ async function createRouter(options) {
|
|
|
914
2386
|
res.status(500).json({ error: error.message });
|
|
915
2387
|
}
|
|
916
2388
|
});
|
|
2389
|
+
const bridgeCfg = readBridgeConfig(config);
|
|
2390
|
+
let tokenVerifier = options.tokenVerifier;
|
|
2391
|
+
if (bridgeCfg.enabled && !tokenVerifier) {
|
|
2392
|
+
try {
|
|
2393
|
+
tokenVerifier = newDefaultVerifier(bridgeCfg);
|
|
2394
|
+
} catch (e) {
|
|
2395
|
+
logger.error(
|
|
2396
|
+
`LiteLLM bridge enabled but misconfigured: ${e.message}`
|
|
2397
|
+
);
|
|
2398
|
+
}
|
|
2399
|
+
}
|
|
2400
|
+
if (bridgeCfg.enabled && tokenVerifier) {
|
|
2401
|
+
const requireClaims = async (req) => {
|
|
2402
|
+
const header = req.headers.authorization ?? "";
|
|
2403
|
+
const token = header.startsWith("Bearer ") ? header.slice(7) : "";
|
|
2404
|
+
if (!token) throw new BridgeAuthError("missing Bearer token");
|
|
2405
|
+
return tokenVerifier.verify(token);
|
|
2406
|
+
};
|
|
2407
|
+
const handleBridgeError = (error, res) => {
|
|
2408
|
+
if (error instanceof BridgeAuthError) {
|
|
2409
|
+
res.status(401).json({ error: "unauthorized", hint: error.message });
|
|
2410
|
+
return;
|
|
2411
|
+
}
|
|
2412
|
+
if (error instanceof ProvisioningError) {
|
|
2413
|
+
res.status(error.status).json(error.body);
|
|
2414
|
+
return;
|
|
2415
|
+
}
|
|
2416
|
+
logger.error("Bridge request failed", error);
|
|
2417
|
+
res.status(500).json({ error: error.message });
|
|
2418
|
+
};
|
|
2419
|
+
router.get("/bridge/health", (_req, res) => {
|
|
2420
|
+
res.json({ status: "ok", bridge: true, clientId: bridgeCfg.clientId });
|
|
2421
|
+
});
|
|
2422
|
+
router.get("/bridge/keys", async (req, res) => {
|
|
2423
|
+
try {
|
|
2424
|
+
const claims = await requireClaims(req);
|
|
2425
|
+
const keys = await bridgeListKeys(
|
|
2426
|
+
client,
|
|
2427
|
+
claims,
|
|
2428
|
+
provisioningEnabled,
|
|
2429
|
+
provisioningDefaults,
|
|
2430
|
+
logger
|
|
2431
|
+
);
|
|
2432
|
+
res.json(keys);
|
|
2433
|
+
} catch (error) {
|
|
2434
|
+
handleBridgeError(error, res);
|
|
2435
|
+
}
|
|
2436
|
+
});
|
|
2437
|
+
router.post("/bridge/keys", async (req, res) => {
|
|
2438
|
+
try {
|
|
2439
|
+
const claims = await requireClaims(req);
|
|
2440
|
+
const result = await bridgeGenerateKey(
|
|
2441
|
+
client,
|
|
2442
|
+
claims,
|
|
2443
|
+
provisioningEnabled,
|
|
2444
|
+
provisioningDefaults,
|
|
2445
|
+
logger,
|
|
2446
|
+
req.body ?? {}
|
|
2447
|
+
);
|
|
2448
|
+
res.json(result);
|
|
2449
|
+
} catch (error) {
|
|
2450
|
+
handleBridgeError(error, res);
|
|
2451
|
+
}
|
|
2452
|
+
});
|
|
2453
|
+
router.get("/bridge/models", async (req, res) => {
|
|
2454
|
+
try {
|
|
2455
|
+
await requireClaims(req);
|
|
2456
|
+
const models = await client.listModels();
|
|
2457
|
+
res.json(models);
|
|
2458
|
+
} catch (error) {
|
|
2459
|
+
handleBridgeError(error, res);
|
|
2460
|
+
}
|
|
2461
|
+
});
|
|
2462
|
+
} else if (bridgeCfg.enabled) {
|
|
2463
|
+
logger.warn(
|
|
2464
|
+
"litellm.bridge.enabled is true but no verifier could be built \u2014 bridge endpoints not mounted"
|
|
2465
|
+
);
|
|
2466
|
+
}
|
|
917
2467
|
return router;
|
|
918
2468
|
}
|
|
919
2469
|
|
|
@@ -932,14 +2482,30 @@ var litellmPlugin = (0, import_backend_plugin_api.createBackendPlugin)({
|
|
|
932
2482
|
async init({ httpRouter, config, logger, auth, discovery }) {
|
|
933
2483
|
const router = await createRouter({ config, logger, auth, discovery });
|
|
934
2484
|
httpRouter.use(router);
|
|
2485
|
+
if (readBridgeConfig(config).enabled) {
|
|
2486
|
+
for (const path of [
|
|
2487
|
+
"/bridge/health",
|
|
2488
|
+
"/bridge/keys",
|
|
2489
|
+
"/bridge/models"
|
|
2490
|
+
]) {
|
|
2491
|
+
httpRouter.addAuthPolicy({ path, allow: "unauthenticated" });
|
|
2492
|
+
}
|
|
2493
|
+
}
|
|
935
2494
|
}
|
|
936
2495
|
});
|
|
937
2496
|
}
|
|
938
2497
|
});
|
|
939
2498
|
// Annotate the CommonJS export names for ESM import in node:
|
|
940
2499
|
0 && (module.exports = {
|
|
2500
|
+
KeycloakJWTVerifier,
|
|
941
2501
|
LiteLLMClient,
|
|
2502
|
+
bridgeGenerateKey,
|
|
2503
|
+
bridgeListKeys,
|
|
942
2504
|
createRouter,
|
|
943
|
-
|
|
2505
|
+
getOrProvisionUserFromClaims,
|
|
2506
|
+
litellmPlugin,
|
|
2507
|
+
newDefaultVerifier,
|
|
2508
|
+
readBridgeConfig,
|
|
2509
|
+
resolveBridgeUserId
|
|
944
2510
|
});
|
|
945
2511
|
//# sourceMappingURL=index.cjs.js.map
|