@liuhlightning/jwt 0.4.0 → 0.5.0
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/README.md +54 -24
- package/dist/error.d.ts +22 -4
- package/dist/error.js +61 -8
- package/dist/index.d.ts +137 -24
- package/dist/index.js +585 -97
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -19,15 +19,25 @@ yarn add @liuhlightning/jwt
|
|
|
19
19
|
```typescript
|
|
20
20
|
import JWT from "@liuhlightning/jwt";
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
// An HMAC secret must be at least 32 bytes.
|
|
23
|
+
const jwt = new JWT("a-string-secret-at-least-256-bits-long");
|
|
23
24
|
```
|
|
24
25
|
|
|
25
26
|
### Sign a token
|
|
26
27
|
|
|
27
28
|
```typescript
|
|
28
|
-
const token = jwt.sign({
|
|
29
|
+
const token = jwt.sign({
|
|
30
|
+
uid: 1234,
|
|
31
|
+
username: "alex",
|
|
32
|
+
exp: jwt.now + 3600, // optional, but recommended
|
|
33
|
+
});
|
|
29
34
|
```
|
|
30
35
|
|
|
36
|
+
`exp` is optional. A token without `exp` never expires, so production tokens
|
|
37
|
+
should normally set it. The `expirationTime` option is an alternative way to set
|
|
38
|
+
it. Set `requireExpiration: true` on the instance (or per call) to reject tokens
|
|
39
|
+
that do not have an expiration time.
|
|
40
|
+
|
|
31
41
|
### Verify a token and use payload
|
|
32
42
|
|
|
33
43
|
```typescript
|
|
@@ -50,20 +60,23 @@ try {
|
|
|
50
60
|
|
|
51
61
|
## Algorithms
|
|
52
62
|
|
|
53
|
-
| Algorithm
|
|
54
|
-
|
|
|
55
|
-
| `HS256` `HS384` `HS512`
|
|
56
|
-
| `RS256` `RS384` `RS512`
|
|
63
|
+
| Algorithm | Key |
|
|
64
|
+
| -------------------------------- | -------------------------------- |
|
|
65
|
+
| `HS256` `HS384` `HS512` | secret (string / Buffer, >= 32B) |
|
|
66
|
+
| `RS256` `RS384` `RS512` | RSA private / public key |
|
|
67
|
+
| `PS256` `PS384` `PS512` | RSA private / public key |
|
|
68
|
+
| `ES256` `ES256K` `ES384` `ES512` | EC private / public key |
|
|
57
69
|
|
|
58
|
-
The default is `HS256` for a secret,
|
|
59
|
-
|
|
70
|
+
The default is `HS256` for a secret, `RS256` (or `PS256` for an RSA-PSS key) for
|
|
71
|
+
an RSA key, and the matching `ES*` algorithm for an EC key. The `alg` in the
|
|
72
|
+
token header must be the same as the algorithm of the instance.
|
|
60
73
|
|
|
61
74
|
### Use RSA key
|
|
62
75
|
|
|
63
76
|
```typescript
|
|
64
77
|
// sign and verify, the public key is derived from the private key
|
|
65
78
|
const signer = new JWT({ privateKey: "-----BEGIN PRIVATE KEY-----..." });
|
|
66
|
-
const token = signer.sign({ uid: 1234 });
|
|
79
|
+
const token = signer.sign({ uid: 1234, exp: signer.now + 3600 });
|
|
67
80
|
|
|
68
81
|
// verify only
|
|
69
82
|
const verifier = new JWT(
|
|
@@ -73,6 +86,10 @@ const verifier = new JWT(
|
|
|
73
86
|
const payload = verifier.verify(token);
|
|
74
87
|
```
|
|
75
88
|
|
|
89
|
+
A PEM string passed directly (`new JWT(pemString)`) is rejected instead of
|
|
90
|
+
being silently used as an HMAC secret. RSA keys must be at least 2048 bits, and
|
|
91
|
+
EC keys must use the curve required by the selected algorithm.
|
|
92
|
+
|
|
76
93
|
A JWK is also supported.
|
|
77
94
|
|
|
78
95
|
```typescript
|
|
@@ -84,22 +101,26 @@ const verifier = new JWT({ publicKey: { format: "jwk", key: jwk } });
|
|
|
84
101
|
### Constructor
|
|
85
102
|
|
|
86
103
|
```typescript
|
|
87
|
-
const jwt = new JWT("
|
|
104
|
+
const jwt = new JWT("a-string-secret-at-least-256-bits-long", {
|
|
88
105
|
algorithm: "HS512",
|
|
89
106
|
issuer: "my-service",
|
|
90
107
|
});
|
|
91
108
|
```
|
|
92
109
|
|
|
93
|
-
| Option
|
|
94
|
-
|
|
|
95
|
-
| `algorithm`
|
|
96
|
-
| `issuer`
|
|
97
|
-
| `subject`
|
|
98
|
-
| `audience`
|
|
99
|
-
| `
|
|
100
|
-
| `
|
|
101
|
-
| `
|
|
102
|
-
| `
|
|
110
|
+
| Option | Claim | Description |
|
|
111
|
+
| ------------------- | ----- | ------------------------------------------------------------- |
|
|
112
|
+
| `algorithm` | | The algorithm used to sign and verify |
|
|
113
|
+
| `issuer` | `iss` | Set when sign, and must be the same when verify |
|
|
114
|
+
| `subject` | `sub` | Set when sign, and must be the same when verify |
|
|
115
|
+
| `audience` | `aud` | `string` or `string[]`, set when sign and matched when verify |
|
|
116
|
+
| `kid` | `kid` | Key id, set when sign and must be the same when verify |
|
|
117
|
+
| `jwtID` | `jti` | Set when sign, and must be the same when verify |
|
|
118
|
+
| `expirationTime` | `exp` | Unix timestamp in seconds, set when sign |
|
|
119
|
+
| `notBefore` | `nbf` | Unix timestamp in seconds, set when sign |
|
|
120
|
+
| `issuedAt` | `iat` | Unix timestamp in seconds, set when sign, default is now |
|
|
121
|
+
| `requireExpiration` | | Require `exp`, default `false` |
|
|
122
|
+
| `clockTolerance` | | Clock tolerance in seconds, default `0` |
|
|
123
|
+
| `maxTokenAge` | | Maximum age in seconds measured from `iat`, verify only |
|
|
103
124
|
|
|
104
125
|
`expirationTime`, `notBefore` and `issuedAt` are absolute timestamps, not durations.
|
|
105
126
|
To make every token expire after a period, set `exp` in the payload.
|
|
@@ -110,9 +131,18 @@ const token = jwt.sign({ uid: 1234, exp: jwt.now + 3600 });
|
|
|
110
131
|
|
|
111
132
|
### Sign and verify
|
|
112
133
|
|
|
113
|
-
`sign(payload, options)` accepts `issuer`, `subject`, `audience`, `jwtID`, `expirationTime`, `notBefore` and `
|
|
134
|
+
`sign(payload, options)` accepts `issuer`, `subject`, `audience`, `kid`, `jwtID`, `expirationTime`, `notBefore`, `issuedAt` and `requireExpiration`.
|
|
135
|
+
|
|
136
|
+
`verify(token, options)` accepts `issuer`, `subject`, `audience`, `kid`, `jwtID`, `requireExpiration`, `clockTolerance`, `maxTokenAge`, and `currentTime` (in milliseconds, default is `Date.now()`). A `currentTime` that is not a finite number is a mistake of the caller, so `verify` throws a `TypeError` for it.
|
|
114
137
|
|
|
115
|
-
|
|
116
|
-
|
|
138
|
+
Tokens are parsed strictly: exactly three parts, canonical unpadded base64url
|
|
139
|
+
segments, and `exp`/`nbf`/`iat` must be finite numbers. The signature is checked
|
|
140
|
+
before any claim in the payload is read.
|
|
117
141
|
|
|
118
|
-
|
|
142
|
+
For registered claim values (`issuer`, `subject`, `audience`, `jwtID`,
|
|
143
|
+
`expirationTime`, `notBefore`, `issuedAt`), the instance options take precedence
|
|
144
|
+
over the options of `sign` / `verify`.
|
|
145
|
+
`requireExpiration`, `allowNoExpiration`, `clockTolerance`, `maxTokenAge` and
|
|
146
|
+
`kid` can be overridden per call: a value passed to `sign` / `verify` takes
|
|
147
|
+
precedence over the instance setting. The claims in the payload take precedence
|
|
148
|
+
over both when sign.
|
package/dist/error.d.ts
CHANGED
|
@@ -13,17 +13,23 @@ export declare class JWTErrorSecretNotFound extends JWTError {
|
|
|
13
13
|
export declare class JWTErrorPrivateKeyNotFound extends JWTError {
|
|
14
14
|
constructor();
|
|
15
15
|
}
|
|
16
|
+
export declare class JWTErrorInvalidKey extends JWTError {
|
|
17
|
+
constructor(reason: string);
|
|
18
|
+
}
|
|
19
|
+
export declare class JWTErrorExpirationRequired extends JWTError {
|
|
20
|
+
constructor();
|
|
21
|
+
}
|
|
16
22
|
export declare class JWTErrorInvalidType extends JWTError {
|
|
17
23
|
constructor(type: string);
|
|
18
24
|
}
|
|
19
25
|
export declare class JWTErrorInvalidIssuer extends JWTError {
|
|
20
|
-
constructor(issuer:
|
|
26
|
+
constructor(issuer: unknown, expect: string);
|
|
21
27
|
}
|
|
22
28
|
export declare class JWTErrorInvalidSubject extends JWTError {
|
|
23
|
-
constructor(subject:
|
|
29
|
+
constructor(subject: unknown, expect: string);
|
|
24
30
|
}
|
|
25
31
|
export declare class JWTErrorInvalidAudience extends JWTError {
|
|
26
|
-
constructor(audience:
|
|
32
|
+
constructor(audience: unknown, expect: string | string[]);
|
|
27
33
|
}
|
|
28
34
|
export declare class JWTErrorExpired extends JWTError {
|
|
29
35
|
constructor(expiredAt: number);
|
|
@@ -31,8 +37,11 @@ export declare class JWTErrorExpired extends JWTError {
|
|
|
31
37
|
export declare class JWTErrorNotBefore extends JWTError {
|
|
32
38
|
constructor(notBefore: number);
|
|
33
39
|
}
|
|
40
|
+
export declare class JWTErrorTokenTooOld extends JWTError {
|
|
41
|
+
constructor(issuedAt: number, maxTokenAge: number);
|
|
42
|
+
}
|
|
34
43
|
export declare class JWTErrorInvalidJwtId extends JWTError {
|
|
35
|
-
constructor(jwtId:
|
|
44
|
+
constructor(jwtId: unknown, expect: string);
|
|
36
45
|
}
|
|
37
46
|
export declare class JWTErrorInvalidSignature extends JWTError {
|
|
38
47
|
constructor();
|
|
@@ -49,4 +58,13 @@ export declare class JWTErrorInvalidAlgorithm extends JWTError {
|
|
|
49
58
|
export declare class JWTErrorInvalidToken extends JWTError {
|
|
50
59
|
constructor(reason: string);
|
|
51
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* A token that could not be decoded or parsed.
|
|
63
|
+
*
|
|
64
|
+
* This is a subclass of {@link JWTErrorInvalidToken} so callers that already
|
|
65
|
+
* catch `JWTErrorInvalidToken` keep working.
|
|
66
|
+
*/
|
|
67
|
+
export declare class JWTErrorMalformed extends JWTErrorInvalidToken {
|
|
68
|
+
constructor(reason: string);
|
|
69
|
+
}
|
|
52
70
|
//# sourceMappingURL=error.d.ts.map
|
package/dist/error.js
CHANGED
|
@@ -1,15 +1,34 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.JWTErrorInvalidToken = exports.JWTErrorInvalidAlgorithm = exports.JWTErrorInvalidTokenLength = exports.JWTErrorPublicKeyNotFound = exports.JWTErrorInvalidSignature = exports.JWTErrorInvalidJwtId = exports.JWTErrorNotBefore = exports.JWTErrorExpired = exports.JWTErrorInvalidAudience = exports.JWTErrorInvalidSubject = exports.JWTErrorInvalidIssuer = exports.JWTErrorInvalidType = exports.JWTErrorPrivateKeyNotFound = exports.JWTErrorSecretNotFound = exports.JWTErrorAlgorithmNotSupport = exports.JWTError = void 0;
|
|
3
|
+
exports.JWTErrorMalformed = exports.JWTErrorInvalidToken = exports.JWTErrorInvalidAlgorithm = exports.JWTErrorInvalidTokenLength = exports.JWTErrorPublicKeyNotFound = exports.JWTErrorInvalidSignature = exports.JWTErrorInvalidJwtId = exports.JWTErrorTokenTooOld = exports.JWTErrorNotBefore = exports.JWTErrorExpired = exports.JWTErrorInvalidAudience = exports.JWTErrorInvalidSubject = exports.JWTErrorInvalidIssuer = exports.JWTErrorInvalidType = exports.JWTErrorExpirationRequired = exports.JWTErrorInvalidKey = exports.JWTErrorPrivateKeyNotFound = exports.JWTErrorSecretNotFound = exports.JWTErrorAlgorithmNotSupport = exports.JWTError = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* Error class for JWT
|
|
6
6
|
*/
|
|
7
7
|
class JWTError extends Error {
|
|
8
8
|
constructor(message) {
|
|
9
9
|
super(message);
|
|
10
|
+
this.name = new.target.name;
|
|
10
11
|
}
|
|
11
12
|
}
|
|
12
13
|
exports.JWTError = JWTError;
|
|
14
|
+
/**
|
|
15
|
+
* The maximum absolute value accepted by `Date`, in milliseconds.
|
|
16
|
+
*
|
|
17
|
+
* `new Date(ms).toISOString()` throws a `RangeError` outside of this range.
|
|
18
|
+
*/
|
|
19
|
+
const MAX_DATE_MS = 8.64e15;
|
|
20
|
+
/**
|
|
21
|
+
* Format a Unix timestamp in seconds without throwing for out-of-range values.
|
|
22
|
+
*/
|
|
23
|
+
function formatTimestamp(seconds) {
|
|
24
|
+
if (!Number.isFinite(seconds))
|
|
25
|
+
return String(seconds);
|
|
26
|
+
const ms = seconds * 1000;
|
|
27
|
+
if (!Number.isFinite(ms) || Math.abs(ms) > MAX_DATE_MS) {
|
|
28
|
+
return `${seconds} (out of range)`;
|
|
29
|
+
}
|
|
30
|
+
return new Date(ms).toISOString();
|
|
31
|
+
}
|
|
13
32
|
class JWTErrorAlgorithmNotSupport extends JWTError {
|
|
14
33
|
constructor(algorithm) {
|
|
15
34
|
super(`Algorithm \`${algorithm}\` is not supported`);
|
|
@@ -28,6 +47,18 @@ class JWTErrorPrivateKeyNotFound extends JWTError {
|
|
|
28
47
|
}
|
|
29
48
|
}
|
|
30
49
|
exports.JWTErrorPrivateKeyNotFound = JWTErrorPrivateKeyNotFound;
|
|
50
|
+
class JWTErrorInvalidKey extends JWTError {
|
|
51
|
+
constructor(reason) {
|
|
52
|
+
super(`Invalid key, ${reason}`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
exports.JWTErrorInvalidKey = JWTErrorInvalidKey;
|
|
56
|
+
class JWTErrorExpirationRequired extends JWTError {
|
|
57
|
+
constructor() {
|
|
58
|
+
super("Token expiration (`exp`) is required");
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.JWTErrorExpirationRequired = JWTErrorExpirationRequired;
|
|
31
62
|
class JWTErrorInvalidType extends JWTError {
|
|
32
63
|
constructor(type) {
|
|
33
64
|
super(`Invalid type \`${type}\`, the \`typ\` must be \`JWT\``);
|
|
@@ -36,37 +67,47 @@ class JWTErrorInvalidType extends JWTError {
|
|
|
36
67
|
exports.JWTErrorInvalidType = JWTErrorInvalidType;
|
|
37
68
|
class JWTErrorInvalidIssuer extends JWTError {
|
|
38
69
|
constructor(issuer, expect) {
|
|
39
|
-
super(`Invalid issuer \`${issuer}\`, the \`iss\` must be \`${expect}\``);
|
|
70
|
+
super(`Invalid issuer \`${String(issuer)}\`, the \`iss\` must be \`${expect}\``);
|
|
40
71
|
}
|
|
41
72
|
}
|
|
42
73
|
exports.JWTErrorInvalidIssuer = JWTErrorInvalidIssuer;
|
|
43
74
|
class JWTErrorInvalidSubject extends JWTError {
|
|
44
75
|
constructor(subject, expect) {
|
|
45
|
-
super(`Invalid subject \`${subject}\`, the \`sub\` must be \`${expect}\``);
|
|
76
|
+
super(`Invalid subject \`${String(subject)}\`, the \`sub\` must be \`${expect}\``);
|
|
46
77
|
}
|
|
47
78
|
}
|
|
48
79
|
exports.JWTErrorInvalidSubject = JWTErrorInvalidSubject;
|
|
49
80
|
class JWTErrorInvalidAudience extends JWTError {
|
|
50
81
|
constructor(audience, expect) {
|
|
51
|
-
|
|
82
|
+
const expected = Array.isArray(expect) ? expect.join(", ") : expect;
|
|
83
|
+
const actual = typeof audience === "string"
|
|
84
|
+
? audience
|
|
85
|
+
: (JSON.stringify(audience) ?? String(audience));
|
|
86
|
+
super(`Invalid audience \`${actual}\`, the \`aud\` must be one of \`${expected}\``);
|
|
52
87
|
}
|
|
53
88
|
}
|
|
54
89
|
exports.JWTErrorInvalidAudience = JWTErrorInvalidAudience;
|
|
55
90
|
class JWTErrorExpired extends JWTError {
|
|
56
91
|
constructor(expiredAt) {
|
|
57
|
-
super(`Token expired at ${
|
|
92
|
+
super(`Token expired at ${formatTimestamp(expiredAt)}`);
|
|
58
93
|
}
|
|
59
94
|
}
|
|
60
95
|
exports.JWTErrorExpired = JWTErrorExpired;
|
|
61
96
|
class JWTErrorNotBefore extends JWTError {
|
|
62
97
|
constructor(notBefore) {
|
|
63
|
-
super(`Token not before ${
|
|
98
|
+
super(`Token not before ${formatTimestamp(notBefore)}`);
|
|
64
99
|
}
|
|
65
100
|
}
|
|
66
101
|
exports.JWTErrorNotBefore = JWTErrorNotBefore;
|
|
102
|
+
class JWTErrorTokenTooOld extends JWTError {
|
|
103
|
+
constructor(issuedAt, maxTokenAge) {
|
|
104
|
+
super(`Token issued at ${formatTimestamp(issuedAt)} is older than the maximum token age of ${maxTokenAge} seconds`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
exports.JWTErrorTokenTooOld = JWTErrorTokenTooOld;
|
|
67
108
|
class JWTErrorInvalidJwtId extends JWTError {
|
|
68
109
|
constructor(jwtId, expect) {
|
|
69
|
-
super(`Invalid jwt id \`${jwtId}\`, the \`jti\` must be \`${expect}\``);
|
|
110
|
+
super(`Invalid jwt id \`${String(jwtId)}\`, the \`jti\` must be \`${expect}\``);
|
|
70
111
|
}
|
|
71
112
|
}
|
|
72
113
|
exports.JWTErrorInvalidJwtId = JWTErrorInvalidJwtId;
|
|
@@ -90,7 +131,7 @@ class JWTErrorInvalidTokenLength extends JWTError {
|
|
|
90
131
|
exports.JWTErrorInvalidTokenLength = JWTErrorInvalidTokenLength;
|
|
91
132
|
class JWTErrorInvalidAlgorithm extends JWTError {
|
|
92
133
|
constructor(algorithm, expect) {
|
|
93
|
-
super(`Invalid algorithm \`${algorithm}\`, the \`alg\` must be \`${expect}\``);
|
|
134
|
+
super(`Invalid algorithm \`${String(algorithm)}\`, the \`alg\` must be \`${expect}\``);
|
|
94
135
|
}
|
|
95
136
|
}
|
|
96
137
|
exports.JWTErrorInvalidAlgorithm = JWTErrorInvalidAlgorithm;
|
|
@@ -100,4 +141,16 @@ class JWTErrorInvalidToken extends JWTError {
|
|
|
100
141
|
}
|
|
101
142
|
}
|
|
102
143
|
exports.JWTErrorInvalidToken = JWTErrorInvalidToken;
|
|
144
|
+
/**
|
|
145
|
+
* A token that could not be decoded or parsed.
|
|
146
|
+
*
|
|
147
|
+
* This is a subclass of {@link JWTErrorInvalidToken} so callers that already
|
|
148
|
+
* catch `JWTErrorInvalidToken` keep working.
|
|
149
|
+
*/
|
|
150
|
+
class JWTErrorMalformed extends JWTErrorInvalidToken {
|
|
151
|
+
constructor(reason) {
|
|
152
|
+
super(reason);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
exports.JWTErrorMalformed = JWTErrorMalformed;
|
|
103
156
|
//# sourceMappingURL=error.js.map
|
package/dist/index.d.ts
CHANGED
|
@@ -1,21 +1,33 @@
|
|
|
1
1
|
import { JsonWebKeyInput, KeyObject, PrivateKeyInput, PublicKeyInput } from "crypto";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* A symmetric secret, or an asymmetric key.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
5
|
+
* A PEM string is only accepted when it is passed through
|
|
6
|
+
* `{ privateKey }` or `{ publicKey }`. Passing a PEM string directly is
|
|
7
|
+
* rejected, because it would otherwise silently be used as an HMAC secret.
|
|
8
|
+
*/
|
|
9
|
+
export type JWTKeySecret = string | Buffer | KeyObject;
|
|
10
|
+
export interface JWTKeySecretObject {
|
|
11
|
+
secret: JWTKeySecret;
|
|
12
|
+
}
|
|
13
|
+
export interface JWTKeyPrivateKey {
|
|
14
|
+
privateKey: PrivateKeyInput | string | Buffer | JsonWebKeyInput | KeyObject;
|
|
15
|
+
}
|
|
16
|
+
export interface JWTKeyPublicKey {
|
|
17
|
+
publicKey: PublicKeyInput | string | Buffer | JsonWebKeyInput | KeyObject;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The key used to sign and verify the token.
|
|
6
21
|
*
|
|
7
|
-
*
|
|
22
|
+
* Use one of:
|
|
8
23
|
*
|
|
9
|
-
*
|
|
24
|
+
* - a string / Buffer / secret `KeyObject`
|
|
25
|
+
* - `{ secret }`
|
|
26
|
+
* - `{ privateKey }`
|
|
27
|
+
* - `{ publicKey }`
|
|
28
|
+
* - `{ privateKey, publicKey }`
|
|
10
29
|
*/
|
|
11
|
-
export type JWTKey = JWTKeySecret | (JWTKeyPrivateKey & JWTKeyPublicKey);
|
|
12
|
-
export type JWTKeySecret = string | Buffer;
|
|
13
|
-
export type JWTKeyPrivateKey = {
|
|
14
|
-
privateKey?: PrivateKeyInput | string | Buffer | JsonWebKeyInput;
|
|
15
|
-
};
|
|
16
|
-
export type JWTKeyPublicKey = {
|
|
17
|
-
publicKey?: PublicKeyInput | string | Buffer | KeyObject | JsonWebKeyInput;
|
|
18
|
-
};
|
|
30
|
+
export type JWTKey = JWTKeySecret | JWTKeySecretObject | JWTKeyPrivateKey | JWTKeyPublicKey | (JWTKeyPrivateKey & JWTKeyPublicKey);
|
|
19
31
|
/**
|
|
20
32
|
* The algorithm used to sign the token
|
|
21
33
|
*
|
|
@@ -27,7 +39,7 @@ export type JWTKeyPublicKey = {
|
|
|
27
39
|
*
|
|
28
40
|
* ES - ECDSA with SHA
|
|
29
41
|
*/
|
|
30
|
-
export type JWTAlgorithm = "HS256" | "HS384" | "HS512" | "PS256" | "PS384" | "PS512" | "RS256" | "RS384" | "RS512" | "ES256" | "ES256K" | "ES384" | "ES512"
|
|
42
|
+
export type JWTAlgorithm = "HS256" | "HS384" | "HS512" | "PS256" | "PS384" | "PS512" | "RS256" | "RS384" | "RS512" | "ES256" | "ES256K" | "ES384" | "ES512";
|
|
31
43
|
/**
|
|
32
44
|
* Options for the JWT class
|
|
33
45
|
*/
|
|
@@ -57,9 +69,17 @@ export interface JWTOptions {
|
|
|
57
69
|
*
|
|
58
70
|
* If set, the token `aud` will be set to this value when sign.
|
|
59
71
|
*
|
|
60
|
-
* If set, the token `aud` must
|
|
72
|
+
* If set, the token `aud` must contain at least one of these values
|
|
61
73
|
*/
|
|
62
|
-
audience?: string;
|
|
74
|
+
audience?: string | string[];
|
|
75
|
+
/**
|
|
76
|
+
* Key ID
|
|
77
|
+
*
|
|
78
|
+
* If set, the token `kid` will be set to this value when sign.
|
|
79
|
+
*
|
|
80
|
+
* If set, the token `kid` must be same value
|
|
81
|
+
*/
|
|
82
|
+
kid?: string;
|
|
63
83
|
/**
|
|
64
84
|
* Expiration time, in seconds
|
|
65
85
|
*
|
|
@@ -86,10 +106,34 @@ export interface JWTOptions {
|
|
|
86
106
|
* If set, the token `jti` must be same value
|
|
87
107
|
*/
|
|
88
108
|
jwtID?: string;
|
|
109
|
+
/**
|
|
110
|
+
* Require the `exp` claim when signing and verifying.
|
|
111
|
+
*
|
|
112
|
+
* Defaults to `false`; `exp` is optional. Set to `true` to reject tokens
|
|
113
|
+
* without an expiration time.
|
|
114
|
+
*/
|
|
115
|
+
requireExpiration?: boolean;
|
|
116
|
+
/**
|
|
117
|
+
* Alias for `requireExpiration: false` when set to `true`.
|
|
118
|
+
*/
|
|
119
|
+
allowNoExpiration?: boolean;
|
|
120
|
+
/**
|
|
121
|
+
* Clock tolerance, in seconds, applied to `exp`, `nbf` and `iat`.
|
|
122
|
+
*
|
|
123
|
+
* Defaults to `0`.
|
|
124
|
+
*/
|
|
125
|
+
clockTolerance?: number;
|
|
126
|
+
/**
|
|
127
|
+
* Maximum token age, in seconds, measured from `iat`.
|
|
128
|
+
*
|
|
129
|
+
* If set, `iat` is required when verifying.
|
|
130
|
+
*/
|
|
131
|
+
maxTokenAge?: number;
|
|
89
132
|
}
|
|
90
133
|
export interface JWTHeader {
|
|
91
134
|
typ?: string;
|
|
92
135
|
alg: JWTAlgorithm | string;
|
|
136
|
+
kid?: string;
|
|
93
137
|
}
|
|
94
138
|
/**
|
|
95
139
|
* Registered claims for a JWT
|
|
@@ -106,7 +150,7 @@ export interface JWTPayloadRegisteredClaims {
|
|
|
106
150
|
/**
|
|
107
151
|
* Audience
|
|
108
152
|
*/
|
|
109
|
-
aud?: string | undefined;
|
|
153
|
+
aud?: string | string[] | undefined;
|
|
110
154
|
/**
|
|
111
155
|
* Expiration Time, in seconds
|
|
112
156
|
*/
|
|
@@ -148,7 +192,13 @@ export interface JWTSignOptions {
|
|
|
148
192
|
*
|
|
149
193
|
* If set, the token `aud` will be set to this value when sign.
|
|
150
194
|
*/
|
|
151
|
-
audience?: string;
|
|
195
|
+
audience?: string | string[];
|
|
196
|
+
/**
|
|
197
|
+
* Key ID
|
|
198
|
+
*
|
|
199
|
+
* If set, the token `kid` will be set to this value when sign.
|
|
200
|
+
*/
|
|
201
|
+
kid?: string;
|
|
152
202
|
/**
|
|
153
203
|
* Expiration time, in seconds
|
|
154
204
|
*
|
|
@@ -173,6 +223,16 @@ export interface JWTSignOptions {
|
|
|
173
223
|
* If set, the token `jti` will be set to this value when sign.
|
|
174
224
|
*/
|
|
175
225
|
jwtID?: string;
|
|
226
|
+
/**
|
|
227
|
+
* Override whether the `exp` claim is required for this call.
|
|
228
|
+
*
|
|
229
|
+
* Defaults to the value configured on the instance.
|
|
230
|
+
*/
|
|
231
|
+
requireExpiration?: boolean;
|
|
232
|
+
/**
|
|
233
|
+
* Alias for `requireExpiration: false` when set to `true`.
|
|
234
|
+
*/
|
|
235
|
+
allowNoExpiration?: boolean;
|
|
176
236
|
}
|
|
177
237
|
export interface JWTVerifyOptions {
|
|
178
238
|
/**
|
|
@@ -190,9 +250,15 @@ export interface JWTVerifyOptions {
|
|
|
190
250
|
/**
|
|
191
251
|
* Audience
|
|
192
252
|
*
|
|
193
|
-
* If set, the token `aud` must
|
|
253
|
+
* If set, the token `aud` must contain at least one of these values
|
|
194
254
|
*/
|
|
195
|
-
audience?: string;
|
|
255
|
+
audience?: string | string[];
|
|
256
|
+
/**
|
|
257
|
+
* Key ID
|
|
258
|
+
*
|
|
259
|
+
* If set, the token `kid` must be same value
|
|
260
|
+
*/
|
|
261
|
+
kid?: string;
|
|
196
262
|
/**
|
|
197
263
|
* Current time, in milliseconds
|
|
198
264
|
*
|
|
@@ -207,6 +273,28 @@ export interface JWTVerifyOptions {
|
|
|
207
273
|
* If set, the token `jti` must be same value
|
|
208
274
|
*/
|
|
209
275
|
jwtID?: string;
|
|
276
|
+
/**
|
|
277
|
+
* Override whether the `exp` claim is required for this call.
|
|
278
|
+
*
|
|
279
|
+
* Defaults to the value configured on the instance.
|
|
280
|
+
*/
|
|
281
|
+
requireExpiration?: boolean;
|
|
282
|
+
/**
|
|
283
|
+
* Alias for `requireExpiration: false` when set to `true`.
|
|
284
|
+
*/
|
|
285
|
+
allowNoExpiration?: boolean;
|
|
286
|
+
/**
|
|
287
|
+
* Clock tolerance, in seconds, applied to `exp`, `nbf` and `iat`.
|
|
288
|
+
*
|
|
289
|
+
* Defaults to the value configured on the instance, or `0`.
|
|
290
|
+
*/
|
|
291
|
+
clockTolerance?: number;
|
|
292
|
+
/**
|
|
293
|
+
* Maximum token age, in seconds, measured from `iat`.
|
|
294
|
+
*
|
|
295
|
+
* If set, `iat` is required.
|
|
296
|
+
*/
|
|
297
|
+
maxTokenAge?: number;
|
|
210
298
|
}
|
|
211
299
|
/**
|
|
212
300
|
* A class to create and verify JSON Web Tokens
|
|
@@ -233,7 +321,7 @@ export declare class JWT {
|
|
|
233
321
|
*
|
|
234
322
|
* Public key is for RS256, RS384, RS512, ES256, ES384, ES512, PS256, PS384, PS512
|
|
235
323
|
*/
|
|
236
|
-
readonly publicKey?: KeyObject;
|
|
324
|
+
readonly publicKey?: KeyObject | undefined;
|
|
237
325
|
/**
|
|
238
326
|
* The algorithm used to sign the token
|
|
239
327
|
*
|
|
@@ -247,14 +335,18 @@ export declare class JWT {
|
|
|
247
335
|
get now(): number;
|
|
248
336
|
issuer?: string | undefined;
|
|
249
337
|
subject?: string | undefined;
|
|
250
|
-
audience?: string | undefined;
|
|
338
|
+
audience?: string | string[] | undefined;
|
|
339
|
+
kid?: string | undefined;
|
|
251
340
|
expirationTime?: number | undefined;
|
|
252
341
|
notBefore?: number | undefined;
|
|
253
342
|
issuedAt?: number | undefined;
|
|
254
343
|
jwtID?: string | undefined;
|
|
344
|
+
requireExpiration: boolean;
|
|
345
|
+
clockTolerance: number;
|
|
346
|
+
maxTokenAge?: number | undefined;
|
|
255
347
|
/**
|
|
256
348
|
* Create a new JWT instance
|
|
257
|
-
* @param
|
|
349
|
+
* @param key The secret or key used to sign and verify the token
|
|
258
350
|
* @param options Options for the token
|
|
259
351
|
*/
|
|
260
352
|
constructor(key: JWTKey, options?: JWTOptions);
|
|
@@ -263,9 +355,10 @@ export declare class JWT {
|
|
|
263
355
|
*/
|
|
264
356
|
private buildHeader;
|
|
265
357
|
private buildPayload;
|
|
358
|
+
private checkSignPayload;
|
|
266
359
|
private buildSignature;
|
|
267
360
|
private buildSignatureHmac;
|
|
268
|
-
private
|
|
361
|
+
private buildSignatureAsymmetric;
|
|
269
362
|
/**
|
|
270
363
|
* @param input The payload of the token
|
|
271
364
|
* @returns The signed token
|
|
@@ -275,7 +368,7 @@ export declare class JWT {
|
|
|
275
368
|
private checkPayload;
|
|
276
369
|
private checkSignature;
|
|
277
370
|
private checkSignatureHmac;
|
|
278
|
-
private
|
|
371
|
+
private checkSignatureAsymmetric;
|
|
279
372
|
/**
|
|
280
373
|
* @param input The token to verify
|
|
281
374
|
* @returns The payload of the token
|
|
@@ -294,12 +387,32 @@ export declare class JWT {
|
|
|
294
387
|
* @returns hash algorithm
|
|
295
388
|
*/
|
|
296
389
|
static hashAlgorithm(algorithm: JWTAlgorithm | string): string;
|
|
390
|
+
private static algorithmInfo;
|
|
391
|
+
private static defaultAlgorithm;
|
|
392
|
+
private static normalizeCurve;
|
|
393
|
+
private static toSecretKey;
|
|
394
|
+
private static toPrivateKey;
|
|
395
|
+
private static toPublicKey;
|
|
396
|
+
private static assertSecretKey;
|
|
397
|
+
private static assertAsymmetricKey;
|
|
398
|
+
private static errorMessage;
|
|
399
|
+
private static resolveRequireExpiration;
|
|
400
|
+
private static assertNumericDate;
|
|
401
|
+
private static assertNonNegativeNumber;
|
|
402
|
+
private static assertString;
|
|
403
|
+
private static assertAudience;
|
|
297
404
|
/**
|
|
298
405
|
* Decode base64 url data
|
|
299
406
|
* @param data The base64 encoded data
|
|
300
407
|
* @returns The decoded data
|
|
301
408
|
*/
|
|
302
409
|
static decode<T = unknown>(data: string, encoding?: BufferEncoding): T;
|
|
410
|
+
/**
|
|
411
|
+
* Decode a base64url segment, rejecting anything that is not the canonical
|
|
412
|
+
* unpadded encoding. This prevents multiple valid serializations of the same
|
|
413
|
+
* token from bypassing exact-string checks.
|
|
414
|
+
*/
|
|
415
|
+
private static decodeBase64Url;
|
|
303
416
|
/**
|
|
304
417
|
* Encode data to base64 url
|
|
305
418
|
* @param data The data to encode
|