@hkdigital/lib-core 0.4.13 → 0.4.14
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/auth/jwt/util.d.ts +38 -0
- package/dist/auth/jwt/util.js +110 -3
- package/dist/auth/jwt.d.ts +0 -1
- package/package.json +1 -1
- package/dist/auth/jwt/core.d.ts +0 -38
- package/dist/auth/jwt/core.js +0 -114
package/dist/auth/jwt/util.d.ts
CHANGED
|
@@ -1,3 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a JSON Web Token (JWT)
|
|
3
|
+
* - Stringifies the claims as JSON object
|
|
4
|
+
* - Encodes the options
|
|
5
|
+
* - Calculates a Message Authentication Code (MAC)
|
|
6
|
+
* (by default a Hash Based Authentication Code (HMAC) will be used: HS512)
|
|
7
|
+
* - Combines the parts into a JWT string
|
|
8
|
+
*
|
|
9
|
+
* @param {import('./typedef.js').JwtPayload} claims - JWT payload/claims
|
|
10
|
+
* @param {import('./typedef.js').Secret} secretOrPrivateKey
|
|
11
|
+
* Secret or private key that is used by the MAC calculation algorithm
|
|
12
|
+
*
|
|
13
|
+
* - To generate a secret for a Hash based Authentication Code (HMAC):
|
|
14
|
+
* use a function like `generateSecretKeyForHmacBase58()`.
|
|
15
|
+
*
|
|
16
|
+
* - For algorithms that use asymmetric keys, the secret is the private key
|
|
17
|
+
* of the key pair.
|
|
18
|
+
*
|
|
19
|
+
* @param {import('./typedef.js').SignOptions} [options] - JWT signing options
|
|
20
|
+
*
|
|
21
|
+
* For more options:
|
|
22
|
+
* @see https://github.com/auth0/node-jsonwebtoken
|
|
23
|
+
*
|
|
24
|
+
* @returns {string} JsonWebToken
|
|
25
|
+
*/
|
|
26
|
+
export function sign(claims: import("./typedef.js").JwtPayload, secretOrPrivateKey: import("./typedef.js").Secret, options?: import("./typedef.js").SignOptions): string;
|
|
27
|
+
/**
|
|
28
|
+
* Decode and verify a JWT token
|
|
29
|
+
* - Forces the use of the algorithm specified in VERIFY_OPTIONS
|
|
30
|
+
*
|
|
31
|
+
* @param {string} token - A JWT token
|
|
32
|
+
* @param {import('./typedef.js').Secret} secretOrPrivateKey
|
|
33
|
+
* The secret of private key to be used for decoding
|
|
34
|
+
* @param {import('./typedef.js').VerifyOptions} [options=VERIFY_OPTIONS] - verify / decode options
|
|
35
|
+
*
|
|
36
|
+
* @returns {import('./typedef.js').JwtPayload} claims - The decoded JWT payload
|
|
37
|
+
*/
|
|
38
|
+
export function verify(token: string, secretOrPrivateKey: import("./typedef.js").Secret, options?: import("./typedef.js").VerifyOptions): import("./typedef.js").JwtPayload;
|
|
1
39
|
/**
|
|
2
40
|
* Casts jsonwebtoken library errors to internal error types
|
|
3
41
|
* @param {Error} error - The original jsonwebtoken error
|
package/dist/auth/jwt/util.js
CHANGED
|
@@ -2,14 +2,26 @@
|
|
|
2
2
|
* JWT utility functions
|
|
3
3
|
*
|
|
4
4
|
* @description
|
|
5
|
-
* This module provides utility functions for JWT operations including
|
|
5
|
+
* This module provides utility functions for JWT operations including
|
|
6
|
+
* sign, verify and error casting.
|
|
6
7
|
*/
|
|
7
8
|
|
|
8
|
-
import jwt
|
|
9
|
+
import jwt from 'jsonwebtoken';
|
|
10
|
+
|
|
11
|
+
import {
|
|
9
12
|
TokenExpiredError as JwtTokenExpiredError,
|
|
10
13
|
JsonWebTokenError as JwtJsonWebTokenError,
|
|
11
14
|
NotBeforeError as JwtNotBeforeError
|
|
12
15
|
} from 'jsonwebtoken';
|
|
16
|
+
|
|
17
|
+
import * as expect from '../../util/expect.js';
|
|
18
|
+
|
|
19
|
+
import {
|
|
20
|
+
JWT_DEFAULT_EXPIRES_IN,
|
|
21
|
+
DEFAULT_ALGORITHM,
|
|
22
|
+
VERIFY_OPTIONS
|
|
23
|
+
} from './constants.js';
|
|
24
|
+
|
|
13
25
|
import {
|
|
14
26
|
TokenExpiredError,
|
|
15
27
|
JsonWebTokenError,
|
|
@@ -17,6 +29,101 @@ import {
|
|
|
17
29
|
NotBeforeError
|
|
18
30
|
} from './errors.js';
|
|
19
31
|
|
|
32
|
+
/**
|
|
33
|
+
* Create a JSON Web Token (JWT)
|
|
34
|
+
* - Stringifies the claims as JSON object
|
|
35
|
+
* - Encodes the options
|
|
36
|
+
* - Calculates a Message Authentication Code (MAC)
|
|
37
|
+
* (by default a Hash Based Authentication Code (HMAC) will be used: HS512)
|
|
38
|
+
* - Combines the parts into a JWT string
|
|
39
|
+
*
|
|
40
|
+
* @param {import('./typedef.js').JwtPayload} claims - JWT payload/claims
|
|
41
|
+
* @param {import('./typedef.js').Secret} secretOrPrivateKey
|
|
42
|
+
* Secret or private key that is used by the MAC calculation algorithm
|
|
43
|
+
*
|
|
44
|
+
* - To generate a secret for a Hash based Authentication Code (HMAC):
|
|
45
|
+
* use a function like `generateSecretKeyForHmacBase58()`.
|
|
46
|
+
*
|
|
47
|
+
* - For algorithms that use asymmetric keys, the secret is the private key
|
|
48
|
+
* of the key pair.
|
|
49
|
+
*
|
|
50
|
+
* @param {import('./typedef.js').SignOptions} [options] - JWT signing options
|
|
51
|
+
*
|
|
52
|
+
* For more options:
|
|
53
|
+
* @see https://github.com/auth0/node-jsonwebtoken
|
|
54
|
+
*
|
|
55
|
+
* @returns {string} JsonWebToken
|
|
56
|
+
*/
|
|
57
|
+
export function sign(
|
|
58
|
+
claims,
|
|
59
|
+
secretOrPrivateKey,
|
|
60
|
+
options={} )
|
|
61
|
+
{
|
|
62
|
+
expect.object( claims );
|
|
63
|
+
expect.defined( secretOrPrivateKey );
|
|
64
|
+
|
|
65
|
+
if( options )
|
|
66
|
+
{
|
|
67
|
+
expect.object( options );
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
options = {};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if( !('algorithm' in options) )
|
|
74
|
+
{
|
|
75
|
+
options.algorithm = DEFAULT_ALGORITHM;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if( !('expiresIn' in options) )
|
|
79
|
+
{
|
|
80
|
+
options.expiresIn = JWT_DEFAULT_EXPIRES_IN;
|
|
81
|
+
}
|
|
82
|
+
else if( !options.expiresIn )
|
|
83
|
+
{
|
|
84
|
+
delete options.expiresIn;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// @ts-ignore
|
|
88
|
+
return jwt.sign( claims, secretOrPrivateKey, options );
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Decode and verify a JWT token
|
|
93
|
+
* - Forces the use of the algorithm specified in VERIFY_OPTIONS
|
|
94
|
+
*
|
|
95
|
+
* @param {string} token - A JWT token
|
|
96
|
+
* @param {import('./typedef.js').Secret} secretOrPrivateKey
|
|
97
|
+
* The secret of private key to be used for decoding
|
|
98
|
+
* @param {import('./typedef.js').VerifyOptions} [options=VERIFY_OPTIONS] - verify / decode options
|
|
99
|
+
*
|
|
100
|
+
* @returns {import('./typedef.js').JwtPayload} claims - The decoded JWT payload
|
|
101
|
+
*/
|
|
102
|
+
export function verify( token, secretOrPrivateKey, options=VERIFY_OPTIONS )
|
|
103
|
+
{
|
|
104
|
+
expect.notEmptyString( token );
|
|
105
|
+
expect.defined( secretOrPrivateKey );
|
|
106
|
+
|
|
107
|
+
if( !('algorithms' in options) )
|
|
108
|
+
{
|
|
109
|
+
options.algorithms = VERIFY_OPTIONS.algorithms;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
try {
|
|
113
|
+
// @ts-ignore
|
|
114
|
+
const decoded = jwt.verify( token, secretOrPrivateKey, options );
|
|
115
|
+
|
|
116
|
+
return decoded;
|
|
117
|
+
}
|
|
118
|
+
catch( e )
|
|
119
|
+
{
|
|
120
|
+
//
|
|
121
|
+
// Cast internal jsonwebtoken errors to Error types defined in this lib
|
|
122
|
+
//
|
|
123
|
+
throw castJwtError(e);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
20
127
|
/**
|
|
21
128
|
* Casts jsonwebtoken library errors to internal error types
|
|
22
129
|
* @param {Error} error - The original jsonwebtoken error
|
|
@@ -40,4 +147,4 @@ export function castJwtError(error) {
|
|
|
40
147
|
|
|
41
148
|
// Return original error if not a known JWT error
|
|
42
149
|
return error;
|
|
43
|
-
}
|
|
150
|
+
}
|
package/dist/auth/jwt.d.ts
CHANGED
package/package.json
CHANGED
package/dist/auth/jwt/core.d.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Create a JSON Web Token (JWT)
|
|
3
|
-
* - Stringifies the claims as JSON object
|
|
4
|
-
* - Encodes the options
|
|
5
|
-
* - Calculates a Message Authentication Code (MAC)
|
|
6
|
-
* (by default a Hash Based Authentication Code (HMAC) will be used: HS512)
|
|
7
|
-
* - Combines the parts into a JWT string
|
|
8
|
-
*
|
|
9
|
-
* @param {import('./typedef.js').JwtPayload} claims - JWT payload/claims
|
|
10
|
-
* @param {import('./typedef.js').Secret} secretOrPrivateKey
|
|
11
|
-
* Secret or private key that is used by the MAC calculation algorithm
|
|
12
|
-
*
|
|
13
|
-
* - To generate a secret for a Hash based Authentication Code (HMAC):
|
|
14
|
-
* use a function like `generateSecretKeyForHmacBase58()`.
|
|
15
|
-
*
|
|
16
|
-
* - For algorithms that use asymmetric keys, the secret is the private key
|
|
17
|
-
* of the key pair.
|
|
18
|
-
*
|
|
19
|
-
* @param {import('./typedef.js').SignOptions} [options] - JWT signing options
|
|
20
|
-
*
|
|
21
|
-
* For more options:
|
|
22
|
-
* @see https://github.com/auth0/node-jsonwebtoken
|
|
23
|
-
*
|
|
24
|
-
* @returns {string} JsonWebToken
|
|
25
|
-
*/
|
|
26
|
-
export function sign(claims: import("./typedef.js").JwtPayload, secretOrPrivateKey: import("./typedef.js").Secret, options?: import("./typedef.js").SignOptions): string;
|
|
27
|
-
/**
|
|
28
|
-
* Decode and verify a JWT token
|
|
29
|
-
* - Forces the use of the algorithm specified in VERIFY_OPTIONS
|
|
30
|
-
*
|
|
31
|
-
* @param {string} token - A JWT token
|
|
32
|
-
* @param {import('./typedef.js').Secret} secretOrPrivateKey
|
|
33
|
-
* The secret of private key to be used for decoding
|
|
34
|
-
* @param {import('./typedef.js').VerifyOptions} [options=VERIFY_OPTIONS] - verify / decode options
|
|
35
|
-
*
|
|
36
|
-
* @returns {import('./typedef.js').JwtPayload} claims - The decoded JWT payload
|
|
37
|
-
*/
|
|
38
|
-
export function verify(token: string, secretOrPrivateKey: import("./typedef.js").Secret, options?: import("./typedef.js").VerifyOptions): import("./typedef.js").JwtPayload;
|
package/dist/auth/jwt/core.js
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Core JWT operations - sign and verify functions
|
|
3
|
-
*
|
|
4
|
-
* @description
|
|
5
|
-
* This module provides the main JWT functionality for signing and verifying tokens.
|
|
6
|
-
* It wraps the jsonwebtoken library with consistent error handling and validation.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import jwt from 'jsonwebtoken';
|
|
10
|
-
|
|
11
|
-
import * as expect from '../../util/expect.js';
|
|
12
|
-
|
|
13
|
-
import {
|
|
14
|
-
JWT_DEFAULT_EXPIRES_IN,
|
|
15
|
-
DEFAULT_ALGORITHM,
|
|
16
|
-
VERIFY_OPTIONS
|
|
17
|
-
} from './constants.js';
|
|
18
|
-
|
|
19
|
-
import { castJwtError } from './util.js';
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Create a JSON Web Token (JWT)
|
|
23
|
-
* - Stringifies the claims as JSON object
|
|
24
|
-
* - Encodes the options
|
|
25
|
-
* - Calculates a Message Authentication Code (MAC)
|
|
26
|
-
* (by default a Hash Based Authentication Code (HMAC) will be used: HS512)
|
|
27
|
-
* - Combines the parts into a JWT string
|
|
28
|
-
*
|
|
29
|
-
* @param {import('./typedef.js').JwtPayload} claims - JWT payload/claims
|
|
30
|
-
* @param {import('./typedef.js').Secret} secretOrPrivateKey
|
|
31
|
-
* Secret or private key that is used by the MAC calculation algorithm
|
|
32
|
-
*
|
|
33
|
-
* - To generate a secret for a Hash based Authentication Code (HMAC):
|
|
34
|
-
* use a function like `generateSecretKeyForHmacBase58()`.
|
|
35
|
-
*
|
|
36
|
-
* - For algorithms that use asymmetric keys, the secret is the private key
|
|
37
|
-
* of the key pair.
|
|
38
|
-
*
|
|
39
|
-
* @param {import('./typedef.js').SignOptions} [options] - JWT signing options
|
|
40
|
-
*
|
|
41
|
-
* For more options:
|
|
42
|
-
* @see https://github.com/auth0/node-jsonwebtoken
|
|
43
|
-
*
|
|
44
|
-
* @returns {string} JsonWebToken
|
|
45
|
-
*/
|
|
46
|
-
export function sign(
|
|
47
|
-
claims,
|
|
48
|
-
secretOrPrivateKey,
|
|
49
|
-
options={} )
|
|
50
|
-
{
|
|
51
|
-
expect.object( claims );
|
|
52
|
-
expect.defined( secretOrPrivateKey );
|
|
53
|
-
|
|
54
|
-
if( options )
|
|
55
|
-
{
|
|
56
|
-
expect.object( options );
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
options = {};
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if( !('algorithm' in options) )
|
|
63
|
-
{
|
|
64
|
-
options.algorithm = DEFAULT_ALGORITHM;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if( !('expiresIn' in options) )
|
|
68
|
-
{
|
|
69
|
-
options.expiresIn = JWT_DEFAULT_EXPIRES_IN;
|
|
70
|
-
}
|
|
71
|
-
else if( !options.expiresIn )
|
|
72
|
-
{
|
|
73
|
-
delete options.expiresIn;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// @ts-ignore
|
|
77
|
-
return jwt.sign( claims, secretOrPrivateKey, options );
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Decode and verify a JWT token
|
|
82
|
-
* - Forces the use of the algorithm specified in VERIFY_OPTIONS
|
|
83
|
-
*
|
|
84
|
-
* @param {string} token - A JWT token
|
|
85
|
-
* @param {import('./typedef.js').Secret} secretOrPrivateKey
|
|
86
|
-
* The secret of private key to be used for decoding
|
|
87
|
-
* @param {import('./typedef.js').VerifyOptions} [options=VERIFY_OPTIONS] - verify / decode options
|
|
88
|
-
*
|
|
89
|
-
* @returns {import('./typedef.js').JwtPayload} claims - The decoded JWT payload
|
|
90
|
-
*/
|
|
91
|
-
export function verify( token, secretOrPrivateKey, options=VERIFY_OPTIONS )
|
|
92
|
-
{
|
|
93
|
-
expect.notEmptyString( token );
|
|
94
|
-
expect.defined( secretOrPrivateKey );
|
|
95
|
-
|
|
96
|
-
if( !('algorithms' in options) )
|
|
97
|
-
{
|
|
98
|
-
options.algorithms = VERIFY_OPTIONS.algorithms;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
try {
|
|
102
|
-
// @ts-ignore
|
|
103
|
-
const decoded = jwt.verify( token, secretOrPrivateKey, options );
|
|
104
|
-
|
|
105
|
-
return decoded;
|
|
106
|
-
}
|
|
107
|
-
catch( e )
|
|
108
|
-
{
|
|
109
|
-
//
|
|
110
|
-
// Cast internal jsonwebtoken errors to Error types defined in this lib
|
|
111
|
-
//
|
|
112
|
-
throw castJwtError(e);
|
|
113
|
-
}
|
|
114
|
-
}
|