@hkdigital/lib-core 0.4.12 → 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/README.md +4 -1
- package/dist/auth/errors.d.ts +1 -1
- package/dist/auth/errors.js +1 -1
- package/dist/auth/jwt/constants.d.ts +6 -0
- package/dist/auth/jwt/constants.js +13 -0
- package/dist/auth/jwt/errors.d.ts +39 -0
- package/dist/auth/jwt/errors.js +74 -0
- package/dist/auth/jwt/generators.d.ts +29 -0
- package/dist/auth/jwt/generators.js +53 -0
- package/dist/auth/jwt/typedef.d.ts +159 -0
- package/dist/auth/jwt/typedef.js +72 -0
- package/dist/auth/jwt/util.d.ts +44 -0
- package/dist/auth/jwt/util.js +150 -0
- package/dist/auth/jwt.d.ts +3 -0
- package/dist/auth/jwt.js +12 -0
- package/dist/generic/data/classes/IterableTree.js +1 -1
- package/dist/generic/data/classes/Selector.js +1 -1
- package/dist/generic/promises/classes/HkPromise.js +1 -1
- package/dist/generic/streaming/classes/ServerEventsStore.js +1 -1
- package/dist/generic/typedef.d.ts +2 -0
- package/dist/generic/typedef.js +2 -0
- package/dist/logging/internal/adapters/console.js +3 -3
- package/dist/logging/internal/adapters/pino.js +17 -3
- package/dist/logging/internal/adapters/pino.js__ +260 -0
- package/dist/logging/internal/test-errors.js +3 -2
- package/dist/network/http/errors.js +10 -4
- package/dist/network/http/headers.js +1 -1
- package/dist/network/http/http-request.js +1 -1
- package/dist/network/http/json-request.js +1 -1
- package/dist/network/http/response.js +1 -1
- package/dist/network/loaders/audio/AudioScene.svelte.js +1 -1
- package/dist/network/loaders/image/ImageScene.svelte.js +1 -1
- package/dist/network/loaders/image/utils/index.js +1 -1
- package/dist/network/states/NetworkLoader.svelte.js +1 -1
- package/dist/network/typedef.d.ts +2 -0
- package/dist/network/typedef.js +3 -1
- package/dist/services/service-base/ServiceBase.js +1 -3
- package/dist/services/typedef.d.ts +2 -0
- package/dist/services/typedef.js +2 -0
- package/dist/state/typedef.d.ts +3 -0
- package/dist/state/typedef.js +3 -0
- package/dist/ui/components/drag-drop/drag-state.svelte.d.ts +3 -3
- package/dist/ui/components/hk-app-layout/HkAppLayout.state.svelte.d.ts +3 -3
- package/dist/ui/components/presenter/util.js +1 -1
- package/dist/ui/components/tab-bar/HkTabBar.state.svelte.d.ts +3 -3
- package/dist/ui/components/tab-bar/HkTabBarSelector.state.svelte.d.ts +3 -3
- package/dist/util/array/index.d.ts +0 -4
- package/dist/util/array/index.js +1 -5
- package/dist/util/array.d.ts +1 -0
- package/dist/util/array.js +9 -0
- package/dist/util/bases/base64.d.ts +8 -0
- package/dist/util/bases/base64.js +23 -0
- package/dist/util/bases/index.d.ts +0 -1
- package/dist/util/bases/index.js +0 -1
- package/dist/util/bases.d.ts +2 -0
- package/dist/util/bases.js +10 -0
- package/dist/util/compare.d.ts +1 -0
- package/dist/util/compare.js +9 -0
- package/dist/util/css/index.d.ts +0 -1
- package/dist/util/css/index.js +0 -1
- package/dist/util/css.d.ts +1 -0
- package/dist/util/css.js +8 -0
- package/dist/util/env.d.ts +1 -0
- package/dist/util/env.js +8 -0
- package/dist/util/exceptions.d.ts +1 -0
- package/dist/util/exceptions.js +8 -0
- package/dist/util/expect/arrays.js +2 -0
- package/dist/util/expect/compounds.d.ts +20 -0
- package/dist/util/expect/compounds.js +48 -0
- package/dist/util/expect/objects.d.ts +28 -0
- package/dist/util/expect/objects.js +70 -0
- package/dist/util/expect/primitives.d.ts +31 -1
- package/dist/util/expect/primitives.js +56 -0
- package/dist/util/expect/values.d.ts +26 -0
- package/dist/util/expect/values.js +48 -0
- package/dist/util/expect.d.ts +6 -0
- package/dist/util/expect.js +31 -0
- package/dist/util/function/index.js +1 -1
- package/dist/util/function.d.ts +1 -0
- package/dist/util/function.js +8 -0
- package/dist/util/geometry.d.ts +1 -0
- package/dist/util/geometry.js +9 -0
- package/dist/util/is.d.ts +1 -0
- package/dist/util/is.js +8 -0
- package/dist/util/iterate/index.js +1 -1
- package/dist/util/iterate.d.ts +1 -0
- package/dist/util/iterate.js +8 -0
- package/dist/util/object/index.js +2 -2
- package/dist/util/object.d.ts +1 -0
- package/dist/util/object.js +8 -0
- package/dist/util/random/bytes.d.ts +28 -0
- package/dist/util/random/bytes.js +72 -0
- package/dist/util/random.d.ts +1 -0
- package/dist/util/random.js +9 -0
- package/dist/util/singleton/index.js +1 -1
- package/dist/util/singleton.d.ts +1 -0
- package/dist/util/singleton.js +8 -0
- package/dist/util/ssr.d.ts +1 -0
- package/dist/util/ssr.js +8 -0
- package/dist/util/string/convert.js +1 -1
- package/dist/util/string/index.d.ts +0 -5
- package/dist/util/string/index.js +0 -5
- package/dist/util/string/interpolate.js +1 -1
- package/dist/util/string.d.ts +5 -0
- package/dist/util/string.js +12 -0
- package/dist/util/svelte/index.d.ts +0 -3
- package/dist/util/svelte/index.js +0 -4
- package/dist/util/svelte.d.ts +3 -0
- package/dist/util/svelte.js +11 -0
- package/dist/util/sveltekit/index.d.ts +0 -1
- package/dist/util/sveltekit/index.js +0 -1
- package/dist/util/sveltekit/route-folders/index.js +1 -1
- package/dist/util/sveltekit.d.ts +1 -0
- package/dist/util/sveltekit.js +8 -0
- package/dist/util/time/index.d.ts +7 -0
- package/dist/util/time/index.js +12 -1
- package/dist/util/time.d.ts +1 -0
- package/dist/util/time.js +1 -0
- package/dist/util/unique.d.ts +1 -0
- package/dist/util/unique.js +8 -0
- package/dist/valibot/{index.d.ts → valibot.d.ts} +1 -2
- package/dist/valibot/{index.js → valibot.js} +1 -6
- package/package.json +3 -1
- package/dist/auth/errors/jwt.d.ts +0 -8
- package/dist/auth/errors/jwt.js +0 -5
- package/dist/util/expect/index.d.ts +0 -103
- package/dist/util/expect/index.js +0 -259
- package/dist/util/index.d.ts +0 -10
- /package/dist/util/{geo → geometry}/index.d.ts +0 -0
- /package/dist/util/{geo → geometry}/index.js +0 -0
- /package/dist/util/{index.js → index.js__} +0 -0
package/README.md
CHANGED
|
@@ -35,6 +35,9 @@ pnpm add @steeze-ui/heroicons
|
|
|
35
35
|
# Logging
|
|
36
36
|
pnpm add pino pino-pretty
|
|
37
37
|
|
|
38
|
+
# JWT authentication (if using auth features)
|
|
39
|
+
pnpm add jsonwebtoken
|
|
40
|
+
|
|
38
41
|
# Linting
|
|
39
42
|
pnpm add @eslint/js eslint-plugin-import
|
|
40
43
|
|
|
@@ -45,7 +48,7 @@ pnpm add vite-imagetools
|
|
|
45
48
|
**For other libraries**, install as dev dependencies and declare as peer dependencies:
|
|
46
49
|
```bash
|
|
47
50
|
# Install as dev dependencies and peer dependencies
|
|
48
|
-
pnpm add -D --save-peer @sveltejs/kit svelte svelte-preprocess runed valibot @skeletonlabs/skeleton @steeze-ui/heroicons pino pino-pretty @eslint/js eslint-plugin-import vite-imagetools
|
|
51
|
+
pnpm add -D --save-peer @sveltejs/kit svelte svelte-preprocess runed valibot @skeletonlabs/skeleton @steeze-ui/heroicons pino pino-pretty jsonwebtoken @eslint/js eslint-plugin-import vite-imagetools
|
|
49
52
|
```
|
|
50
53
|
|
|
51
54
|
### Design System & Configuration
|
package/dist/auth/errors.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from "./errors
|
|
1
|
+
export * from "./jwt/errors.js";
|
package/dist/auth/errors.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './errors
|
|
1
|
+
export * from './jwt/errors.js';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export const JWT_DEFAULT_EXPIRES_IN = '30h';
|
|
2
|
+
export const JWT_NEVER_EXPIRES = null;
|
|
3
|
+
|
|
4
|
+
export const DEFAULT_ALGORITHM = 'HS512';
|
|
5
|
+
|
|
6
|
+
export const VERIFY_OPTIONS =
|
|
7
|
+
{
|
|
8
|
+
//
|
|
9
|
+
// Never forget to make this explicit to
|
|
10
|
+
// prevent signature stripping attacks
|
|
11
|
+
//
|
|
12
|
+
algorithms: [ DEFAULT_ALGORITHM ]
|
|
13
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export class SecretKeyError extends Error {
|
|
2
|
+
}
|
|
3
|
+
export class TokenExpiredError extends Error {
|
|
4
|
+
/**
|
|
5
|
+
* @param {string} message - Error message
|
|
6
|
+
* @param {Date|{expiredAt?: Date}} [details] - When token expired or details object
|
|
7
|
+
* @param {Error} [cause] - Original error
|
|
8
|
+
*/
|
|
9
|
+
constructor(message: string, details?: Date | {
|
|
10
|
+
expiredAt?: Date;
|
|
11
|
+
}, cause?: Error);
|
|
12
|
+
expiredAt: Date;
|
|
13
|
+
cause: Error;
|
|
14
|
+
}
|
|
15
|
+
export class JsonWebTokenError extends Error {
|
|
16
|
+
/**
|
|
17
|
+
* @param {string} message - Error message
|
|
18
|
+
* @param {Error|{inner?: Error}} [details] - Inner error or details object
|
|
19
|
+
* @param {Error} [cause] - Original error
|
|
20
|
+
*/
|
|
21
|
+
constructor(message: string, details?: Error | {
|
|
22
|
+
inner?: Error;
|
|
23
|
+
}, cause?: Error);
|
|
24
|
+
inner: Error;
|
|
25
|
+
cause: Error;
|
|
26
|
+
}
|
|
27
|
+
export class InvalidSignatureError extends JsonWebTokenError {
|
|
28
|
+
}
|
|
29
|
+
export class NotBeforeError extends JsonWebTokenError {
|
|
30
|
+
/**
|
|
31
|
+
* @param {string} message - Error message
|
|
32
|
+
* @param {Date|{date?: Date}} [details] - Date when token becomes valid or details object
|
|
33
|
+
* @param {Error} [cause] - Original error
|
|
34
|
+
*/
|
|
35
|
+
constructor(message: string, details?: Date | {
|
|
36
|
+
date?: Date;
|
|
37
|
+
}, cause?: Error);
|
|
38
|
+
date: Date;
|
|
39
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
export class SecretKeyError extends Error {}
|
|
2
|
+
|
|
3
|
+
export class TokenExpiredError extends Error {
|
|
4
|
+
/**
|
|
5
|
+
* @param {string} message - Error message
|
|
6
|
+
* @param {Date|{expiredAt?: Date}} [details] - When token expired or details object
|
|
7
|
+
* @param {Error} [cause] - Original error
|
|
8
|
+
*/
|
|
9
|
+
constructor(message, details, cause) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.name = 'TokenExpiredError';
|
|
12
|
+
|
|
13
|
+
// Handle both Date and object formats for details
|
|
14
|
+
if (details instanceof Date) {
|
|
15
|
+
this.expiredAt = details;
|
|
16
|
+
} else {
|
|
17
|
+
this.expiredAt = details?.expiredAt ?? null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
this.cause = cause ?? null;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export class JsonWebTokenError extends Error {
|
|
25
|
+
/**
|
|
26
|
+
* @param {string} message - Error message
|
|
27
|
+
* @param {Error|{inner?: Error}} [details] - Inner error or details object
|
|
28
|
+
* @param {Error} [cause] - Original error
|
|
29
|
+
*/
|
|
30
|
+
constructor(message, details, cause) {
|
|
31
|
+
super(message);
|
|
32
|
+
this.name = 'JsonWebTokenError';
|
|
33
|
+
|
|
34
|
+
// Handle both Error and object formats for details
|
|
35
|
+
if (details instanceof Error) {
|
|
36
|
+
this.inner = details;
|
|
37
|
+
} else {
|
|
38
|
+
this.inner = details?.inner ?? null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
this.cause = cause ?? null;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export class InvalidSignatureError extends JsonWebTokenError {
|
|
46
|
+
/**
|
|
47
|
+
* @param {string} message - Error message
|
|
48
|
+
* @param {Error|{inner?: Error}} [details] - Inner error or details object
|
|
49
|
+
* @param {Error} [cause] - Original error
|
|
50
|
+
*/
|
|
51
|
+
constructor(message, details, cause) {
|
|
52
|
+
super(message, details, cause);
|
|
53
|
+
this.name = 'InvalidSignatureError';
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export class NotBeforeError extends JsonWebTokenError {
|
|
58
|
+
/**
|
|
59
|
+
* @param {string} message - Error message
|
|
60
|
+
* @param {Date|{date?: Date}} [details] - Date when token becomes valid or details object
|
|
61
|
+
* @param {Error} [cause] - Original error
|
|
62
|
+
*/
|
|
63
|
+
constructor(message, details, cause) {
|
|
64
|
+
super(message, null, cause);
|
|
65
|
+
this.name = 'NotBeforeError';
|
|
66
|
+
|
|
67
|
+
// Handle both Date and object formats for details
|
|
68
|
+
if (details instanceof Date) {
|
|
69
|
+
this.date = details;
|
|
70
|
+
} else {
|
|
71
|
+
this.date = details?.date ?? null;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generate a secret key with the specified number of bytes
|
|
3
|
+
* - The default length is 64 bytes, which is 512 bits, which is a nice secret
|
|
4
|
+
* key length for the HS512 algorithm
|
|
5
|
+
*
|
|
6
|
+
* @returns {string} a base64 encoded secret key string
|
|
7
|
+
*/
|
|
8
|
+
export function generateSecretKeyBase64(numberOfBytes?: number): string;
|
|
9
|
+
/**
|
|
10
|
+
* Create a string that can be used as secret key for HMAC
|
|
11
|
+
* - An HMAC is an Hash based Message Authentication Code
|
|
12
|
+
*
|
|
13
|
+
* - The formula for calculating a HMAC is:
|
|
14
|
+
*
|
|
15
|
+
* HMAC = hashFunc(secret key + message)
|
|
16
|
+
*
|
|
17
|
+
* - This function generates a long random secret
|
|
18
|
+
*
|
|
19
|
+
* - The secret key is quite long because long keys are more likely to
|
|
20
|
+
* resistent brute force attacks
|
|
21
|
+
*
|
|
22
|
+
* - The returned secret is a base58 encoded string
|
|
23
|
+
*
|
|
24
|
+
* @note The standard javascript random generator is used. For more secure
|
|
25
|
+
* secret keys consider using crypto
|
|
26
|
+
*
|
|
27
|
+
* @returns {string} generated secret, formatted as base 58 string
|
|
28
|
+
*/
|
|
29
|
+
export function generateSecretKeyForHmacBase58(): string;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JWT secret key generation utilities
|
|
3
|
+
*
|
|
4
|
+
* @description
|
|
5
|
+
* This module provides utilities for generating cryptographically secure
|
|
6
|
+
* secret keys for JWT signing and verification.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { base58fromNumber, bytesToNumber } from '../../util/bases.js';
|
|
10
|
+
import { randomBytes, randomBytesBase64 } from '../../util/random.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Generate a secret key with the specified number of bytes
|
|
14
|
+
* - The default length is 64 bytes, which is 512 bits, which is a nice secret
|
|
15
|
+
* key length for the HS512 algorithm
|
|
16
|
+
*
|
|
17
|
+
* @returns {string} a base64 encoded secret key string
|
|
18
|
+
*/
|
|
19
|
+
export function generateSecretKeyBase64( numberOfBytes=64 )
|
|
20
|
+
{
|
|
21
|
+
return randomBytesBase64( numberOfBytes );
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Create a string that can be used as secret key for HMAC
|
|
26
|
+
* - An HMAC is an Hash based Message Authentication Code
|
|
27
|
+
*
|
|
28
|
+
* - The formula for calculating a HMAC is:
|
|
29
|
+
*
|
|
30
|
+
* HMAC = hashFunc(secret key + message)
|
|
31
|
+
*
|
|
32
|
+
* - This function generates a long random secret
|
|
33
|
+
*
|
|
34
|
+
* - The secret key is quite long because long keys are more likely to
|
|
35
|
+
* resistent brute force attacks
|
|
36
|
+
*
|
|
37
|
+
* - The returned secret is a base58 encoded string
|
|
38
|
+
*
|
|
39
|
+
* @note The standard javascript random generator is used. For more secure
|
|
40
|
+
* secret keys consider using crypto
|
|
41
|
+
*
|
|
42
|
+
* @returns {string} generated secret, formatted as base 58 string
|
|
43
|
+
*/
|
|
44
|
+
export function generateSecretKeyForHmacBase58()
|
|
45
|
+
{
|
|
46
|
+
const numberOfBytes = 64;
|
|
47
|
+
|
|
48
|
+
const bytes = randomBytes( numberOfBytes );
|
|
49
|
+
|
|
50
|
+
const numericValue = bytesToNumber( bytes );
|
|
51
|
+
|
|
52
|
+
return base58fromNumber( numericValue );
|
|
53
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Standard JWT claims (RFC 7519)
|
|
3
|
+
*/
|
|
4
|
+
export type JwtStandardClaims = {
|
|
5
|
+
/**
|
|
6
|
+
* - Issuer
|
|
7
|
+
*/
|
|
8
|
+
iss?: string;
|
|
9
|
+
/**
|
|
10
|
+
* - Subject
|
|
11
|
+
*/
|
|
12
|
+
sub?: string;
|
|
13
|
+
/**
|
|
14
|
+
* - Audience
|
|
15
|
+
*/
|
|
16
|
+
aud?: string | string[];
|
|
17
|
+
/**
|
|
18
|
+
* - Expiration time (seconds since epoch)
|
|
19
|
+
*/
|
|
20
|
+
exp?: number;
|
|
21
|
+
/**
|
|
22
|
+
* - Not before (seconds since epoch)
|
|
23
|
+
*/
|
|
24
|
+
nbf?: number;
|
|
25
|
+
/**
|
|
26
|
+
* - Issued at (seconds since epoch)
|
|
27
|
+
*/
|
|
28
|
+
iat?: number;
|
|
29
|
+
/**
|
|
30
|
+
* - JWT ID
|
|
31
|
+
*/
|
|
32
|
+
jti?: string;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* JWT payload - standard claims plus any custom data
|
|
36
|
+
*/
|
|
37
|
+
export type JwtPayload = JwtStandardClaims & Record<string, any>;
|
|
38
|
+
/**
|
|
39
|
+
* JWT signing options
|
|
40
|
+
*/
|
|
41
|
+
export type SignOptions = {
|
|
42
|
+
/**
|
|
43
|
+
* - Signing algorithm (HS256, RS256, etc.)
|
|
44
|
+
*/
|
|
45
|
+
algorithm?: string;
|
|
46
|
+
/**
|
|
47
|
+
* - Expiration time ('1h', 3600, etc.)
|
|
48
|
+
*/
|
|
49
|
+
expiresIn?: string | number;
|
|
50
|
+
/**
|
|
51
|
+
* - Not valid before time
|
|
52
|
+
*/
|
|
53
|
+
notBefore?: string | number;
|
|
54
|
+
/**
|
|
55
|
+
* - Audience claim
|
|
56
|
+
*/
|
|
57
|
+
audience?: string | string[];
|
|
58
|
+
/**
|
|
59
|
+
* - Issuer claim
|
|
60
|
+
*/
|
|
61
|
+
issuer?: string;
|
|
62
|
+
/**
|
|
63
|
+
* - JWT ID claim
|
|
64
|
+
*/
|
|
65
|
+
jwtid?: string;
|
|
66
|
+
/**
|
|
67
|
+
* - Subject claim
|
|
68
|
+
*/
|
|
69
|
+
subject?: string;
|
|
70
|
+
/**
|
|
71
|
+
* - Skip iat claim
|
|
72
|
+
*/
|
|
73
|
+
noTimestamp?: boolean;
|
|
74
|
+
/**
|
|
75
|
+
* - Additional header claims
|
|
76
|
+
*/
|
|
77
|
+
header?: any;
|
|
78
|
+
/**
|
|
79
|
+
* - Key ID header claim
|
|
80
|
+
*/
|
|
81
|
+
keyid?: string;
|
|
82
|
+
/**
|
|
83
|
+
* - Modify payload object directly
|
|
84
|
+
*/
|
|
85
|
+
mutatePayload?: boolean;
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* JWT verification options
|
|
89
|
+
*/
|
|
90
|
+
export type VerifyOptions = {
|
|
91
|
+
/**
|
|
92
|
+
* - Allowed algorithms
|
|
93
|
+
*/
|
|
94
|
+
algorithms?: string[];
|
|
95
|
+
/**
|
|
96
|
+
* - Expected audience
|
|
97
|
+
*/
|
|
98
|
+
audience?: string | string[];
|
|
99
|
+
/**
|
|
100
|
+
* - Return object with payload and header
|
|
101
|
+
*/
|
|
102
|
+
complete?: boolean;
|
|
103
|
+
/**
|
|
104
|
+
* - Expected issuer
|
|
105
|
+
*/
|
|
106
|
+
issuer?: string;
|
|
107
|
+
/**
|
|
108
|
+
* - Skip expiration validation
|
|
109
|
+
*/
|
|
110
|
+
ignoreExpiration?: boolean;
|
|
111
|
+
/**
|
|
112
|
+
* - Skip notBefore validation
|
|
113
|
+
*/
|
|
114
|
+
ignoreNotBefore?: boolean;
|
|
115
|
+
/**
|
|
116
|
+
* - Expected subject
|
|
117
|
+
*/
|
|
118
|
+
subject?: string;
|
|
119
|
+
/**
|
|
120
|
+
* - Clock tolerance in seconds
|
|
121
|
+
*/
|
|
122
|
+
clockTolerance?: number;
|
|
123
|
+
/**
|
|
124
|
+
* - Maximum token age in seconds
|
|
125
|
+
*/
|
|
126
|
+
maxAge?: number;
|
|
127
|
+
/**
|
|
128
|
+
* - Current time override (seconds)
|
|
129
|
+
*/
|
|
130
|
+
clockTimestamp?: number;
|
|
131
|
+
/**
|
|
132
|
+
* - Expected nonce claim
|
|
133
|
+
*/
|
|
134
|
+
nonce?: string;
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* JWT secret types
|
|
138
|
+
*/
|
|
139
|
+
export type Secret = string | Buffer | {
|
|
140
|
+
key: string | Buffer;
|
|
141
|
+
passphrase: string;
|
|
142
|
+
};
|
|
143
|
+
/**
|
|
144
|
+
* Decoded JWT result
|
|
145
|
+
*/
|
|
146
|
+
export type JwtDecoded = {
|
|
147
|
+
/**
|
|
148
|
+
* - The decoded payload
|
|
149
|
+
*/
|
|
150
|
+
payload: JwtPayload;
|
|
151
|
+
/**
|
|
152
|
+
* - The decoded header
|
|
153
|
+
*/
|
|
154
|
+
header: any;
|
|
155
|
+
/**
|
|
156
|
+
* - The signature
|
|
157
|
+
*/
|
|
158
|
+
signature: string;
|
|
159
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JWT type definitions
|
|
3
|
+
*
|
|
4
|
+
* @description
|
|
5
|
+
* Type definitions for JWT operations, based on jsonwebtoken library types.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Standard JWT claims (RFC 7519)
|
|
10
|
+
* @typedef {Object} JwtStandardClaims
|
|
11
|
+
* @property {string} [iss] - Issuer
|
|
12
|
+
* @property {string} [sub] - Subject
|
|
13
|
+
* @property {string|string[]} [aud] - Audience
|
|
14
|
+
* @property {number} [exp] - Expiration time (seconds since epoch)
|
|
15
|
+
* @property {number} [nbf] - Not before (seconds since epoch)
|
|
16
|
+
* @property {number} [iat] - Issued at (seconds since epoch)
|
|
17
|
+
* @property {string} [jti] - JWT ID
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* JWT payload - standard claims plus any custom data
|
|
22
|
+
* @typedef {JwtStandardClaims & Record<string, any>} JwtPayload
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* JWT signing options
|
|
27
|
+
* @typedef {Object} SignOptions
|
|
28
|
+
* @property {string} [algorithm] - Signing algorithm (HS256, RS256, etc.)
|
|
29
|
+
* @property {string|number} [expiresIn] - Expiration time ('1h', 3600, etc.)
|
|
30
|
+
* @property {string|number} [notBefore] - Not valid before time
|
|
31
|
+
* @property {string|string[]} [audience] - Audience claim
|
|
32
|
+
* @property {string} [issuer] - Issuer claim
|
|
33
|
+
* @property {string} [jwtid] - JWT ID claim
|
|
34
|
+
* @property {string} [subject] - Subject claim
|
|
35
|
+
* @property {boolean} [noTimestamp] - Skip iat claim
|
|
36
|
+
* @property {Object} [header] - Additional header claims
|
|
37
|
+
* @property {string} [keyid] - Key ID header claim
|
|
38
|
+
* @property {boolean} [mutatePayload] - Modify payload object directly
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* JWT verification options
|
|
43
|
+
* @typedef {Object} VerifyOptions
|
|
44
|
+
* @property {string[]} [algorithms] - Allowed algorithms
|
|
45
|
+
* @property {string|string[]} [audience] - Expected audience
|
|
46
|
+
* @property {boolean} [complete] - Return object with payload and header
|
|
47
|
+
* @property {string} [issuer] - Expected issuer
|
|
48
|
+
* @property {boolean} [ignoreExpiration] - Skip expiration validation
|
|
49
|
+
* @property {boolean} [ignoreNotBefore] - Skip notBefore validation
|
|
50
|
+
* @property {string} [subject] - Expected subject
|
|
51
|
+
* @property {number} [clockTolerance] - Clock tolerance in seconds
|
|
52
|
+
* @property {number} [maxAge] - Maximum token age in seconds
|
|
53
|
+
* @property {number} [clockTimestamp] - Current time override (seconds)
|
|
54
|
+
* @property {string} [nonce] - Expected nonce claim
|
|
55
|
+
*/
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* JWT secret types
|
|
59
|
+
* @typedef {string|Buffer|{key: string|Buffer, passphrase: string}} Secret
|
|
60
|
+
*/
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Decoded JWT result
|
|
65
|
+
* @typedef {Object} JwtDecoded
|
|
66
|
+
* @property {JwtPayload} payload - The decoded payload
|
|
67
|
+
* @property {Object} header - The decoded header
|
|
68
|
+
* @property {string} signature - The signature
|
|
69
|
+
*/
|
|
70
|
+
|
|
71
|
+
// Export types for use in JSDoc
|
|
72
|
+
export {};
|
|
@@ -0,0 +1,44 @@
|
|
|
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;
|
|
39
|
+
/**
|
|
40
|
+
* Casts jsonwebtoken library errors to internal error types
|
|
41
|
+
* @param {Error} error - The original jsonwebtoken error
|
|
42
|
+
* @returns {Error} - The corresponding internal error
|
|
43
|
+
*/
|
|
44
|
+
export function castJwtError(error: Error): Error;
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JWT utility functions
|
|
3
|
+
*
|
|
4
|
+
* @description
|
|
5
|
+
* This module provides utility functions for JWT operations including
|
|
6
|
+
* sign, verify and error casting.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import jwt from 'jsonwebtoken';
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
TokenExpiredError as JwtTokenExpiredError,
|
|
13
|
+
JsonWebTokenError as JwtJsonWebTokenError,
|
|
14
|
+
NotBeforeError as JwtNotBeforeError
|
|
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
|
+
|
|
25
|
+
import {
|
|
26
|
+
TokenExpiredError,
|
|
27
|
+
JsonWebTokenError,
|
|
28
|
+
InvalidSignatureError,
|
|
29
|
+
NotBeforeError
|
|
30
|
+
} from './errors.js';
|
|
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
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Casts jsonwebtoken library errors to internal error types
|
|
129
|
+
* @param {Error} error - The original jsonwebtoken error
|
|
130
|
+
* @returns {Error} - The corresponding internal error
|
|
131
|
+
*/
|
|
132
|
+
export function castJwtError(error) {
|
|
133
|
+
if (error instanceof JwtTokenExpiredError) {
|
|
134
|
+
return new TokenExpiredError(error.message, error.expiredAt, error);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (error instanceof JwtNotBeforeError) {
|
|
138
|
+
return new NotBeforeError(error.message, error.date, error);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (error instanceof JwtJsonWebTokenError) {
|
|
142
|
+
if (error.message === 'invalid signature') {
|
|
143
|
+
return new InvalidSignatureError(error.message, error, error);
|
|
144
|
+
}
|
|
145
|
+
return new JsonWebTokenError(error.message, error, error);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Return original error if not a known JWT error
|
|
149
|
+
return error;
|
|
150
|
+
}
|
package/dist/auth/jwt.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JWT utilities main export
|
|
3
|
+
*
|
|
4
|
+
* @description
|
|
5
|
+
* This module provides a clean API for JWT operations including signing,
|
|
6
|
+
* verifying tokens, and generating secret keys.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export * from './jwt/core.js';
|
|
10
|
+
export * from './jwt/generators.js';
|
|
11
|
+
export * from './jwt/errors.js';
|
|
12
|
+
export * from './jwt/constants.js';
|