vortez 5.0.0-dev.17 → 5.0.0-dev.19

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 (85) hide show
  1. package/.gitignore +9 -4
  2. package/README.md +681 -176
  3. package/build/Vortez.d.ts +1 -0
  4. package/build/Vortez.js +1 -0
  5. package/build/Vortez.js.map +1 -1
  6. package/build/beta/JwtManager/HeaderValidator.d.ts +25 -0
  7. package/build/beta/JwtManager/HeaderValidator.js +47 -0
  8. package/build/beta/JwtManager/HeaderValidator.js.map +1 -0
  9. package/build/beta/JwtManager/Jwt.d.ts +14 -6
  10. package/build/beta/JwtManager/Jwt.js +15 -7
  11. package/build/beta/JwtManager/Jwt.js.map +1 -1
  12. package/build/beta/JwtManager/JwtManager.d.ts +31 -24
  13. package/build/beta/JwtManager/JwtManager.js +61 -40
  14. package/build/beta/JwtManager/JwtManager.js.map +1 -1
  15. package/build/beta/JwtManager/JwtUtils.d.ts +19 -4
  16. package/build/beta/JwtManager/JwtUtils.js +18 -0
  17. package/build/beta/JwtManager/JwtUtils.js.map +1 -1
  18. package/build/beta/JwtManager/KeyEntry.d.ts +52 -0
  19. package/build/beta/JwtManager/KeyEntry.js +42 -0
  20. package/build/beta/JwtManager/KeyEntry.js.map +1 -0
  21. package/build/beta/JwtManager/KeyGenerator.d.ts +5 -0
  22. package/build/beta/JwtManager/KeyGenerator.js +12 -9
  23. package/build/beta/JwtManager/KeyGenerator.js.map +1 -1
  24. package/build/server/Response.d.ts +1 -1
  25. package/build/server/Response.js +1 -1
  26. package/build/server/Response.js.map +1 -1
  27. package/build/server/Server.d.ts +4 -4
  28. package/build/server/Server.js +5 -5
  29. package/build/server/Server.js.map +1 -1
  30. package/build/server/ServerDebug.d.ts +10 -1
  31. package/build/server/ServerDebug.js +85 -17
  32. package/build/server/ServerDebug.js.map +1 -1
  33. package/build/server/config/Config.d.ts +274 -47
  34. package/build/server/config/Config.js +68 -47
  35. package/build/server/config/Config.js.map +1 -1
  36. package/build/server/config/{ConfigLoader.d.ts → Loader.d.ts} +4 -5
  37. package/build/server/config/{ConfigLoader.js → Loader.js} +7 -10
  38. package/build/server/config/Loader.js.map +1 -0
  39. package/build/server/router/Router.d.ts +87 -30
  40. package/build/server/router/Router.js +110 -48
  41. package/build/server/router/Router.js.map +1 -1
  42. package/build/server/router/algorithm/Algorithm.d.ts +39 -0
  43. package/build/server/router/algorithm/Algorithm.js +20 -0
  44. package/build/server/router/algorithm/Algorithm.js.map +1 -0
  45. package/build/server/router/algorithm/FIFO.d.ts +15 -0
  46. package/build/server/router/algorithm/FIFO.js +24 -0
  47. package/build/server/router/algorithm/FIFO.js.map +1 -0
  48. package/build/server/router/algorithm/Tree.d.ts +38 -0
  49. package/build/server/router/algorithm/Tree.js +126 -0
  50. package/build/server/router/algorithm/Tree.js.map +1 -0
  51. package/build/server/router/middleware/WsMiddleware.js +1 -1
  52. package/build/server/router/middleware/WsMiddleware.js.map +1 -1
  53. package/build/utilities/Flatten.d.ts +56 -0
  54. package/build/utilities/Flatten.js +59 -0
  55. package/build/utilities/Flatten.js.map +1 -0
  56. package/build/utilities/Utilities.d.ts +7 -58
  57. package/build/utilities/Utilities.js +8 -33
  58. package/build/utilities/Utilities.js.map +1 -1
  59. package/build/utilities/schema/Introspection.d.ts +24 -0
  60. package/build/utilities/schema/Introspection.js +87 -0
  61. package/build/utilities/schema/Introspection.js.map +1 -0
  62. package/build/utilities/schema/JSONSchema.d.ts +68 -0
  63. package/build/utilities/schema/JSONSchema.js +13 -0
  64. package/build/utilities/schema/JSONSchema.js.map +1 -0
  65. package/build/utilities/schema/Schema.d.ts +253 -0
  66. package/build/utilities/schema/Schema.js +241 -0
  67. package/build/utilities/schema/Schema.js.map +1 -0
  68. package/build/utilities/schema/SchemaError.d.ts +10 -0
  69. package/build/utilities/schema/SchemaError.js +13 -0
  70. package/build/utilities/schema/SchemaError.js.map +1 -0
  71. package/build/utilities/schema/Validator.d.ts +94 -0
  72. package/build/utilities/schema/Validator.js +246 -0
  73. package/build/utilities/schema/Validator.js.map +1 -0
  74. package/package.json +1 -1
  75. package/tests/config/config.js +233 -0
  76. package/tests/jwtManager/jwtManager.js +310 -46
  77. package/tests/router.js +596 -0
  78. package/tests/schema/schema.js +368 -0
  79. package/tests/test.env +0 -0
  80. package/tests/test.js +3 -3
  81. package/build/server/config/ConfigLoader.js.map +0 -1
  82. package/build/server/config/ConfigValidator.d.ts +0 -71
  83. package/build/server/config/ConfigValidator.js +0 -131
  84. package/build/server/config/ConfigValidator.js.map +0 -1
  85. package/examples/in-docs.js +0 -96
