@twin.org/web 0.0.1-next.5 → 0.0.1-next.51
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 +271 -96
- package/dist/esm/index.mjs +272 -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/fetchHelper.d.ts +7 -0
- package/dist/types/utils/jwk.d.ts +34 -0
- package/dist/types/utils/jws.d.ts +22 -0
- package/dist/types/utils/jwt.d.ts +67 -29
- package/docs/changelog.md +31 -1
- package/docs/reference/classes/FetchError.md +13 -5
- package/docs/reference/classes/FetchHelper.md +85 -19
- package/docs/reference/classes/Jwk.md +101 -0
- package/docs/reference/classes/Jws.md +81 -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/esm/index.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { BaseError, StringHelper, Guards, Is, AsyncCache, ObjectHelper,
|
|
2
|
-
import {
|
|
1
|
+
import { BaseError, StringHelper, Guards, Is, AsyncCache, ObjectHelper, GeneralError, Converter } from '@twin.org/core';
|
|
2
|
+
import { Ed25519 } from '@twin.org/crypto';
|
|
3
|
+
import { importJWK, CompactSign, flattenedVerify, SignJWT, jwtVerify } from 'jose';
|
|
3
4
|
|
|
4
5
|
// Copyright 2024 IOTA Stiftung.
|
|
5
6
|
// SPDX-License-Identifier: Apache-2.0.
|
|
@@ -37,35 +38,35 @@ const HeaderTypes = {
|
|
|
37
38
|
/**
|
|
38
39
|
* Content Type.
|
|
39
40
|
*/
|
|
40
|
-
ContentType: "
|
|
41
|
+
ContentType: "content-type",
|
|
41
42
|
/**
|
|
42
43
|
* Content Length.
|
|
43
44
|
*/
|
|
44
|
-
ContentLength: "
|
|
45
|
+
ContentLength: "content-length",
|
|
45
46
|
/**
|
|
46
47
|
* Content Disposition.
|
|
47
48
|
*/
|
|
48
|
-
ContentDisposition: "
|
|
49
|
+
ContentDisposition: "content-disposition",
|
|
49
50
|
/**
|
|
50
51
|
* Accept.
|
|
51
52
|
*/
|
|
52
|
-
Accept: "
|
|
53
|
+
Accept: "accept",
|
|
53
54
|
/**
|
|
54
55
|
* Authorization.
|
|
55
56
|
*/
|
|
56
|
-
Authorization: "
|
|
57
|
+
Authorization: "authorization",
|
|
57
58
|
/**
|
|
58
59
|
* Cookie.
|
|
59
60
|
*/
|
|
60
|
-
Cookie: "
|
|
61
|
+
Cookie: "cookie",
|
|
61
62
|
/**
|
|
62
63
|
* Set Cookie.
|
|
63
64
|
*/
|
|
64
|
-
SetCookie: "
|
|
65
|
+
SetCookie: "set-cookie",
|
|
65
66
|
/**
|
|
66
67
|
* Location
|
|
67
68
|
*/
|
|
68
|
-
Location: "
|
|
69
|
+
Location: "location"
|
|
69
70
|
};
|
|
70
71
|
|
|
71
72
|
// Copyright 2024 IOTA Stiftung.
|
|
@@ -343,23 +344,6 @@ const HttpStatusCode = {
|
|
|
343
344
|
networkAuthenticationRequired: 511
|
|
344
345
|
};
|
|
345
346
|
|
|
346
|
-
// Copyright 2024 IOTA Stiftung.
|
|
347
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
348
|
-
/**
|
|
349
|
-
* The cryptographic algorithms supported for JSON Web Tokens and JSON Web Keys.
|
|
350
|
-
*/
|
|
351
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
352
|
-
const JwtAlgorithms = {
|
|
353
|
-
/**
|
|
354
|
-
* HMAC using SHA-256.
|
|
355
|
-
*/
|
|
356
|
-
HS256: "HS256",
|
|
357
|
-
/**
|
|
358
|
-
* EdDSA using Ed25519.
|
|
359
|
-
*/
|
|
360
|
-
EdDSA: "EdDSA"
|
|
361
|
-
};
|
|
362
|
-
|
|
363
347
|
// Copyright 2024 IOTA Stiftung.
|
|
364
348
|
// SPDX-License-Identifier: Apache-2.0.
|
|
365
349
|
/**
|
|
@@ -387,6 +371,10 @@ const MimeTypes = {
|
|
|
387
371
|
* JSON-LD - application/ld+json
|
|
388
372
|
*/
|
|
389
373
|
JsonLd: "application/ld+json",
|
|
374
|
+
/**
|
|
375
|
+
* JWT - application/jwt
|
|
376
|
+
*/
|
|
377
|
+
Jwt: "application/jwt",
|
|
390
378
|
/**
|
|
391
379
|
* XML - application/xml
|
|
392
380
|
*/
|
|
@@ -696,6 +684,15 @@ class FetchHelper {
|
|
|
696
684
|
static async getCacheEntry(url) {
|
|
697
685
|
return AsyncCache.get(`${FetchHelper._CACHE_PREFIX}${url}`);
|
|
698
686
|
}
|
|
687
|
+
/**
|
|
688
|
+
* Set a cache entry.
|
|
689
|
+
* @param url The url for the request.
|
|
690
|
+
* @param value The value to cache.
|
|
691
|
+
* @returns The cache entry if it exists.
|
|
692
|
+
*/
|
|
693
|
+
static async setCacheEntry(url, value) {
|
|
694
|
+
AsyncCache.set(`${FetchHelper._CACHE_PREFIX}${url}`, value);
|
|
695
|
+
}
|
|
699
696
|
/**
|
|
700
697
|
* Remove a cache entry.
|
|
701
698
|
* @param url The url for the request.
|
|
@@ -708,7 +705,145 @@ class FetchHelper {
|
|
|
708
705
|
// Copyright 2024 IOTA Stiftung.
|
|
709
706
|
// SPDX-License-Identifier: Apache-2.0.
|
|
710
707
|
/**
|
|
711
|
-
* Class to
|
|
708
|
+
* Class to handle JSON Web Keys.
|
|
709
|
+
*/
|
|
710
|
+
class Jwk {
|
|
711
|
+
/**
|
|
712
|
+
* Runtime name for the class.
|
|
713
|
+
* @internal
|
|
714
|
+
*/
|
|
715
|
+
static _CLASS_NAME = "Jwk";
|
|
716
|
+
/**
|
|
717
|
+
* Convert the JWK to a crypto key.
|
|
718
|
+
* @param jwk The JWK to convert.
|
|
719
|
+
* @returns The crypto key.
|
|
720
|
+
*/
|
|
721
|
+
static async toCryptoKey(jwk) {
|
|
722
|
+
Guards.object(Jwk._CLASS_NAME, "jwk", jwk);
|
|
723
|
+
try {
|
|
724
|
+
return importJWK(jwk);
|
|
725
|
+
}
|
|
726
|
+
catch (err) {
|
|
727
|
+
throw new GeneralError(Jwk._CLASS_NAME, "jwkImportFailed", undefined, err);
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
/**
|
|
731
|
+
* Convert the Ed25519 private key to a crypto key.
|
|
732
|
+
* @param privateKey The private key to use.
|
|
733
|
+
* @returns The crypto key.
|
|
734
|
+
*/
|
|
735
|
+
static async fromEd25519Private(privateKey) {
|
|
736
|
+
Guards.uint8Array(Jwk._CLASS_NAME, "privateKey", privateKey);
|
|
737
|
+
const publicKey = Ed25519.publicKeyFromPrivateKey(privateKey);
|
|
738
|
+
const jwk = {
|
|
739
|
+
kty: "OKP",
|
|
740
|
+
use: "enc",
|
|
741
|
+
alg: "EdDSA",
|
|
742
|
+
crv: "Ed25519",
|
|
743
|
+
x: Converter.bytesToBase64Url(publicKey),
|
|
744
|
+
d: Converter.bytesToBase64Url(privateKey)
|
|
745
|
+
};
|
|
746
|
+
return jwk;
|
|
747
|
+
}
|
|
748
|
+
/**
|
|
749
|
+
* Convert the Ed25519 public key to a crypto key.
|
|
750
|
+
* @param publicKey The private key to use.
|
|
751
|
+
* @returns The crypto key.
|
|
752
|
+
*/
|
|
753
|
+
static async fromEd25519Public(publicKey) {
|
|
754
|
+
Guards.uint8Array(Jwk._CLASS_NAME, "publicKey", publicKey);
|
|
755
|
+
const jwk = {
|
|
756
|
+
kty: "OKP",
|
|
757
|
+
use: "sig",
|
|
758
|
+
alg: "EdDSA",
|
|
759
|
+
crv: "Ed25519",
|
|
760
|
+
x: Converter.bytesToBase64Url(publicKey)
|
|
761
|
+
};
|
|
762
|
+
return jwk;
|
|
763
|
+
}
|
|
764
|
+
/**
|
|
765
|
+
* Convert the JWK to raw keys.
|
|
766
|
+
* @param jwk The JWK to convert to raw.
|
|
767
|
+
* @returns The crypto key.
|
|
768
|
+
*/
|
|
769
|
+
static async toRaw(jwk) {
|
|
770
|
+
Guards.object(Jwk._CLASS_NAME, "jwk", jwk);
|
|
771
|
+
let publicKey;
|
|
772
|
+
let privateKey;
|
|
773
|
+
if (Is.stringBase64Url(jwk.x)) {
|
|
774
|
+
publicKey = Converter.base64UrlToBytes(jwk.x);
|
|
775
|
+
}
|
|
776
|
+
if (Is.stringBase64Url(jwk.d)) {
|
|
777
|
+
privateKey = Converter.base64UrlToBytes(jwk.d);
|
|
778
|
+
}
|
|
779
|
+
return {
|
|
780
|
+
publicKey,
|
|
781
|
+
privateKey
|
|
782
|
+
};
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
// Copyright 2024 IOTA Stiftung.
|
|
787
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
788
|
+
/**
|
|
789
|
+
* Class to handle JSON Web Signatures.
|
|
790
|
+
*/
|
|
791
|
+
class Jws {
|
|
792
|
+
/**
|
|
793
|
+
* Runtime name for the class.
|
|
794
|
+
* @internal
|
|
795
|
+
*/
|
|
796
|
+
static _CLASS_NAME = "Jws";
|
|
797
|
+
/**
|
|
798
|
+
* Create a signature.
|
|
799
|
+
* @param privateKey The private key to use.
|
|
800
|
+
* @param hash The hash to sign.
|
|
801
|
+
* @param algOverride An optional algorithm override.
|
|
802
|
+
* @returns The signature.
|
|
803
|
+
*/
|
|
804
|
+
static async create(privateKey, hash, algOverride) {
|
|
805
|
+
Guards.defined(Jws._CLASS_NAME, "privateKey", privateKey);
|
|
806
|
+
Guards.uint8Array(Jws._CLASS_NAME, "hash", hash);
|
|
807
|
+
try {
|
|
808
|
+
const jws = await new CompactSign(hash)
|
|
809
|
+
.setProtectedHeader({
|
|
810
|
+
alg: Is.uint8Array(privateKey) ? (algOverride ?? "EdDSA") : privateKey.algorithm.name,
|
|
811
|
+
b64: false,
|
|
812
|
+
crit: ["b64"]
|
|
813
|
+
})
|
|
814
|
+
.sign(privateKey);
|
|
815
|
+
return jws;
|
|
816
|
+
}
|
|
817
|
+
catch (err) {
|
|
818
|
+
throw new GeneralError(Jws._CLASS_NAME, "createFailed", undefined, err);
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
/**
|
|
822
|
+
* Verify a signature.
|
|
823
|
+
* @param jws The signature to verify.
|
|
824
|
+
* @param publicKey The public key to verify the signature with.
|
|
825
|
+
* @param hash The hash to verify.
|
|
826
|
+
* @returns True if the signature was verified.
|
|
827
|
+
*/
|
|
828
|
+
static async verify(jws, publicKey, hash) {
|
|
829
|
+
Guards.stringValue(Jws._CLASS_NAME, "jws", jws);
|
|
830
|
+
Guards.defined(Jws._CLASS_NAME, "publicKey", publicKey);
|
|
831
|
+
Guards.uint8Array(Jws._CLASS_NAME, "hash", hash);
|
|
832
|
+
try {
|
|
833
|
+
const jwsParts = jws.split(".");
|
|
834
|
+
await flattenedVerify({ protected: jwsParts[0], payload: hash, signature: jwsParts[2] }, publicKey);
|
|
835
|
+
return true;
|
|
836
|
+
}
|
|
837
|
+
catch (err) {
|
|
838
|
+
throw new GeneralError(Jws._CLASS_NAME, "verifyFailed", undefined, err);
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
// Copyright 2024 IOTA Stiftung.
|
|
844
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
845
|
+
/**
|
|
846
|
+
* Class to handle JSON Web Tokens.
|
|
712
847
|
*/
|
|
713
848
|
class Jwt {
|
|
714
849
|
/**
|
|
@@ -725,9 +860,8 @@ class Jwt {
|
|
|
725
860
|
*/
|
|
726
861
|
static async encode(header, payload, key) {
|
|
727
862
|
Guards.object(Jwt._CLASS_NAME, "header", header);
|
|
728
|
-
Guards.arrayOneOf(Jwt._CLASS_NAME, "header.alg", header.alg, Object.values(JwtAlgorithms));
|
|
729
863
|
Guards.object(Jwt._CLASS_NAME, "payload", payload);
|
|
730
|
-
Guards.
|
|
864
|
+
Guards.defined(Jwt._CLASS_NAME, "key", key);
|
|
731
865
|
return Jwt.internalEncode(header, payload, key);
|
|
732
866
|
}
|
|
733
867
|
/**
|
|
@@ -739,7 +873,7 @@ class Jwt {
|
|
|
739
873
|
*/
|
|
740
874
|
static async encodeWithSigner(header, payload, signer) {
|
|
741
875
|
Guards.object(Jwt._CLASS_NAME, "header", header);
|
|
742
|
-
Guards.
|
|
876
|
+
Guards.stringValue(Jwt._CLASS_NAME, "header.alg", header.alg);
|
|
743
877
|
Guards.object(Jwt._CLASS_NAME, "payload", payload);
|
|
744
878
|
Guards.function(Jwt._CLASS_NAME, "signer", signer);
|
|
745
879
|
return Jwt.internalEncode(header, payload, undefined, signer);
|
|
@@ -786,13 +920,8 @@ class Jwt {
|
|
|
786
920
|
*/
|
|
787
921
|
static async verify(token, key) {
|
|
788
922
|
Guards.stringValue(Jwt._CLASS_NAME, "token", token);
|
|
789
|
-
Guards.
|
|
790
|
-
|
|
791
|
-
const verified = await Jwt.verifySignature(decoded.header, decoded.payload, decoded.signature, key);
|
|
792
|
-
return {
|
|
793
|
-
verified,
|
|
794
|
-
...decoded
|
|
795
|
-
};
|
|
923
|
+
Guards.defined(Jwt._CLASS_NAME, "key", key);
|
|
924
|
+
return Jwt.verifySignature(token, key);
|
|
796
925
|
}
|
|
797
926
|
/**
|
|
798
927
|
* Verify a token.
|
|
@@ -803,79 +932,131 @@ class Jwt {
|
|
|
803
932
|
static async verifyWithVerifier(token, verifier) {
|
|
804
933
|
Guards.stringValue(Jwt._CLASS_NAME, "token", token);
|
|
805
934
|
Guards.function(Jwt._CLASS_NAME, "verifier", verifier);
|
|
806
|
-
|
|
807
|
-
const decoded = await Jwt.decode(token);
|
|
808
|
-
const verified = await Jwt.verifySignature(decoded.header, decoded.payload, decoded.signature, undefined, verifier);
|
|
809
|
-
return {
|
|
810
|
-
verified,
|
|
811
|
-
...decoded
|
|
812
|
-
};
|
|
935
|
+
return Jwt.verifySignature(token, undefined, verifier);
|
|
813
936
|
}
|
|
814
937
|
/**
|
|
815
938
|
* Verify a token by parts.
|
|
816
|
-
* @param
|
|
817
|
-
* @param payload The payload to verify.
|
|
818
|
-
* @param signature The signature to verify.
|
|
939
|
+
* @param token The token to verify.
|
|
819
940
|
* @param key The key for verifying the token, if not provided no verification occurs.
|
|
820
941
|
* @param verifier Custom verification method.
|
|
821
942
|
* @returns True if the parts are verified.
|
|
822
943
|
*/
|
|
823
|
-
static async verifySignature(
|
|
944
|
+
static async verifySignature(token, key, verifier) {
|
|
945
|
+
Guards.stringValue(Jwt._CLASS_NAME, "token", token);
|
|
824
946
|
const hasKey = Is.notEmpty(key);
|
|
825
947
|
const hasVerifier = Is.notEmpty(verifier);
|
|
826
948
|
if (!hasKey && !hasVerifier) {
|
|
827
949
|
throw new GeneralError(Jwt._CLASS_NAME, "noKeyOrVerifier");
|
|
828
950
|
}
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
Is.object(payload) &&
|
|
832
|
-
Is.uint8Array(signature) &&
|
|
833
|
-
Is.arrayOneOf(header.alg, Object.values(JwtAlgorithms))) {
|
|
834
|
-
const segments = [];
|
|
835
|
-
const headerBytes = Converter.utf8ToBytes(JSON.stringify(header));
|
|
836
|
-
segments.push(Converter.bytesToBase64Url(headerBytes));
|
|
837
|
-
const payloadBytes = Converter.utf8ToBytes(JSON.stringify(payload));
|
|
838
|
-
segments.push(Converter.bytesToBase64Url(payloadBytes));
|
|
839
|
-
const jwtHeaderAndPayload = Converter.utf8ToBytes(segments.join("."));
|
|
840
|
-
verifier ??= async (alg, k, p, s) => Jwt.defaultVerifier(alg, k, p, s);
|
|
841
|
-
verified = await verifier(header.alg, key, jwtHeaderAndPayload, signature);
|
|
842
|
-
}
|
|
843
|
-
return verified;
|
|
951
|
+
verifier ??= async (t, k) => Jwt.defaultVerifier(t, k);
|
|
952
|
+
return verifier(token, key);
|
|
844
953
|
}
|
|
845
954
|
/**
|
|
846
955
|
* The default signer for the JWT.
|
|
847
|
-
* @param
|
|
848
|
-
* @param key The key to sign with.
|
|
956
|
+
* @param header The header to sign.
|
|
849
957
|
* @param payload The payload to sign.
|
|
958
|
+
* @param key The optional key to sign with.
|
|
850
959
|
* @returns The signature.
|
|
851
960
|
*/
|
|
852
|
-
static async defaultSigner(
|
|
853
|
-
Guards.
|
|
854
|
-
Guards.
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
961
|
+
static async defaultSigner(header, payload, key) {
|
|
962
|
+
Guards.object(Jwt._CLASS_NAME, "header", header);
|
|
963
|
+
Guards.object(Jwt._CLASS_NAME, "payload", payload);
|
|
964
|
+
Guards.defined(Jwt._CLASS_NAME, "key", key);
|
|
965
|
+
const signer = new SignJWT(payload);
|
|
966
|
+
signer.setProtectedHeader(header);
|
|
967
|
+
let finalKey = key;
|
|
968
|
+
if (header.alg === "EdDSA" && Is.uint8Array(key)) {
|
|
969
|
+
// Jose does not support Ed25519 keys in raw format, so we need to convert it to PKCS8.
|
|
970
|
+
finalKey = await Ed25519.privateKeyToPkcs8(key);
|
|
858
971
|
}
|
|
859
|
-
return
|
|
972
|
+
return signer.sign(finalKey);
|
|
860
973
|
}
|
|
861
974
|
/**
|
|
862
975
|
* The default verifier for the JWT.
|
|
863
|
-
* @param
|
|
976
|
+
* @param token The token to verify.
|
|
864
977
|
* @param key The key to verify with.
|
|
865
|
-
* @
|
|
866
|
-
* @param signature The signature to verify.
|
|
867
|
-
* @returns True if the signature was verified.
|
|
978
|
+
* @returns The header and payload if verification successful.
|
|
868
979
|
*/
|
|
869
|
-
static async defaultVerifier(
|
|
870
|
-
Guards.
|
|
871
|
-
Guards.
|
|
980
|
+
static async defaultVerifier(token, key) {
|
|
981
|
+
Guards.stringValue(Jwt._CLASS_NAME, "token", token);
|
|
982
|
+
Guards.defined(Jwt._CLASS_NAME, "key", key);
|
|
983
|
+
try {
|
|
984
|
+
const result = await jwtVerify(token, key);
|
|
985
|
+
return {
|
|
986
|
+
header: result.protectedHeader,
|
|
987
|
+
payload: result.payload
|
|
988
|
+
};
|
|
989
|
+
}
|
|
990
|
+
catch (err) {
|
|
991
|
+
throw new GeneralError(Jwt._CLASS_NAME, "verifyFailed", undefined, err);
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
/**
|
|
995
|
+
* Create bytes for signing from header and payload.
|
|
996
|
+
* @param header The header.
|
|
997
|
+
* @param payload The payload.
|
|
998
|
+
* @returns The bytes to sign.
|
|
999
|
+
*/
|
|
1000
|
+
static toSigningBytes(header, payload) {
|
|
1001
|
+
Guards.object(Jwt._CLASS_NAME, "header", header);
|
|
1002
|
+
Guards.object(Jwt._CLASS_NAME, "payload", payload);
|
|
1003
|
+
const segments = [];
|
|
1004
|
+
const headerBytes = Converter.utf8ToBytes(JSON.stringify(header));
|
|
1005
|
+
segments.push(Converter.bytesToBase64Url(headerBytes));
|
|
1006
|
+
const payloadBytes = Converter.utf8ToBytes(JSON.stringify(payload));
|
|
1007
|
+
segments.push(Converter.bytesToBase64Url(payloadBytes));
|
|
1008
|
+
return Converter.utf8ToBytes(segments.join("."));
|
|
1009
|
+
}
|
|
1010
|
+
/**
|
|
1011
|
+
* Create header and payload from signing bytes.
|
|
1012
|
+
* @param signingBytes The signing bytes from a token.
|
|
1013
|
+
* @returns The header and payload.
|
|
1014
|
+
* @throws If the signing bytes are invalid
|
|
1015
|
+
*/
|
|
1016
|
+
static fromSigningBytes(signingBytes) {
|
|
1017
|
+
Guards.uint8Array(Jwt._CLASS_NAME, "signingBytes", signingBytes);
|
|
1018
|
+
const segments = Converter.bytesToUtf8(signingBytes).split(".");
|
|
1019
|
+
if (segments.length !== 2) {
|
|
1020
|
+
throw new GeneralError(Jwt._CLASS_NAME, "invalidSigningBytes");
|
|
1021
|
+
}
|
|
1022
|
+
const headerBytes = Converter.base64UrlToBytes(segments[0]);
|
|
1023
|
+
const payloadBytes = Converter.base64UrlToBytes(segments[1]);
|
|
1024
|
+
return {
|
|
1025
|
+
header: ObjectHelper.fromBytes(headerBytes),
|
|
1026
|
+
payload: ObjectHelper.fromBytes(payloadBytes)
|
|
1027
|
+
};
|
|
1028
|
+
}
|
|
1029
|
+
/**
|
|
1030
|
+
* Convert signed bytes and signature bytes to token.
|
|
1031
|
+
* @param signingBytes The signed bytes.
|
|
1032
|
+
* @param signature The signature.
|
|
1033
|
+
* @returns The token.
|
|
1034
|
+
*/
|
|
1035
|
+
static tokenFromBytes(signingBytes, signature) {
|
|
1036
|
+
Guards.uint8Array(Jwt._CLASS_NAME, "signingBytes", signingBytes);
|
|
872
1037
|
Guards.uint8Array(Jwt._CLASS_NAME, "signature", signature);
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
1038
|
+
const signedBytesUtf8 = Converter.bytesToUtf8(signingBytes);
|
|
1039
|
+
const signatureBase64 = Converter.bytesToBase64Url(signature);
|
|
1040
|
+
return `${signedBytesUtf8}.${signatureBase64}`;
|
|
1041
|
+
}
|
|
1042
|
+
/**
|
|
1043
|
+
* Convert the token to signing bytes and signature bytes.
|
|
1044
|
+
* @param token The token to convert to bytes.
|
|
1045
|
+
* @returns The decoded bytes.
|
|
1046
|
+
* @throws If the token is invalid.
|
|
1047
|
+
*/
|
|
1048
|
+
static tokenToBytes(token) {
|
|
1049
|
+
Guards.stringValue(Jwt._CLASS_NAME, "token", token);
|
|
1050
|
+
const segments = token.split(".");
|
|
1051
|
+
if (segments.length !== 3) {
|
|
1052
|
+
throw new GeneralError(Jwt._CLASS_NAME, "invalidTokenParts");
|
|
877
1053
|
}
|
|
878
|
-
|
|
1054
|
+
const signingBytes = Converter.utf8ToBytes(`${segments[0]}.${segments[1]}`);
|
|
1055
|
+
const signature = Converter.base64UrlToBytes(segments[2]);
|
|
1056
|
+
return {
|
|
1057
|
+
signingBytes,
|
|
1058
|
+
signature
|
|
1059
|
+
};
|
|
879
1060
|
}
|
|
880
1061
|
/**
|
|
881
1062
|
* Encode a token.
|
|
@@ -892,19 +1073,11 @@ class Jwt {
|
|
|
892
1073
|
if (!hasKey && !hasSigner) {
|
|
893
1074
|
throw new GeneralError(Jwt._CLASS_NAME, "noKeyOrSigner");
|
|
894
1075
|
}
|
|
895
|
-
signer ??= async (
|
|
1076
|
+
signer ??= async (h, p, k) => Jwt.defaultSigner(h, p, k);
|
|
896
1077
|
if (Is.undefined(header.typ)) {
|
|
897
1078
|
header.typ = "JWT";
|
|
898
1079
|
}
|
|
899
|
-
|
|
900
|
-
const headerBytes = Converter.utf8ToBytes(JSON.stringify(header));
|
|
901
|
-
segments.push(Converter.bytesToBase64Url(headerBytes));
|
|
902
|
-
const payloadBytes = Converter.utf8ToBytes(JSON.stringify(payload));
|
|
903
|
-
segments.push(Converter.bytesToBase64Url(payloadBytes));
|
|
904
|
-
const jwtHeaderAndPayload = Converter.utf8ToBytes(segments.join("."));
|
|
905
|
-
const sigBytes = await signer(header.alg, key, jwtHeaderAndPayload);
|
|
906
|
-
segments.push(Converter.bytesToBase64Url(sigBytes));
|
|
907
|
-
return segments.join(".");
|
|
1080
|
+
return signer(header, payload, key);
|
|
908
1081
|
}
|
|
909
1082
|
}
|
|
910
1083
|
|
|
@@ -920,7 +1093,7 @@ class MimeTypeHelper {
|
|
|
920
1093
|
* @returns The mime type if detected.
|
|
921
1094
|
*/
|
|
922
1095
|
static async detect(data) {
|
|
923
|
-
if (!Is.uint8Array(data)) {
|
|
1096
|
+
if (!Is.uint8Array(data) || data.length === 0) {
|
|
924
1097
|
return undefined;
|
|
925
1098
|
}
|
|
926
1099
|
// Image
|
|
@@ -992,12 +1165,13 @@ class MimeTypeHelper {
|
|
|
992
1165
|
[MimeTypes.Javascript]: "js",
|
|
993
1166
|
[MimeTypes.Json]: "json",
|
|
994
1167
|
[MimeTypes.JsonLd]: "jsonld",
|
|
1168
|
+
[MimeTypes.Jwt]: "jwt",
|
|
995
1169
|
[MimeTypes.Xml]: "xml",
|
|
996
1170
|
[MimeTypes.OctetStream]: "bin",
|
|
997
1171
|
[MimeTypes.Gzip]: "gzip",
|
|
998
1172
|
[MimeTypes.Bzip2]: "bz2",
|
|
999
1173
|
[MimeTypes.Zip]: "zip",
|
|
1000
|
-
[MimeTypes.Pdf]: "
|
|
1174
|
+
[MimeTypes.Pdf]: "pdf",
|
|
1001
1175
|
[MimeTypes.Gif]: "gif",
|
|
1002
1176
|
[MimeTypes.Bmp]: "bmp",
|
|
1003
1177
|
[MimeTypes.Jpeg]: "jpeg",
|
|
@@ -1043,4 +1217,4 @@ class MimeTypeHelper {
|
|
|
1043
1217
|
}
|
|
1044
1218
|
}
|
|
1045
1219
|
|
|
1046
|
-
export { FetchError, FetchHelper, HeaderTypes, HttpMethod, HttpStatusCode,
|
|
1220
|
+
export { FetchError, FetchHelper, HeaderTypes, HttpMethod, HttpStatusCode, Jwk, Jws, Jwt, MimeTypeHelper, MimeTypes };
|
package/dist/types/index.d.ts
CHANGED
|
@@ -7,8 +7,10 @@ export * from "./models/IHttpHeaders";
|
|
|
7
7
|
export * from "./models/IJwk";
|
|
8
8
|
export * from "./models/IJwtHeader";
|
|
9
9
|
export * from "./models/IJwtPayload";
|
|
10
|
-
export * from "./models/
|
|
10
|
+
export * from "./models/jwkCryptoKey";
|
|
11
11
|
export * from "./models/mimeTypes";
|
|
12
12
|
export * from "./utils/fetchHelper";
|
|
13
|
+
export * from "./utils/jwk";
|
|
14
|
+
export * from "./utils/jws";
|
|
13
15
|
export * from "./utils/jwt";
|
|
14
16
|
export * from "./utils/mimeTypeHelper";
|
|
@@ -1,62 +1,6 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { JWK } from "jose";
|
|
2
2
|
/**
|
|
3
3
|
* The fields in a JSON Web Key.
|
|
4
4
|
*/
|
|
5
|
-
export interface IJwk {
|
|
6
|
-
/**
|
|
7
|
-
* Additional fields in the key.
|
|
8
|
-
*/
|
|
9
|
-
[key: string]: unknown;
|
|
10
|
-
/**
|
|
11
|
-
* The cryptographic algorithm for the key.
|
|
12
|
-
*/
|
|
13
|
-
alg?: JwtAlgorithms;
|
|
14
|
-
/**
|
|
15
|
-
* The intended use for the key.
|
|
16
|
-
*/
|
|
17
|
-
use?: string;
|
|
18
|
-
/**
|
|
19
|
-
* The operation(s) that the key is intended to be used for.
|
|
20
|
-
*/
|
|
21
|
-
key_ops?: string[];
|
|
22
|
-
/**
|
|
23
|
-
* The key type parameter.
|
|
24
|
-
*/
|
|
25
|
-
kty: string;
|
|
26
|
-
/**
|
|
27
|
-
* The public key parameters.
|
|
28
|
-
*/
|
|
29
|
-
n?: string;
|
|
30
|
-
/**
|
|
31
|
-
* Exponent parameter.
|
|
32
|
-
*/
|
|
33
|
-
e?: string;
|
|
34
|
-
/**
|
|
35
|
-
* The private key parameters.
|
|
36
|
-
*/
|
|
37
|
-
d?: string;
|
|
38
|
-
/**
|
|
39
|
-
* The private key parameters.
|
|
40
|
-
*/
|
|
41
|
-
p?: string;
|
|
42
|
-
/**
|
|
43
|
-
* The private key parameters.
|
|
44
|
-
*/
|
|
45
|
-
q?: string;
|
|
46
|
-
/**
|
|
47
|
-
* The private key parameters.
|
|
48
|
-
*/
|
|
49
|
-
dp?: string;
|
|
50
|
-
/**
|
|
51
|
-
* The private key parameters.
|
|
52
|
-
*/
|
|
53
|
-
dq?: string;
|
|
54
|
-
/**
|
|
55
|
-
* The private key parameters.
|
|
56
|
-
*/
|
|
57
|
-
qi?: string;
|
|
58
|
-
/**
|
|
59
|
-
* The key ID.
|
|
60
|
-
*/
|
|
61
|
-
kid?: string;
|
|
5
|
+
export interface IJwk extends JWK {
|
|
62
6
|
}
|
|
@@ -1,22 +1,6 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { JWTHeaderParameters } from "jose";
|
|
2
2
|
/**
|
|
3
3
|
* The fields in a JSON Web Token header.
|
|
4
4
|
*/
|
|
5
|
-
export interface IJwtHeader {
|
|
6
|
-
/**
|
|
7
|
-
* Additional fields in the header.
|
|
8
|
-
*/
|
|
9
|
-
[key: string]: unknown;
|
|
10
|
-
/**
|
|
11
|
-
* The type of the token.
|
|
12
|
-
*/
|
|
13
|
-
typ?: string;
|
|
14
|
-
/**
|
|
15
|
-
* The algorithm used to sign the token.
|
|
16
|
-
*/
|
|
17
|
-
alg: JwtAlgorithms;
|
|
18
|
-
/**
|
|
19
|
-
* The key ID.
|
|
20
|
-
*/
|
|
21
|
-
kid?: string;
|
|
5
|
+
export interface IJwtHeader extends JWTHeaderParameters {
|
|
22
6
|
}
|
|
@@ -1,37 +1,6 @@
|
|
|
1
|
+
import type { JWTPayload } from "jose";
|
|
1
2
|
/**
|
|
2
3
|
* The fields in a JSON Web Token payload.
|
|
3
4
|
*/
|
|
4
|
-
export interface IJwtPayload {
|
|
5
|
-
/**
|
|
6
|
-
* Additional fields in the payload.
|
|
7
|
-
*/
|
|
8
|
-
[key: string]: unknown;
|
|
9
|
-
/**
|
|
10
|
-
* The issuer of the token.
|
|
11
|
-
*/
|
|
12
|
-
iss?: string;
|
|
13
|
-
/**
|
|
14
|
-
* The subject of the token.
|
|
15
|
-
*/
|
|
16
|
-
sub?: string;
|
|
17
|
-
/**
|
|
18
|
-
* The audience of the token.
|
|
19
|
-
*/
|
|
20
|
-
aud?: string;
|
|
21
|
-
/**
|
|
22
|
-
* The expiration time of the token.
|
|
23
|
-
*/
|
|
24
|
-
exp?: number;
|
|
25
|
-
/**
|
|
26
|
-
* The not before time of the token.
|
|
27
|
-
*/
|
|
28
|
-
nbf?: number;
|
|
29
|
-
/**
|
|
30
|
-
* The issued at time of the token.
|
|
31
|
-
*/
|
|
32
|
-
iat?: number;
|
|
33
|
-
/**
|
|
34
|
-
* The JWT ID.
|
|
35
|
-
*/
|
|
36
|
-
jti?: string;
|
|
5
|
+
export interface IJwtPayload extends JWTPayload {
|
|
37
6
|
}
|