@twin.org/web 0.0.1-next.37 → 0.0.1-next.39
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 +78 -21
- package/dist/esm/index.mjs +79 -22
- package/dist/types/utils/jwt.d.ts +41 -8
- package/docs/changelog.md +1 -1
- package/docs/reference/classes/Jwt.md +158 -28
- package/locales/en.json +3 -1
- package/package.json +3 -3
package/dist/cjs/index.cjs
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var core = require('@twin.org/core');
|
|
4
4
|
var jose = require('jose');
|
|
5
|
+
var crypto = require('@twin.org/crypto');
|
|
5
6
|
|
|
6
7
|
// Copyright 2024 IOTA Stiftung.
|
|
7
8
|
// SPDX-License-Identifier: Apache-2.0.
|
|
@@ -835,34 +836,23 @@ class Jwt {
|
|
|
835
836
|
}
|
|
836
837
|
/**
|
|
837
838
|
* The default signer for the JWT.
|
|
838
|
-
* @param alg The algorithm to use.
|
|
839
|
-
* @param key The key to sign with.
|
|
840
839
|
* @param header The header to sign.
|
|
841
840
|
* @param payload The payload to sign.
|
|
841
|
+
* @param key The optional key to sign with.
|
|
842
842
|
* @returns The signature.
|
|
843
843
|
*/
|
|
844
|
-
static async defaultSigner(
|
|
845
|
-
core.Guards.defined(Jwt._CLASS_NAME, "key", key);
|
|
844
|
+
static async defaultSigner(header, payload, key) {
|
|
846
845
|
core.Guards.object(Jwt._CLASS_NAME, "header", header);
|
|
847
846
|
core.Guards.object(Jwt._CLASS_NAME, "payload", payload);
|
|
847
|
+
core.Guards.defined(Jwt._CLASS_NAME, "key", key);
|
|
848
848
|
const signer = new jose.SignJWT(payload);
|
|
849
|
-
header.alg = alg;
|
|
850
849
|
signer.setProtectedHeader(header);
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
//
|
|
854
|
-
|
|
855
|
-
// The ASN.1 sequence is 48 46 02 01 00 30 05 06 03 2b 65 70 04 20 04 20
|
|
856
|
-
const pkcs8Prefix = new Uint8Array([
|
|
857
|
-
48, 46, 2, 1, 0, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32
|
|
858
|
-
]); // 0x302e020100300506032b657004220420
|
|
859
|
-
const pkcs8PrivateKey = core.Uint8ArrayHelper.concat([pkcs8Prefix, key]);
|
|
860
|
-
const imported = await crypto.subtle.importKey("pkcs8", pkcs8PrivateKey, "Ed25519", false, [
|
|
861
|
-
"sign"
|
|
862
|
-
]);
|
|
863
|
-
return signer.sign(imported);
|
|
850
|
+
let finalKey = key;
|
|
851
|
+
if (header.alg === "EdDSA" && core.Is.uint8Array(key)) {
|
|
852
|
+
// Jose does not support Ed25519 keys in raw format, so we need to convert it to PKCS8.
|
|
853
|
+
finalKey = await crypto.Ed25519.privateKeyToPKCS8(key);
|
|
864
854
|
}
|
|
865
|
-
return signer.sign(
|
|
855
|
+
return signer.sign(finalKey);
|
|
866
856
|
}
|
|
867
857
|
/**
|
|
868
858
|
* The default verifier for the JWT.
|
|
@@ -884,6 +874,73 @@ class Jwt {
|
|
|
884
874
|
throw new core.GeneralError(Jwt._CLASS_NAME, "verifyFailed", undefined, err);
|
|
885
875
|
}
|
|
886
876
|
}
|
|
877
|
+
/**
|
|
878
|
+
* Create bytes for signing from header and payload.
|
|
879
|
+
* @param header The header.
|
|
880
|
+
* @param payload The payload.
|
|
881
|
+
* @returns The bytes to sign.
|
|
882
|
+
*/
|
|
883
|
+
static toSigningBytes(header, payload) {
|
|
884
|
+
core.Guards.object(Jwt._CLASS_NAME, "header", header);
|
|
885
|
+
core.Guards.object(Jwt._CLASS_NAME, "payload", payload);
|
|
886
|
+
const segments = [];
|
|
887
|
+
const headerBytes = core.Converter.utf8ToBytes(JSON.stringify(header));
|
|
888
|
+
segments.push(core.Converter.bytesToBase64Url(headerBytes));
|
|
889
|
+
const payloadBytes = core.Converter.utf8ToBytes(JSON.stringify(payload));
|
|
890
|
+
segments.push(core.Converter.bytesToBase64Url(payloadBytes));
|
|
891
|
+
return core.Converter.utf8ToBytes(segments.join("."));
|
|
892
|
+
}
|
|
893
|
+
/**
|
|
894
|
+
* Create header and payload from signing bytes.
|
|
895
|
+
* @param signingBytes The signing bytes from a token.
|
|
896
|
+
* @returns The header and payload.
|
|
897
|
+
* @throws If the signing bytes are invalid
|
|
898
|
+
*/
|
|
899
|
+
static fromSigningBytes(signingBytes) {
|
|
900
|
+
core.Guards.uint8Array(Jwt._CLASS_NAME, "signingBytes", signingBytes);
|
|
901
|
+
const segments = core.Converter.bytesToUtf8(signingBytes).split(".");
|
|
902
|
+
if (segments.length !== 2) {
|
|
903
|
+
throw new core.GeneralError(Jwt._CLASS_NAME, "invalidSigningBytes");
|
|
904
|
+
}
|
|
905
|
+
const headerBytes = core.Converter.base64UrlToBytes(segments[0]);
|
|
906
|
+
const payloadBytes = core.Converter.base64UrlToBytes(segments[1]);
|
|
907
|
+
return {
|
|
908
|
+
header: core.ObjectHelper.fromBytes(headerBytes),
|
|
909
|
+
payload: core.ObjectHelper.fromBytes(payloadBytes)
|
|
910
|
+
};
|
|
911
|
+
}
|
|
912
|
+
/**
|
|
913
|
+
* Convert signed bytes and signature bytes to token.
|
|
914
|
+
* @param signingBytes The signed bytes.
|
|
915
|
+
* @param signature The signature.
|
|
916
|
+
* @returns The token.
|
|
917
|
+
*/
|
|
918
|
+
static tokenFromBytes(signingBytes, signature) {
|
|
919
|
+
core.Guards.uint8Array(Jwt._CLASS_NAME, "signingBytes", signingBytes);
|
|
920
|
+
core.Guards.uint8Array(Jwt._CLASS_NAME, "signature", signature);
|
|
921
|
+
const signedBytesUtf8 = core.Converter.bytesToUtf8(signingBytes);
|
|
922
|
+
const signatureBase64 = core.Converter.bytesToBase64Url(signature);
|
|
923
|
+
return `${signedBytesUtf8}.${signatureBase64}`;
|
|
924
|
+
}
|
|
925
|
+
/**
|
|
926
|
+
* Convert the token to signing bytes and signature bytes.
|
|
927
|
+
* @param token The token to convert to bytes.
|
|
928
|
+
* @returns The decoded bytes.
|
|
929
|
+
* @throws If the token is invalid.
|
|
930
|
+
*/
|
|
931
|
+
static tokenToBytes(token) {
|
|
932
|
+
core.Guards.stringValue(Jwt._CLASS_NAME, "token", token);
|
|
933
|
+
const segments = token.split(".");
|
|
934
|
+
if (segments.length !== 3) {
|
|
935
|
+
throw new core.GeneralError(Jwt._CLASS_NAME, "invalidTokenParts");
|
|
936
|
+
}
|
|
937
|
+
const signingBytes = core.Converter.utf8ToBytes(`${segments[0]}.${segments[1]}`);
|
|
938
|
+
const signature = core.Converter.base64UrlToBytes(segments[2]);
|
|
939
|
+
return {
|
|
940
|
+
signingBytes,
|
|
941
|
+
signature
|
|
942
|
+
};
|
|
943
|
+
}
|
|
887
944
|
/**
|
|
888
945
|
* Encode a token.
|
|
889
946
|
* @param header The header to encode.
|
|
@@ -899,11 +956,11 @@ class Jwt {
|
|
|
899
956
|
if (!hasKey && !hasSigner) {
|
|
900
957
|
throw new core.GeneralError(Jwt._CLASS_NAME, "noKeyOrSigner");
|
|
901
958
|
}
|
|
902
|
-
signer ??= async (
|
|
959
|
+
signer ??= async (h, p, k) => Jwt.defaultSigner(h, p, k);
|
|
903
960
|
if (core.Is.undefined(header.typ)) {
|
|
904
961
|
header.typ = "JWT";
|
|
905
962
|
}
|
|
906
|
-
return signer(header
|
|
963
|
+
return signer(header, payload, key);
|
|
907
964
|
}
|
|
908
965
|
}
|
|
909
966
|
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { BaseError, StringHelper, Guards, Is, AsyncCache, ObjectHelper, GeneralError, Converter
|
|
1
|
+
import { BaseError, StringHelper, Guards, Is, AsyncCache, ObjectHelper, GeneralError, Converter } from '@twin.org/core';
|
|
2
2
|
import { importJWK, SignJWT, jwtVerify } from 'jose';
|
|
3
|
+
import { Ed25519 } from '@twin.org/crypto';
|
|
3
4
|
|
|
4
5
|
// Copyright 2024 IOTA Stiftung.
|
|
5
6
|
// SPDX-License-Identifier: Apache-2.0.
|
|
@@ -833,34 +834,23 @@ class Jwt {
|
|
|
833
834
|
}
|
|
834
835
|
/**
|
|
835
836
|
* The default signer for the JWT.
|
|
836
|
-
* @param alg The algorithm to use.
|
|
837
|
-
* @param key The key to sign with.
|
|
838
837
|
* @param header The header to sign.
|
|
839
838
|
* @param payload The payload to sign.
|
|
839
|
+
* @param key The optional key to sign with.
|
|
840
840
|
* @returns The signature.
|
|
841
841
|
*/
|
|
842
|
-
static async defaultSigner(
|
|
843
|
-
Guards.defined(Jwt._CLASS_NAME, "key", key);
|
|
842
|
+
static async defaultSigner(header, payload, key) {
|
|
844
843
|
Guards.object(Jwt._CLASS_NAME, "header", header);
|
|
845
844
|
Guards.object(Jwt._CLASS_NAME, "payload", payload);
|
|
845
|
+
Guards.defined(Jwt._CLASS_NAME, "key", key);
|
|
846
846
|
const signer = new SignJWT(payload);
|
|
847
|
-
header.alg = alg;
|
|
848
847
|
signer.setProtectedHeader(header);
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
//
|
|
852
|
-
|
|
853
|
-
// The ASN.1 sequence is 48 46 02 01 00 30 05 06 03 2b 65 70 04 20 04 20
|
|
854
|
-
const pkcs8Prefix = new Uint8Array([
|
|
855
|
-
48, 46, 2, 1, 0, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32
|
|
856
|
-
]); // 0x302e020100300506032b657004220420
|
|
857
|
-
const pkcs8PrivateKey = Uint8ArrayHelper.concat([pkcs8Prefix, key]);
|
|
858
|
-
const imported = await crypto.subtle.importKey("pkcs8", pkcs8PrivateKey, "Ed25519", false, [
|
|
859
|
-
"sign"
|
|
860
|
-
]);
|
|
861
|
-
return signer.sign(imported);
|
|
848
|
+
let finalKey = key;
|
|
849
|
+
if (header.alg === "EdDSA" && Is.uint8Array(key)) {
|
|
850
|
+
// Jose does not support Ed25519 keys in raw format, so we need to convert it to PKCS8.
|
|
851
|
+
finalKey = await Ed25519.privateKeyToPKCS8(key);
|
|
862
852
|
}
|
|
863
|
-
return signer.sign(
|
|
853
|
+
return signer.sign(finalKey);
|
|
864
854
|
}
|
|
865
855
|
/**
|
|
866
856
|
* The default verifier for the JWT.
|
|
@@ -882,6 +872,73 @@ class Jwt {
|
|
|
882
872
|
throw new GeneralError(Jwt._CLASS_NAME, "verifyFailed", undefined, err);
|
|
883
873
|
}
|
|
884
874
|
}
|
|
875
|
+
/**
|
|
876
|
+
* Create bytes for signing from header and payload.
|
|
877
|
+
* @param header The header.
|
|
878
|
+
* @param payload The payload.
|
|
879
|
+
* @returns The bytes to sign.
|
|
880
|
+
*/
|
|
881
|
+
static toSigningBytes(header, payload) {
|
|
882
|
+
Guards.object(Jwt._CLASS_NAME, "header", header);
|
|
883
|
+
Guards.object(Jwt._CLASS_NAME, "payload", payload);
|
|
884
|
+
const segments = [];
|
|
885
|
+
const headerBytes = Converter.utf8ToBytes(JSON.stringify(header));
|
|
886
|
+
segments.push(Converter.bytesToBase64Url(headerBytes));
|
|
887
|
+
const payloadBytes = Converter.utf8ToBytes(JSON.stringify(payload));
|
|
888
|
+
segments.push(Converter.bytesToBase64Url(payloadBytes));
|
|
889
|
+
return Converter.utf8ToBytes(segments.join("."));
|
|
890
|
+
}
|
|
891
|
+
/**
|
|
892
|
+
* Create header and payload from signing bytes.
|
|
893
|
+
* @param signingBytes The signing bytes from a token.
|
|
894
|
+
* @returns The header and payload.
|
|
895
|
+
* @throws If the signing bytes are invalid
|
|
896
|
+
*/
|
|
897
|
+
static fromSigningBytes(signingBytes) {
|
|
898
|
+
Guards.uint8Array(Jwt._CLASS_NAME, "signingBytes", signingBytes);
|
|
899
|
+
const segments = Converter.bytesToUtf8(signingBytes).split(".");
|
|
900
|
+
if (segments.length !== 2) {
|
|
901
|
+
throw new GeneralError(Jwt._CLASS_NAME, "invalidSigningBytes");
|
|
902
|
+
}
|
|
903
|
+
const headerBytes = Converter.base64UrlToBytes(segments[0]);
|
|
904
|
+
const payloadBytes = Converter.base64UrlToBytes(segments[1]);
|
|
905
|
+
return {
|
|
906
|
+
header: ObjectHelper.fromBytes(headerBytes),
|
|
907
|
+
payload: ObjectHelper.fromBytes(payloadBytes)
|
|
908
|
+
};
|
|
909
|
+
}
|
|
910
|
+
/**
|
|
911
|
+
* Convert signed bytes and signature bytes to token.
|
|
912
|
+
* @param signingBytes The signed bytes.
|
|
913
|
+
* @param signature The signature.
|
|
914
|
+
* @returns The token.
|
|
915
|
+
*/
|
|
916
|
+
static tokenFromBytes(signingBytes, signature) {
|
|
917
|
+
Guards.uint8Array(Jwt._CLASS_NAME, "signingBytes", signingBytes);
|
|
918
|
+
Guards.uint8Array(Jwt._CLASS_NAME, "signature", signature);
|
|
919
|
+
const signedBytesUtf8 = Converter.bytesToUtf8(signingBytes);
|
|
920
|
+
const signatureBase64 = Converter.bytesToBase64Url(signature);
|
|
921
|
+
return `${signedBytesUtf8}.${signatureBase64}`;
|
|
922
|
+
}
|
|
923
|
+
/**
|
|
924
|
+
* Convert the token to signing bytes and signature bytes.
|
|
925
|
+
* @param token The token to convert to bytes.
|
|
926
|
+
* @returns The decoded bytes.
|
|
927
|
+
* @throws If the token is invalid.
|
|
928
|
+
*/
|
|
929
|
+
static tokenToBytes(token) {
|
|
930
|
+
Guards.stringValue(Jwt._CLASS_NAME, "token", token);
|
|
931
|
+
const segments = token.split(".");
|
|
932
|
+
if (segments.length !== 3) {
|
|
933
|
+
throw new GeneralError(Jwt._CLASS_NAME, "invalidTokenParts");
|
|
934
|
+
}
|
|
935
|
+
const signingBytes = Converter.utf8ToBytes(`${segments[0]}.${segments[1]}`);
|
|
936
|
+
const signature = Converter.base64UrlToBytes(segments[2]);
|
|
937
|
+
return {
|
|
938
|
+
signingBytes,
|
|
939
|
+
signature
|
|
940
|
+
};
|
|
941
|
+
}
|
|
885
942
|
/**
|
|
886
943
|
* Encode a token.
|
|
887
944
|
* @param header The header to encode.
|
|
@@ -897,11 +954,11 @@ class Jwt {
|
|
|
897
954
|
if (!hasKey && !hasSigner) {
|
|
898
955
|
throw new GeneralError(Jwt._CLASS_NAME, "noKeyOrSigner");
|
|
899
956
|
}
|
|
900
|
-
signer ??= async (
|
|
957
|
+
signer ??= async (h, p, k) => Jwt.defaultSigner(h, p, k);
|
|
901
958
|
if (Is.undefined(header.typ)) {
|
|
902
959
|
header.typ = "JWT";
|
|
903
960
|
}
|
|
904
|
-
return signer(header
|
|
961
|
+
return signer(header, payload, key);
|
|
905
962
|
}
|
|
906
963
|
}
|
|
907
964
|
|
|
@@ -12,7 +12,7 @@ export declare class Jwt {
|
|
|
12
12
|
* @param key The key for signing the token, can be omitted if a signer is provided.
|
|
13
13
|
* @returns The encoded token.
|
|
14
14
|
*/
|
|
15
|
-
static encode<
|
|
15
|
+
static encode<T extends IJwtHeader, U extends IJwtPayload>(header: T, payload: U, key: JwkCryptoKey): Promise<string>;
|
|
16
16
|
/**
|
|
17
17
|
* Encode a token.
|
|
18
18
|
* @param header The header to encode.
|
|
@@ -20,15 +20,15 @@ export declare class Jwt {
|
|
|
20
20
|
* @param signer Custom signer method.
|
|
21
21
|
* @returns The encoded token.
|
|
22
22
|
*/
|
|
23
|
-
static encodeWithSigner<
|
|
23
|
+
static encodeWithSigner<T extends IJwtHeader, U extends IJwtPayload>(header: T, payload: U, signer: (header: IJwtHeader, payload: IJwtPayload, key: JwkCryptoKey | undefined) => Promise<string>): Promise<string>;
|
|
24
24
|
/**
|
|
25
25
|
* Decode a token.
|
|
26
26
|
* @param token The token to decode.
|
|
27
27
|
* @returns The decoded payload.
|
|
28
28
|
*/
|
|
29
|
-
static decode<
|
|
30
|
-
header?:
|
|
31
|
-
payload?:
|
|
29
|
+
static decode<T extends IJwtHeader, U extends IJwtPayload>(token: string): Promise<{
|
|
30
|
+
header?: T;
|
|
31
|
+
payload?: U;
|
|
32
32
|
signature?: Uint8Array;
|
|
33
33
|
}>;
|
|
34
34
|
/**
|
|
@@ -70,13 +70,12 @@ export declare class Jwt {
|
|
|
70
70
|
}>;
|
|
71
71
|
/**
|
|
72
72
|
* The default signer for the JWT.
|
|
73
|
-
* @param alg The algorithm to use.
|
|
74
|
-
* @param key The key to sign with.
|
|
75
73
|
* @param header The header to sign.
|
|
76
74
|
* @param payload The payload to sign.
|
|
75
|
+
* @param key The optional key to sign with.
|
|
77
76
|
* @returns The signature.
|
|
78
77
|
*/
|
|
79
|
-
static defaultSigner(
|
|
78
|
+
static defaultSigner(header: IJwtHeader, payload: IJwtPayload, key: JwkCryptoKey | undefined): Promise<string>;
|
|
80
79
|
/**
|
|
81
80
|
* The default verifier for the JWT.
|
|
82
81
|
* @param token The token to verify.
|
|
@@ -87,4 +86,38 @@ export declare class Jwt {
|
|
|
87
86
|
header: T;
|
|
88
87
|
payload: U;
|
|
89
88
|
}>;
|
|
89
|
+
/**
|
|
90
|
+
* Create bytes for signing from header and payload.
|
|
91
|
+
* @param header The header.
|
|
92
|
+
* @param payload The payload.
|
|
93
|
+
* @returns The bytes to sign.
|
|
94
|
+
*/
|
|
95
|
+
static toSigningBytes<T extends IJwtHeader, U extends IJwtPayload>(header: T, payload: U): Uint8Array;
|
|
96
|
+
/**
|
|
97
|
+
* Create header and payload from signing bytes.
|
|
98
|
+
* @param signingBytes The signing bytes from a token.
|
|
99
|
+
* @returns The header and payload.
|
|
100
|
+
* @throws If the signing bytes are invalid
|
|
101
|
+
*/
|
|
102
|
+
static fromSigningBytes<T extends IJwtHeader, U extends IJwtPayload>(signingBytes: Uint8Array): {
|
|
103
|
+
header: T;
|
|
104
|
+
payload: U;
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* Convert signed bytes and signature bytes to token.
|
|
108
|
+
* @param signingBytes The signed bytes.
|
|
109
|
+
* @param signature The signature.
|
|
110
|
+
* @returns The token.
|
|
111
|
+
*/
|
|
112
|
+
static tokenFromBytes(signingBytes: Uint8Array, signature: Uint8Array): string;
|
|
113
|
+
/**
|
|
114
|
+
* Convert the token to signing bytes and signature bytes.
|
|
115
|
+
* @param token The token to convert to bytes.
|
|
116
|
+
* @returns The decoded bytes.
|
|
117
|
+
* @throws If the token is invalid.
|
|
118
|
+
*/
|
|
119
|
+
static tokenToBytes(token: string): {
|
|
120
|
+
signingBytes: Uint8Array;
|
|
121
|
+
signature: Uint8Array;
|
|
122
|
+
};
|
|
90
123
|
}
|
package/docs/changelog.md
CHANGED
|
@@ -16,27 +16,27 @@ Class to handle JSON Web Tokens.
|
|
|
16
16
|
|
|
17
17
|
### encode()
|
|
18
18
|
|
|
19
|
-
> `static` **encode**\<`
|
|
19
|
+
> `static` **encode**\<`T`, `U`\>(`header`, `payload`, `key`): `Promise`\<`string`\>
|
|
20
20
|
|
|
21
21
|
Encode a token.
|
|
22
22
|
|
|
23
23
|
#### Type Parameters
|
|
24
24
|
|
|
25
|
-
• **
|
|
25
|
+
• **T** *extends* [`IJwtHeader`](../interfaces/IJwtHeader.md)
|
|
26
26
|
|
|
27
|
-
• **
|
|
27
|
+
• **U** *extends* [`IJwtPayload`](../interfaces/IJwtPayload.md)
|
|
28
28
|
|
|
29
29
|
#### Parameters
|
|
30
30
|
|
|
31
31
|
##### header
|
|
32
32
|
|
|
33
|
-
`
|
|
33
|
+
`T`
|
|
34
34
|
|
|
35
35
|
The header to encode.
|
|
36
36
|
|
|
37
37
|
##### payload
|
|
38
38
|
|
|
39
|
-
`
|
|
39
|
+
`U`
|
|
40
40
|
|
|
41
41
|
The payload to encode.
|
|
42
42
|
|
|
@@ -56,33 +56,33 @@ The encoded token.
|
|
|
56
56
|
|
|
57
57
|
### encodeWithSigner()
|
|
58
58
|
|
|
59
|
-
> `static` **encodeWithSigner**\<`
|
|
59
|
+
> `static` **encodeWithSigner**\<`T`, `U`\>(`header`, `payload`, `signer`): `Promise`\<`string`\>
|
|
60
60
|
|
|
61
61
|
Encode a token.
|
|
62
62
|
|
|
63
63
|
#### Type Parameters
|
|
64
64
|
|
|
65
|
-
• **
|
|
65
|
+
• **T** *extends* [`IJwtHeader`](../interfaces/IJwtHeader.md)
|
|
66
66
|
|
|
67
|
-
• **
|
|
67
|
+
• **U** *extends* [`IJwtPayload`](../interfaces/IJwtPayload.md)
|
|
68
68
|
|
|
69
69
|
#### Parameters
|
|
70
70
|
|
|
71
71
|
##### header
|
|
72
72
|
|
|
73
|
-
`
|
|
73
|
+
`T`
|
|
74
74
|
|
|
75
75
|
The header to encode.
|
|
76
76
|
|
|
77
77
|
##### payload
|
|
78
78
|
|
|
79
|
-
`
|
|
79
|
+
`U`
|
|
80
80
|
|
|
81
81
|
The payload to encode.
|
|
82
82
|
|
|
83
83
|
##### signer
|
|
84
84
|
|
|
85
|
-
(`
|
|
85
|
+
(`header`, `payload`, `key`) => `Promise`\<`string`\>
|
|
86
86
|
|
|
87
87
|
Custom signer method.
|
|
88
88
|
|
|
@@ -96,15 +96,15 @@ The encoded token.
|
|
|
96
96
|
|
|
97
97
|
### decode()
|
|
98
98
|
|
|
99
|
-
> `static` **decode**\<`
|
|
99
|
+
> `static` **decode**\<`T`, `U`\>(`token`): `Promise`\<\{ `header`: `T`; `payload`: `U`; `signature`: `Uint8Array`; \}\>
|
|
100
100
|
|
|
101
101
|
Decode a token.
|
|
102
102
|
|
|
103
103
|
#### Type Parameters
|
|
104
104
|
|
|
105
|
-
• **
|
|
105
|
+
• **T** *extends* [`IJwtHeader`](../interfaces/IJwtHeader.md)
|
|
106
106
|
|
|
107
|
-
• **
|
|
107
|
+
• **U** *extends* [`IJwtPayload`](../interfaces/IJwtPayload.md)
|
|
108
108
|
|
|
109
109
|
#### Parameters
|
|
110
110
|
|
|
@@ -116,7 +116,7 @@ The token to decode.
|
|
|
116
116
|
|
|
117
117
|
#### Returns
|
|
118
118
|
|
|
119
|
-
`Promise`\<\{ `header`: `
|
|
119
|
+
`Promise`\<\{ `header`: `T`; `payload`: `U`; `signature`: `Uint8Array`; \}\>
|
|
120
120
|
|
|
121
121
|
The decoded payload.
|
|
122
122
|
|
|
@@ -232,24 +232,12 @@ True if the parts are verified.
|
|
|
232
232
|
|
|
233
233
|
### defaultSigner()
|
|
234
234
|
|
|
235
|
-
> `static` **defaultSigner**(`
|
|
235
|
+
> `static` **defaultSigner**(`header`, `payload`, `key`): `Promise`\<`string`\>
|
|
236
236
|
|
|
237
237
|
The default signer for the JWT.
|
|
238
238
|
|
|
239
239
|
#### Parameters
|
|
240
240
|
|
|
241
|
-
##### alg
|
|
242
|
-
|
|
243
|
-
`string`
|
|
244
|
-
|
|
245
|
-
The algorithm to use.
|
|
246
|
-
|
|
247
|
-
##### key
|
|
248
|
-
|
|
249
|
-
The key to sign with.
|
|
250
|
-
|
|
251
|
-
`undefined` | [`JwkCryptoKey`](../type-aliases/JwkCryptoKey.md)
|
|
252
|
-
|
|
253
241
|
##### header
|
|
254
242
|
|
|
255
243
|
[`IJwtHeader`](../interfaces/IJwtHeader.md)
|
|
@@ -262,6 +250,12 @@ The header to sign.
|
|
|
262
250
|
|
|
263
251
|
The payload to sign.
|
|
264
252
|
|
|
253
|
+
##### key
|
|
254
|
+
|
|
255
|
+
The optional key to sign with.
|
|
256
|
+
|
|
257
|
+
`undefined` | [`JwkCryptoKey`](../type-aliases/JwkCryptoKey.md)
|
|
258
|
+
|
|
265
259
|
#### Returns
|
|
266
260
|
|
|
267
261
|
`Promise`\<`string`\>
|
|
@@ -301,3 +295,139 @@ The key to verify with.
|
|
|
301
295
|
`Promise`\<\{ `header`: `T`; `payload`: `U`; \}\>
|
|
302
296
|
|
|
303
297
|
True if the signature was verified.
|
|
298
|
+
|
|
299
|
+
***
|
|
300
|
+
|
|
301
|
+
### toSigningBytes()
|
|
302
|
+
|
|
303
|
+
> `static` **toSigningBytes**\<`T`, `U`\>(`header`, `payload`): `Uint8Array`
|
|
304
|
+
|
|
305
|
+
Create bytes for signing from header and payload.
|
|
306
|
+
|
|
307
|
+
#### Type Parameters
|
|
308
|
+
|
|
309
|
+
• **T** *extends* [`IJwtHeader`](../interfaces/IJwtHeader.md)
|
|
310
|
+
|
|
311
|
+
• **U** *extends* [`IJwtPayload`](../interfaces/IJwtPayload.md)
|
|
312
|
+
|
|
313
|
+
#### Parameters
|
|
314
|
+
|
|
315
|
+
##### header
|
|
316
|
+
|
|
317
|
+
`T`
|
|
318
|
+
|
|
319
|
+
The header.
|
|
320
|
+
|
|
321
|
+
##### payload
|
|
322
|
+
|
|
323
|
+
`U`
|
|
324
|
+
|
|
325
|
+
The payload.
|
|
326
|
+
|
|
327
|
+
#### Returns
|
|
328
|
+
|
|
329
|
+
`Uint8Array`
|
|
330
|
+
|
|
331
|
+
The bytes to sign.
|
|
332
|
+
|
|
333
|
+
***
|
|
334
|
+
|
|
335
|
+
### fromSigningBytes()
|
|
336
|
+
|
|
337
|
+
> `static` **fromSigningBytes**\<`T`, `U`\>(`signingBytes`): `object`
|
|
338
|
+
|
|
339
|
+
Create header and payload from signing bytes.
|
|
340
|
+
|
|
341
|
+
#### Type Parameters
|
|
342
|
+
|
|
343
|
+
• **T** *extends* [`IJwtHeader`](../interfaces/IJwtHeader.md)
|
|
344
|
+
|
|
345
|
+
• **U** *extends* [`IJwtPayload`](../interfaces/IJwtPayload.md)
|
|
346
|
+
|
|
347
|
+
#### Parameters
|
|
348
|
+
|
|
349
|
+
##### signingBytes
|
|
350
|
+
|
|
351
|
+
`Uint8Array`
|
|
352
|
+
|
|
353
|
+
The signing bytes from a token.
|
|
354
|
+
|
|
355
|
+
#### Returns
|
|
356
|
+
|
|
357
|
+
`object`
|
|
358
|
+
|
|
359
|
+
The header and payload.
|
|
360
|
+
|
|
361
|
+
##### header
|
|
362
|
+
|
|
363
|
+
> **header**: `T`
|
|
364
|
+
|
|
365
|
+
##### payload
|
|
366
|
+
|
|
367
|
+
> **payload**: `U`
|
|
368
|
+
|
|
369
|
+
#### Throws
|
|
370
|
+
|
|
371
|
+
If the signing bytes are invalid
|
|
372
|
+
|
|
373
|
+
***
|
|
374
|
+
|
|
375
|
+
### tokenFromBytes()
|
|
376
|
+
|
|
377
|
+
> `static` **tokenFromBytes**(`signingBytes`, `signature`): `string`
|
|
378
|
+
|
|
379
|
+
Convert signed bytes and signature bytes to token.
|
|
380
|
+
|
|
381
|
+
#### Parameters
|
|
382
|
+
|
|
383
|
+
##### signingBytes
|
|
384
|
+
|
|
385
|
+
`Uint8Array`
|
|
386
|
+
|
|
387
|
+
The signed bytes.
|
|
388
|
+
|
|
389
|
+
##### signature
|
|
390
|
+
|
|
391
|
+
`Uint8Array`
|
|
392
|
+
|
|
393
|
+
The signature.
|
|
394
|
+
|
|
395
|
+
#### Returns
|
|
396
|
+
|
|
397
|
+
`string`
|
|
398
|
+
|
|
399
|
+
The token.
|
|
400
|
+
|
|
401
|
+
***
|
|
402
|
+
|
|
403
|
+
### tokenToBytes()
|
|
404
|
+
|
|
405
|
+
> `static` **tokenToBytes**(`token`): `object`
|
|
406
|
+
|
|
407
|
+
Convert the token to signing bytes and signature bytes.
|
|
408
|
+
|
|
409
|
+
#### Parameters
|
|
410
|
+
|
|
411
|
+
##### token
|
|
412
|
+
|
|
413
|
+
`string`
|
|
414
|
+
|
|
415
|
+
The token to convert to bytes.
|
|
416
|
+
|
|
417
|
+
#### Returns
|
|
418
|
+
|
|
419
|
+
`object`
|
|
420
|
+
|
|
421
|
+
The decoded bytes.
|
|
422
|
+
|
|
423
|
+
##### signingBytes
|
|
424
|
+
|
|
425
|
+
> **signingBytes**: `Uint8Array`
|
|
426
|
+
|
|
427
|
+
##### signature
|
|
428
|
+
|
|
429
|
+
> **signature**: `Uint8Array`
|
|
430
|
+
|
|
431
|
+
#### Throws
|
|
432
|
+
|
|
433
|
+
If the token is invalid.
|
package/locales/en.json
CHANGED
|
@@ -10,7 +10,9 @@
|
|
|
10
10
|
"jwt": {
|
|
11
11
|
"noKeyOrSigner": "No key or signer was provided for JWT creation",
|
|
12
12
|
"noKeyOrVerifier": "No key or verifier was provided for JWT creation",
|
|
13
|
-
"verifyFailed": "Failed to verify JWT"
|
|
13
|
+
"verifyFailed": "Failed to verify JWT",
|
|
14
|
+
"invalidTokenParts": "The JSON Web Token could not be parsed, it should contain three parts separated by dots",
|
|
15
|
+
"invalidSigningBytes": "The signing bytes are invalid, it should contain two parts separated by a dot"
|
|
14
16
|
},
|
|
15
17
|
"jwk": {
|
|
16
18
|
"jwkImportFailed": "Failed to import JWK"
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twin.org/web",
|
|
3
|
-
"version": "0.0.1-next.
|
|
3
|
+
"version": "0.0.1-next.39",
|
|
4
4
|
"description": "Contains classes for use with web operations",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -14,8 +14,8 @@
|
|
|
14
14
|
"node": ">=20.0.0"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@twin.org/core": "0.0.1-next.
|
|
18
|
-
"@twin.org/crypto": "0.0.1-next.
|
|
17
|
+
"@twin.org/core": "0.0.1-next.39",
|
|
18
|
+
"@twin.org/crypto": "0.0.1-next.39",
|
|
19
19
|
"@twin.org/nameof": "next",
|
|
20
20
|
"jose": "6.0.8"
|
|
21
21
|
},
|