@twin.org/web 0.0.1-next.37 → 0.0.1-next.38

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.
@@ -835,20 +835,18 @@ class Jwt {
835
835
  }
836
836
  /**
837
837
  * The default signer for the JWT.
838
- * @param alg The algorithm to use.
839
- * @param key The key to sign with.
840
838
  * @param header The header to sign.
841
839
  * @param payload The payload to sign.
840
+ * @param key The optional key to sign with.
842
841
  * @returns The signature.
843
842
  */
844
- static async defaultSigner(alg, key, header, payload) {
845
- core.Guards.defined(Jwt._CLASS_NAME, "key", key);
843
+ static async defaultSigner(header, payload, key) {
846
844
  core.Guards.object(Jwt._CLASS_NAME, "header", header);
847
845
  core.Guards.object(Jwt._CLASS_NAME, "payload", payload);
846
+ core.Guards.defined(Jwt._CLASS_NAME, "key", key);
848
847
  const signer = new jose.SignJWT(payload);
849
- header.alg = alg;
850
848
  signer.setProtectedHeader(header);
851
- if (alg === "EdDSA" && core.Is.uint8Array(key)) {
849
+ if (header.alg === "EdDSA" && core.Is.uint8Array(key)) {
852
850
  // crypto.subtle.importKey does not support Ed25519 keys in raw format.
853
851
  // We need to convert the key to PKCS8 format before importing.
854
852
  // The PKCS8 format is the raw key prefixed with the ASN.1 sequence for an Ed25519 private key.
@@ -884,6 +882,31 @@ class Jwt {
884
882
  throw new core.GeneralError(Jwt._CLASS_NAME, "verifyFailed", undefined, err);
885
883
  }
886
884
  }
885
+ /**
886
+ * Create bytes for signing from header and payload.
887
+ * @param header The header.
888
+ * @param payload The payload.
889
+ * @returns The bytes to sign.
890
+ */
891
+ static createSignBytes(header, payload) {
892
+ const segments = [];
893
+ const headerBytes = core.Converter.utf8ToBytes(JSON.stringify(header));
894
+ segments.push(core.Converter.bytesToBase64Url(headerBytes));
895
+ const payloadBytes = core.Converter.utf8ToBytes(JSON.stringify(payload));
896
+ segments.push(core.Converter.bytesToBase64Url(payloadBytes));
897
+ return core.Converter.utf8ToBytes(segments.join("."));
898
+ }
899
+ /**
900
+ * Create token from bytes and signature.
901
+ * @param signedBytes The signed bytes.
902
+ * @param signature The signature.
903
+ * @returns The token.
904
+ */
905
+ static createTokenFromBytes(signedBytes, signature) {
906
+ const signedBytesUtf8 = core.Converter.bytesToUtf8(signedBytes);
907
+ const signatureBase64 = core.Converter.bytesToBase64Url(signature);
908
+ return `${signedBytesUtf8}.${signatureBase64}`;
909
+ }
887
910
  /**
888
911
  * Encode a token.
889
912
  * @param header The header to encode.
@@ -899,11 +922,11 @@ class Jwt {
899
922
  if (!hasKey && !hasSigner) {
900
923
  throw new core.GeneralError(Jwt._CLASS_NAME, "noKeyOrSigner");
901
924
  }
902
- signer ??= async (alg, k, h, p) => Jwt.defaultSigner(alg, k, h, p);
925
+ signer ??= async (h, p, k) => Jwt.defaultSigner(h, p, k);
903
926
  if (core.Is.undefined(header.typ)) {
904
927
  header.typ = "JWT";
905
928
  }
906
- return signer(header.alg, key, header, payload);
929
+ return signer(header, payload, key);
907
930
  }
908
931
  }
909
932
 
@@ -833,20 +833,18 @@ class Jwt {
833
833
  }
834
834
  /**
835
835
  * The default signer for the JWT.
836
- * @param alg The algorithm to use.
837
- * @param key The key to sign with.
838
836
  * @param header The header to sign.
839
837
  * @param payload The payload to sign.
838
+ * @param key The optional key to sign with.
840
839
  * @returns The signature.
841
840
  */
842
- static async defaultSigner(alg, key, header, payload) {
843
- Guards.defined(Jwt._CLASS_NAME, "key", key);
841
+ static async defaultSigner(header, payload, key) {
844
842
  Guards.object(Jwt._CLASS_NAME, "header", header);
845
843
  Guards.object(Jwt._CLASS_NAME, "payload", payload);
844
+ Guards.defined(Jwt._CLASS_NAME, "key", key);
846
845
  const signer = new SignJWT(payload);
847
- header.alg = alg;
848
846
  signer.setProtectedHeader(header);
849
- if (alg === "EdDSA" && Is.uint8Array(key)) {
847
+ if (header.alg === "EdDSA" && Is.uint8Array(key)) {
850
848
  // crypto.subtle.importKey does not support Ed25519 keys in raw format.
851
849
  // We need to convert the key to PKCS8 format before importing.
852
850
  // The PKCS8 format is the raw key prefixed with the ASN.1 sequence for an Ed25519 private key.
@@ -882,6 +880,31 @@ class Jwt {
882
880
  throw new GeneralError(Jwt._CLASS_NAME, "verifyFailed", undefined, err);
883
881
  }
884
882
  }
883
+ /**
884
+ * Create bytes for signing from header and payload.
885
+ * @param header The header.
886
+ * @param payload The payload.
887
+ * @returns The bytes to sign.
888
+ */
889
+ static createSignBytes(header, payload) {
890
+ const segments = [];
891
+ const headerBytes = Converter.utf8ToBytes(JSON.stringify(header));
892
+ segments.push(Converter.bytesToBase64Url(headerBytes));
893
+ const payloadBytes = Converter.utf8ToBytes(JSON.stringify(payload));
894
+ segments.push(Converter.bytesToBase64Url(payloadBytes));
895
+ return Converter.utf8ToBytes(segments.join("."));
896
+ }
897
+ /**
898
+ * Create token from bytes and signature.
899
+ * @param signedBytes The signed bytes.
900
+ * @param signature The signature.
901
+ * @returns The token.
902
+ */
903
+ static createTokenFromBytes(signedBytes, signature) {
904
+ const signedBytesUtf8 = Converter.bytesToUtf8(signedBytes);
905
+ const signatureBase64 = Converter.bytesToBase64Url(signature);
906
+ return `${signedBytesUtf8}.${signatureBase64}`;
907
+ }
885
908
  /**
886
909
  * Encode a token.
887
910
  * @param header The header to encode.
@@ -897,11 +920,11 @@ class Jwt {
897
920
  if (!hasKey && !hasSigner) {
898
921
  throw new GeneralError(Jwt._CLASS_NAME, "noKeyOrSigner");
899
922
  }
900
- signer ??= async (alg, k, h, p) => Jwt.defaultSigner(alg, k, h, p);
923
+ signer ??= async (h, p, k) => Jwt.defaultSigner(h, p, k);
901
924
  if (Is.undefined(header.typ)) {
902
925
  header.typ = "JWT";
903
926
  }
904
- return signer(header.alg, key, header, payload);
927
+ return signer(header, payload, key);
905
928
  }
906
929
  }
907
930
 
@@ -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<U extends IJwtHeader, T extends IJwtPayload>(header: U, payload: T, key: JwkCryptoKey): Promise<string>;
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<U extends IJwtHeader, T extends IJwtPayload>(header: U, payload: T, signer: (alg: string, key: JwkCryptoKey | undefined, header: IJwtHeader, payload: IJwtPayload) => Promise<string>): Promise<string>;
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<U extends IJwtHeader, T extends IJwtPayload>(token: string): Promise<{
30
- header?: U;
31
- payload?: T;
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(alg: string, key: JwkCryptoKey | undefined, header: IJwtHeader, payload: IJwtPayload): Promise<string>;
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,18 @@ 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 createSignBytes<T extends IJwtHeader, U extends IJwtPayload>(header: T, payload: U): Uint8Array;
96
+ /**
97
+ * Create token from bytes and signature.
98
+ * @param signedBytes The signed bytes.
99
+ * @param signature The signature.
100
+ * @returns The token.
101
+ */
102
+ static createTokenFromBytes(signedBytes: Uint8Array, signature: Uint8Array): string;
90
103
  }
package/docs/changelog.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # @twin.org/web - Changelog
2
2
 
3
- ## 0.0.1-next.37
3
+ ## 0.0.1-next.38
4
4
 
5
5
  - Initial Release
@@ -16,27 +16,27 @@ Class to handle JSON Web Tokens.
16
16
 
17
17
  ### encode()
18
18
 
19
- > `static` **encode**\<`U`, `T`\>(`header`, `payload`, `key`): `Promise`\<`string`\>
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
- • **U** *extends* [`IJwtHeader`](../interfaces/IJwtHeader.md)
25
+ • **T** *extends* [`IJwtHeader`](../interfaces/IJwtHeader.md)
26
26
 
27
- • **T** *extends* [`IJwtPayload`](../interfaces/IJwtPayload.md)
27
+ • **U** *extends* [`IJwtPayload`](../interfaces/IJwtPayload.md)
28
28
 
29
29
  #### Parameters
30
30
 
31
31
  ##### header
32
32
 
33
- `U`
33
+ `T`
34
34
 
35
35
  The header to encode.
36
36
 
37
37
  ##### payload
38
38
 
39
- `T`
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**\<`U`, `T`\>(`header`, `payload`, `signer`): `Promise`\<`string`\>
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
- • **U** *extends* [`IJwtHeader`](../interfaces/IJwtHeader.md)
65
+ • **T** *extends* [`IJwtHeader`](../interfaces/IJwtHeader.md)
66
66
 
67
- • **T** *extends* [`IJwtPayload`](../interfaces/IJwtPayload.md)
67
+ • **U** *extends* [`IJwtPayload`](../interfaces/IJwtPayload.md)
68
68
 
69
69
  #### Parameters
70
70
 
71
71
  ##### header
72
72
 
73
- `U`
73
+ `T`
74
74
 
75
75
  The header to encode.
76
76
 
77
77
  ##### payload
78
78
 
79
- `T`
79
+ `U`
80
80
 
81
81
  The payload to encode.
82
82
 
83
83
  ##### signer
84
84
 
85
- (`alg`, `key`, `header`, `payload`) => `Promise`\<`string`\>
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**\<`U`, `T`\>(`token`): `Promise`\<\{ `header`: `U`; `payload`: `T`; `signature`: `Uint8Array`; \}\>
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
- • **U** *extends* [`IJwtHeader`](../interfaces/IJwtHeader.md)
105
+ • **T** *extends* [`IJwtHeader`](../interfaces/IJwtHeader.md)
106
106
 
107
- • **T** *extends* [`IJwtPayload`](../interfaces/IJwtPayload.md)
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`: `U`; `payload`: `T`; `signature`: `Uint8Array`; \}\>
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**(`alg`, `key`, `header`, `payload`): `Promise`\<`string`\>
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,65 @@ 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
+ ### createSignBytes()
302
+
303
+ > `static` **createSignBytes**\<`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
+ ### createTokenFromBytes()
336
+
337
+ > `static` **createTokenFromBytes**(`signedBytes`, `signature`): `string`
338
+
339
+ Create token from bytes and signature.
340
+
341
+ #### Parameters
342
+
343
+ ##### signedBytes
344
+
345
+ `Uint8Array`
346
+
347
+ The signed bytes.
348
+
349
+ ##### signature
350
+
351
+ `Uint8Array`
352
+
353
+ The signature.
354
+
355
+ #### Returns
356
+
357
+ `string`
358
+
359
+ The token.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/web",
3
- "version": "0.0.1-next.37",
3
+ "version": "0.0.1-next.38",
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.37",
18
- "@twin.org/crypto": "0.0.1-next.37",
17
+ "@twin.org/core": "0.0.1-next.38",
18
+ "@twin.org/crypto": "0.0.1-next.38",
19
19
  "@twin.org/nameof": "next",
20
20
  "jose": "6.0.8"
21
21
  },