package/build/Vortez.d.ts CHANGED
@@ -10,4 +10,5 @@ export { Utilities } from "./utilities/Utilities.js";
10
10
  export * as Beta from "./beta/Beta.js";
11
11
  export { ServerError } from "./server/ServerError.js";
12
12
  export { Router } from "./server/router/Router.js";
13
+ export { Config } from "./server/config/Config.js";
13
14
  export { Server as default, Server as Vortez } from "./server/Server.js";
package/build/Vortez.js CHANGED
@@ -10,5 +10,6 @@ export { Utilities } from "./utilities/Utilities.js";
10
10
  export * as Beta from "./beta/Beta.js";
11
11
  export { ServerError } from "./server/ServerError.js";
12
12
  export { Router } from "./server/router/Router.js";
13
+ export { Config } from "./server/config/Config.js";
13
14
  export { Server as default, Server as Vortez } from "./server/Server.js";
14
15
  //# sourceMappingURL=Vortez.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"Vortez.js","sourceRoot":"","sources":["../src/Vortez.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AACrD,OAAO,KAAK,IAAI,MAAM,gBAAgB,CAAC;AACvC,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AACtD,OAAO,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAC;AACnD,OAAO,EAAE,MAAM,IAAI,OAAO,EAAE,MAAM,IAAI,MAAM,EAAE,MAAM,oBAAoB,CAAC"}
