@twin.org/web 0.0.1-next.4 → 0.0.1-next.40

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.
@@ -1,8 +1,8 @@
1
1
  import type { IJwtHeader } from "../models/IJwtHeader";
2
2
  import type { IJwtPayload } from "../models/IJwtPayload";
3
- import { JwtAlgorithms } from "../models/jwtAlgorithms";
3
+ import type { JwkCryptoKey } from "../models/jwkCryptoKey";
4
4
  /**
5
- * Class to encode and decode JSON Web Tokens.
5
+ * Class to handle JSON Web Tokens.
6
6
  */
7
7
  export declare class Jwt {
8
8
  /**
@@ -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: Uint8Array): 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: JwtAlgorithms, key: Uint8Array | undefined, payload: Uint8Array) => Promise<Uint8Array>): 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
  /**
@@ -37,11 +37,9 @@ export declare class Jwt {
37
37
  * @param key The key for verifying the token
38
38
  * @returns The decoded payload.
39
39
  */
40
- static verify<U extends IJwtHeader, T extends IJwtPayload>(token: string, key: Uint8Array): Promise<{
41
- verified: boolean;
42
- header?: U;
43
- payload?: T;
44
- signature?: Uint8Array;
40
+ static verify<T extends IJwtHeader, U extends IJwtPayload>(token: string, key: JwkCryptoKey): Promise<{
41
+ header: T;
42
+ payload: U;
45
43
  }>;
46
44
  /**
47
45
  * Verify a token.
@@ -49,37 +47,77 @@ export declare class Jwt {
49
47
  * @param verifier Custom verification method.
50
48
  * @returns The decoded payload.
51
49
  */
52
- static verifyWithVerifier<U extends IJwtHeader, T extends IJwtPayload>(token: string, verifier: (alg: JwtAlgorithms, key: Uint8Array | undefined, payload: Uint8Array, signature: Uint8Array) => Promise<boolean>): Promise<{
53
- verified: boolean;
54
- header?: U;
55
- payload?: T;
56
- signature?: Uint8Array;
50
+ static verifyWithVerifier<T extends IJwtHeader, U extends IJwtPayload>(token: string, verifier: (token: string, key: JwkCryptoKey | undefined) => Promise<{
51
+ header: T;
52
+ payload: U;
53
+ }>): Promise<{
54
+ header: T;
55
+ payload: U;
57
56
  }>;
58
57
  /**
59
58
  * Verify a token by parts.
60
- * @param header The header to verify.
61
- * @param payload The payload to verify.
62
- * @param signature The signature to verify.
59
+ * @param token The token to verify.
63
60
  * @param key The key for verifying the token, if not provided no verification occurs.
64
61
  * @param verifier Custom verification method.
65
62
  * @returns True if the parts are verified.
66
63
  */
67
- static verifySignature<U extends IJwtHeader, T extends IJwtPayload>(header?: U, payload?: T, signature?: Uint8Array, key?: Uint8Array, verifier?: (alg: JwtAlgorithms, key: Uint8Array | undefined, payload: Uint8Array, signature: Uint8Array) => Promise<boolean>): Promise<boolean>;
64
+ static verifySignature<T extends IJwtHeader, U extends IJwtPayload>(token: string, key?: JwkCryptoKey, verifier?: (token: string, key: JwkCryptoKey | undefined) => Promise<{
65
+ header: T;
66
+ payload: U;
67
+ }>): Promise<{
68
+ header: T;
69
+ payload: U;
70
+ }>;
68
71
  /**
69
72
  * The default signer for the JWT.
70
- * @param alg The algorithm to use.
71
- * @param key The key to sign with.
73
+ * @param header The header to sign.
72
74
  * @param payload The payload to sign.
75
+ * @param key The optional key to sign with.
73
76
  * @returns The signature.
74
77
  */
75
- static defaultSigner(alg: JwtAlgorithms, key: Uint8Array | undefined, payload: Uint8Array): Promise<Uint8Array>;
78
+ static defaultSigner(header: IJwtHeader, payload: IJwtPayload, key: JwkCryptoKey | undefined): Promise<string>;
76
79
  /**
77
80
  * The default verifier for the JWT.
78
- * @param alg The algorithm to use.
81
+ * @param token The token to verify.
79
82
  * @param key The key to verify with.
80
- * @param payload The payload to verify.
81
- * @param signature The signature to verify.
82
- * @returns True if the signature was verified.
83
+ * @returns The header and payload if verification successful.
84
+ */
85
+ static defaultVerifier<T extends IJwtHeader, U extends IJwtPayload>(token: string, key: JwkCryptoKey | undefined): Promise<{
86
+ header: T;
87
+ payload: U;
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.
83
118
  */
84
- static defaultVerifier(alg: JwtAlgorithms, key: Uint8Array | undefined, payload: Uint8Array, signature: Uint8Array): Promise<boolean>;
119
+ static tokenToBytes(token: string): {
120
+ signingBytes: Uint8Array;
121
+ signature: Uint8Array;
122
+ };
85
123
  }
package/docs/changelog.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # @twin.org/web - Changelog
2
2
 
3
- ## 0.0.1-next.4
3
+ ## 0.0.1-next.40
4
4
 
5
5
  - Initial Release
@@ -16,23 +16,31 @@ Create a new instance of FetchError.
16
16
 
17
17
  #### Parameters
18
18
 
19
- **source**: `string`
19
+ ##### source
20
+
21
+ `string`
20
22
 
21
23
  The source of the error.
22
24
 
23
- **message**: `string`
25
+ ##### message
26
+
27
+ `string`
24
28
 
25
29
  The message as a code.
26
30
 
27
- **httpStatus**: [`HttpStatusCode`](../type-aliases/HttpStatusCode.md)
31
+ ##### httpStatus
32
+
33
+ [`HttpStatusCode`](../type-aliases/HttpStatusCode.md)
28
34
 
29
35
  The http status code.
30
36
 
31
- **properties?**
37
+ ##### properties?
32
38
 
33
39
  Any additional information for the error.
34
40
 
35
- **inner?**: `unknown`
41
+ ##### inner?
42
+
43
+ `unknown`
36
44
 
37
45
  The inner error if we have wrapped another error.
38
46
 
@@ -22,23 +22,33 @@ Perform a fetch request.
22
22
 
23
23
  #### Parameters
24
24
 
25
- **source**: `string`
25
+ ##### source
26
+
27
+ `string`
26
28
 
27
29
  The source for the request.
28
30
 
29
- **url**: `string`
31
+ ##### url
32
+
33
+ `string`
30
34
 
31
35
  The url for the request.
32
36
 
33
- **method**: [`HttpMethod`](../type-aliases/HttpMethod.md)
37
+ ##### method
38
+
39
+ [`HttpMethod`](../type-aliases/HttpMethod.md)
34
40
 
35
41
  The http method.
36
42
 
37
- **body?**: `string` \| `Uint8Array`
43
+ ##### body?
38
44
 
39
45
  Request to send to the endpoint.
40
46
 
41
- **options?**: `Omit`\<[`IFetchOptions`](../interfaces/IFetchOptions.md), `"cacheTtlSeconds"`\>
47
+ `string` | `Uint8Array`
48
+
49
+ ##### options?
50
+
51
+ `Omit`\<[`IFetchOptions`](../interfaces/IFetchOptions.md), `"cacheTtlSeconds"`\>
42
52
 
43
53
  Options for sending the requests.
44
54
 
@@ -64,23 +74,33 @@ Perform a request in json format.
64
74
 
65
75
  #### Parameters
66
76
 
67
- **source**: `string`
77
+ ##### source
78
+
79
+ `string`
68
80
 
69
81
  The source for the request.
70
82
 
71
- **url**: `string`
83
+ ##### url
84
+
85
+ `string`
72
86
 
73
87
  The url for the request.
74
88
 
75
- **method**: [`HttpMethod`](../type-aliases/HttpMethod.md)
89
+ ##### method
90
+
91
+ [`HttpMethod`](../type-aliases/HttpMethod.md)
76
92
 
77
93
  The http method.
78
94
 
79
- **requestData?**: `T`
95
+ ##### requestData?
96
+
97
+ `T`
80
98
 
81
99
  Request to send to the endpoint.
82
100
 
83
- **options?**: [`IFetchOptions`](../interfaces/IFetchOptions.md)
101
+ ##### options?
102
+
103
+ [`IFetchOptions`](../interfaces/IFetchOptions.md)
84
104
 
85
105
  Options for sending the requests.
86
106
 
@@ -104,23 +124,33 @@ Perform a request for binary data.
104
124
 
105
125
  #### Parameters
106
126
 
107
- **source**: `string`
127
+ ##### source
128
+
129
+ `string`
108
130
 
109
131
  The source for the request.
110
132
 
111
- **url**: `string`
133
+ ##### url
134
+
135
+ `string`
112
136
 
113
137
  The url for the request.
114
138
 
115
- **method**: `"GET"` \| `"POST"`
139
+ ##### method
116
140
 
117
141
  The http method.
118
142
 
119
- **requestData?**: `Uint8Array`
143
+ `"GET"` | `"POST"`
144
+
145
+ ##### requestData?
146
+
147
+ `Uint8Array`
120
148
 
121
149
  Request to send to the endpoint.
122
150
 
123
- **options?**: [`IFetchOptions`](../interfaces/IFetchOptions.md)
151
+ ##### options?
152
+
153
+ [`IFetchOptions`](../interfaces/IFetchOptions.md)
124
154
 
125
155
  Options for sending the requests.
126
156
 
@@ -156,7 +186,9 @@ Get a cache entry.
156
186
 
157
187
  #### Parameters
158
188
 
159
- **url**: `string`
189
+ ##### url
190
+
191
+ `string`
160
192
 
161
193
  The url for the request.
162
194
 
@@ -176,7 +208,9 @@ Remove a cache entry.
176
208
 
177
209
  #### Parameters
178
210
 
179
- **url**: `string`
211
+ ##### url
212
+
213
+ `string`
180
214
 
181
215
  The url for the request.
182
216
 
@@ -0,0 +1,35 @@
1
+ # Class: Jwk
2
+
3
+ Class to handle JSON Web Keys.
4
+
5
+ ## Constructors
6
+
7
+ ### new Jwk()
8
+
9
+ > **new Jwk**(): [`Jwk`](Jwk.md)
10
+
11
+ #### Returns
12
+
13
+ [`Jwk`](Jwk.md)
14
+
15
+ ## Methods
16
+
17
+ ### toCryptoKey()
18
+
19
+ > `static` **toCryptoKey**(`jwk`): `Promise`\<[`JwkCryptoKey`](../type-aliases/JwkCryptoKey.md)\>
20
+
21
+ Convert the JWK to a crypto key.
22
+
23
+ #### Parameters
24
+
25
+ ##### jwk
26
+
27
+ [`IJwk`](../interfaces/IJwk.md)
28
+
29
+ The JWK to convert.
30
+
31
+ #### Returns
32
+
33
+ `Promise`\<[`JwkCryptoKey`](../type-aliases/JwkCryptoKey.md)\>
34
+
35
+ The crypto key.