@twin.org/web 0.0.1-next.9 → 0.0.2-next.3
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 +298 -94
- package/dist/esm/index.mjs +299 -96
- 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 +480 -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/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, JsonHelper } from '@twin.org/core';
|
|
2
|
+
import { Ed25519, Sha256 } 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.
|
|
@@ -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
|
*/
|
|
@@ -399,6 +387,10 @@ const MimeTypes = {
|
|
|
399
387
|
* Application GZIP - application/gzip
|
|
400
388
|
*/
|
|
401
389
|
Gzip: "application/gzip",
|
|
390
|
+
/**
|
|
391
|
+
* Application deflate - application/zlib
|
|
392
|
+
*/
|
|
393
|
+
Zlib: "application/zlib",
|
|
402
394
|
/**
|
|
403
395
|
* Application BZIP2 - application/x-bzip2
|
|
404
396
|
*/
|
|
@@ -553,7 +545,7 @@ class FetchHelper {
|
|
|
553
545
|
if (isErr && Is.stringValue(err.message) && err.message.includes("Failed to fetch")) {
|
|
554
546
|
lastError = new FetchError(source, `${FetchHelper._CLASS_NAME_CAMEL_CASE}.connectivity`, HttpStatusCode.serviceUnavailable, {
|
|
555
547
|
url
|
|
556
|
-
});
|
|
548
|
+
}, err);
|
|
557
549
|
}
|
|
558
550
|
else {
|
|
559
551
|
const isAbort = isErr && err.name === "AbortError";
|
|
@@ -568,7 +560,7 @@ class FetchHelper {
|
|
|
568
560
|
if (isErr && "statusText" in err) {
|
|
569
561
|
props.statusText = err.statusText;
|
|
570
562
|
}
|
|
571
|
-
lastError = new FetchError(source, `${FetchHelper._CLASS_NAME_CAMEL_CASE}.${isAbort ? "timeout" : "general"}`, httpStatus, props);
|
|
563
|
+
lastError = new FetchError(source, `${FetchHelper._CLASS_NAME_CAMEL_CASE}.${isAbort ? "timeout" : "general"}`, httpStatus, props, err);
|
|
572
564
|
}
|
|
573
565
|
}
|
|
574
566
|
finally {
|
|
@@ -625,12 +617,15 @@ class FetchHelper {
|
|
|
625
617
|
}
|
|
626
618
|
}
|
|
627
619
|
const errorResponseData = await response.json();
|
|
620
|
+
const errorResponse = BaseError.fromError(errorResponseData);
|
|
621
|
+
const isErrorEmpty = BaseError.isEmpty(errorResponse);
|
|
628
622
|
// False positive as FetchError is derived from Error
|
|
629
623
|
// eslint-disable-next-line @typescript-eslint/only-throw-error
|
|
630
624
|
throw new FetchError(source, `${FetchHelper._CLASS_NAME_CAMEL_CASE}.failureStatusText`, response.status, {
|
|
631
625
|
statusText: response.statusText,
|
|
632
|
-
url
|
|
633
|
-
|
|
626
|
+
url,
|
|
627
|
+
data: isErrorEmpty ? errorResponseData : undefined
|
|
628
|
+
}, isErrorEmpty ? undefined : errorResponse);
|
|
634
629
|
}
|
|
635
630
|
/**
|
|
636
631
|
* Perform a request for binary data.
|
|
@@ -675,12 +670,15 @@ class FetchHelper {
|
|
|
675
670
|
}
|
|
676
671
|
}
|
|
677
672
|
const errorResponseData = await response.json();
|
|
673
|
+
const errorResponse = BaseError.fromError(errorResponseData);
|
|
674
|
+
const isErrorEmpty = BaseError.isEmpty(errorResponse);
|
|
678
675
|
// False positive as FetchError is derived from Error
|
|
679
676
|
// eslint-disable-next-line @typescript-eslint/only-throw-error
|
|
680
677
|
throw new FetchError(source, `${FetchHelper._CLASS_NAME_CAMEL_CASE}.failureStatusText`, response.status, {
|
|
681
678
|
statusText: response.statusText,
|
|
682
|
-
url
|
|
683
|
-
|
|
679
|
+
url,
|
|
680
|
+
data: isErrorEmpty ? errorResponseData : undefined
|
|
681
|
+
}, isErrorEmpty ? undefined : errorResponse);
|
|
684
682
|
}
|
|
685
683
|
/**
|
|
686
684
|
* Clears the cache.
|
|
@@ -696,6 +694,15 @@ class FetchHelper {
|
|
|
696
694
|
static async getCacheEntry(url) {
|
|
697
695
|
return AsyncCache.get(`${FetchHelper._CACHE_PREFIX}${url}`);
|
|
698
696
|
}
|
|
697
|
+
/**
|
|
698
|
+
* Set a cache entry.
|
|
699
|
+
* @param url The url for the request.
|
|
700
|
+
* @param value The value to cache.
|
|
701
|
+
* @returns The cache entry if it exists.
|
|
702
|
+
*/
|
|
703
|
+
static async setCacheEntry(url, value) {
|
|
704
|
+
AsyncCache.set(`${FetchHelper._CACHE_PREFIX}${url}`, value);
|
|
705
|
+
}
|
|
699
706
|
/**
|
|
700
707
|
* Remove a cache entry.
|
|
701
708
|
* @param url The url for the request.
|
|
@@ -708,7 +715,158 @@ class FetchHelper {
|
|
|
708
715
|
// Copyright 2024 IOTA Stiftung.
|
|
709
716
|
// SPDX-License-Identifier: Apache-2.0.
|
|
710
717
|
/**
|
|
711
|
-
* Class to
|
|
718
|
+
* Class to handle JSON Web Keys.
|
|
719
|
+
*/
|
|
720
|
+
class Jwk {
|
|
721
|
+
/**
|
|
722
|
+
* Runtime name for the class.
|
|
723
|
+
* @internal
|
|
724
|
+
*/
|
|
725
|
+
static _CLASS_NAME = "Jwk";
|
|
726
|
+
/**
|
|
727
|
+
* Convert the JWK to a crypto key.
|
|
728
|
+
* @param jwk The JWK to convert.
|
|
729
|
+
* @param alg The alg to be used, defaults to jwk.alg.
|
|
730
|
+
* @returns The crypto key.
|
|
731
|
+
*/
|
|
732
|
+
static async toCryptoKey(jwk, alg) {
|
|
733
|
+
Guards.object(Jwk._CLASS_NAME, "jwk", jwk);
|
|
734
|
+
try {
|
|
735
|
+
return importJWK(jwk, alg);
|
|
736
|
+
}
|
|
737
|
+
catch (err) {
|
|
738
|
+
throw new GeneralError(Jwk._CLASS_NAME, "jwkImportFailed", undefined, err);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
/**
|
|
742
|
+
* Convert the Ed25519 private key to a crypto key.
|
|
743
|
+
* @param privateKey The private key to use.
|
|
744
|
+
* @returns The crypto key.
|
|
745
|
+
*/
|
|
746
|
+
static async fromEd25519Private(privateKey) {
|
|
747
|
+
Guards.uint8Array(Jwk._CLASS_NAME, "privateKey", privateKey);
|
|
748
|
+
const publicKey = Ed25519.publicKeyFromPrivateKey(privateKey);
|
|
749
|
+
const jwk = {
|
|
750
|
+
kty: "OKP",
|
|
751
|
+
use: "enc",
|
|
752
|
+
alg: "EdDSA",
|
|
753
|
+
crv: "Ed25519",
|
|
754
|
+
x: Converter.bytesToBase64Url(publicKey),
|
|
755
|
+
d: Converter.bytesToBase64Url(privateKey)
|
|
756
|
+
};
|
|
757
|
+
return jwk;
|
|
758
|
+
}
|
|
759
|
+
/**
|
|
760
|
+
* Convert the Ed25519 public key to a crypto key.
|
|
761
|
+
* @param publicKey The private key to use.
|
|
762
|
+
* @returns The crypto key.
|
|
763
|
+
*/
|
|
764
|
+
static async fromEd25519Public(publicKey) {
|
|
765
|
+
Guards.uint8Array(Jwk._CLASS_NAME, "publicKey", publicKey);
|
|
766
|
+
const jwk = {
|
|
767
|
+
kty: "OKP",
|
|
768
|
+
use: "sig",
|
|
769
|
+
alg: "EdDSA",
|
|
770
|
+
crv: "Ed25519",
|
|
771
|
+
x: Converter.bytesToBase64Url(publicKey)
|
|
772
|
+
};
|
|
773
|
+
return jwk;
|
|
774
|
+
}
|
|
775
|
+
/**
|
|
776
|
+
* Convert the JWK to raw keys.
|
|
777
|
+
* @param jwk The JWK to convert to raw.
|
|
778
|
+
* @returns The crypto key.
|
|
779
|
+
*/
|
|
780
|
+
static async toRaw(jwk) {
|
|
781
|
+
Guards.object(Jwk._CLASS_NAME, "jwk", jwk);
|
|
782
|
+
let publicKey;
|
|
783
|
+
let privateKey;
|
|
784
|
+
if (Is.stringBase64Url(jwk.x)) {
|
|
785
|
+
publicKey = Converter.base64UrlToBytes(jwk.x);
|
|
786
|
+
}
|
|
787
|
+
if (Is.stringBase64Url(jwk.d)) {
|
|
788
|
+
privateKey = Converter.base64UrlToBytes(jwk.d);
|
|
789
|
+
}
|
|
790
|
+
return {
|
|
791
|
+
publicKey,
|
|
792
|
+
privateKey
|
|
793
|
+
};
|
|
794
|
+
}
|
|
795
|
+
/**
|
|
796
|
+
* Generate a KID for the JWK.
|
|
797
|
+
* @param jwk The JWK to generate a KID for.
|
|
798
|
+
* @returns The KID.
|
|
799
|
+
*/
|
|
800
|
+
static async generateKid(jwk) {
|
|
801
|
+
Guards.object(Jwk._CLASS_NAME, "jwk", jwk);
|
|
802
|
+
const kidProps = ObjectHelper.pick(jwk, ["crv", "kty", "x"]);
|
|
803
|
+
const canonicalJson = JsonHelper.canonicalize(kidProps);
|
|
804
|
+
const hash = Sha256.sum256(Converter.utf8ToBytes(canonicalJson));
|
|
805
|
+
return Converter.bytesToBase64Url(hash);
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
// Copyright 2024 IOTA Stiftung.
|
|
810
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
811
|
+
/**
|
|
812
|
+
* Class to handle JSON Web Signatures.
|
|
813
|
+
*/
|
|
814
|
+
class Jws {
|
|
815
|
+
/**
|
|
816
|
+
* Runtime name for the class.
|
|
817
|
+
* @internal
|
|
818
|
+
*/
|
|
819
|
+
static _CLASS_NAME = "Jws";
|
|
820
|
+
/**
|
|
821
|
+
* Create a signature.
|
|
822
|
+
* @param privateKey The private key to use.
|
|
823
|
+
* @param hash The hash to sign.
|
|
824
|
+
* @param algOverride An optional algorithm override.
|
|
825
|
+
* @returns The signature.
|
|
826
|
+
*/
|
|
827
|
+
static async create(privateKey, hash, algOverride) {
|
|
828
|
+
Guards.defined(Jws._CLASS_NAME, "privateKey", privateKey);
|
|
829
|
+
Guards.uint8Array(Jws._CLASS_NAME, "hash", hash);
|
|
830
|
+
try {
|
|
831
|
+
const jws = await new CompactSign(hash)
|
|
832
|
+
.setProtectedHeader({
|
|
833
|
+
alg: algOverride ?? (Is.uint8Array(privateKey) ? "EdDSA" : privateKey.algorithm.name),
|
|
834
|
+
b64: false,
|
|
835
|
+
crit: ["b64"]
|
|
836
|
+
})
|
|
837
|
+
.sign(privateKey);
|
|
838
|
+
return jws;
|
|
839
|
+
}
|
|
840
|
+
catch (err) {
|
|
841
|
+
throw new GeneralError(Jws._CLASS_NAME, "createFailed", undefined, err);
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
/**
|
|
845
|
+
* Verify a signature.
|
|
846
|
+
* @param jws The signature to verify.
|
|
847
|
+
* @param publicKey The public key to verify the signature with.
|
|
848
|
+
* @param hash The hash to verify.
|
|
849
|
+
* @returns True if the signature was verified.
|
|
850
|
+
*/
|
|
851
|
+
static async verify(jws, publicKey, hash) {
|
|
852
|
+
Guards.stringValue(Jws._CLASS_NAME, "jws", jws);
|
|
853
|
+
Guards.defined(Jws._CLASS_NAME, "publicKey", publicKey);
|
|
854
|
+
Guards.uint8Array(Jws._CLASS_NAME, "hash", hash);
|
|
855
|
+
try {
|
|
856
|
+
const jwsParts = jws.split(".");
|
|
857
|
+
await flattenedVerify({ protected: jwsParts[0], payload: hash, signature: jwsParts[2] }, publicKey);
|
|
858
|
+
return true;
|
|
859
|
+
}
|
|
860
|
+
catch (err) {
|
|
861
|
+
throw new GeneralError(Jws._CLASS_NAME, "verifyFailed", undefined, err);
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
// Copyright 2024 IOTA Stiftung.
|
|
867
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
868
|
+
/**
|
|
869
|
+
* Class to handle JSON Web Tokens.
|
|
712
870
|
*/
|
|
713
871
|
class Jwt {
|
|
714
872
|
/**
|
|
@@ -725,9 +883,8 @@ class Jwt {
|
|
|
725
883
|
*/
|
|
726
884
|
static async encode(header, payload, key) {
|
|
727
885
|
Guards.object(Jwt._CLASS_NAME, "header", header);
|
|
728
|
-
Guards.arrayOneOf(Jwt._CLASS_NAME, "header.alg", header.alg, Object.values(JwtAlgorithms));
|
|
729
886
|
Guards.object(Jwt._CLASS_NAME, "payload", payload);
|
|
730
|
-
Guards.
|
|
887
|
+
Guards.defined(Jwt._CLASS_NAME, "key", key);
|
|
731
888
|
return Jwt.internalEncode(header, payload, key);
|
|
732
889
|
}
|
|
733
890
|
/**
|
|
@@ -739,7 +896,7 @@ class Jwt {
|
|
|
739
896
|
*/
|
|
740
897
|
static async encodeWithSigner(header, payload, signer) {
|
|
741
898
|
Guards.object(Jwt._CLASS_NAME, "header", header);
|
|
742
|
-
Guards.
|
|
899
|
+
Guards.stringValue(Jwt._CLASS_NAME, "header.alg", header.alg);
|
|
743
900
|
Guards.object(Jwt._CLASS_NAME, "payload", payload);
|
|
744
901
|
Guards.function(Jwt._CLASS_NAME, "signer", signer);
|
|
745
902
|
return Jwt.internalEncode(header, payload, undefined, signer);
|
|
@@ -786,13 +943,8 @@ class Jwt {
|
|
|
786
943
|
*/
|
|
787
944
|
static async verify(token, key) {
|
|
788
945
|
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
|
-
};
|
|
946
|
+
Guards.defined(Jwt._CLASS_NAME, "key", key);
|
|
947
|
+
return Jwt.verifySignature(token, key);
|
|
796
948
|
}
|
|
797
949
|
/**
|
|
798
950
|
* Verify a token.
|
|
@@ -803,79 +955,131 @@ class Jwt {
|
|
|
803
955
|
static async verifyWithVerifier(token, verifier) {
|
|
804
956
|
Guards.stringValue(Jwt._CLASS_NAME, "token", token);
|
|
805
957
|
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
|
-
};
|
|
958
|
+
return Jwt.verifySignature(token, undefined, verifier);
|
|
813
959
|
}
|
|
814
960
|
/**
|
|
815
961
|
* Verify a token by parts.
|
|
816
|
-
* @param
|
|
817
|
-
* @param payload The payload to verify.
|
|
818
|
-
* @param signature The signature to verify.
|
|
962
|
+
* @param token The token to verify.
|
|
819
963
|
* @param key The key for verifying the token, if not provided no verification occurs.
|
|
820
964
|
* @param verifier Custom verification method.
|
|
821
965
|
* @returns True if the parts are verified.
|
|
822
966
|
*/
|
|
823
|
-
static async verifySignature(
|
|
967
|
+
static async verifySignature(token, key, verifier) {
|
|
968
|
+
Guards.stringValue(Jwt._CLASS_NAME, "token", token);
|
|
824
969
|
const hasKey = Is.notEmpty(key);
|
|
825
970
|
const hasVerifier = Is.notEmpty(verifier);
|
|
826
971
|
if (!hasKey && !hasVerifier) {
|
|
827
972
|
throw new GeneralError(Jwt._CLASS_NAME, "noKeyOrVerifier");
|
|
828
973
|
}
|
|
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;
|
|
974
|
+
verifier ??= async (t, k) => Jwt.defaultVerifier(t, k);
|
|
975
|
+
return verifier(token, key);
|
|
844
976
|
}
|
|
845
977
|
/**
|
|
846
978
|
* The default signer for the JWT.
|
|
847
|
-
* @param
|
|
848
|
-
* @param key The key to sign with.
|
|
979
|
+
* @param header The header to sign.
|
|
849
980
|
* @param payload The payload to sign.
|
|
981
|
+
* @param key The optional key to sign with.
|
|
850
982
|
* @returns The signature.
|
|
851
983
|
*/
|
|
852
|
-
static async defaultSigner(
|
|
853
|
-
Guards.
|
|
854
|
-
Guards.
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
984
|
+
static async defaultSigner(header, payload, key) {
|
|
985
|
+
Guards.object(Jwt._CLASS_NAME, "header", header);
|
|
986
|
+
Guards.object(Jwt._CLASS_NAME, "payload", payload);
|
|
987
|
+
Guards.defined(Jwt._CLASS_NAME, "key", key);
|
|
988
|
+
const signer = new SignJWT(payload);
|
|
989
|
+
signer.setProtectedHeader(header);
|
|
990
|
+
let finalKey = key;
|
|
991
|
+
if (header.alg === "EdDSA" && Is.uint8Array(key)) {
|
|
992
|
+
// Jose does not support Ed25519 keys in raw format, so we need to convert it to PKCS8.
|
|
993
|
+
finalKey = await Ed25519.privateKeyToPkcs8(key);
|
|
858
994
|
}
|
|
859
|
-
return
|
|
995
|
+
return signer.sign(finalKey);
|
|
860
996
|
}
|
|
861
997
|
/**
|
|
862
998
|
* The default verifier for the JWT.
|
|
863
|
-
* @param
|
|
999
|
+
* @param token The token to verify.
|
|
864
1000
|
* @param key The key to verify with.
|
|
865
|
-
* @
|
|
866
|
-
|
|
867
|
-
|
|
1001
|
+
* @returns The header and payload if verification successful.
|
|
1002
|
+
*/
|
|
1003
|
+
static async defaultVerifier(token, key) {
|
|
1004
|
+
Guards.stringValue(Jwt._CLASS_NAME, "token", token);
|
|
1005
|
+
Guards.defined(Jwt._CLASS_NAME, "key", key);
|
|
1006
|
+
try {
|
|
1007
|
+
const result = await jwtVerify(token, key);
|
|
1008
|
+
return {
|
|
1009
|
+
header: result.protectedHeader,
|
|
1010
|
+
payload: result.payload
|
|
1011
|
+
};
|
|
1012
|
+
}
|
|
1013
|
+
catch (err) {
|
|
1014
|
+
throw new GeneralError(Jwt._CLASS_NAME, "verifyFailed", undefined, err);
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
/**
|
|
1018
|
+
* Create bytes for signing from header and payload.
|
|
1019
|
+
* @param header The header.
|
|
1020
|
+
* @param payload The payload.
|
|
1021
|
+
* @returns The bytes to sign.
|
|
868
1022
|
*/
|
|
869
|
-
static
|
|
870
|
-
Guards.
|
|
871
|
-
Guards.
|
|
1023
|
+
static toSigningBytes(header, payload) {
|
|
1024
|
+
Guards.object(Jwt._CLASS_NAME, "header", header);
|
|
1025
|
+
Guards.object(Jwt._CLASS_NAME, "payload", payload);
|
|
1026
|
+
const segments = [];
|
|
1027
|
+
const headerBytes = Converter.utf8ToBytes(JSON.stringify(header));
|
|
1028
|
+
segments.push(Converter.bytesToBase64Url(headerBytes));
|
|
1029
|
+
const payloadBytes = Converter.utf8ToBytes(JSON.stringify(payload));
|
|
1030
|
+
segments.push(Converter.bytesToBase64Url(payloadBytes));
|
|
1031
|
+
return Converter.utf8ToBytes(segments.join("."));
|
|
1032
|
+
}
|
|
1033
|
+
/**
|
|
1034
|
+
* Create header and payload from signing bytes.
|
|
1035
|
+
* @param signingBytes The signing bytes from a token.
|
|
1036
|
+
* @returns The header and payload.
|
|
1037
|
+
* @throws If the signing bytes are invalid
|
|
1038
|
+
*/
|
|
1039
|
+
static fromSigningBytes(signingBytes) {
|
|
1040
|
+
Guards.uint8Array(Jwt._CLASS_NAME, "signingBytes", signingBytes);
|
|
1041
|
+
const segments = Converter.bytesToUtf8(signingBytes).split(".");
|
|
1042
|
+
if (segments.length !== 2) {
|
|
1043
|
+
throw new GeneralError(Jwt._CLASS_NAME, "invalidSigningBytes");
|
|
1044
|
+
}
|
|
1045
|
+
const headerBytes = Converter.base64UrlToBytes(segments[0]);
|
|
1046
|
+
const payloadBytes = Converter.base64UrlToBytes(segments[1]);
|
|
1047
|
+
return {
|
|
1048
|
+
header: ObjectHelper.fromBytes(headerBytes),
|
|
1049
|
+
payload: ObjectHelper.fromBytes(payloadBytes)
|
|
1050
|
+
};
|
|
1051
|
+
}
|
|
1052
|
+
/**
|
|
1053
|
+
* Convert signed bytes and signature bytes to token.
|
|
1054
|
+
* @param signingBytes The signed bytes.
|
|
1055
|
+
* @param signature The signature.
|
|
1056
|
+
* @returns The token.
|
|
1057
|
+
*/
|
|
1058
|
+
static tokenFromBytes(signingBytes, signature) {
|
|
1059
|
+
Guards.uint8Array(Jwt._CLASS_NAME, "signingBytes", signingBytes);
|
|
872
1060
|
Guards.uint8Array(Jwt._CLASS_NAME, "signature", signature);
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
1061
|
+
const signedBytesUtf8 = Converter.bytesToUtf8(signingBytes);
|
|
1062
|
+
const signatureBase64 = Converter.bytesToBase64Url(signature);
|
|
1063
|
+
return `${signedBytesUtf8}.${signatureBase64}`;
|
|
1064
|
+
}
|
|
1065
|
+
/**
|
|
1066
|
+
* Convert the token to signing bytes and signature bytes.
|
|
1067
|
+
* @param token The token to convert to bytes.
|
|
1068
|
+
* @returns The decoded bytes.
|
|
1069
|
+
* @throws If the token is invalid.
|
|
1070
|
+
*/
|
|
1071
|
+
static tokenToBytes(token) {
|
|
1072
|
+
Guards.stringValue(Jwt._CLASS_NAME, "token", token);
|
|
1073
|
+
const segments = token.split(".");
|
|
1074
|
+
if (segments.length !== 3) {
|
|
1075
|
+
throw new GeneralError(Jwt._CLASS_NAME, "invalidTokenParts");
|
|
877
1076
|
}
|
|
878
|
-
|
|
1077
|
+
const signingBytes = Converter.utf8ToBytes(`${segments[0]}.${segments[1]}`);
|
|
1078
|
+
const signature = Converter.base64UrlToBytes(segments[2]);
|
|
1079
|
+
return {
|
|
1080
|
+
signingBytes,
|
|
1081
|
+
signature
|
|
1082
|
+
};
|
|
879
1083
|
}
|
|
880
1084
|
/**
|
|
881
1085
|
* Encode a token.
|
|
@@ -892,19 +1096,11 @@ class Jwt {
|
|
|
892
1096
|
if (!hasKey && !hasSigner) {
|
|
893
1097
|
throw new GeneralError(Jwt._CLASS_NAME, "noKeyOrSigner");
|
|
894
1098
|
}
|
|
895
|
-
signer ??= async (
|
|
1099
|
+
signer ??= async (h, p, k) => Jwt.defaultSigner(h, p, k);
|
|
896
1100
|
if (Is.undefined(header.typ)) {
|
|
897
1101
|
header.typ = "JWT";
|
|
898
1102
|
}
|
|
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(".");
|
|
1103
|
+
return signer(header, payload, key);
|
|
908
1104
|
}
|
|
909
1105
|
}
|
|
910
1106
|
|
|
@@ -920,7 +1116,7 @@ class MimeTypeHelper {
|
|
|
920
1116
|
* @returns The mime type if detected.
|
|
921
1117
|
*/
|
|
922
1118
|
static async detect(data) {
|
|
923
|
-
if (!Is.uint8Array(data)) {
|
|
1119
|
+
if (!Is.uint8Array(data) || data.length === 0) {
|
|
924
1120
|
return undefined;
|
|
925
1121
|
}
|
|
926
1122
|
// Image
|
|
@@ -944,6 +1140,11 @@ class MimeTypeHelper {
|
|
|
944
1140
|
if (MimeTypeHelper.checkBytes(data, [0x1f, 0x8b, 0x8])) {
|
|
945
1141
|
return MimeTypes.Gzip;
|
|
946
1142
|
}
|
|
1143
|
+
if (MimeTypeHelper.checkBytes(data, [0x78, 0x01]) ||
|
|
1144
|
+
MimeTypeHelper.checkBytes(data, [0x78, 0x9c]) ||
|
|
1145
|
+
MimeTypeHelper.checkBytes(data, [0x78, 0xda])) {
|
|
1146
|
+
return MimeTypes.Zlib;
|
|
1147
|
+
}
|
|
947
1148
|
if (MimeTypeHelper.checkBytes(data, [0x42, 0x5a, 0x68])) {
|
|
948
1149
|
return MimeTypes.Bzip2;
|
|
949
1150
|
}
|
|
@@ -992,12 +1193,14 @@ class MimeTypeHelper {
|
|
|
992
1193
|
[MimeTypes.Javascript]: "js",
|
|
993
1194
|
[MimeTypes.Json]: "json",
|
|
994
1195
|
[MimeTypes.JsonLd]: "jsonld",
|
|
1196
|
+
[MimeTypes.Jwt]: "jwt",
|
|
995
1197
|
[MimeTypes.Xml]: "xml",
|
|
996
1198
|
[MimeTypes.OctetStream]: "bin",
|
|
997
1199
|
[MimeTypes.Gzip]: "gzip",
|
|
1200
|
+
[MimeTypes.Zlib]: "zlib",
|
|
998
1201
|
[MimeTypes.Bzip2]: "bz2",
|
|
999
1202
|
[MimeTypes.Zip]: "zip",
|
|
1000
|
-
[MimeTypes.Pdf]: "
|
|
1203
|
+
[MimeTypes.Pdf]: "pdf",
|
|
1001
1204
|
[MimeTypes.Gif]: "gif",
|
|
1002
1205
|
[MimeTypes.Bmp]: "bmp",
|
|
1003
1206
|
[MimeTypes.Jpeg]: "jpeg",
|
|
@@ -1043,4 +1246,4 @@ class MimeTypeHelper {
|
|
|
1043
1246
|
}
|
|
1044
1247
|
}
|
|
1045
1248
|
|
|
1046
|
-
export { FetchError, FetchHelper, HeaderTypes, HttpMethod, HttpStatusCode,
|
|
1249
|
+
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
|
}
|