@hkdigital/lib-core 0.4.11 → 0.4.13

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.
Files changed (135) hide show
  1. package/README.md +4 -1
  2. package/dist/auth/errors.d.ts +1 -1
  3. package/dist/auth/errors.js +1 -1
  4. package/dist/auth/jwt/constants.d.ts +6 -0
  5. package/dist/auth/jwt/constants.js +13 -0
  6. package/dist/auth/jwt/core.d.ts +38 -0
  7. package/dist/auth/jwt/core.js +114 -0
  8. package/dist/auth/jwt/errors.d.ts +39 -0
  9. package/dist/auth/jwt/errors.js +74 -0
  10. package/dist/auth/jwt/generators.d.ts +29 -0
  11. package/dist/auth/jwt/generators.js +53 -0
  12. package/dist/auth/jwt/typedef.d.ts +159 -0
  13. package/dist/auth/jwt/typedef.js +72 -0
  14. package/dist/auth/jwt/util.d.ts +6 -0
  15. package/dist/auth/jwt/util.js +43 -0
  16. package/dist/auth/jwt.d.ts +4 -0
  17. package/dist/auth/jwt.js +12 -0
  18. package/dist/config/README.md +28 -0
  19. package/dist/design/README.md +12 -0
  20. package/dist/generic/data/classes/IterableTree.js +1 -1
  21. package/dist/generic/data/classes/Selector.js +1 -1
  22. package/dist/generic/promises/classes/HkPromise.js +1 -1
  23. package/dist/generic/streaming/classes/ServerEventsStore.js +1 -1
  24. package/dist/generic/typedef.d.ts +2 -0
  25. package/dist/generic/typedef.js +2 -0
  26. package/dist/logging/internal/adapters/console.js +3 -3
  27. package/dist/logging/internal/adapters/pino.js +17 -3
  28. package/dist/logging/internal/adapters/pino.js__ +260 -0
  29. package/dist/logging/internal/test-errors.js +3 -2
  30. package/dist/network/http/errors.js +10 -4
  31. package/dist/network/http/headers.js +1 -1
  32. package/dist/network/http/http-request.js +1 -1
  33. package/dist/network/http/json-request.js +1 -1
  34. package/dist/network/http/response.js +1 -1
  35. package/dist/network/loaders/audio/AudioScene.svelte.js +1 -1
  36. package/dist/network/loaders/image/ImageScene.svelte.js +1 -1
  37. package/dist/network/loaders/image/utils/index.js +1 -1
  38. package/dist/network/states/NetworkLoader.svelte.js +1 -1
  39. package/dist/network/typedef.d.ts +2 -0
  40. package/dist/network/typedef.js +3 -1
  41. package/dist/services/service-base/ServiceBase.js +1 -3
  42. package/dist/services/typedef.d.ts +2 -0
  43. package/dist/services/typedef.js +2 -0
  44. package/dist/state/typedef.d.ts +3 -0
  45. package/dist/state/typedef.js +3 -0
  46. package/dist/ui/components/drag-drop/drag-state.svelte.d.ts +3 -3
  47. package/dist/ui/components/hk-app-layout/HkAppLayout.state.svelte.d.ts +3 -3
  48. package/dist/ui/components/presenter/util.js +1 -1
  49. package/dist/ui/components/tab-bar/HkTabBar.state.svelte.d.ts +3 -3
  50. package/dist/ui/components/tab-bar/HkTabBarSelector.state.svelte.d.ts +3 -3
  51. package/dist/util/array/index.d.ts +0 -4
  52. package/dist/util/array/index.js +1 -5
  53. package/dist/util/array.d.ts +1 -0
  54. package/dist/util/array.js +9 -0
  55. package/dist/util/bases/base64.d.ts +8 -0
  56. package/dist/util/bases/base64.js +23 -0
  57. package/dist/util/bases/index.d.ts +0 -1
  58. package/dist/util/bases/index.js +0 -1
  59. package/dist/util/bases.d.ts +2 -0
  60. package/dist/util/bases.js +10 -0
  61. package/dist/util/compare.d.ts +1 -0
  62. package/dist/util/compare.js +9 -0
  63. package/dist/util/css/index.d.ts +0 -1
  64. package/dist/util/css/index.js +0 -1
  65. package/dist/util/css.d.ts +1 -0
  66. package/dist/util/css.js +8 -0
  67. package/dist/util/env.d.ts +1 -0
  68. package/dist/util/env.js +8 -0
  69. package/dist/util/exceptions.d.ts +1 -0
  70. package/dist/util/exceptions.js +8 -0
  71. package/dist/util/expect/arrays.js +2 -0
  72. package/dist/util/expect/compounds.d.ts +20 -0
  73. package/dist/util/expect/compounds.js +48 -0
  74. package/dist/util/expect/objects.d.ts +28 -0
  75. package/dist/util/expect/objects.js +70 -0
  76. package/dist/util/expect/primitives.d.ts +31 -1
  77. package/dist/util/expect/primitives.js +56 -0
  78. package/dist/util/expect/values.d.ts +26 -0
  79. package/dist/util/expect/values.js +48 -0
  80. package/dist/util/expect.d.ts +6 -0
  81. package/dist/util/expect.js +31 -0
  82. package/dist/util/function/index.js +1 -1
  83. package/dist/util/function.d.ts +1 -0
  84. package/dist/util/function.js +8 -0
  85. package/dist/util/geometry.d.ts +1 -0
  86. package/dist/util/geometry.js +9 -0
  87. package/dist/util/is.d.ts +1 -0
  88. package/dist/util/is.js +8 -0
  89. package/dist/util/iterate/index.js +1 -1
  90. package/dist/util/iterate.d.ts +1 -0
  91. package/dist/util/iterate.js +8 -0
  92. package/dist/util/object/index.js +2 -2
  93. package/dist/util/object.d.ts +1 -0
  94. package/dist/util/object.js +8 -0
  95. package/dist/util/random/bytes.d.ts +28 -0
  96. package/dist/util/random/bytes.js +72 -0
  97. package/dist/util/random.d.ts +1 -0
  98. package/dist/util/random.js +9 -0
  99. package/dist/util/singleton/index.js +1 -1
  100. package/dist/util/singleton.d.ts +1 -0
  101. package/dist/util/singleton.js +8 -0
  102. package/dist/util/ssr.d.ts +1 -0
  103. package/dist/util/ssr.js +8 -0
  104. package/dist/util/string/convert.js +1 -1
  105. package/dist/util/string/index.d.ts +0 -5
  106. package/dist/util/string/index.js +0 -5
  107. package/dist/util/string/interpolate.js +1 -1
  108. package/dist/util/string.d.ts +5 -0
  109. package/dist/util/string.js +12 -0
  110. package/dist/util/svelte/index.d.ts +0 -3
  111. package/dist/util/svelte/index.js +0 -4
  112. package/dist/util/svelte.d.ts +3 -0
  113. package/dist/util/svelte.js +11 -0
  114. package/dist/util/sveltekit/index.d.ts +0 -1
  115. package/dist/util/sveltekit/index.js +0 -1
  116. package/dist/util/sveltekit/route-folders/index.js +1 -1
  117. package/dist/util/sveltekit.d.ts +1 -0
  118. package/dist/util/sveltekit.js +8 -0
  119. package/dist/util/time/index.d.ts +7 -0
  120. package/dist/util/time/index.js +12 -1
  121. package/dist/util/time.d.ts +1 -0
  122. package/dist/util/time.js +1 -0
  123. package/dist/util/unique.d.ts +1 -0
  124. package/dist/util/unique.js +8 -0
  125. package/dist/valibot/{index.d.ts → valibot.d.ts} +1 -2
  126. package/dist/valibot/{index.js → valibot.js} +1 -6
  127. package/package.json +3 -1
  128. package/dist/auth/errors/jwt.d.ts +0 -8
  129. package/dist/auth/errors/jwt.js +0 -5
  130. package/dist/util/expect/index.d.ts +0 -103
  131. package/dist/util/expect/index.js +0 -259
  132. package/dist/util/index.d.ts +0 -10
  133. /package/dist/util/{geo → geometry}/index.d.ts +0 -0
  134. /package/dist/util/{geo → geometry}/index.js +0 -0
  135. /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
