@twin.org/web 0.0.1-next.6 → 0.0.1-next.62
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 +274 -98
- package/dist/esm/index.mjs +275 -100
- 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 +35 -0
- package/dist/types/utils/jws.d.ts +22 -0
- package/dist/types/utils/jwt.d.ts +67 -29
- package/docs/changelog.md +205 -1
- package/docs/reference/classes/FetchError.md +16 -8
- package/docs/reference/classes/FetchHelper.md +104 -28
- package/docs/reference/classes/Jwk.md +107 -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/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
|
*/
|
|
@@ -553,7 +541,7 @@ class FetchHelper {
|
|
|
553
541
|
if (isErr && Is.stringValue(err.message) && err.message.includes("Failed to fetch")) {
|
|
554
542
|
lastError = new FetchError(source, `${FetchHelper._CLASS_NAME_CAMEL_CASE}.connectivity`, HttpStatusCode.serviceUnavailable, {
|
|
555
543
|
url
|
|
556
|
-
});
|
|
544
|
+
}, err);
|
|
557
545
|
}
|
|
558
546
|
else {
|
|
559
547
|
const isAbort = isErr && err.name === "AbortError";
|
|
@@ -568,7 +556,7 @@ class FetchHelper {
|
|
|
568
556
|
if (isErr && "statusText" in err) {
|
|
569
557
|
props.statusText = err.statusText;
|
|
570
558
|
}
|
|
571
|
-
lastError = new FetchError(source, `${FetchHelper._CLASS_NAME_CAMEL_CASE}.${isAbort ? "timeout" : "general"}`, httpStatus, props);
|
|
559
|
+
lastError = new FetchError(source, `${FetchHelper._CLASS_NAME_CAMEL_CASE}.${isAbort ? "timeout" : "general"}`, httpStatus, props, err);
|
|
572
560
|
}
|
|
573
561
|
}
|
|
574
562
|
finally {
|
|
@@ -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,146 @@ 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
|
+
* @param alg The alg to be used, defaults to jwk.alg.
|
|
720
|
+
* @returns The crypto key.
|
|
721
|
+
*/
|
|
722
|
+
static async toCryptoKey(jwk, alg) {
|
|
723
|
+
Guards.object(Jwk._CLASS_NAME, "jwk", jwk);
|
|
724
|
+
try {
|
|
725
|
+
return importJWK(jwk, alg);
|
|
726
|
+
}
|
|
727
|
+
catch (err) {
|
|
728
|
+
throw new GeneralError(Jwk._CLASS_NAME, "jwkImportFailed", undefined, err);
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
/**
|
|
732
|
+
* Convert the Ed25519 private key to a crypto key.
|
|
733
|
+
* @param privateKey The private key to use.
|
|
734
|
+
* @returns The crypto key.
|
|
735
|
+
*/
|
|
736
|
+
static async fromEd25519Private(privateKey) {
|
|
737
|
+
Guards.uint8Array(Jwk._CLASS_NAME, "privateKey", privateKey);
|
|
738
|
+
const publicKey = Ed25519.publicKeyFromPrivateKey(privateKey);
|
|
739
|
+
const jwk = {
|
|
740
|
+
kty: "OKP",
|
|
741
|
+
use: "enc",
|
|
742
|
+
alg: "EdDSA",
|
|
743
|
+
crv: "Ed25519",
|
|
744
|
+
x: Converter.bytesToBase64Url(publicKey),
|
|
745
|
+
d: Converter.bytesToBase64Url(privateKey)
|
|
746
|
+
};
|
|
747
|
+
return jwk;
|
|
748
|
+
}
|
|
749
|
+
/**
|
|
750
|
+
* Convert the Ed25519 public key to a crypto key.
|
|
751
|
+
* @param publicKey The private key to use.
|
|
752
|
+
* @returns The crypto key.
|
|
753
|
+
*/
|
|
754
|
+
static async fromEd25519Public(publicKey) {
|
|
755
|
+
Guards.uint8Array(Jwk._CLASS_NAME, "publicKey", publicKey);
|
|
756
|
+
const jwk = {
|
|
757
|
+
kty: "OKP",
|
|
758
|
+
use: "sig",
|
|
759
|
+
alg: "EdDSA",
|
|
760
|
+
crv: "Ed25519",
|
|
761
|
+
x: Converter.bytesToBase64Url(publicKey)
|
|
762
|
+
};
|
|
763
|
+
return jwk;
|
|
764
|
+
}
|
|
765
|
+
/**
|
|
766
|
+
* Convert the JWK to raw keys.
|
|
767
|
+
* @param jwk The JWK to convert to raw.
|
|
768
|
+
* @returns The crypto key.
|
|
769
|
+
*/
|
|
770
|
+
static async toRaw(jwk) {
|
|
771
|
+
Guards.object(Jwk._CLASS_NAME, "jwk", jwk);
|
|
772
|
+
let publicKey;
|
|
773
|
+
let privateKey;
|
|
774
|
+
if (Is.stringBase64Url(jwk.x)) {
|
|
775
|
+
publicKey = Converter.base64UrlToBytes(jwk.x);
|
|
776
|
+
}
|
|
777
|
+
if (Is.stringBase64Url(jwk.d)) {
|
|
778
|
+
privateKey = Converter.base64UrlToBytes(jwk.d);
|
|
779
|
+
}
|
|
780
|
+
return {
|
|
781
|
+
publicKey,
|
|
782
|
+
privateKey
|
|
783
|
+
};
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
// Copyright 2024 IOTA Stiftung.
|
|
788
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
789
|
+
/**
|
|
790
|
+
* Class to handle JSON Web Signatures.
|
|
791
|
+
*/
|
|
792
|
+
class Jws {
|
|
793
|
+
/**
|
|
794
|
+
* Runtime name for the class.
|
|
795
|
+
* @internal
|
|
796
|
+
*/
|
|
797
|
+
static _CLASS_NAME = "Jws";
|
|
798
|
+
/**
|
|
799
|
+
* Create a signature.
|
|
800
|
+
* @param privateKey The private key to use.
|
|
801
|
+
* @param hash The hash to sign.
|
|
802
|
+
* @param algOverride An optional algorithm override.
|
|
803
|
+
* @returns The signature.
|
|
804
|
+
*/
|
|
805
|
+
static async create(privateKey, hash, algOverride) {
|
|
806
|
+
Guards.defined(Jws._CLASS_NAME, "privateKey", privateKey);
|
|
807
|
+
Guards.uint8Array(Jws._CLASS_NAME, "hash", hash);
|
|
808
|
+
try {
|
|
809
|
+
const jws = await new CompactSign(hash)
|
|
810
|
+
.setProtectedHeader({
|
|
811
|
+
alg: algOverride ?? (Is.uint8Array(privateKey) ? "EdDSA" : privateKey.algorithm.name),
|
|
812
|
+
b64: false,
|
|
813
|
+
crit: ["b64"]
|
|
814
|
+
})
|
|
815
|
+
.sign(privateKey);
|
|
816
|
+
return jws;
|
|
817
|
+
}
|
|
818
|
+
catch (err) {
|
|
819
|
+
throw new GeneralError(Jws._CLASS_NAME, "createFailed", undefined, err);
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
/**
|
|
823
|
+
* Verify a signature.
|
|
824
|
+
* @param jws The signature to verify.
|
|
825
|
+
* @param publicKey The public key to verify the signature with.
|
|
826
|
+
* @param hash The hash to verify.
|
|
827
|
+
* @returns True if the signature was verified.
|
|
828
|
+
*/
|
|
829
|
+
static async verify(jws, publicKey, hash) {
|
|
830
|
+
Guards.stringValue(Jws._CLASS_NAME, "jws", jws);
|
|
831
|
+
Guards.defined(Jws._CLASS_NAME, "publicKey", publicKey);
|
|
832
|
+
Guards.uint8Array(Jws._CLASS_NAME, "hash", hash);
|
|
833
|
+
try {
|
|
834
|
+
const jwsParts = jws.split(".");
|
|
835
|
+
await flattenedVerify({ protected: jwsParts[0], payload: hash, signature: jwsParts[2] }, publicKey);
|
|
836
|
+
return true;
|
|
837
|
+
}
|
|
838
|
+
catch (err) {
|
|
839
|
+
throw new GeneralError(Jws._CLASS_NAME, "verifyFailed", undefined, err);
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
// Copyright 2024 IOTA Stiftung.
|
|
845
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
846
|
+
/**
|
|
847
|
+
* Class to handle JSON Web Tokens.
|
|
712
848
|
*/
|
|
713
849
|
class Jwt {
|
|
714
850
|
/**
|
|
@@ -725,9 +861,8 @@ class Jwt {
|
|
|
725
861
|
*/
|
|
726
862
|
static async encode(header, payload, key) {
|
|
727
863
|
Guards.object(Jwt._CLASS_NAME, "header", header);
|
|
728
|
-
Guards.arrayOneOf(Jwt._CLASS_NAME, "header.alg", header.alg, Object.values(JwtAlgorithms));
|
|
729
864
|
Guards.object(Jwt._CLASS_NAME, "payload", payload);
|
|
730
|
-
Guards.
|
|
865
|
+
Guards.defined(Jwt._CLASS_NAME, "key", key);
|
|
731
866
|
return Jwt.internalEncode(header, payload, key);
|
|
732
867
|
}
|
|
733
868
|
/**
|
|
@@ -739,7 +874,7 @@ class Jwt {
|
|
|
739
874
|
*/
|
|
740
875
|
static async encodeWithSigner(header, payload, signer) {
|
|
741
876
|
Guards.object(Jwt._CLASS_NAME, "header", header);
|
|
742
|
-
Guards.
|
|
877
|
+
Guards.stringValue(Jwt._CLASS_NAME, "header.alg", header.alg);
|
|
743
878
|
Guards.object(Jwt._CLASS_NAME, "payload", payload);
|
|
744
879
|
Guards.function(Jwt._CLASS_NAME, "signer", signer);
|
|
745
880
|
return Jwt.internalEncode(header, payload, undefined, signer);
|
|
@@ -786,13 +921,8 @@ class Jwt {
|
|
|
786
921
|
*/
|
|
787
922
|
static async verify(token, key) {
|
|
788
923
|
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
|
-
};
|
|
924
|
+
Guards.defined(Jwt._CLASS_NAME, "key", key);
|
|
925
|
+
return Jwt.verifySignature(token, key);
|
|
796
926
|
}
|
|
797
927
|
/**
|
|
798
928
|
* Verify a token.
|
|
@@ -803,79 +933,131 @@ class Jwt {
|
|
|
803
933
|
static async verifyWithVerifier(token, verifier) {
|
|
804
934
|
Guards.stringValue(Jwt._CLASS_NAME, "token", token);
|
|
805
935
|
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
|
-
};
|
|
936
|
+
return Jwt.verifySignature(token, undefined, verifier);
|
|
813
937
|
}
|
|
814
938
|
/**
|
|
815
939
|
* Verify a token by parts.
|
|
816
|
-
* @param
|
|
817
|
-
* @param payload The payload to verify.
|
|
818
|
-
* @param signature The signature to verify.
|
|
940
|
+
* @param token The token to verify.
|
|
819
941
|
* @param key The key for verifying the token, if not provided no verification occurs.
|
|
820
942
|
* @param verifier Custom verification method.
|
|
821
943
|
* @returns True if the parts are verified.
|
|
822
944
|
*/
|
|
823
|
-
static async verifySignature(
|
|
945
|
+
static async verifySignature(token, key, verifier) {
|
|
946
|
+
Guards.stringValue(Jwt._CLASS_NAME, "token", token);
|
|
824
947
|
const hasKey = Is.notEmpty(key);
|
|
825
948
|
const hasVerifier = Is.notEmpty(verifier);
|
|
826
949
|
if (!hasKey && !hasVerifier) {
|
|
827
950
|
throw new GeneralError(Jwt._CLASS_NAME, "noKeyOrVerifier");
|
|
828
951
|
}
|
|
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;
|
|
952
|
+
verifier ??= async (t, k) => Jwt.defaultVerifier(t, k);
|
|
953
|
+
return verifier(token, key);
|
|
844
954
|
}
|
|
845
955
|
/**
|
|
846
956
|
* The default signer for the JWT.
|
|
847
|
-
* @param
|
|
848
|
-
* @param key The key to sign with.
|
|
957
|
+
* @param header The header to sign.
|
|
849
958
|
* @param payload The payload to sign.
|
|
959
|
+
* @param key The optional key to sign with.
|
|
850
960
|
* @returns The signature.
|
|
851
961
|
*/
|
|
852
|
-
static async defaultSigner(
|
|
853
|
-
Guards.
|
|
854
|
-
Guards.
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
962
|
+
static async defaultSigner(header, payload, key) {
|
|
963
|
+
Guards.object(Jwt._CLASS_NAME, "header", header);
|
|
964
|
+
Guards.object(Jwt._CLASS_NAME, "payload", payload);
|
|
965
|
+
Guards.defined(Jwt._CLASS_NAME, "key", key);
|
|
966
|
+
const signer = new SignJWT(payload);
|
|
967
|
+
signer.setProtectedHeader(header);
|
|
968
|
+
let finalKey = key;
|
|
969
|
+
if (header.alg === "EdDSA" && Is.uint8Array(key)) {
|
|
970
|
+
// Jose does not support Ed25519 keys in raw format, so we need to convert it to PKCS8.
|
|
971
|
+
finalKey = await Ed25519.privateKeyToPkcs8(key);
|
|
858
972
|
}
|
|
859
|
-
return
|
|
973
|
+
return signer.sign(finalKey);
|
|
860
974
|
}
|
|
861
975
|
/**
|
|
862
976
|
* The default verifier for the JWT.
|
|
863
|
-
* @param
|
|
977
|
+
* @param token The token to verify.
|
|
864
978
|
* @param key The key to verify with.
|
|
865
|
-
* @
|
|
866
|
-
|
|
867
|
-
|
|
979
|
+
* @returns The header and payload if verification successful.
|
|
980
|
+
*/
|
|
981
|
+
static async defaultVerifier(token, key) {
|
|
982
|
+
Guards.stringValue(Jwt._CLASS_NAME, "token", token);
|
|
983
|
+
Guards.defined(Jwt._CLASS_NAME, "key", key);
|
|
984
|
+
try {
|
|
985
|
+
const result = await jwtVerify(token, key);
|
|
986
|
+
return {
|
|
987
|
+
header: result.protectedHeader,
|
|
988
|
+
payload: result.payload
|
|
989
|
+
};
|
|
990
|
+
}
|
|
991
|
+
catch (err) {
|
|
992
|
+
throw new GeneralError(Jwt._CLASS_NAME, "verifyFailed", undefined, err);
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
/**
|
|
996
|
+
* Create bytes for signing from header and payload.
|
|
997
|
+
* @param header The header.
|
|
998
|
+
* @param payload The payload.
|
|
999
|
+
* @returns The bytes to sign.
|
|
868
1000
|
*/
|
|
869
|
-
static
|
|
870
|
-
Guards.
|
|
871
|
-
Guards.
|
|
1001
|
+
static toSigningBytes(header, payload) {
|
|
1002
|
+
Guards.object(Jwt._CLASS_NAME, "header", header);
|
|
1003
|
+
Guards.object(Jwt._CLASS_NAME, "payload", payload);
|
|
1004
|
+
const segments = [];
|
|
1005
|
+
const headerBytes = Converter.utf8ToBytes(JSON.stringify(header));
|
|
1006
|
+
segments.push(Converter.bytesToBase64Url(headerBytes));
|
|
1007
|
+
const payloadBytes = Converter.utf8ToBytes(JSON.stringify(payload));
|
|
1008
|
+
segments.push(Converter.bytesToBase64Url(payloadBytes));
|
|
1009
|
+
return Converter.utf8ToBytes(segments.join("."));
|
|
1010
|
+
}
|
|
1011
|
+
/**
|
|
1012
|
+
* Create header and payload from signing bytes.
|
|
1013
|
+
* @param signingBytes The signing bytes from a token.
|
|
1014
|
+
* @returns The header and payload.
|
|
1015
|
+
* @throws If the signing bytes are invalid
|
|
1016
|
+
*/
|
|
1017
|
+
static fromSigningBytes(signingBytes) {
|
|
1018
|
+
Guards.uint8Array(Jwt._CLASS_NAME, "signingBytes", signingBytes);
|
|
1019
|
+
const segments = Converter.bytesToUtf8(signingBytes).split(".");
|
|
1020
|
+
if (segments.length !== 2) {
|
|
1021
|
+
throw new GeneralError(Jwt._CLASS_NAME, "invalidSigningBytes");
|
|
1022
|
+
}
|
|
1023
|
+
const headerBytes = Converter.base64UrlToBytes(segments[0]);
|
|
1024
|
+
const payloadBytes = Converter.base64UrlToBytes(segments[1]);
|
|
1025
|
+
return {
|
|
1026
|
+
header: ObjectHelper.fromBytes(headerBytes),
|
|
1027
|
+
payload: ObjectHelper.fromBytes(payloadBytes)
|
|
1028
|
+
};
|
|
1029
|
+
}
|
|
1030
|
+
/**
|
|
1031
|
+
* Convert signed bytes and signature bytes to token.
|
|
1032
|
+
* @param signingBytes The signed bytes.
|
|
1033
|
+
* @param signature The signature.
|
|
1034
|
+
* @returns The token.
|
|
1035
|
+
*/
|
|
1036
|
+
static tokenFromBytes(signingBytes, signature) {
|
|
1037
|
+
Guards.uint8Array(Jwt._CLASS_NAME, "signingBytes", signingBytes);
|
|
872
1038
|
Guards.uint8Array(Jwt._CLASS_NAME, "signature", signature);
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
1039
|
+
const signedBytesUtf8 = Converter.bytesToUtf8(signingBytes);
|
|
1040
|
+
const signatureBase64 = Converter.bytesToBase64Url(signature);
|
|
1041
|
+
return `${signedBytesUtf8}.${signatureBase64}`;
|
|
1042
|
+
}
|
|
1043
|
+
/**
|
|
1044
|
+
* Convert the token to signing bytes and signature bytes.
|
|
1045
|
+
* @param token The token to convert to bytes.
|
|
1046
|
+
* @returns The decoded bytes.
|
|
1047
|
+
* @throws If the token is invalid.
|
|
1048
|
+
*/
|
|
1049
|
+
static tokenToBytes(token) {
|
|
1050
|
+
Guards.stringValue(Jwt._CLASS_NAME, "token", token);
|
|
1051
|
+
const segments = token.split(".");
|
|
1052
|
+
if (segments.length !== 3) {
|
|
1053
|
+
throw new GeneralError(Jwt._CLASS_NAME, "invalidTokenParts");
|
|
877
1054
|
}
|
|
878
|
-
|
|
1055
|
+
const signingBytes = Converter.utf8ToBytes(`${segments[0]}.${segments[1]}`);
|
|
1056
|
+
const signature = Converter.base64UrlToBytes(segments[2]);
|
|
1057
|
+
return {
|
|
1058
|
+
signingBytes,
|
|
1059
|
+
signature
|
|
1060
|
+
};
|
|
879
1061
|
}
|
|
880
1062
|
/**
|
|
881
1063
|
* Encode a token.
|
|
@@ -892,19 +1074,11 @@ class Jwt {
|
|
|
892
1074
|
if (!hasKey && !hasSigner) {
|
|
893
1075
|
throw new GeneralError(Jwt._CLASS_NAME, "noKeyOrSigner");
|
|
894
1076
|
}
|
|
895
|
-
signer ??= async (
|
|
1077
|
+
signer ??= async (h, p, k) => Jwt.defaultSigner(h, p, k);
|
|
896
1078
|
if (Is.undefined(header.typ)) {
|
|
897
1079
|
header.typ = "JWT";
|
|
898
1080
|
}
|
|
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(".");
|
|
1081
|
+
return signer(header, payload, key);
|
|
908
1082
|
}
|
|
909
1083
|
}
|
|
910
1084
|
|
|
@@ -920,7 +1094,7 @@ class MimeTypeHelper {
|
|
|
920
1094
|
* @returns The mime type if detected.
|
|
921
1095
|
*/
|
|
922
1096
|
static async detect(data) {
|
|
923
|
-
if (!Is.uint8Array(data)) {
|
|
1097
|
+
if (!Is.uint8Array(data) || data.length === 0) {
|
|
924
1098
|
return undefined;
|
|
925
1099
|
}
|
|
926
1100
|
// Image
|
|
@@ -992,12 +1166,13 @@ class MimeTypeHelper {
|
|
|
992
1166
|
[MimeTypes.Javascript]: "js",
|
|
993
1167
|
[MimeTypes.Json]: "json",
|
|
994
1168
|
[MimeTypes.JsonLd]: "jsonld",
|
|
1169
|
+
[MimeTypes.Jwt]: "jwt",
|
|
995
1170
|
[MimeTypes.Xml]: "xml",
|
|
996
1171
|
[MimeTypes.OctetStream]: "bin",
|
|
997
1172
|
[MimeTypes.Gzip]: "gzip",
|
|
998
1173
|
[MimeTypes.Bzip2]: "bz2",
|
|
999
1174
|
[MimeTypes.Zip]: "zip",
|
|
1000
|
-
[MimeTypes.Pdf]: "
|
|
1175
|
+
[MimeTypes.Pdf]: "pdf",
|
|
1001
1176
|
[MimeTypes.Gif]: "gif",
|
|
1002
1177
|
[MimeTypes.Bmp]: "bmp",
|
|
1003
1178
|
[MimeTypes.Jpeg]: "jpeg",
|
|
@@ -1043,4 +1218,4 @@ class MimeTypeHelper {
|
|
|
1043
1218
|
}
|
|
1044
1219
|
}
|
|
1045
1220
|
|
|
1046
|
-
export { FetchError, FetchHelper, HeaderTypes, HttpMethod, HttpStatusCode,
|
|
1221
|
+
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
|
}
|