@twin.org/web 0.0.1-next.7 → 0.0.1-next.70
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +288 -90
- package/dist/esm/index.mjs +289 -92
- package/dist/types/index.d.ts +3 -1
- package/dist/types/models/IJwk.d.ts +2 -58
- package/dist/types/models/IJwtHeader.d.ts +2 -18
- package/dist/types/models/IJwtPayload.d.ts +2 -33
- package/dist/types/models/jwkCryptoKey.d.ts +4 -0
- package/dist/types/models/mimeTypes.d.ts +8 -0
- package/dist/types/utils/fetchHelper.d.ts +7 -0
- package/dist/types/utils/jwk.d.ts +41 -0
- package/dist/types/utils/jws.d.ts +22 -0
- package/dist/types/utils/jwt.d.ts +67 -29
- package/docs/changelog.md +361 -1
- package/docs/reference/classes/FetchError.md +16 -8
- package/docs/reference/classes/FetchHelper.md +104 -28
- package/docs/reference/classes/Jwk.md +129 -0
- package/docs/reference/classes/Jws.md +81 -0
- package/docs/reference/classes/Jwt.md +261 -105
- package/docs/reference/classes/MimeTypeHelper.md +9 -5
- package/docs/reference/index.md +3 -2
- package/docs/reference/interfaces/IHttpHeaders.md +1 -1
- package/docs/reference/interfaces/IJwk.md +2 -106
- package/docs/reference/interfaces/IJwtHeader.md +5 -23
- package/docs/reference/interfaces/IJwtPayload.md +5 -55
- package/docs/reference/type-aliases/HeaderTypes.md +1 -1
- package/docs/reference/type-aliases/HttpMethod.md +1 -1
- package/docs/reference/type-aliases/HttpStatusCode.md +1 -1
- package/docs/reference/type-aliases/JwkCryptoKey.md +5 -0
- package/docs/reference/type-aliases/MimeTypes.md +1 -1
- package/docs/reference/variables/MimeTypes.md +12 -0
- package/locales/en.json +11 -1
- package/package.json +7 -6
- package/dist/types/models/jwtAlgorithms.d.ts +0 -17
- package/docs/reference/type-aliases/JwtAlgorithms.md +0 -5
- package/docs/reference/variables/JwtAlgorithms.md +0 -19
package/dist/cjs/index.cjs
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var core = require('@twin.org/core');
|
|
4
4
|
var crypto = require('@twin.org/crypto');
|
|
5
|
+
var jose = require('jose');
|
|
5
6
|
|
|
6
7
|
// Copyright 2024 IOTA Stiftung.
|
|
7
8
|
// SPDX-License-Identifier: Apache-2.0.
|
|
@@ -345,23 +346,6 @@ const HttpStatusCode = {
|
|
|
345
346
|
networkAuthenticationRequired: 511
|
|
346
347
|
};
|
|
347
348
|
|
|
348
|
-
// Copyright 2024 IOTA Stiftung.
|
|
349
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
350
|
-
/**
|
|
351
|
-
* The cryptographic algorithms supported for JSON Web Tokens and JSON Web Keys.
|
|
352
|
-
*/
|
|
353
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
354
|
-
const JwtAlgorithms = {
|
|
355
|
-
/**
|
|
356
|
-
* HMAC using SHA-256.
|
|
357
|
-
*/
|
|
358
|
-
HS256: "HS256",
|
|
359
|
-
/**
|
|
360
|
-
* EdDSA using Ed25519.
|
|
361
|
-
*/
|
|
362
|
-
EdDSA: "EdDSA"
|
|
363
|
-
};
|
|
364
|
-
|
|
365
349
|
// Copyright 2024 IOTA Stiftung.
|
|
366
350
|
// SPDX-License-Identifier: Apache-2.0.
|
|
367
351
|
/**
|
|
@@ -389,6 +373,10 @@ const MimeTypes = {
|
|
|
389
373
|
* JSON-LD - application/ld+json
|
|
390
374
|
*/
|
|
391
375
|
JsonLd: "application/ld+json",
|
|
376
|
+
/**
|
|
377
|
+
* JWT - application/jwt
|
|
378
|
+
*/
|
|
379
|
+
Jwt: "application/jwt",
|
|
392
380
|
/**
|
|
393
381
|
* XML - application/xml
|
|
394
382
|
*/
|
|
@@ -401,6 +389,10 @@ const MimeTypes = {
|
|
|
401
389
|
* Application GZIP - application/gzip
|
|
402
390
|
*/
|
|
403
391
|
Gzip: "application/gzip",
|
|
392
|
+
/**
|
|
393
|
+
* Application deflate - application/zlib
|
|
394
|
+
*/
|
|
395
|
+
Zlib: "application/zlib",
|
|
404
396
|
/**
|
|
405
397
|
* Application BZIP2 - application/x-bzip2
|
|
406
398
|
*/
|
|
@@ -555,7 +547,7 @@ class FetchHelper {
|
|
|
555
547
|
if (isErr && core.Is.stringValue(err.message) && err.message.includes("Failed to fetch")) {
|
|
556
548
|
lastError = new FetchError(source, `${FetchHelper._CLASS_NAME_CAMEL_CASE}.connectivity`, HttpStatusCode.serviceUnavailable, {
|
|
557
549
|
url
|
|
558
|
-
});
|
|
550
|
+
}, err);
|
|
559
551
|
}
|
|
560
552
|
else {
|
|
561
553
|
const isAbort = isErr && err.name === "AbortError";
|
|
@@ -570,7 +562,7 @@ class FetchHelper {
|
|
|
570
562
|
if (isErr && "statusText" in err) {
|
|
571
563
|
props.statusText = err.statusText;
|
|
572
564
|
}
|
|
573
|
-
lastError = new FetchError(source, `${FetchHelper._CLASS_NAME_CAMEL_CASE}.${isAbort ? "timeout" : "general"}`, httpStatus, props);
|
|
565
|
+
lastError = new FetchError(source, `${FetchHelper._CLASS_NAME_CAMEL_CASE}.${isAbort ? "timeout" : "general"}`, httpStatus, props, err);
|
|
574
566
|
}
|
|
575
567
|
}
|
|
576
568
|
finally {
|
|
@@ -698,6 +690,15 @@ class FetchHelper {
|
|
|
698
690
|
static async getCacheEntry(url) {
|
|
699
691
|
return core.AsyncCache.get(`${FetchHelper._CACHE_PREFIX}${url}`);
|
|
700
692
|
}
|
|
693
|
+
/**
|
|
694
|
+
* Set a cache entry.
|
|
695
|
+
* @param url The url for the request.
|
|
696
|
+
* @param value The value to cache.
|
|
697
|
+
* @returns The cache entry if it exists.
|
|
698
|
+
*/
|
|
699
|
+
static async setCacheEntry(url, value) {
|
|
700
|
+
core.AsyncCache.set(`${FetchHelper._CACHE_PREFIX}${url}`, value);
|
|
701
|
+
}
|
|
701
702
|
/**
|
|
702
703
|
* Remove a cache entry.
|
|
703
704
|
* @param url The url for the request.
|
|
@@ -710,7 +711,158 @@ class FetchHelper {
|
|
|
710
711
|
// Copyright 2024 IOTA Stiftung.
|
|
711
712
|
// SPDX-License-Identifier: Apache-2.0.
|
|
712
713
|
/**
|
|
713
|
-
* Class to
|
|
714
|
+
* Class to handle JSON Web Keys.
|
|
715
|
+
*/
|
|
716
|
+
class Jwk {
|
|
717
|
+
/**
|
|
718
|
+
* Runtime name for the class.
|
|
719
|
+
* @internal
|
|
720
|
+
*/
|
|
721
|
+
static _CLASS_NAME = "Jwk";
|
|
722
|
+
/**
|
|
723
|
+
* Convert the JWK to a crypto key.
|
|
724
|
+
* @param jwk The JWK to convert.
|
|
725
|
+
* @param alg The alg to be used, defaults to jwk.alg.
|
|
726
|
+
* @returns The crypto key.
|
|
727
|
+
*/
|
|
728
|
+
static async toCryptoKey(jwk, alg) {
|
|
729
|
+
core.Guards.object(Jwk._CLASS_NAME, "jwk", jwk);
|
|
730
|
+
try {
|
|
731
|
+
return jose.importJWK(jwk, alg);
|
|
732
|
+
}
|
|
733
|
+
catch (err) {
|
|
734
|
+
throw new core.GeneralError(Jwk._CLASS_NAME, "jwkImportFailed", undefined, err);
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
/**
|
|
738
|
+
* Convert the Ed25519 private key to a crypto key.
|
|
739
|
+
* @param privateKey The private key to use.
|
|
740
|
+
* @returns The crypto key.
|
|
741
|
+
*/
|
|
742
|
+
static async fromEd25519Private(privateKey) {
|
|
743
|
+
core.Guards.uint8Array(Jwk._CLASS_NAME, "privateKey", privateKey);
|
|
744
|
+
const publicKey = crypto.Ed25519.publicKeyFromPrivateKey(privateKey);
|
|
745
|
+
const jwk = {
|
|
746
|
+
kty: "OKP",
|
|
747
|
+
use: "enc",
|
|
748
|
+
alg: "EdDSA",
|
|
749
|
+
crv: "Ed25519",
|
|
750
|
+
x: core.Converter.bytesToBase64Url(publicKey),
|
|
751
|
+
d: core.Converter.bytesToBase64Url(privateKey)
|
|
752
|
+
};
|
|
753
|
+
return jwk;
|
|
754
|
+
}
|
|
755
|
+
/**
|
|
756
|
+
* Convert the Ed25519 public key to a crypto key.
|
|
757
|
+
* @param publicKey The private key to use.
|
|
758
|
+
* @returns The crypto key.
|
|
759
|
+
*/
|
|
760
|
+
static async fromEd25519Public(publicKey) {
|
|
761
|
+
core.Guards.uint8Array(Jwk._CLASS_NAME, "publicKey", publicKey);
|
|
762
|
+
const jwk = {
|
|
763
|
+
kty: "OKP",
|
|
764
|
+
use: "sig",
|
|
765
|
+
alg: "EdDSA",
|
|
766
|
+
crv: "Ed25519",
|
|
767
|
+
x: core.Converter.bytesToBase64Url(publicKey)
|
|
768
|
+
};
|
|
769
|
+
return jwk;
|
|
770
|
+
}
|
|
771
|
+
/**
|
|
772
|
+
* Convert the JWK to raw keys.
|
|
773
|
+
* @param jwk The JWK to convert to raw.
|
|
774
|
+
* @returns The crypto key.
|
|
775
|
+
*/
|
|
776
|
+
static async toRaw(jwk) {
|
|
777
|
+
core.Guards.object(Jwk._CLASS_NAME, "jwk", jwk);
|
|
778
|
+
let publicKey;
|
|
779
|
+
let privateKey;
|
|
780
|
+
if (core.Is.stringBase64Url(jwk.x)) {
|
|
781
|
+
publicKey = core.Converter.base64UrlToBytes(jwk.x);
|
|
782
|
+
}
|
|
783
|
+
if (core.Is.stringBase64Url(jwk.d)) {
|
|
784
|
+
privateKey = core.Converter.base64UrlToBytes(jwk.d);
|
|
785
|
+
}
|
|
786
|
+
return {
|
|
787
|
+
publicKey,
|
|
788
|
+
privateKey
|
|
789
|
+
};
|
|
790
|
+
}
|
|
791
|
+
/**
|
|
792
|
+
* Generate a KID for the JWK.
|
|
793
|
+
* @param jwk The JWK to generate a KID for.
|
|
794
|
+
* @returns The KID.
|
|
795
|
+
*/
|
|
796
|
+
static async generateKid(jwk) {
|
|
797
|
+
core.Guards.object(Jwk._CLASS_NAME, "jwk", jwk);
|
|
798
|
+
const kidProps = core.ObjectHelper.pick(jwk, ["crv", "kty", "x"]);
|
|
799
|
+
const canonicalJson = core.JsonHelper.canonicalize(kidProps);
|
|
800
|
+
const hash = crypto.Sha256.sum256(core.Converter.utf8ToBytes(canonicalJson));
|
|
801
|
+
return core.Converter.bytesToBase64Url(hash);
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
// Copyright 2024 IOTA Stiftung.
|
|
806
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
807
|
+
/**
|
|
808
|
+
* Class to handle JSON Web Signatures.
|
|
809
|
+
*/
|
|
810
|
+
class Jws {
|
|
811
|
+
/**
|
|
812
|
+
* Runtime name for the class.
|
|
813
|
+
* @internal
|
|
814
|
+
*/
|
|
815
|
+
static _CLASS_NAME = "Jws";
|
|
816
|
+
/**
|
|
817
|
+
* Create a signature.
|
|
818
|
+
* @param privateKey The private key to use.
|
|
819
|
+
* @param hash The hash to sign.
|
|
820
|
+
* @param algOverride An optional algorithm override.
|
|
821
|
+
* @returns The signature.
|
|
822
|
+
*/
|
|
823
|
+
static async create(privateKey, hash, algOverride) {
|
|
824
|
+
core.Guards.defined(Jws._CLASS_NAME, "privateKey", privateKey);
|
|
825
|
+
core.Guards.uint8Array(Jws._CLASS_NAME, "hash", hash);
|
|
826
|
+
try {
|
|
827
|
+
const jws = await new jose.CompactSign(hash)
|
|
828
|
+
.setProtectedHeader({
|
|
829
|
+
alg: algOverride ?? (core.Is.uint8Array(privateKey) ? "EdDSA" : privateKey.algorithm.name),
|
|
830
|
+
b64: false,
|
|
831
|
+
crit: ["b64"]
|
|
832
|
+
})
|
|
833
|
+
.sign(privateKey);
|
|
834
|
+
return jws;
|
|
835
|
+
}
|
|
836
|
+
catch (err) {
|
|
837
|
+
throw new core.GeneralError(Jws._CLASS_NAME, "createFailed", undefined, err);
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
/**
|
|
841
|
+
* Verify a signature.
|
|
842
|
+
* @param jws The signature to verify.
|
|
843
|
+
* @param publicKey The public key to verify the signature with.
|
|
844
|
+
* @param hash The hash to verify.
|
|
845
|
+
* @returns True if the signature was verified.
|
|
846
|
+
*/
|
|
847
|
+
static async verify(jws, publicKey, hash) {
|
|
848
|
+
core.Guards.stringValue(Jws._CLASS_NAME, "jws", jws);
|
|
849
|
+
core.Guards.defined(Jws._CLASS_NAME, "publicKey", publicKey);
|
|
850
|
+
core.Guards.uint8Array(Jws._CLASS_NAME, "hash", hash);
|
|
851
|
+
try {
|
|
852
|
+
const jwsParts = jws.split(".");
|
|
853
|
+
await jose.flattenedVerify({ protected: jwsParts[0], payload: hash, signature: jwsParts[2] }, publicKey);
|
|
854
|
+
return true;
|
|
855
|
+
}
|
|
856
|
+
catch (err) {
|
|
857
|
+
throw new core.GeneralError(Jws._CLASS_NAME, "verifyFailed", undefined, err);
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
// Copyright 2024 IOTA Stiftung.
|
|
863
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
864
|
+
/**
|
|
865
|
+
* Class to handle JSON Web Tokens.
|
|
714
866
|
*/
|
|
715
867
|
class Jwt {
|
|
716
868
|
/**
|
|
@@ -727,9 +879,8 @@ class Jwt {
|
|
|
727
879
|
*/
|
|
728
880
|
static async encode(header, payload, key) {
|
|
729
881
|
core.Guards.object(Jwt._CLASS_NAME, "header", header);
|
|
730
|
-
core.Guards.arrayOneOf(Jwt._CLASS_NAME, "header.alg", header.alg, Object.values(JwtAlgorithms));
|
|
731
882
|
core.Guards.object(Jwt._CLASS_NAME, "payload", payload);
|
|
732
|
-
core.Guards.
|
|
883
|
+
core.Guards.defined(Jwt._CLASS_NAME, "key", key);
|
|
733
884
|
return Jwt.internalEncode(header, payload, key);
|
|
734
885
|
}
|
|
735
886
|
/**
|
|
@@ -741,7 +892,7 @@ class Jwt {
|
|
|
741
892
|
*/
|
|
742
893
|
static async encodeWithSigner(header, payload, signer) {
|
|
743
894
|
core.Guards.object(Jwt._CLASS_NAME, "header", header);
|
|
744
|
-
core.Guards.
|
|
895
|
+
core.Guards.stringValue(Jwt._CLASS_NAME, "header.alg", header.alg);
|
|
745
896
|
core.Guards.object(Jwt._CLASS_NAME, "payload", payload);
|
|
746
897
|
core.Guards.function(Jwt._CLASS_NAME, "signer", signer);
|
|
747
898
|
return Jwt.internalEncode(header, payload, undefined, signer);
|
|
@@ -788,13 +939,8 @@ class Jwt {
|
|
|
788
939
|
*/
|
|
789
940
|
static async verify(token, key) {
|
|
790
941
|
core.Guards.stringValue(Jwt._CLASS_NAME, "token", token);
|
|
791
|
-
core.Guards.
|
|
792
|
-
|
|
793
|
-
const verified = await Jwt.verifySignature(decoded.header, decoded.payload, decoded.signature, key);
|
|
794
|
-
return {
|
|
795
|
-
verified,
|
|
796
|
-
...decoded
|
|
797
|
-
};
|
|
942
|
+
core.Guards.defined(Jwt._CLASS_NAME, "key", key);
|
|
943
|
+
return Jwt.verifySignature(token, key);
|
|
798
944
|
}
|
|
799
945
|
/**
|
|
800
946
|
* Verify a token.
|
|
@@ -805,79 +951,131 @@ class Jwt {
|
|
|
805
951
|
static async verifyWithVerifier(token, verifier) {
|
|
806
952
|
core.Guards.stringValue(Jwt._CLASS_NAME, "token", token);
|
|
807
953
|
core.Guards.function(Jwt._CLASS_NAME, "verifier", verifier);
|
|
808
|
-
|
|
809
|
-
const decoded = await Jwt.decode(token);
|
|
810
|
-
const verified = await Jwt.verifySignature(decoded.header, decoded.payload, decoded.signature, undefined, verifier);
|
|
811
|
-
return {
|
|
812
|
-
verified,
|
|
813
|
-
...decoded
|
|
814
|
-
};
|
|
954
|
+
return Jwt.verifySignature(token, undefined, verifier);
|
|
815
955
|
}
|
|
816
956
|
/**
|
|
817
957
|
* Verify a token by parts.
|
|
818
|
-
* @param
|
|
819
|
-
* @param payload The payload to verify.
|
|
820
|
-
* @param signature The signature to verify.
|
|
958
|
+
* @param token The token to verify.
|
|
821
959
|
* @param key The key for verifying the token, if not provided no verification occurs.
|
|
822
960
|
* @param verifier Custom verification method.
|
|
823
961
|
* @returns True if the parts are verified.
|
|
824
962
|
*/
|
|
825
|
-
static async verifySignature(
|
|
963
|
+
static async verifySignature(token, key, verifier) {
|
|
964
|
+
core.Guards.stringValue(Jwt._CLASS_NAME, "token", token);
|
|
826
965
|
const hasKey = core.Is.notEmpty(key);
|
|
827
966
|
const hasVerifier = core.Is.notEmpty(verifier);
|
|
828
967
|
if (!hasKey && !hasVerifier) {
|
|
829
968
|
throw new core.GeneralError(Jwt._CLASS_NAME, "noKeyOrVerifier");
|
|
830
969
|
}
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
core.Is.object(payload) &&
|
|
834
|
-
core.Is.uint8Array(signature) &&
|
|
835
|
-
core.Is.arrayOneOf(header.alg, Object.values(JwtAlgorithms))) {
|
|
836
|
-
const segments = [];
|
|
837
|
-
const headerBytes = core.Converter.utf8ToBytes(JSON.stringify(header));
|
|
838
|
-
segments.push(core.Converter.bytesToBase64Url(headerBytes));
|
|
839
|
-
const payloadBytes = core.Converter.utf8ToBytes(JSON.stringify(payload));
|
|
840
|
-
segments.push(core.Converter.bytesToBase64Url(payloadBytes));
|
|
841
|
-
const jwtHeaderAndPayload = core.Converter.utf8ToBytes(segments.join("."));
|
|
842
|
-
verifier ??= async (alg, k, p, s) => Jwt.defaultVerifier(alg, k, p, s);
|
|
843
|
-
verified = await verifier(header.alg, key, jwtHeaderAndPayload, signature);
|
|
844
|
-
}
|
|
845
|
-
return verified;
|
|
970
|
+
verifier ??= async (t, k) => Jwt.defaultVerifier(t, k);
|
|
971
|
+
return verifier(token, key);
|
|
846
972
|
}
|
|
847
973
|
/**
|
|
848
974
|
* The default signer for the JWT.
|
|
849
|
-
* @param
|
|
850
|
-
* @param key The key to sign with.
|
|
975
|
+
* @param header The header to sign.
|
|
851
976
|
* @param payload The payload to sign.
|
|
977
|
+
* @param key The optional key to sign with.
|
|
852
978
|
* @returns The signature.
|
|
853
979
|
*/
|
|
854
|
-
static async defaultSigner(
|
|
855
|
-
core.Guards.
|
|
856
|
-
core.Guards.
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
980
|
+
static async defaultSigner(header, payload, key) {
|
|
981
|
+
core.Guards.object(Jwt._CLASS_NAME, "header", header);
|
|
982
|
+
core.Guards.object(Jwt._CLASS_NAME, "payload", payload);
|
|
983
|
+
core.Guards.defined(Jwt._CLASS_NAME, "key", key);
|
|
984
|
+
const signer = new jose.SignJWT(payload);
|
|
985
|
+
signer.setProtectedHeader(header);
|
|
986
|
+
let finalKey = key;
|
|
987
|
+
if (header.alg === "EdDSA" && core.Is.uint8Array(key)) {
|
|
988
|
+
// Jose does not support Ed25519 keys in raw format, so we need to convert it to PKCS8.
|
|
989
|
+
finalKey = await crypto.Ed25519.privateKeyToPkcs8(key);
|
|
860
990
|
}
|
|
861
|
-
return
|
|
991
|
+
return signer.sign(finalKey);
|
|
862
992
|
}
|
|
863
993
|
/**
|
|
864
994
|
* The default verifier for the JWT.
|
|
865
|
-
* @param
|
|
995
|
+
* @param token The token to verify.
|
|
866
996
|
* @param key The key to verify with.
|
|
867
|
-
* @
|
|
868
|
-
|
|
869
|
-
|
|
997
|
+
* @returns The header and payload if verification successful.
|
|
998
|
+
*/
|
|
999
|
+
static async defaultVerifier(token, key) {
|
|
1000
|
+
core.Guards.stringValue(Jwt._CLASS_NAME, "token", token);
|
|
1001
|
+
core.Guards.defined(Jwt._CLASS_NAME, "key", key);
|
|
1002
|
+
try {
|
|
1003
|
+
const result = await jose.jwtVerify(token, key);
|
|
1004
|
+
return {
|
|
1005
|
+
header: result.protectedHeader,
|
|
1006
|
+
payload: result.payload
|
|
1007
|
+
};
|
|
1008
|
+
}
|
|
1009
|
+
catch (err) {
|
|
1010
|
+
throw new core.GeneralError(Jwt._CLASS_NAME, "verifyFailed", undefined, err);
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
/**
|
|
1014
|
+
* Create bytes for signing from header and payload.
|
|
1015
|
+
* @param header The header.
|
|
1016
|
+
* @param payload The payload.
|
|
1017
|
+
* @returns The bytes to sign.
|
|
870
1018
|
*/
|
|
871
|
-
static
|
|
872
|
-
core.Guards.
|
|
873
|
-
core.Guards.
|
|
1019
|
+
static toSigningBytes(header, payload) {
|
|
1020
|
+
core.Guards.object(Jwt._CLASS_NAME, "header", header);
|
|
1021
|
+
core.Guards.object(Jwt._CLASS_NAME, "payload", payload);
|
|
1022
|
+
const segments = [];
|
|
1023
|
+
const headerBytes = core.Converter.utf8ToBytes(JSON.stringify(header));
|
|
1024
|
+
segments.push(core.Converter.bytesToBase64Url(headerBytes));
|
|
1025
|
+
const payloadBytes = core.Converter.utf8ToBytes(JSON.stringify(payload));
|
|
1026
|
+
segments.push(core.Converter.bytesToBase64Url(payloadBytes));
|
|
1027
|
+
return core.Converter.utf8ToBytes(segments.join("."));
|
|
1028
|
+
}
|
|
1029
|
+
/**
|
|
1030
|
+
* Create header and payload from signing bytes.
|
|
1031
|
+
* @param signingBytes The signing bytes from a token.
|
|
1032
|
+
* @returns The header and payload.
|
|
1033
|
+
* @throws If the signing bytes are invalid
|
|
1034
|
+
*/
|
|
1035
|
+
static fromSigningBytes(signingBytes) {
|
|
1036
|
+
core.Guards.uint8Array(Jwt._CLASS_NAME, "signingBytes", signingBytes);
|
|
1037
|
+
const segments = core.Converter.bytesToUtf8(signingBytes).split(".");
|
|
1038
|
+
if (segments.length !== 2) {
|
|
1039
|
+
throw new core.GeneralError(Jwt._CLASS_NAME, "invalidSigningBytes");
|
|
1040
|
+
}
|
|
1041
|
+
const headerBytes = core.Converter.base64UrlToBytes(segments[0]);
|
|
1042
|
+
const payloadBytes = core.Converter.base64UrlToBytes(segments[1]);
|
|
1043
|
+
return {
|
|
1044
|
+
header: core.ObjectHelper.fromBytes(headerBytes),
|
|
1045
|
+
payload: core.ObjectHelper.fromBytes(payloadBytes)
|
|
1046
|
+
};
|
|
1047
|
+
}
|
|
1048
|
+
/**
|
|
1049
|
+
* Convert signed bytes and signature bytes to token.
|
|
1050
|
+
* @param signingBytes The signed bytes.
|
|
1051
|
+
* @param signature The signature.
|
|
1052
|
+
* @returns The token.
|
|
1053
|
+
*/
|
|
1054
|
+
static tokenFromBytes(signingBytes, signature) {
|
|
1055
|
+
core.Guards.uint8Array(Jwt._CLASS_NAME, "signingBytes", signingBytes);
|
|
874
1056
|
core.Guards.uint8Array(Jwt._CLASS_NAME, "signature", signature);
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
1057
|
+
const signedBytesUtf8 = core.Converter.bytesToUtf8(signingBytes);
|
|
1058
|
+
const signatureBase64 = core.Converter.bytesToBase64Url(signature);
|
|
1059
|
+
return `${signedBytesUtf8}.${signatureBase64}`;
|
|
1060
|
+
}
|
|
1061
|
+
/**
|
|
1062
|
+
* Convert the token to signing bytes and signature bytes.
|
|
1063
|
+
* @param token The token to convert to bytes.
|
|
1064
|
+
* @returns The decoded bytes.
|
|
1065
|
+
* @throws If the token is invalid.
|
|
1066
|
+
*/
|
|
1067
|
+
static tokenToBytes(token) {
|
|
1068
|
+
core.Guards.stringValue(Jwt._CLASS_NAME, "token", token);
|
|
1069
|
+
const segments = token.split(".");
|
|
1070
|
+
if (segments.length !== 3) {
|
|
1071
|
+
throw new core.GeneralError(Jwt._CLASS_NAME, "invalidTokenParts");
|
|
879
1072
|
}
|
|
880
|
-
|
|
1073
|
+
const signingBytes = core.Converter.utf8ToBytes(`${segments[0]}.${segments[1]}`);
|
|
1074
|
+
const signature = core.Converter.base64UrlToBytes(segments[2]);
|
|
1075
|
+
return {
|
|
1076
|
+
signingBytes,
|
|
1077
|
+
signature
|
|
1078
|
+
};
|
|
881
1079
|
}
|
|
882
1080
|
/**
|
|
883
1081
|
* Encode a token.
|
|
@@ -894,19 +1092,11 @@ class Jwt {
|
|
|
894
1092
|
if (!hasKey && !hasSigner) {
|
|
895
1093
|
throw new core.GeneralError(Jwt._CLASS_NAME, "noKeyOrSigner");
|
|
896
1094
|
}
|
|
897
|
-
signer ??= async (
|
|
1095
|
+
signer ??= async (h, p, k) => Jwt.defaultSigner(h, p, k);
|
|
898
1096
|
if (core.Is.undefined(header.typ)) {
|
|
899
1097
|
header.typ = "JWT";
|
|
900
1098
|
}
|
|
901
|
-
|
|
902
|
-
const headerBytes = core.Converter.utf8ToBytes(JSON.stringify(header));
|
|
903
|
-
segments.push(core.Converter.bytesToBase64Url(headerBytes));
|
|
904
|
-
const payloadBytes = core.Converter.utf8ToBytes(JSON.stringify(payload));
|
|
905
|
-
segments.push(core.Converter.bytesToBase64Url(payloadBytes));
|
|
906
|
-
const jwtHeaderAndPayload = core.Converter.utf8ToBytes(segments.join("."));
|
|
907
|
-
const sigBytes = await signer(header.alg, key, jwtHeaderAndPayload);
|
|
908
|
-
segments.push(core.Converter.bytesToBase64Url(sigBytes));
|
|
909
|
-
return segments.join(".");
|
|
1099
|
+
return signer(header, payload, key);
|
|
910
1100
|
}
|
|
911
1101
|
}
|
|
912
1102
|
|
|
@@ -922,7 +1112,7 @@ class MimeTypeHelper {
|
|
|
922
1112
|
* @returns The mime type if detected.
|
|
923
1113
|
*/
|
|
924
1114
|
static async detect(data) {
|
|
925
|
-
if (!core.Is.uint8Array(data)) {
|
|
1115
|
+
if (!core.Is.uint8Array(data) || data.length === 0) {
|
|
926
1116
|
return undefined;
|
|
927
1117
|
}
|
|
928
1118
|
// Image
|
|
@@ -946,6 +1136,11 @@ class MimeTypeHelper {
|
|
|
946
1136
|
if (MimeTypeHelper.checkBytes(data, [0x1f, 0x8b, 0x8])) {
|
|
947
1137
|
return MimeTypes.Gzip;
|
|
948
1138
|
}
|
|
1139
|
+
if (MimeTypeHelper.checkBytes(data, [0x78, 0x01]) ||
|
|
1140
|
+
MimeTypeHelper.checkBytes(data, [0x78, 0x9c]) ||
|
|
1141
|
+
MimeTypeHelper.checkBytes(data, [0x78, 0xda])) {
|
|
1142
|
+
return MimeTypes.Zlib;
|
|
1143
|
+
}
|
|
949
1144
|
if (MimeTypeHelper.checkBytes(data, [0x42, 0x5a, 0x68])) {
|
|
950
1145
|
return MimeTypes.Bzip2;
|
|
951
1146
|
}
|
|
@@ -994,12 +1189,14 @@ class MimeTypeHelper {
|
|
|
994
1189
|
[MimeTypes.Javascript]: "js",
|
|
995
1190
|
[MimeTypes.Json]: "json",
|
|
996
1191
|
[MimeTypes.JsonLd]: "jsonld",
|
|
1192
|
+
[MimeTypes.Jwt]: "jwt",
|
|
997
1193
|
[MimeTypes.Xml]: "xml",
|
|
998
1194
|
[MimeTypes.OctetStream]: "bin",
|
|
999
1195
|
[MimeTypes.Gzip]: "gzip",
|
|
1196
|
+
[MimeTypes.Zlib]: "zlib",
|
|
1000
1197
|
[MimeTypes.Bzip2]: "bz2",
|
|
1001
1198
|
[MimeTypes.Zip]: "zip",
|
|
1002
|
-
[MimeTypes.Pdf]: "
|
|
1199
|
+
[MimeTypes.Pdf]: "pdf",
|
|
1003
1200
|
[MimeTypes.Gif]: "gif",
|
|
1004
1201
|
[MimeTypes.Bmp]: "bmp",
|
|
1005
1202
|
[MimeTypes.Jpeg]: "jpeg",
|
|
@@ -1050,7 +1247,8 @@ exports.FetchHelper = FetchHelper;
|
|
|
1050
1247
|
exports.HeaderTypes = HeaderTypes;
|
|
1051
1248
|
exports.HttpMethod = HttpMethod;
|
|
1052
1249
|
exports.HttpStatusCode = HttpStatusCode;
|
|
1250
|
+
exports.Jwk = Jwk;
|
|
1251
|
+
exports.Jws = Jws;
|
|
1053
1252
|
exports.Jwt = Jwt;
|
|
1054
|
-
exports.JwtAlgorithms = JwtAlgorithms;
|
|
1055
1253
|
exports.MimeTypeHelper = MimeTypeHelper;
|
|
1056
1254
|
exports.MimeTypes = MimeTypes;
|