@twin.org/web 0.0.1-next.4 → 0.0.1-next.41
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 +251 -96
- package/dist/esm/index.mjs +252 -98
- 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/headerTypes.d.ts +8 -8
- package/dist/types/models/jwkCryptoKey.d.ts +4 -0
- package/dist/types/models/mimeTypes.d.ts +4 -0
- package/dist/types/utils/jwk.d.ts +25 -0
- package/dist/types/utils/jws.d.ts +20 -0
- package/dist/types/utils/jwt.d.ts +67 -29
- package/docs/changelog.md +1 -1
- package/docs/reference/classes/FetchError.md +13 -5
- package/docs/reference/classes/FetchHelper.md +51 -17
- package/docs/reference/classes/Jwk.md +79 -0
- package/docs/reference/classes/Jws.md +75 -0
- package/docs/reference/classes/Jwt.md +222 -102
- package/docs/reference/classes/MimeTypeHelper.md +6 -2
- 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 +4 -24
- package/docs/reference/interfaces/IJwtPayload.md +4 -56
- package/docs/reference/type-aliases/JwkCryptoKey.md +5 -0
- package/docs/reference/variables/HeaderTypes.md +8 -8
- package/docs/reference/variables/MimeTypes.md +6 -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.
|
|
@@ -39,35 +40,35 @@ const HeaderTypes = {
|
|
|
39
40
|
/**
|
|
40
41
|
* Content Type.
|
|
41
42
|
*/
|
|
42
|
-
ContentType: "
|
|
43
|
+
ContentType: "content-type",
|
|
43
44
|
/**
|
|
44
45
|
* Content Length.
|
|
45
46
|
*/
|
|
46
|
-
ContentLength: "
|
|
47
|
+
ContentLength: "content-length",
|
|
47
48
|
/**
|
|
48
49
|
* Content Disposition.
|
|
49
50
|
*/
|
|
50
|
-
ContentDisposition: "
|
|
51
|
+
ContentDisposition: "content-disposition",
|
|
51
52
|
/**
|
|
52
53
|
* Accept.
|
|
53
54
|
*/
|
|
54
|
-
Accept: "
|
|
55
|
+
Accept: "accept",
|
|
55
56
|
/**
|
|
56
57
|
* Authorization.
|
|
57
58
|
*/
|
|
58
|
-
Authorization: "
|
|
59
|
+
Authorization: "authorization",
|
|
59
60
|
/**
|
|
60
61
|
* Cookie.
|
|
61
62
|
*/
|
|
62
|
-
Cookie: "
|
|
63
|
+
Cookie: "cookie",
|
|
63
64
|
/**
|
|
64
65
|
* Set Cookie.
|
|
65
66
|
*/
|
|
66
|
-
SetCookie: "
|
|
67
|
+
SetCookie: "set-cookie",
|
|
67
68
|
/**
|
|
68
69
|
* Location
|
|
69
70
|
*/
|
|
70
|
-
Location: "
|
|
71
|
+
Location: "location"
|
|
71
72
|
};
|
|
72
73
|
|
|
73
74
|
// Copyright 2024 IOTA Stiftung.
|
|
@@ -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
|
*/
|
|
@@ -710,7 +698,134 @@ class FetchHelper {
|
|
|
710
698
|
// Copyright 2024 IOTA Stiftung.
|
|
711
699
|
// SPDX-License-Identifier: Apache-2.0.
|
|
712
700
|
/**
|
|
713
|
-
* Class to
|
|
701
|
+
* Class to handle JSON Web Keys.
|
|
702
|
+
*/
|
|
703
|
+
class Jwk {
|
|
704
|
+
/**
|
|
705
|
+
* Runtime name for the class.
|
|
706
|
+
* @internal
|
|
707
|
+
*/
|
|
708
|
+
static _CLASS_NAME = "Jwk";
|
|
709
|
+
/**
|
|
710
|
+
* Convert the JWK to a crypto key.
|
|
711
|
+
* @param jwk The JWK to convert.
|
|
712
|
+
* @returns The crypto key.
|
|
713
|
+
*/
|
|
714
|
+
static async toCryptoKey(jwk) {
|
|
715
|
+
core.Guards.object(Jwk._CLASS_NAME, "jwk", jwk);
|
|
716
|
+
try {
|
|
717
|
+
return jose.importJWK(jwk);
|
|
718
|
+
}
|
|
719
|
+
catch (err) {
|
|
720
|
+
throw new core.GeneralError(Jwk._CLASS_NAME, "jwkImportFailed", undefined, err);
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
/**
|
|
724
|
+
* Convert the Ed25519 private key to a crypto key.
|
|
725
|
+
* @param privateKey The private key to use.
|
|
726
|
+
* @returns The crypto key.
|
|
727
|
+
*/
|
|
728
|
+
static async fromEd25519Private(privateKey) {
|
|
729
|
+
core.Guards.uint8Array(Jwk._CLASS_NAME, "privateKey", privateKey);
|
|
730
|
+
try {
|
|
731
|
+
const publicKey = crypto.Ed25519.publicKeyFromPrivateKey(privateKey);
|
|
732
|
+
const jwk = {
|
|
733
|
+
kty: "OKP",
|
|
734
|
+
use: "sig",
|
|
735
|
+
alg: "EdDSA",
|
|
736
|
+
crv: "Ed25519",
|
|
737
|
+
x: core.Converter.bytesToBase64Url(publicKey),
|
|
738
|
+
d: core.Converter.bytesToBase64Url(privateKey)
|
|
739
|
+
};
|
|
740
|
+
return jose.importJWK(jwk);
|
|
741
|
+
}
|
|
742
|
+
catch (err) {
|
|
743
|
+
throw new core.GeneralError(Jwk._CLASS_NAME, "jwkImportFailed", undefined, err);
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
/**
|
|
747
|
+
* Convert the Ed25519 public key to a crypto key.
|
|
748
|
+
* @param publicKey The private key to use.
|
|
749
|
+
* @returns The crypto key.
|
|
750
|
+
*/
|
|
751
|
+
static async fromEd25519Public(publicKey) {
|
|
752
|
+
core.Guards.uint8Array(Jwk._CLASS_NAME, "publicKey", publicKey);
|
|
753
|
+
try {
|
|
754
|
+
const jwk = {
|
|
755
|
+
kty: "OKP",
|
|
756
|
+
use: "sig",
|
|
757
|
+
alg: "EdDSA",
|
|
758
|
+
crv: "Ed25519",
|
|
759
|
+
x: core.Converter.bytesToBase64Url(publicKey)
|
|
760
|
+
};
|
|
761
|
+
return jose.importJWK(jwk);
|
|
762
|
+
}
|
|
763
|
+
catch (err) {
|
|
764
|
+
throw new core.GeneralError(Jwk._CLASS_NAME, "jwkImportFailed", undefined, err);
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
// Copyright 2024 IOTA Stiftung.
|
|
770
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
771
|
+
/**
|
|
772
|
+
* Class to handle JSON Web Signatures.
|
|
773
|
+
*/
|
|
774
|
+
class Jws {
|
|
775
|
+
/**
|
|
776
|
+
* Runtime name for the class.
|
|
777
|
+
* @internal
|
|
778
|
+
*/
|
|
779
|
+
static _CLASS_NAME = "Jws";
|
|
780
|
+
/**
|
|
781
|
+
* Create a signature.
|
|
782
|
+
* @param privateKey The private key to use.
|
|
783
|
+
* @param hash The hash to sign.
|
|
784
|
+
* @returns The signature.
|
|
785
|
+
*/
|
|
786
|
+
static async create(privateKey, hash) {
|
|
787
|
+
core.Guards.defined(Jws._CLASS_NAME, "privateKey", privateKey);
|
|
788
|
+
core.Guards.uint8Array(Jws._CLASS_NAME, "hash", hash);
|
|
789
|
+
try {
|
|
790
|
+
const jws = await new jose.CompactSign(hash)
|
|
791
|
+
.setProtectedHeader({
|
|
792
|
+
alg: privateKey.algorithm.name,
|
|
793
|
+
b64: false,
|
|
794
|
+
crit: ["b64"]
|
|
795
|
+
})
|
|
796
|
+
.sign(privateKey);
|
|
797
|
+
return jws;
|
|
798
|
+
}
|
|
799
|
+
catch (err) {
|
|
800
|
+
throw new core.GeneralError(Jws._CLASS_NAME, "createFailed", undefined, err);
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
/**
|
|
804
|
+
* Verify a signature.
|
|
805
|
+
* @param jws The signature to verify.
|
|
806
|
+
* @param publicKey The public key to verify the signature with.
|
|
807
|
+
* @param hash The hash to verify.
|
|
808
|
+
* @returns True if the signature was verified.
|
|
809
|
+
*/
|
|
810
|
+
static async verify(jws, publicKey, hash) {
|
|
811
|
+
core.Guards.stringValue(Jws._CLASS_NAME, "jws", jws);
|
|
812
|
+
core.Guards.defined(Jws._CLASS_NAME, "publicKey", publicKey);
|
|
813
|
+
core.Guards.uint8Array(Jws._CLASS_NAME, "hash", hash);
|
|
814
|
+
try {
|
|
815
|
+
const jwsParts = jws.split(".");
|
|
816
|
+
await jose.flattenedVerify({ protected: jwsParts[0], payload: hash, signature: jwsParts[2] }, publicKey);
|
|
817
|
+
return true;
|
|
818
|
+
}
|
|
819
|
+
catch (err) {
|
|
820
|
+
throw new core.GeneralError(Jws._CLASS_NAME, "verifyFailed", undefined, err);
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
// Copyright 2024 IOTA Stiftung.
|
|
826
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
827
|
+
/**
|
|
828
|
+
* Class to handle JSON Web Tokens.
|
|
714
829
|
*/
|
|
715
830
|
class Jwt {
|
|
716
831
|
/**
|
|
@@ -727,9 +842,8 @@ class Jwt {
|
|
|
727
842
|
*/
|
|
728
843
|
static async encode(header, payload, key) {
|
|
729
844
|
core.Guards.object(Jwt._CLASS_NAME, "header", header);
|
|
730
|
-
core.Guards.arrayOneOf(Jwt._CLASS_NAME, "header.alg", header.alg, Object.values(JwtAlgorithms));
|
|
731
845
|
core.Guards.object(Jwt._CLASS_NAME, "payload", payload);
|
|
732
|
-
core.Guards.
|
|
846
|
+
core.Guards.defined(Jwt._CLASS_NAME, "key", key);
|
|
733
847
|
return Jwt.internalEncode(header, payload, key);
|
|
734
848
|
}
|
|
735
849
|
/**
|
|
@@ -741,7 +855,7 @@ class Jwt {
|
|
|
741
855
|
*/
|
|
742
856
|
static async encodeWithSigner(header, payload, signer) {
|
|
743
857
|
core.Guards.object(Jwt._CLASS_NAME, "header", header);
|
|
744
|
-
core.Guards.
|
|
858
|
+
core.Guards.stringValue(Jwt._CLASS_NAME, "header.alg", header.alg);
|
|
745
859
|
core.Guards.object(Jwt._CLASS_NAME, "payload", payload);
|
|
746
860
|
core.Guards.function(Jwt._CLASS_NAME, "signer", signer);
|
|
747
861
|
return Jwt.internalEncode(header, payload, undefined, signer);
|
|
@@ -788,13 +902,8 @@ class Jwt {
|
|
|
788
902
|
*/
|
|
789
903
|
static async verify(token, key) {
|
|
790
904
|
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
|
-
};
|
|
905
|
+
core.Guards.defined(Jwt._CLASS_NAME, "key", key);
|
|
906
|
+
return Jwt.verifySignature(token, key);
|
|
798
907
|
}
|
|
799
908
|
/**
|
|
800
909
|
* Verify a token.
|
|
@@ -805,79 +914,131 @@ class Jwt {
|
|
|
805
914
|
static async verifyWithVerifier(token, verifier) {
|
|
806
915
|
core.Guards.stringValue(Jwt._CLASS_NAME, "token", token);
|
|
807
916
|
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
|
-
};
|
|
917
|
+
return Jwt.verifySignature(token, undefined, verifier);
|
|
815
918
|
}
|
|
816
919
|
/**
|
|
817
920
|
* Verify a token by parts.
|
|
818
|
-
* @param
|
|
819
|
-
* @param payload The payload to verify.
|
|
820
|
-
* @param signature The signature to verify.
|
|
921
|
+
* @param token The token to verify.
|
|
821
922
|
* @param key The key for verifying the token, if not provided no verification occurs.
|
|
822
923
|
* @param verifier Custom verification method.
|
|
823
924
|
* @returns True if the parts are verified.
|
|
824
925
|
*/
|
|
825
|
-
static async verifySignature(
|
|
926
|
+
static async verifySignature(token, key, verifier) {
|
|
927
|
+
core.Guards.stringValue(Jwt._CLASS_NAME, "token", token);
|
|
826
928
|
const hasKey = core.Is.notEmpty(key);
|
|
827
929
|
const hasVerifier = core.Is.notEmpty(verifier);
|
|
828
930
|
if (!hasKey && !hasVerifier) {
|
|
829
931
|
throw new core.GeneralError(Jwt._CLASS_NAME, "noKeyOrVerifier");
|
|
830
932
|
}
|
|
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;
|
|
933
|
+
verifier ??= async (t, k) => Jwt.defaultVerifier(t, k);
|
|
934
|
+
return verifier(token, key);
|
|
846
935
|
}
|
|
847
936
|
/**
|
|
848
937
|
* The default signer for the JWT.
|
|
849
|
-
* @param
|
|
850
|
-
* @param key The key to sign with.
|
|
938
|
+
* @param header The header to sign.
|
|
851
939
|
* @param payload The payload to sign.
|
|
940
|
+
* @param key The optional key to sign with.
|
|
852
941
|
* @returns The signature.
|
|
853
942
|
*/
|
|
854
|
-
static async defaultSigner(
|
|
855
|
-
core.Guards.
|
|
856
|
-
core.Guards.
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
943
|
+
static async defaultSigner(header, payload, key) {
|
|
944
|
+
core.Guards.object(Jwt._CLASS_NAME, "header", header);
|
|
945
|
+
core.Guards.object(Jwt._CLASS_NAME, "payload", payload);
|
|
946
|
+
core.Guards.defined(Jwt._CLASS_NAME, "key", key);
|
|
947
|
+
const signer = new jose.SignJWT(payload);
|
|
948
|
+
signer.setProtectedHeader(header);
|
|
949
|
+
let finalKey = key;
|
|
950
|
+
if (header.alg === "EdDSA" && core.Is.uint8Array(key)) {
|
|
951
|
+
// Jose does not support Ed25519 keys in raw format, so we need to convert it to PKCS8.
|
|
952
|
+
finalKey = await crypto.Ed25519.privateKeyToPKCS8(key);
|
|
860
953
|
}
|
|
861
|
-
return
|
|
954
|
+
return signer.sign(finalKey);
|
|
862
955
|
}
|
|
863
956
|
/**
|
|
864
957
|
* The default verifier for the JWT.
|
|
865
|
-
* @param
|
|
958
|
+
* @param token The token to verify.
|
|
866
959
|
* @param key The key to verify with.
|
|
867
|
-
* @
|
|
868
|
-
|
|
869
|
-
|
|
960
|
+
* @returns The header and payload if verification successful.
|
|
961
|
+
*/
|
|
962
|
+
static async defaultVerifier(token, key) {
|
|
963
|
+
core.Guards.stringValue(Jwt._CLASS_NAME, "token", token);
|
|
964
|
+
core.Guards.defined(Jwt._CLASS_NAME, "key", key);
|
|
965
|
+
try {
|
|
966
|
+
const result = await jose.jwtVerify(token, key);
|
|
967
|
+
return {
|
|
968
|
+
header: result.protectedHeader,
|
|
969
|
+
payload: result.payload
|
|
970
|
+
};
|
|
971
|
+
}
|
|
972
|
+
catch (err) {
|
|
973
|
+
throw new core.GeneralError(Jwt._CLASS_NAME, "verifyFailed", undefined, err);
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
/**
|
|
977
|
+
* Create bytes for signing from header and payload.
|
|
978
|
+
* @param header The header.
|
|
979
|
+
* @param payload The payload.
|
|
980
|
+
* @returns The bytes to sign.
|
|
981
|
+
*/
|
|
982
|
+
static toSigningBytes(header, payload) {
|
|
983
|
+
core.Guards.object(Jwt._CLASS_NAME, "header", header);
|
|
984
|
+
core.Guards.object(Jwt._CLASS_NAME, "payload", payload);
|
|
985
|
+
const segments = [];
|
|
986
|
+
const headerBytes = core.Converter.utf8ToBytes(JSON.stringify(header));
|
|
987
|
+
segments.push(core.Converter.bytesToBase64Url(headerBytes));
|
|
988
|
+
const payloadBytes = core.Converter.utf8ToBytes(JSON.stringify(payload));
|
|
989
|
+
segments.push(core.Converter.bytesToBase64Url(payloadBytes));
|
|
990
|
+
return core.Converter.utf8ToBytes(segments.join("."));
|
|
991
|
+
}
|
|
992
|
+
/**
|
|
993
|
+
* Create header and payload from signing bytes.
|
|
994
|
+
* @param signingBytes The signing bytes from a token.
|
|
995
|
+
* @returns The header and payload.
|
|
996
|
+
* @throws If the signing bytes are invalid
|
|
997
|
+
*/
|
|
998
|
+
static fromSigningBytes(signingBytes) {
|
|
999
|
+
core.Guards.uint8Array(Jwt._CLASS_NAME, "signingBytes", signingBytes);
|
|
1000
|
+
const segments = core.Converter.bytesToUtf8(signingBytes).split(".");
|
|
1001
|
+
if (segments.length !== 2) {
|
|
1002
|
+
throw new core.GeneralError(Jwt._CLASS_NAME, "invalidSigningBytes");
|
|
1003
|
+
}
|
|
1004
|
+
const headerBytes = core.Converter.base64UrlToBytes(segments[0]);
|
|
1005
|
+
const payloadBytes = core.Converter.base64UrlToBytes(segments[1]);
|
|
1006
|
+
return {
|
|
1007
|
+
header: core.ObjectHelper.fromBytes(headerBytes),
|
|
1008
|
+
payload: core.ObjectHelper.fromBytes(payloadBytes)
|
|
1009
|
+
};
|
|
1010
|
+
}
|
|
1011
|
+
/**
|
|
1012
|
+
* Convert signed bytes and signature bytes to token.
|
|
1013
|
+
* @param signingBytes The signed bytes.
|
|
1014
|
+
* @param signature The signature.
|
|
1015
|
+
* @returns The token.
|
|
870
1016
|
*/
|
|
871
|
-
static
|
|
872
|
-
core.Guards.uint8Array(Jwt._CLASS_NAME, "
|
|
873
|
-
core.Guards.uint8Array(Jwt._CLASS_NAME, "payload", payload);
|
|
1017
|
+
static tokenFromBytes(signingBytes, signature) {
|
|
1018
|
+
core.Guards.uint8Array(Jwt._CLASS_NAME, "signingBytes", signingBytes);
|
|
874
1019
|
core.Guards.uint8Array(Jwt._CLASS_NAME, "signature", signature);
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
1020
|
+
const signedBytesUtf8 = core.Converter.bytesToUtf8(signingBytes);
|
|
1021
|
+
const signatureBase64 = core.Converter.bytesToBase64Url(signature);
|
|
1022
|
+
return `${signedBytesUtf8}.${signatureBase64}`;
|
|
1023
|
+
}
|
|
1024
|
+
/**
|
|
1025
|
+
* Convert the token to signing bytes and signature bytes.
|
|
1026
|
+
* @param token The token to convert to bytes.
|
|
1027
|
+
* @returns The decoded bytes.
|
|
1028
|
+
* @throws If the token is invalid.
|
|
1029
|
+
*/
|
|
1030
|
+
static tokenToBytes(token) {
|
|
1031
|
+
core.Guards.stringValue(Jwt._CLASS_NAME, "token", token);
|
|
1032
|
+
const segments = token.split(".");
|
|
1033
|
+
if (segments.length !== 3) {
|
|
1034
|
+
throw new core.GeneralError(Jwt._CLASS_NAME, "invalidTokenParts");
|
|
879
1035
|
}
|
|
880
|
-
|
|
1036
|
+
const signingBytes = core.Converter.utf8ToBytes(`${segments[0]}.${segments[1]}`);
|
|
1037
|
+
const signature = core.Converter.base64UrlToBytes(segments[2]);
|
|
1038
|
+
return {
|
|
1039
|
+
signingBytes,
|
|
1040
|
+
signature
|
|
1041
|
+
};
|
|
881
1042
|
}
|
|
882
1043
|
/**
|
|
883
1044
|
* Encode a token.
|
|
@@ -894,19 +1055,11 @@ class Jwt {
|
|
|
894
1055
|
if (!hasKey && !hasSigner) {
|
|
895
1056
|
throw new core.GeneralError(Jwt._CLASS_NAME, "noKeyOrSigner");
|
|
896
1057
|
}
|
|
897
|
-
signer ??= async (
|
|
1058
|
+
signer ??= async (h, p, k) => Jwt.defaultSigner(h, p, k);
|
|
898
1059
|
if (core.Is.undefined(header.typ)) {
|
|
899
1060
|
header.typ = "JWT";
|
|
900
1061
|
}
|
|
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(".");
|
|
1062
|
+
return signer(header, payload, key);
|
|
910
1063
|
}
|
|
911
1064
|
}
|
|
912
1065
|
|
|
@@ -922,7 +1075,7 @@ class MimeTypeHelper {
|
|
|
922
1075
|
* @returns The mime type if detected.
|
|
923
1076
|
*/
|
|
924
1077
|
static async detect(data) {
|
|
925
|
-
if (!core.Is.uint8Array(data)) {
|
|
1078
|
+
if (!core.Is.uint8Array(data) || data.length === 0) {
|
|
926
1079
|
return undefined;
|
|
927
1080
|
}
|
|
928
1081
|
// Image
|
|
@@ -994,12 +1147,13 @@ class MimeTypeHelper {
|
|
|
994
1147
|
[MimeTypes.Javascript]: "js",
|
|
995
1148
|
[MimeTypes.Json]: "json",
|
|
996
1149
|
[MimeTypes.JsonLd]: "jsonld",
|
|
1150
|
+
[MimeTypes.Jwt]: "jwt",
|
|
997
1151
|
[MimeTypes.Xml]: "xml",
|
|
998
1152
|
[MimeTypes.OctetStream]: "bin",
|
|
999
1153
|
[MimeTypes.Gzip]: "gzip",
|
|
1000
1154
|
[MimeTypes.Bzip2]: "bz2",
|
|
1001
1155
|
[MimeTypes.Zip]: "zip",
|
|
1002
|
-
[MimeTypes.Pdf]: "
|
|
1156
|
+
[MimeTypes.Pdf]: "pdf",
|
|
1003
1157
|
[MimeTypes.Gif]: "gif",
|
|
1004
1158
|
[MimeTypes.Bmp]: "bmp",
|
|
1005
1159
|
[MimeTypes.Jpeg]: "jpeg",
|
|
@@ -1050,7 +1204,8 @@ exports.FetchHelper = FetchHelper;
|
|
|
1050
1204
|
exports.HeaderTypes = HeaderTypes;
|
|
1051
1205
|
exports.HttpMethod = HttpMethod;
|
|
1052
1206
|
exports.HttpStatusCode = HttpStatusCode;
|
|
1207
|
+
exports.Jwk = Jwk;
|
|
1208
|
+
exports.Jws = Jws;
|
|
1053
1209
|
exports.Jwt = Jwt;
|
|
1054
|
-
exports.JwtAlgorithms = JwtAlgorithms;
|
|
1055
1210
|
exports.MimeTypeHelper = MimeTypeHelper;
|
|
1056
1211
|
exports.MimeTypes = MimeTypes;
|