@@ -1 +1 @@
1
- export * from "./errors/jwt.js";
1
+ export * from "./jwt/errors.js";
@@ -1 +1 @@
1
- export * from './errors/jwt.js';
1
+ export * from './jwt/errors.js';
@@ -0,0 +1,6 @@
1
+ export const JWT_DEFAULT_EXPIRES_IN: "30h";
2
+ export const JWT_NEVER_EXPIRES: any;
3
+ export const DEFAULT_ALGORITHM: "HS512";
4
+ export namespace VERIFY_OPTIONS {
5
+ let algorithms: string[];
6
+ }
@@ -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,38 @@
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;
@@ -0,0 +1,114 @@
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
+ }
@@ -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,6 @@
1
+ /**
2
+ * Casts jsonwebtoken library errors to internal error types
3
+ * @param {Error} error - The original jsonwebtoken error
4
+ * @returns {Error} - The corresponding internal error
5
+ */
6
+ export function castJwtError(error: Error): Error;
@@ -0,0 +1,43 @@
1
+ /**
2
+ * JWT utility functions
3
+ *
4
+ * @description
5
+ * This module provides utility functions for JWT operations including error casting.
6
+ */
7
+
8
+ import jwt, {
9
+ TokenExpiredError as JwtTokenExpiredError,
10
+ JsonWebTokenError as JwtJsonWebTokenError,
11
+ NotBeforeError as JwtNotBeforeError
12
+ } from 'jsonwebtoken';
13
+ import {
14
+ TokenExpiredError,
15
+ JsonWebTokenError,
16
+ InvalidSignatureError,
17
+ NotBeforeError
18
+ } from './errors.js';
19
+
20
+ /**
21
+ * Casts jsonwebtoken library errors to internal error types
22
+ * @param {Error} error - The original jsonwebtoken error
23
+ * @returns {Error} - The corresponding internal error
24
+ */
25
+ export function castJwtError(error) {
26
+ if (error instanceof JwtTokenExpiredError) {
27
+ return new TokenExpiredError(error.message, error.expiredAt, error);
28
+ }
29
+
30
+ if (error instanceof JwtNotBeforeError) {
31
+ return new NotBeforeError(error.message, error.date, error);
32
+ }
33
+
34
+ if (error instanceof JwtJsonWebTokenError) {
35
+ if (error.message === 'invalid signature') {
36
+ return new InvalidSignatureError(error.message, error, error);
37
+ }
38
+ return new JsonWebTokenError(error.message, error, error);
39
+ }
40
+
41
+ // Return original error if not a known JWT error
42
+ return error;
43
+ }
@@ -0,0 +1,4 @@
1
+ export * from "./jwt/core.js";
2
+ export * from "./jwt/generators.js";
3
+ export * from "./jwt/errors.js";
4
+ export * from "./jwt/constants.js";