1
+ {"version":3,"file":"Vortez.js","sourceRoot":"","sources":["../src/Vortez.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AACrD,OAAO,KAAK,IAAI,MAAM,gBAAgB,CAAC;AACvC,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AACtD,OAAO,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAC;AACnD,OAAO,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAC;AACnD,OAAO,EAAE,MAAM,IAAI,OAAO,EAAE,MAAM,IAAI,MAAM,EAAE,MAAM,oBAAoB,CAAC"}
@@ -0,0 +1,25 @@
1
+ import type Jwt from "./Jwt.js";
2
+ export declare class HeaderValidator {
3
+ static ALLOWED_ALGO_PREFIXES: string[];
4
+ static ALLOWED_ALGO_LENGTHS: number[];
5
+ /**
6
+ * Static method to validate the JWT header. It checks if the header is a non-null object and then calls specific validation methods for the algorithm, type, and expiration claims. If any of the validations fail, it throws an error indicating the issue with the header.
7
+ * @param header The JWT header object to validate.
8
+ * @throws Will throw an error if the header is not a non-null object or if any of the specific validations fail.
9
+ */
10
+ static validate(header: any): asserts header is Jwt.Header;
11
+ /**
12
+ * Static method to validate the `alg` (algorithm) property in the JWT header. It checks if the `alg` property is present, ensures that it follows the expected format (a prefix followed by a length), and verifies that the prefix and length are among the allowed values. If any of these conditions are not met, it throws an error indicating the issue with the `alg` property.
13
+ * @param header The JWT header object to validate, which should contain the `alg` property.
14
+ * @throws Will throw an error if the `alg` property is missing, does not follow the expected format, or has an unsupported prefix or length.
15
+ */
16
+ static validateAlgorithm(header: any): void;
17
+ /**
18
+ * Static method to validate the `typ` (type) property in the JWT header. It checks if the `typ` property is present, ensures that it is a string, and verifies that its value is "jwt" (case-insensitive). If any of these conditions are not met, it throws an error indicating the issue with the `typ` property.
19
+ * @param header The JWT header object to validate, which should contain the `typ` property.
20
+ * @throws Will throw an error if the `typ` property is missing, is not a string, or does not equal "jwt" (case-insensitive).
21
+ */
22
+ static validateType(header: any): void;
23
+ }
24
+ export declare namespace HeaderValidator { }
25
+ export default HeaderValidator;
@@ -0,0 +1,47 @@
1
+ export class HeaderValidator {
2
+ static ALLOWED_ALGO_PREFIXES = ['HS', 'RS', 'PS', 'ES'];
3
+ static ALLOWED_ALGO_LENGTHS = [256, 384, 512];
4
+ /**
5
+ * Static method to validate the JWT header. It checks if the header is a non-null object and then calls specific validation methods for the algorithm, type, and expiration claims. If any of the validations fail, it throws an error indicating the issue with the header.
6
+ * @param header The JWT header object to validate.
7
+ * @throws Will throw an error if the header is not a non-null object or if any of the specific validations fail.
8
+ */
9
+ static validate(header) {
10
+ if (typeof header !== 'object' || header === null)
11
+ throw new Error('Header must be a non-null object');
12
+ this.validateAlgorithm(header);
13
+ this.validateType(header);
14
+ }
15
+ /**
16
+ * Static method to validate the `alg` (algorithm) property in the JWT header. It checks if the `alg` property is present, ensures that it follows the expected format (a prefix followed by a length), and verifies that the prefix and length are among the allowed values. If any of these conditions are not met, it throws an error indicating the issue with the `alg` property.
17
+ * @param header The JWT header object to validate, which should contain the `alg` property.
18
+ * @throws Will throw an error if the `alg` property is missing, does not follow the expected format, or has an unsupported prefix or length.
19
+ */
20
+ static validateAlgorithm(header) {
21
+ if (!('alg' in header))
22
+ throw new Error('Header must contain "alg" property');
23
+ const [prefix, lengthStr] = header.alg.match(/([A-Z]+)(\d+)/)?.slice(1) || [];
24
+ if (!prefix || !lengthStr)
25
+ throw new Error('Invalid "alg" format in header');
26
+ const length = parseInt(lengthStr, 10);
27
+ if (!HeaderValidator.ALLOWED_ALGO_PREFIXES.includes(prefix))
28
+ throw new Error(`Unsupported algorithm prefix: ${prefix}`);
29
+ if (!HeaderValidator.ALLOWED_ALGO_LENGTHS.includes(length))
30
+ throw new Error(`Unsupported algorithm length: ${length}`);
31
+ }
32
+ /**
33
+ * Static method to validate the `typ` (type) property in the JWT header. It checks if the `typ` property is present, ensures that it is a string, and verifies that its value is "jwt" (case-insensitive). If any of these conditions are not met, it throws an error indicating the issue with the `typ` property.
34
+ * @param header The JWT header object to validate, which should contain the `typ` property.
35
+ * @throws Will throw an error if the `typ` property is missing, is not a string, or does not equal "jwt" (case-insensitive).
36
+ */
37
+ static validateType(header) {
38
+ if (!('typ' in header))
39
+ throw new Error('Header must contain "typ" property');
40
+ if (typeof header.typ !== 'string')
41
+ throw new Error('The "typ" property must be a string');
42
+ if (header.typ.toLowerCase() !== 'jwt')
43
+ throw new Error('The "typ" property must be "jwt"');
44
+ }
45
+ }
46
+ export default HeaderValidator;
47
+ //# sourceMappingURL=HeaderValidator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"HeaderValidator.js","sourceRoot":"","sources":["../../../src/beta/JwtManager/HeaderValidator.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,eAAe;IACjB,MAAM,CAAC,qBAAqB,GAAG,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;IACxD,MAAM,CAAC,oBAAoB,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;IACrD;;;;OAIG;IACI,MAAM,CAAC,QAAQ,CAAC,MAAW;QAC9B,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,KAAK,IAAI;YAAE,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAC;QACvG,IAAI,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC;QAC/B,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC;IAC9B,CAAC;IACD;;;;OAIG;IACI,MAAM,CAAC,iBAAiB,CAAC,MAAW;QACvC,IAAI,CAAC,CAAC,KAAK,IAAI,MAAM,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAC;QAC9E,MAAM,CAAE,MAAM,EAAE,SAAS,CAAE,GAAG,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,eAAe,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;QAChF,IAAI,CAAC,MAAM,IAAI,CAAC,SAAS;YAAE,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;QAC7E,MAAM,MAAM,GAAG,QAAQ,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC;QACvC,IAAI,CAAC,eAAe,CAAC,qBAAqB,CAAC,QAAQ,CAAC,MAAM,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,iCAAiC,MAAM,EAAE,CAAC,CAAC;QACxH,IAAI,CAAC,eAAe,CAAC,oBAAoB,CAAC,QAAQ,CAAC,MAAM,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,iCAAiC,MAAM,EAAE,CAAC,CAAC;IAC3H,CAAC;IACD;;;;OAIG;IACI,MAAM,CAAC,YAAY,CAAC,MAAW;QAClC,IAAI,CAAC,CAAC,KAAK,IAAI,MAAM,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAC;QAC9E,IAAI,OAAO,MAAM,CAAC,GAAG,KAAK,QAAQ;YAAE,MAAM,IAAI,KAAK,CAAC,qCAAqC,CAAC,CAAC;QAC3F,IAAI,MAAM,CAAC,GAAG,CAAC,WAAW,EAAE,KAAK,KAAK;YAAE,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAC;IAChG,CAAC;;AAGL,eAAe,eAAe,CAAC"}
@@ -1,17 +1,24 @@
1
- import type JwtManager from './JwtManager.js';
1
+ import type Algorithm from './algorithm/Algorithm.js';
2
+ import type KeyEntry from './KeyEntry.js';
2
3
  export declare class Jwt implements Jwt.Jwt {
3
4
  readonly header: Jwt.Header;
4
5
  readonly payload: Jwt.Payload;
5
6
  readonly signature: string;
6
- protected readonly manager: JwtManager;
7
- constructor(header: Jwt.Header, payload: Jwt.Payload, signature: string, manager: JwtManager);
7
+ protected readonly signer: Algorithm;
8
+ constructor(header: Jwt.Header, payload: Jwt.Payload, signature: string, signer: Algorithm);
8
9
  /**
9
10
  * Checks if the JWT has expired based on the `exp` claim in the payload. If the `exp` claim is not present, it returns `false`, indicating that the token is not considered expired. If the `exp` claim is present, it compares the current time (in seconds since the Unix epoch) with the expiration time specified in the `exp` claim and returns `true` if the token has expired, or `false` otherwise.
10
11
  * @returns `true` if the JWT has expired; otherwise, `false`.
11
12
  */
12
- expired(): boolean;
13
+ get expired(): boolean;
13
14
  toJSON(): Jwt.Jwt;
14
15
  toString(): string;
16
+ /**
17
+ * Static method to check if a given expiration time (in seconds since the Unix epoch) has passed. It compares the current time with the provided expiration time and returns `true` if the current time is greater than or equal to the expiration time, indicating that the token has expired, or `false` otherwise.
18
+ * @param exp The expiration time to check, represented as a number of seconds since the Unix epoch.
19
+ * @returns `true` if the current time is greater than or equal to the expiration time; otherwise, `false`.
20
+ */
21
+ static isExpired(exp: number): boolean;
15
22
  }
16
23
  export declare namespace Jwt {
17
24
  interface Jwt {
@@ -20,12 +27,13 @@ export declare namespace Jwt {
20
27
  signature: string;
21
28
  }
22
29
  interface Header {
23
- alg: JwtManager.AlgorithmName;
30
+ alg: KeyEntry.AlgorithmName;
24
31
  typ: 'jwt';
25
- exp?: number;
32
+ kid?: string;
26
33
  [key: string]: any;
27
34
  }
28
35
  interface Payload {
36
+ exp?: number;
29
37
  [key: string]: any;
30
38
  }
31
39
  }
@@ -3,22 +3,21 @@ export class Jwt {
3
3
  header;
4
4
  payload;
5
5
  signature;
6
- manager;
7
- constructor(header, payload, signature, manager) {
6
+ signer;
7
+ constructor(header, payload, signature, signer) {
8
8
  this.header = header;
9
9
  this.payload = payload;
10
10
  this.signature = signature;
11
- this.manager = manager;
11
+ this.signer = signer;
12
12
  }
13
13
  /**
14
14
  * Checks if the JWT has expired based on the `exp` claim in the payload. If the `exp` claim is not present, it returns `false`, indicating that the token is not considered expired. If the `exp` claim is present, it compares the current time (in seconds since the Unix epoch) with the expiration time specified in the `exp` claim and returns `true` if the token has expired, or `false` otherwise.
15
15
  * @returns `true` if the JWT has expired; otherwise, `false`.
16
16
  */
17
- expired() {
18
- if (!this.header.exp)
17
+ get expired() {
18
+ if (this.payload.exp == null)
19
19
  return false;
20
- const now = Math.floor(Date.now() / 1000);
21
- return now >= this.header.exp;
20
+ return Jwt.isExpired(this.payload.exp);
22
21
  }
23
22
  toJSON() {
24
23
  return { header: this.header, payload: this.payload, signature: this.signature };
@@ -28,6 +27,15 @@ export class Jwt {
28
27
  const encodedPayload = JwtUtils.objectToBase64Url(this.payload);
29
28
  return `${encodedHeader}.${encodedPayload}.${this.signature}`;
30
29
  }
30
+ /**
31
+ * Static method to check if a given expiration time (in seconds since the Unix epoch) has passed. It compares the current time with the provided expiration time and returns `true` if the current time is greater than or equal to the expiration time, indicating that the token has expired, or `false` otherwise.
32
+ * @param exp The expiration time to check, represented as a number of seconds since the Unix epoch.
33
+ * @returns `true` if the current time is greater than or equal to the expiration time; otherwise, `false`.
34
+ */
35
+ static isExpired(exp) {
36
+ const now = JwtUtils.nowInSeconds();
37
+ return now >= exp;
38
+ }
31
39
  }
32
40
  export default Jwt;
33
41
  //# sourceMappingURL=Jwt.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"Jwt.js","sourceRoot":"","sources":["../../../src/beta/JwtManager/Jwt.ts"],"names":[],"mappings":"AAEA,OAAO,QAAQ,MAAM,eAAe,CAAC;AAErC,MAAM,OAAO,GAAG;IAEQ;IACA;IACA;IACG;IAJvB,YACoB,MAAkB,EAClB,OAAoB,EACpB,SAAiB,EACd,OAAmB;QAHtB,WAAM,GAAN,MAAM,CAAY;QAClB,YAAO,GAAP,OAAO,CAAa;QACpB,cAAS,GAAT,SAAS,CAAQ;QACd,YAAO,GAAP,OA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,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IAC3C,CAAC;IAEM,MAAM;QACT,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,SAAS,EAAE,IAAI,CAAC,SAAS,EAAE,CAAC;IACrF,CAAC;IACM,QAAQ;QACX,MAAM,aAAa,GAAG,QAAQ,CAAC,iBAAiB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAC9D,MAAM,cAAc,GAAG,QAAQ,CAAC,iBAAiB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAChE,OAAO,GAAG,aAAa,IAAI,cAAc,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;IAClE,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,SAAS,CAAC,GAAW;QAC/B,MAAM,GAAG,GAAG,QAAQ,CAAC,YAAY,EAAE,CAAC;QACpC,OAAO,GAAG,IAAI,GAAG,CAAC;IACtB,CAAC;CACJ;AAkBD,eAAe,GAAG,CAAC"}
@@ -1,49 +1,56 @@
1
- import RSA from "./algorithm/RSA.js";
2
- import HMAC from "./algorithm/HMAC.js";
3
- import ECDSA from "./algorithm/ECDSA.js";
4
- import RSAPSS from "./algorithm/RSAPSS.js";
5
1
  import _KeyGenerator from "./KeyGenerator.js";
6
- import _JwtUtils from "./JwtUtils.js";
7
2
  import _Algorithm from "./algorithm/Algorithm.js";
3
+ import _JwtUtils from "./JwtUtils.js";
4
+ import _KeyEntry from "./KeyEntry.js";
8
5
  import _Jwt from "./Jwt.js";
9
6
  export { KeyGenerator } from "./KeyGenerator.js";
10
7
  export { Algorithm } from "./algorithm/Algorithm.js";
11
8
  export { JwtUtils } from "./JwtUtils.js";
9
+ export { KeyEntry as KIDEntry } from "./KeyEntry.js";
12
10
  export { Jwt } from "./Jwt.js";
13
- export declare const AlgorithmMap: {
14
- HS: typeof HMAC;
15
- RS: typeof RSA;
16
- PS: typeof RSAPSS;
17
- ES: typeof ECDSA;
18
- };
19
11
  export declare class JwtManager {
20
- readonly algorithmName: JwtManager.AlgorithmName;
21
- protected readonly algorithm: JwtManager.Algorithm;
22
- constructor(algorithmName: JwtManager.AlgorithmName, key: JwtManager.Algorithm.Key | JwtManager.Algorithm.KeyOptions);
12
+ protected KIDMap: JwtManager.KIDMap;
13
+ protected defaultEntry: JwtManager.KeyEntry;
14
+ constructor(entry: JwtManager.KeyOption, ...entries: JwtManager.KeyOption[]);
15
+ /**
16
+ * Adds a new key entry to the manager's KID map.
17
+ * The provided entry can be either an instance of `KIDEntry` or an object containing the necessary properties to create a new `KIDEntry`.
18
+ * The method normalizes the entry using the `normalizeEntry` static method and then adds it to the KID map using the entry's `kid` property as the key.
19
+ * @param entry The key entry to be added, which can be either an instance of `KIDEntry` or an object containing the properties needed to create a new `KIDEntry`.
20
+ */
21
+ addKey(entry: JwtManager.KeyOption): void;
22
+ /**
23
+ * Removes the key associated with the specified key ID (kid) from the manager's KID map.
24
+ * If the specified kid does not exist in the map, this method does nothing.
25
+ * @param kid The key ID of the key to be removed from the manager's KID map.
26
+ */
27
+ delKey(kid: string): void;
23
28
  /**
24
29
  * Signs the given payload with the specified header using the configured algorithm and key.
25
30
  * @param payload The payload to be signed, represented as a JavaScript object.
26
- * @param header The header to be included in the JWT, represented as a JavaScript object. The `alg` property will be automatically set based on the manager's algorithm.
31
+ * @param header The header to be included in the JWT, represented as a JavaScript object.
32
+ * - The `alg` property will be automatically set based on the manager's algorithm.
27
33
  * @returns The complete JWT as a string, consisting of the base64url-encoded header, payload, and signature.
28
34
  */
29
35
  sign(payload: JwtManager.Jwt.Payload, header?: Partial<JwtManager.Jwt.Header>): string;
30
36
  parse(token: string): JwtManager.Jwt;
31
- toJSON(): object;
32
- toString(): string;
33
37
  /**
34
- * Determines the appropriate algorithm class based on the provided options and creates an instance of it.
35
- * @param algorithm The name of the algorithm to be used for signing and verifying JWTs (e.g., "HS256", "RS384", "ES512").
36
- * @param key The secret key or private key to be used for signing and verifying JWTs.
37
- * @returns An instance of the corresponding Algorithm subclass.
38
+ * Normalizes a given key entry option into a `KIDEntry` instance. If the provided entry is already an instance of `KIDEntry`, it is returned as-is.
39
+ * If the entry is provided as an object containing the necessary properties to create a new `KIDEntry`.
40
+ * @param entry The key entry option to be normalized, which can be either an instance of `KIDEntry` or an object containing the properties needed to create a new `KIDEntry`.
41
+ * @returns A `KIDEntry` instance corresponding to the provided entry option.
38
42
  */
39
- protected static getAlgorithm(algorithm: JwtManager.AlgorithmName, key: JwtManager.Algorithm.Key | JwtManager.Algorithm.KeyOptions): JwtManager.Algorithm;
43
+ static normalizeEntry(entry: JwtManager.KeyOption): JwtManager.KeyEntry;
40
44
  }
41
45
  export declare namespace JwtManager {
42
46
  export import Algorithm = _Algorithm;
43
47
  export import KeyGenerator = _KeyGenerator;
44
48
  export import JwtUtils = _JwtUtils;
49
+ export import KeyEntry = _KeyEntry;
45
50
  export import Jwt = _Jwt;
46
- type AlgPrefix = 'HS' | 'RS' | 'ES' | 'PS';
47
- type AlgorithmName = `${AlgPrefix}${JwtManager.Algorithm.HashLength}`;
51
+ type KeyOption = KeyEntry | KeyEntry.KeyEntryOptions;
52
+ interface KIDMap {
53
+ [kid: string]: KeyEntry;
54
+ }
48
55
  }
49
56
  export default JwtManager;
@@ -1,73 +1,94 @@
1
- import RSA from "./algorithm/RSA.js";
2
- import HMAC from "./algorithm/HMAC.js";
3
- import ECDSA from "./algorithm/ECDSA.js";
4
- import RSAPSS from "./algorithm/RSAPSS.js";
5
1
  import _KeyGenerator from "./KeyGenerator.js";
6
- import _JwtUtils from "./JwtUtils.js";
7
2
  import _Algorithm from "./algorithm/Algorithm.js";
3
+ import _JwtUtils from "./JwtUtils.js";
4
+ import _KeyEntry from "./KeyEntry.js";
8
5
  import _Jwt from "./Jwt.js";
9
6
  export { KeyGenerator } from "./KeyGenerator.js";
10
7
  export { Algorithm } from "./algorithm/Algorithm.js";
11
8
  export { JwtUtils } from "./JwtUtils.js";
9
+ export { KeyEntry as KIDEntry } from "./KeyEntry.js";
12
10
  export { Jwt } from "./Jwt.js";
13
- export const AlgorithmMap = {
14
- HS: HMAC,
15
- RS: RSA,
16
- PS: RSAPSS,
17
- ES: ECDSA
18
- };
19
11
  export class JwtManager {
20
- algorithmName;
21
- algorithm;
22
- constructor(algorithmName, key) {
23
- this.algorithmName = algorithmName;
24
- this.algorithm = JwtManager.getAlgorithm(algorithmName, key);
12
+ KIDMap = {};
13
+ defaultEntry;
14
+ constructor(entry, ...entries) {
15
+ this.defaultEntry = JwtManager.normalizeEntry(entry);
16
+ entries = [this.defaultEntry, ...entries];
17
+ for (const e of entries) {
18
+ this.addKey(e);
19
+ }
20
+ }
21
+ /**
22
+ * Adds a new key entry to the manager's KID map.
23
+ * The provided entry can be either an instance of `KIDEntry` or an object containing the necessary properties to create a new `KIDEntry`.
24
+ * The method normalizes the entry using the `normalizeEntry` static method and then adds it to the KID map using the entry's `kid` property as the key.
25
+ * @param entry The key entry to be added, which can be either an instance of `KIDEntry` or an object containing the properties needed to create a new `KIDEntry`.
26
+ */
27
+ addKey(entry) {
28
+ const kidEntry = JwtManager.normalizeEntry(entry);
29
+ this.KIDMap[kidEntry.kid] = kidEntry;
25
30
  }
31
+ /**
32
+ * Removes the key associated with the specified key ID (kid) from the manager's KID map.
33
+ * If the specified kid does not exist in the map, this method does nothing.
34
+ * @param kid The key ID of the key to be removed from the manager's KID map.
35
+ */
36
+ delKey(kid) { delete this.KIDMap[kid]; }
26
37
  /**
27
38
  * Signs the given payload with the specified header using the configured algorithm and key.
28
39
  * @param payload The payload to be signed, represented as a JavaScript object.
29
- * @param header The header to be included in the JWT, represented as a JavaScript object. The `alg` property will be automatically set based on the manager's algorithm.
40
+ * @param header The header to be included in the JWT, represented as a JavaScript object.
41
+ * - The `alg` property will be automatically set based on the manager's algorithm.
30
42
  * @returns The complete JWT as a string, consisting of the base64url-encoded header, payload, and signature.
31
43
  */
32
44
  sign(payload, header = {}) {
33
- header = { ...header, alg: this.algorithmName };
45
+ const kid = header.kid || this.defaultEntry.kid;
46
+ const entry = this.KIDMap[kid];
47
+ if (!entry)
48
+ throw new Error(`No key found for kid: ${kid}`);
49
+ const { signer, alg } = entry;
50
+ header = { ...header, alg, kid, typ: 'jwt' };
34
51
  const encodedHeader = JwtManager.JwtUtils.objectToBase64Url(header);
35
52
  const encodedPayload = JwtManager.JwtUtils.objectToBase64Url(payload);
36
53
  const content = `${encodedHeader}.${encodedPayload}`;
37
- const signature = this.algorithm.sign(content);
54
+ const signature = signer.sign(content);
38
55
  return `${content}.${signature}`;
39
56
  }
40
57
  parse(token) {
41
- const parts = token.split('.');
42
- if (parts.length !== 3)
43
- throw new Error('Invalid JWT format');
44
- const [encodedHeader, encodedPayload, signature] = parts;
45
- const verified = this.algorithm.verify(`${encodedHeader}.${encodedPayload}`, signature);
46
- if (!verified)
47
- throw new Error('Invalid JWT signature');
48
- const header = JwtManager.JwtUtils.base64UrlToObject(encodedHeader);
49
- const payload = JwtManager.JwtUtils.base64UrlToObject(encodedPayload);
50
- return new JwtManager.Jwt(header, payload, signature, this);
58
+ const { header, payload, signature, encodedHeader, encodedPayload } = JwtManager.JwtUtils.getJwtParts(token);
59
+ const kid = header.kid || this.defaultEntry.kid;
60
+ const entry = this.KIDMap[kid];
61
+ if (!entry)
62
+ throw new Error(`No key found for kid: ${kid}`);
63
+ const signer = entry.signer;
64
+ const content = `${encodedHeader}.${encodedPayload}`;
65
+ if (!signer.verify(content, signature))
66
+ throw new Error('Invalid signature');
67
+ const jwt = new JwtManager.Jwt(header, payload, signature, signer);
68
+ if (jwt.expired)
69
+ throw new Error('Token has expired');
70
+ return jwt;
51
71
  }
52
- toJSON() { return { algorithm: this.algorithmName }; }
53
- toString() { return `Jwt manager with algorithm ${this.algorithmName}`; }
72
+ // toJSON(): object { return { algorithm: this.algorithmName }; }
73
+ // toString(): string { return `Jwt manager with algorithm ${this.algorithmName}`; }
54
74
  /**
55
- * Determines the appropriate algorithm class based on the provided options and creates an instance of it.
56
- * @param algorithm The name of the algorithm to be used for signing and verifying JWTs (e.g., "HS256", "RS384", "ES512").
57
- * @param key The secret key or private key to be used for signing and verifying JWTs.
58
- * @returns An instance of the corresponding Algorithm subclass.
75
+ * Normalizes a given key entry option into a `KIDEntry` instance. If the provided entry is already an instance of `KIDEntry`, it is returned as-is.
76
+ * If the entry is provided as an object containing the necessary properties to create a new `KIDEntry`.
77
+ * @param entry The key entry option to be normalized, which can be either an instance of `KIDEntry` or an object containing the properties needed to create a new `KIDEntry`.
78
+ * @returns A `KIDEntry` instance corresponding to the provided entry option.
59
79
  */
60
- static getAlgorithm(algorithm, key) {
61
- const prefix = JwtManager.JwtUtils.getAlgPrefix(algorithm);
62
- const hashLength = JwtManager.JwtUtils.getHashLength(algorithm);
63
- const alg = AlgorithmMap[prefix];
64
- return new alg(hashLength, key);
80
+ static normalizeEntry(entry) {
81
+ if (entry instanceof JwtManager.KeyEntry)
82
+ return entry;
83
+ const { alg: algorithm, key, kid } = entry;
84
+ return new JwtManager.KeyEntry(algorithm, key, kid);
65
85
  }
66
86
  }
67
87
  (function (JwtManager) {
68
88
  JwtManager.Algorithm = _Algorithm;
69
89
  JwtManager.KeyGenerator = _KeyGenerator;
70
90
  JwtManager.JwtUtils = _JwtUtils;
91
+ JwtManager.KeyEntry = _KeyEntry;
71
92
  JwtManager.Jwt = _Jwt;
72
93
  })(JwtManager || (JwtManager = {}));
73
94
  export default JwtManager;
@@ -1 +1 @@
1
- {"version":3,"file":"JwtManager.js","sourceRoot":"","sources":["../../../src/beta/JwtManager/JwtManager.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,oBAAoB,CAAC;AACrC,OAAO,IAAI,MAAM,qBAAqB,CAAC;AACvC,OAAO,KAAK,MAAM,sBAAsB,CAAC;AACzC,OAAO,MAAM,MAAM,uBAAuB,CAAC;AAE3C,OAAO,aAAa,MAAM,mBAAmB,CAAC;AAC9C,OAAO,SAAS,MAAM,eAAe,CAAC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from "./KeyEntry.js";
3
+ import type Jwt from './Jwt.js';
3
4
  export declare class JwtUtils {
5
+ /**
6
+ * Returns the current time in seconds since the Unix epoch. This is commonly used for setting the `iat` (issued at) and `exp` (expiration) claims in JWTs.
7
+ * @returns The current time in seconds since January 1, 1970 (Unix epoch).
8
+ */
9
+ static nowInSeconds(): number;
4
10
  /**
5
11
  * Converts a JavaScript object to a base64url-encoded string.
6
12
  * @param obj The object to be converted.
@@ -19,14 +25,23 @@ export declare class JwtUtils {
19
25
  * @returns The algorithm prefix (e.g., "HS", "RS", "ES", "PS").
20
26
  * @throws An error if the algorithm is unsupported.
21
27
  */
22
- static getAlgPrefix(alg: JwtManager.AlgorithmName): JwtManager.AlgPrefix;
28
+ static getAlgPrefix(alg: KeyEntry.AlgorithmName): KeyEntry.AlgPrefix;
23
29
  /**
24
30
  * Extracts the hash length from the given algorithm name and validates it.
25
31
  * @param alg The algorithm name (e.g., "HS256", "RS384", "ES512").
26
32
  * @returns The hash length as a string ("256", "384", or "512").
27
33
  * @throws An error if the algorithm is unsupported or if the hash length is invalid.
28
34
  */
29
- static getHashLength(alg: JwtManager.AlgorithmName): Algorithm.HashLength;
35
+ static getHashLength(alg: KeyEntry.AlgorithmName): Algorithm.HashLength;
36
+ static getJwtParts(token: string): JwtUtils.JwtParts;
37
+ }
38
+ export declare namespace JwtUtils {
39
+ interface JwtParts {
40
+ encodedHeader: string;
41
+ encodedPayload: string;
42
+ signature: string;
43
+ header: Jwt.Header;
44
+ payload: Jwt.Payload;
45
+ }
30
46
  }
31
- export declare namespace JwtUtils { }
32
47
  export default JwtUtils;
@@ -1,4 +1,12 @@
1
+ import HeaderValidator from "./HeaderValidator.js";
1
2
  export class JwtUtils {
3
+ /**
4
+ * Returns the current time in seconds since the Unix epoch. This is commonly used for setting the `iat` (issued at) and `exp` (expiration) claims in JWTs.
5
+ * @returns The current time in seconds since January 1, 1970 (Unix epoch).
6
+ */
7
+ static nowInSeconds() {
8
+ return Math.floor(Date.now() / 1000);
9
+ }
2
10
  /**
3
11
  * Converts a JavaScript object to a base64url-encoded string.
4
12
  * @param obj The object to be converted.
@@ -45,6 +53,16 @@ export class JwtUtils {
45
53
  }
46
54
  return hashLength;
47
55
  }
56
+ static getJwtParts(token) {
57
+ const parts = token.split('.');
58
+ if (parts.length !== 3)
59
+ throw new Error('Invalid JWT format');
60
+ const [encodedHeader, encodedPayload, signature] = parts;
61
+ const header = this.base64UrlToObject(encodedHeader);
62
+ HeaderValidator.validate(header);
63
+ const payload = this.base64UrlToObject(encodedPayload);
64
+ return { encodedHeader, encodedPayload, signature, header, payload };
65
+ }
48
66
  }
49
67
  export default JwtUtils;
50
68
  //# sourceMappingURL=JwtUtils.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"JwtUtils.js","sourceRoot":"","sources":["../../../src/beta/JwtManager/JwtUtils.ts"],"names":[],"mappings":"AAGA,MAAM,OAAO,QAAQ;IACjB;;;;OAIG;IACI,MAAM,CAAC,iBAAiB,CAAC,GAAW;QACvC,MAAM,UAAU,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;QACvC,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;QAChD,OAAO,MAAM,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;IACxC,CAAC;IACD;;;;OAIG;IACI,MAAM,CAAC,iBAAiB,CAAI,SAAiB;QAChD,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC;QACnD,MAAM,UAAU,GAAG,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;QAC5C,OAAO,IAAI,CAAC,KAAK,CAAC,UAAU,CAAM,CAAC;IACvC,CAAC;IACD;;;;;OAKG;IACI,MAAM,CAAC,YAAY,CAAC,GAA6B;QACpD,MAAM,MAAM,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QAC/B,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C,MAAM,CAAC,CAAC;QAEjC,MAAM,OAAO,GAAG,IAAI,CAAC,iBAAiB,CAAc,cAAc,CAAC,CAAC;QAEpE,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC;IACzE,CAAC;CACJ;AAUD,eAAe,QAAQ,CAAC"}
@@ -0,0 +1,52 @@
1
+ import type JwtManager from "./JwtManager.js";
2
+ import Algorithm from "./algorithm/Algorithm.js";
3
+ import RSAPSS from "./algorithm/RSAPSS.js";
4
+ import ECDSA from "./algorithm/ECDSA.js";
5
+ import HMAC from "./algorithm/HMAC.js";
6
+ import RSA from "./algorithm/RSA.js";
7
+ export declare class KeyEntry {
8
+ readonly alg: KeyEntry.AlgorithmName;
9
+ protected static AlgorithmMap: {
10
+ HS: typeof HMAC;
11
+ RS: typeof RSA;
12
+ ES: typeof ECDSA;
13
+ PS: typeof RSAPSS;
14
+ };
15
+ readonly kid: string;
16
+ readonly signer: Algorithm;
17
+ /**
18
+ * Creates a new KIDEntry instance with the specified algorithm, key, and optional key ID (kid).
19
+ * If the kid is not provided, it generates a random one based on the algorithm name and a random string.
20
+ * The signer is created using the `createSigner` static method, which determines the appropriate signing algorithm based on the provided algorithm name and key.
21
+ * @param alg The name of the signing algorithm (e.g., "HS256", "RS384", "ES512").
22
+ * @param key The key or key options to be used for signing, which can be an instance of `Algorithm` or an object containing key options.
23
+ * @param kid An optional key ID to uniquely identify the key. If not provided, a random one will be generated.
24
+ */
25
+ constructor(alg: KeyEntry.AlgorithmName, key: KeyEntry.Signer, kid?: string);
26
+ /**
27
+ * Creates an instance of the appropriate signing algorithm based on the provided algorithm name and key.
28
+ * It extracts the algorithm prefix and hash length from the algorithm name,
29
+ * looks up the corresponding algorithm class in the `AlgorithmMap`,and returns a new instance of that class initialized
30
+ * with the specified hash length and key.
31
+ * @param alg The algorithm name (e.g., "HS256", "RS384", "ES512").
32
+ * @param key The key or key options to be used for signing, which can be an instance of `Algorithm` or an object containing key options.
33
+ * @returns An instance of the appropriate signing algorithm initialized with the specified hash length and key.
34
+ */
35
+ static createSigner(alg: KeyEntry.AlgorithmName, key: Algorithm.Key | Algorithm.KeyOptions): Algorithm;
36
+ }
37
+ export declare namespace KeyEntry {
38
+ type AlgPrefix = 'HS' | 'RS' | 'ES' | 'PS';
39
+ type AlgorithmName = `${AlgPrefix}${JwtManager.Algorithm.HashLength}`;
40
+ type Signer = Algorithm | Algorithm.Key | Algorithm.KeyOptions;
41
+ interface KeyEntryOptions {
42
+ key: Signer;
43
+ alg: AlgorithmName;
44
+ kid?: string;
45
+ }
46
+ interface KeyEntry extends KeyEntryOptions {
47
+ kid: string;
48
+ signer: Algorithm;
49
+ alg: AlgorithmName;
50
+ }
51
+ }
52
+ export default KeyEntry;
@@ -0,0 +1,42 @@
1
+ import Algorithm from "./algorithm/Algorithm.js";
2
+ import RSAPSS from "./algorithm/RSAPSS.js";
3
+ import ECDSA from "./algorithm/ECDSA.js";
4
+ import HMAC from "./algorithm/HMAC.js";
5
+ import RSA from "./algorithm/RSA.js";
6
+ import JwtUtils from "./JwtUtils.js";
7
+ export class KeyEntry {
8
+ alg;
9
+ static AlgorithmMap = { HS: HMAC, RS: RSA, ES: ECDSA, PS: RSAPSS };
10
+ kid;
11
+ signer;
12
+ /**
13
+ * Creates a new KIDEntry instance with the specified algorithm, key, and optional key ID (kid).
14
+ * If the kid is not provided, it generates a random one based on the algorithm name and a random string.
15
+ * The signer is created using the `createSigner` static method, which determines the appropriate signing algorithm based on the provided algorithm name and key.
16
+ * @param alg The name of the signing algorithm (e.g., "HS256", "RS384", "ES512").
17
+ * @param key The key or key options to be used for signing, which can be an instance of `Algorithm` or an object containing key options.
18
+ * @param kid An optional key ID to uniquely identify the key. If not provided, a random one will be generated.
19
+ */
20
+ constructor(alg, key, kid) {
21
+ this.alg = alg;
22
+ this.kid = kid || `${alg}-${Math.random().toString(36).substring(2, 8)}`;
23
+ this.signer = key instanceof Algorithm ? key : KeyEntry.createSigner(alg, key);
24
+ }
25
+ /**
26
+ * Creates an instance of the appropriate signing algorithm based on the provided algorithm name and key.
27
+ * It extracts the algorithm prefix and hash length from the algorithm name,
28
+ * looks up the corresponding algorithm class in the `AlgorithmMap`,and returns a new instance of that class initialized
29
+ * with the specified hash length and key.
30
+ * @param alg The algorithm name (e.g., "HS256", "RS384", "ES512").
31
+ * @param key The key or key options to be used for signing, which can be an instance of `Algorithm` or an object containing key options.
32
+ * @returns An instance of the appropriate signing algorithm initialized with the specified hash length and key.
33
+ */
34
+ static createSigner(alg, key) {
35
+ const prefix = JwtUtils.getAlgPrefix(alg);
36
+ const hashLength = JwtUtils.getHashLength(alg);
37
+ const algorithm = this.AlgorithmMap[prefix];
38
+ return new algorithm(hashLength, key);
39
+ }
40
+ }
41
+ export default KeyEntry;
42
+ //# sourceMappingURL=KeyEntry.js.map