@alanszp/jwt 12.0.0 → 13.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (155) hide show
  1. package/babel.config.js +7 -0
  2. package/dist/BitmaskUtils.d.ts +48 -0
  3. package/dist/BitmaskUtils.js +64 -0
  4. package/dist/BitmaskUtils.js.map +1 -0
  5. package/dist/BitmaskUtils.test.d.ts +1 -0
  6. package/dist/BitmaskUtils.test.js +152 -0
  7. package/dist/BitmaskUtils.test.js.map +1 -0
  8. package/dist/JWTUser.d.ts +58 -2
  9. package/dist/JWTUser.js +158 -3
  10. package/dist/JWTUser.js.map +1 -1
  11. package/dist/PermissionService.d.ts +26 -0
  12. package/dist/PermissionService.js +108 -0
  13. package/dist/PermissionService.js.map +1 -0
  14. package/dist/axiosPermissionsResolutionFactory.d.ts +4 -0
  15. package/dist/axiosPermissionsResolutionFactory.js +52 -0
  16. package/dist/axiosPermissionsResolutionFactory.js.map +1 -0
  17. package/dist/errors/NoPermissionError.d.ts +7 -0
  18. package/dist/errors/NoPermissionError.js +20 -0
  19. package/dist/errors/NoPermissionError.js.map +1 -0
  20. package/dist/errors/PermissionNotFound.d.ts +9 -0
  21. package/dist/errors/PermissionNotFound.js +24 -0
  22. package/dist/errors/PermissionNotFound.js.map +1 -0
  23. package/dist/errors/PermissionServiceError.d.ts +3 -0
  24. package/dist/errors/PermissionServiceError.js +8 -0
  25. package/dist/errors/PermissionServiceError.js.map +1 -0
  26. package/dist/errors/PermissionServiceNotInstantiated.d.ts +7 -0
  27. package/dist/errors/PermissionServiceNotInstantiated.js +17 -0
  28. package/dist/errors/PermissionServiceNotInstantiated.js.map +1 -0
  29. package/dist/errors/PermissionServiceRequestError.d.ts +9 -0
  30. package/dist/errors/PermissionServiceRequestError.js +23 -0
  31. package/dist/errors/PermissionServiceRequestError.js.map +1 -0
  32. package/dist/index.d.ts +7 -0
  33. package/dist/index.js +7 -0
  34. package/dist/index.js.map +1 -1
  35. package/dist/types.d.ts +7 -0
  36. package/jest.config.js +10 -0
  37. package/package.json +13 -5
  38. package/tsconfig.json +5 -2
  39. package/.gitignore +0 -3
  40. package/.npmignore +0 -3
  41. package/node_modules/@types/node/LICENSE +0 -21
  42. package/node_modules/@types/node/README.md +0 -15
  43. package/node_modules/@types/node/assert/strict.d.ts +0 -8
  44. package/node_modules/@types/node/assert.d.ts +0 -996
  45. package/node_modules/@types/node/async_hooks.d.ts +0 -539
  46. package/node_modules/@types/node/buffer.d.ts +0 -2362
  47. package/node_modules/@types/node/child_process.d.ts +0 -1540
  48. package/node_modules/@types/node/cluster.d.ts +0 -432
  49. package/node_modules/@types/node/console.d.ts +0 -415
  50. package/node_modules/@types/node/constants.d.ts +0 -19
  51. package/node_modules/@types/node/crypto.d.ts +0 -4487
  52. package/node_modules/@types/node/dgram.d.ts +0 -596
  53. package/node_modules/@types/node/diagnostics_channel.d.ts +0 -545
  54. package/node_modules/@types/node/dns/promises.d.ts +0 -425
  55. package/node_modules/@types/node/dns.d.ts +0 -809
  56. package/node_modules/@types/node/dom-events.d.ts +0 -122
  57. package/node_modules/@types/node/domain.d.ts +0 -170
  58. package/node_modules/@types/node/events.d.ts +0 -879
  59. package/node_modules/@types/node/fs/promises.d.ts +0 -1239
  60. package/node_modules/@types/node/fs.d.ts +0 -4311
  61. package/node_modules/@types/node/globals.d.ts +0 -411
  62. package/node_modules/@types/node/globals.global.d.ts +0 -1
  63. package/node_modules/@types/node/http.d.ts +0 -1887
  64. package/node_modules/@types/node/http2.d.ts +0 -2382
  65. package/node_modules/@types/node/https.d.ts +0 -550
  66. package/node_modules/@types/node/index.d.ts +0 -88
  67. package/node_modules/@types/node/inspector.d.ts +0 -2747
  68. package/node_modules/@types/node/module.d.ts +0 -315
  69. package/node_modules/@types/node/net.d.ts +0 -949
  70. package/node_modules/@types/node/os.d.ts +0 -478
  71. package/node_modules/@types/node/package.json +0 -229
  72. package/node_modules/@types/node/path.d.ts +0 -191
  73. package/node_modules/@types/node/perf_hooks.d.ts +0 -645
  74. package/node_modules/@types/node/process.d.ts +0 -1561
  75. package/node_modules/@types/node/punycode.d.ts +0 -117
  76. package/node_modules/@types/node/querystring.d.ts +0 -141
  77. package/node_modules/@types/node/readline/promises.d.ts +0 -150
  78. package/node_modules/@types/node/readline.d.ts +0 -539
  79. package/node_modules/@types/node/repl.d.ts +0 -430
  80. package/node_modules/@types/node/stream/consumers.d.ts +0 -12
  81. package/node_modules/@types/node/stream/promises.d.ts +0 -83
  82. package/node_modules/@types/node/stream/web.d.ts +0 -366
  83. package/node_modules/@types/node/stream.d.ts +0 -1701
  84. package/node_modules/@types/node/string_decoder.d.ts +0 -67
  85. package/node_modules/@types/node/test.d.ts +0 -1465
  86. package/node_modules/@types/node/timers/promises.d.ts +0 -93
  87. package/node_modules/@types/node/timers.d.ts +0 -240
  88. package/node_modules/@types/node/tls.d.ts +0 -1210
  89. package/node_modules/@types/node/trace_events.d.ts +0 -182
  90. package/node_modules/@types/node/ts4.8/assert/strict.d.ts +0 -8
  91. package/node_modules/@types/node/ts4.8/assert.d.ts +0 -996
  92. package/node_modules/@types/node/ts4.8/async_hooks.d.ts +0 -539
  93. package/node_modules/@types/node/ts4.8/buffer.d.ts +0 -2362
  94. package/node_modules/@types/node/ts4.8/child_process.d.ts +0 -1540
  95. package/node_modules/@types/node/ts4.8/cluster.d.ts +0 -432
  96. package/node_modules/@types/node/ts4.8/console.d.ts +0 -415
  97. package/node_modules/@types/node/ts4.8/constants.d.ts +0 -19
  98. package/node_modules/@types/node/ts4.8/crypto.d.ts +0 -4487
  99. package/node_modules/@types/node/ts4.8/dgram.d.ts +0 -596
  100. package/node_modules/@types/node/ts4.8/diagnostics_channel.d.ts +0 -545
  101. package/node_modules/@types/node/ts4.8/dns/promises.d.ts +0 -425
  102. package/node_modules/@types/node/ts4.8/dns.d.ts +0 -809
  103. package/node_modules/@types/node/ts4.8/dom-events.d.ts +0 -122
  104. package/node_modules/@types/node/ts4.8/domain.d.ts +0 -170
  105. package/node_modules/@types/node/ts4.8/events.d.ts +0 -879
  106. package/node_modules/@types/node/ts4.8/fs/promises.d.ts +0 -1239
  107. package/node_modules/@types/node/ts4.8/fs.d.ts +0 -4311
  108. package/node_modules/@types/node/ts4.8/globals.d.ts +0 -411
  109. package/node_modules/@types/node/ts4.8/globals.global.d.ts +0 -1
  110. package/node_modules/@types/node/ts4.8/http.d.ts +0 -1887
  111. package/node_modules/@types/node/ts4.8/http2.d.ts +0 -2382
  112. package/node_modules/@types/node/ts4.8/https.d.ts +0 -550
  113. package/node_modules/@types/node/ts4.8/index.d.ts +0 -88
  114. package/node_modules/@types/node/ts4.8/inspector.d.ts +0 -2747
  115. package/node_modules/@types/node/ts4.8/module.d.ts +0 -315
  116. package/node_modules/@types/node/ts4.8/net.d.ts +0 -949
  117. package/node_modules/@types/node/ts4.8/os.d.ts +0 -478
  118. package/node_modules/@types/node/ts4.8/path.d.ts +0 -191
  119. package/node_modules/@types/node/ts4.8/perf_hooks.d.ts +0 -645
  120. package/node_modules/@types/node/ts4.8/process.d.ts +0 -1561
  121. package/node_modules/@types/node/ts4.8/punycode.d.ts +0 -117
  122. package/node_modules/@types/node/ts4.8/querystring.d.ts +0 -141
  123. package/node_modules/@types/node/ts4.8/readline/promises.d.ts +0 -150
  124. package/node_modules/@types/node/ts4.8/readline.d.ts +0 -539
  125. package/node_modules/@types/node/ts4.8/repl.d.ts +0 -430
  126. package/node_modules/@types/node/ts4.8/stream/consumers.d.ts +0 -12
  127. package/node_modules/@types/node/ts4.8/stream/promises.d.ts +0 -83
  128. package/node_modules/@types/node/ts4.8/stream/web.d.ts +0 -366
  129. package/node_modules/@types/node/ts4.8/stream.d.ts +0 -1701
  130. package/node_modules/@types/node/ts4.8/string_decoder.d.ts +0 -67
  131. package/node_modules/@types/node/ts4.8/test.d.ts +0 -1465
  132. package/node_modules/@types/node/ts4.8/timers/promises.d.ts +0 -93
  133. package/node_modules/@types/node/ts4.8/timers.d.ts +0 -240
  134. package/node_modules/@types/node/ts4.8/tls.d.ts +0 -1210
  135. package/node_modules/@types/node/ts4.8/trace_events.d.ts +0 -182
  136. package/node_modules/@types/node/ts4.8/tty.d.ts +0 -208
  137. package/node_modules/@types/node/ts4.8/url.d.ts +0 -927
  138. package/node_modules/@types/node/ts4.8/util.d.ts +0 -2183
  139. package/node_modules/@types/node/ts4.8/v8.d.ts +0 -764
  140. package/node_modules/@types/node/ts4.8/vm.d.ts +0 -903
  141. package/node_modules/@types/node/ts4.8/wasi.d.ts +0 -179
  142. package/node_modules/@types/node/ts4.8/worker_threads.d.ts +0 -691
  143. package/node_modules/@types/node/ts4.8/zlib.d.ts +0 -517
  144. package/node_modules/@types/node/tty.d.ts +0 -208
  145. package/node_modules/@types/node/url.d.ts +0 -927
  146. package/node_modules/@types/node/util.d.ts +0 -2183
  147. package/node_modules/@types/node/v8.d.ts +0 -764
  148. package/node_modules/@types/node/vm.d.ts +0 -903
  149. package/node_modules/@types/node/wasi.d.ts +0 -179
  150. package/node_modules/@types/node/worker_threads.d.ts +0 -691
  151. package/node_modules/@types/node/zlib.d.ts +0 -517
  152. package/src/JWTUser.ts +0 -85
  153. package/src/index.ts +0 -3
  154. package/src/jwt.ts +0 -81
  155. package/src/types.ts +0 -31
@@ -0,0 +1,7 @@
1
+ // Used by Jest
2
+ module.exports = {
3
+ presets: [
4
+ ["@babel/preset-env", { targets: { node: "current" } }],
5
+ "@babel/preset-typescript",
6
+ ],
7
+ };
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Bitmask utility class
3
+ * - Use to handle binary numbers
4
+ * - Support binary numbers larger than 32 bits (Node.js uses 32-bit numbers)
5
+ */
6
+ export declare class BitmaskUtils {
7
+ /**
8
+ * Encode a position into a binary number
9
+ * @example Bitmask.encodeFromPosition(0) // 1 or 0b1
10
+ * @example Bitmask.encodeFromPosition(1) // 2 or 0b10
11
+ * @example Bitmask.encodeFromPosition(2) // 4 or 0b100
12
+ * @example Bitmask.encodeFromPosition(3) // 8 or 0b1000
13
+ */
14
+ static encodeFromPosition(position: number): bigint;
15
+ /**
16
+ * Check if a bitmask has a specific bit set
17
+ * @param bitmask - the bitmask to check
18
+ * @param check - the bit to check (also a bitmask with only one bit set)
19
+ * @example Bitmask.checkBitmask(0b1010, 0b1000) // true
20
+ * @example Bitmask.checkBitmask(0b1010, 0b0100) // false
21
+ */
22
+ static checkBitmask(bitmask: bigint, check: bigint): boolean;
23
+ /**
24
+ * Combine multiple bitmasks into one
25
+ * Uses the bitwise OR operator to combine the bitmasks
26
+ * @param bitmasks - An array of bitmasks
27
+ * @returns The combined bitmask
28
+ * @example Bitmask.combineBitmasks([0b1000, 0b0100]) // 0b1100
29
+ * @example Bitmask.combineBitmasks([0b1000, 0b0100, 0b0010]) // 0b1110
30
+ */
31
+ static combineBitmasks(bitmasks: bigint[]): bigint;
32
+ /**
33
+ * Decode a base64 encoded string into a binary number
34
+ * @param base64 Base64 encoded bitmask (valid utf-8 string of a number)
35
+ * @returns The decoded bitmask
36
+ * @example Bitmask.decodeFromBase64("AQ==") // 1 or 0b1
37
+ * @example Bitmask.decodeFromBase64("Ag==") // 2 or 0b10
38
+ */
39
+ static decodeFromBase64(base64: string): bigint;
40
+ /**
41
+ * Encode a binary number into a base64 string
42
+ * @param bitmask The bitmask to encode
43
+ * @returns The base64 encoded bitmask
44
+ * @example Bitmask.encodeToBase64(1) // "AQ=="
45
+ * @example Bitmask.encodeToBase64(2) // "Ag=="
46
+ */
47
+ static encodeToBase64(bitmask: bigint): string;
48
+ }
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BitmaskUtils = void 0;
4
+ /**
5
+ * Bitmask utility class
6
+ * - Use to handle binary numbers
7
+ * - Support binary numbers larger than 32 bits (Node.js uses 32-bit numbers)
8
+ */
9
+ class BitmaskUtils {
10
+ /**
11
+ * Encode a position into a binary number
12
+ * @example Bitmask.encodeFromPosition(0) // 1 or 0b1
13
+ * @example Bitmask.encodeFromPosition(1) // 2 or 0b10
14
+ * @example Bitmask.encodeFromPosition(2) // 4 or 0b100
15
+ * @example Bitmask.encodeFromPosition(3) // 8 or 0b1000
16
+ */
17
+ static encodeFromPosition(position) {
18
+ return BigInt(1) << BigInt(position);
19
+ }
20
+ /**
21
+ * Check if a bitmask has a specific bit set
22
+ * @param bitmask - the bitmask to check
23
+ * @param check - the bit to check (also a bitmask with only one bit set)
24
+ * @example Bitmask.checkBitmask(0b1010, 0b1000) // true
25
+ * @example Bitmask.checkBitmask(0b1010, 0b0100) // false
26
+ */
27
+ static checkBitmask(bitmask, check) {
28
+ return (bitmask & check) === check;
29
+ }
30
+ /**
31
+ * Combine multiple bitmasks into one
32
+ * Uses the bitwise OR operator to combine the bitmasks
33
+ * @param bitmasks - An array of bitmasks
34
+ * @returns The combined bitmask
35
+ * @example Bitmask.combineBitmasks([0b1000, 0b0100]) // 0b1100
36
+ * @example Bitmask.combineBitmasks([0b1000, 0b0100, 0b0010]) // 0b1110
37
+ */
38
+ static combineBitmasks(bitmasks) {
39
+ return bitmasks.reduce((acc, bitmask) => acc | bitmask, BigInt(0));
40
+ }
41
+ /**
42
+ * Decode a base64 encoded string into a binary number
43
+ * @param base64 Base64 encoded bitmask (valid utf-8 string of a number)
44
+ * @returns The decoded bitmask
45
+ * @example Bitmask.decodeFromBase64("AQ==") // 1 or 0b1
46
+ * @example Bitmask.decodeFromBase64("Ag==") // 2 or 0b10
47
+ */
48
+ static decodeFromBase64(base64) {
49
+ const string = Buffer.from(base64, "base64").toString("utf-8");
50
+ return BigInt(string);
51
+ }
52
+ /**
53
+ * Encode a binary number into a base64 string
54
+ * @param bitmask The bitmask to encode
55
+ * @returns The base64 encoded bitmask
56
+ * @example Bitmask.encodeToBase64(1) // "AQ=="
57
+ * @example Bitmask.encodeToBase64(2) // "Ag=="
58
+ */
59
+ static encodeToBase64(bitmask) {
60
+ return Buffer.from(bitmask.toString()).toString("base64");
61
+ }
62
+ }
63
+ exports.BitmaskUtils = BitmaskUtils;
64
+ //# sourceMappingURL=BitmaskUtils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BitmaskUtils.js","sourceRoot":"","sources":["../src/BitmaskUtils.ts"],"names":[],"mappings":";;;AAAA;;;;GAIG;AACH,MAAa,YAAY;IACvB;;;;;;OAMG;IACI,MAAM,CAAC,kBAAkB,CAAC,QAAgB;QAC/C,OAAO,MAAM,CAAC,CAAC,CAAC,IAAI,MAAM,CAAC,QAAQ,CAAC,CAAC;IACvC,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CAAC,YAAY,CAAC,OAAe,EAAE,KAAa;QACvD,OAAO,CAAC,OAAO,GAAG,KAAK,CAAC,KAAK,KAAK,CAAC;IACrC,CAAC;IAED;;;;;;;OAOG;IACI,MAAM,CAAC,eAAe,CAAC,QAAkB;QAC9C,OAAO,QAAQ,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,OAAO,EAAE,EAAE,CAAC,GAAG,GAAG,OAAO,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;IACrE,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CAAC,gBAAgB,CAAC,MAAc;QAC3C,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;QAC/D,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC;IACxB,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CAAC,cAAc,CAAC,OAAe;QAC1C,OAAO,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;IAC5D,CAAC;CACF;AAzDD,oCAyDC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,152 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const BitmaskUtils_1 = require("./BitmaskUtils");
4
+ describe("BitmaskUtils", () => {
5
+ describe("encodeFromPosition", () => {
6
+ it.each([
7
+ { position: 0, expected: BigInt(0b1) },
8
+ { position: 1, expected: BigInt(0b10) },
9
+ { position: 5, expected: BigInt(0b100000) },
10
+ { position: 10, expected: BigInt(0b10000000000) },
11
+ { position: 15, expected: BigInt(0b1000000000000000) },
12
+ { position: 30, expected: BigInt(0b1000000000000000000000000000000) },
13
+ {
14
+ position: 31,
15
+ expected: BigInt(0b10000000000000000000000000000000),
16
+ },
17
+ // 32 is the maximum position for a 32-bit number - Node.js uses 32-bit numbers (when using unsigned ints), we should support more than 32 bits
18
+ {
19
+ position: 32,
20
+ expected: BigInt("0b100000000000000000000000000000000"),
21
+ },
22
+ {
23
+ position: 50,
24
+ expected: BigInt("0b100000000000000000000000000000000000000000000000000"),
25
+ },
26
+ {
27
+ position: 100,
28
+ expected: BigInt("0b10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
29
+ },
30
+ ])("Should encode a numeric position $position to a binary number", ({ position, expected }) => {
31
+ const result = BitmaskUtils_1.BitmaskUtils.encodeFromPosition(position);
32
+ expect(result).toStrictEqual(expected);
33
+ });
34
+ });
35
+ describe("checkBitmask", () => {
36
+ it.each([
37
+ {
38
+ bitmask: BigInt(0b1010),
39
+ position: 3,
40
+ expected: true,
41
+ },
42
+ {
43
+ bitmask: BigInt(0b1010),
44
+ position: 2,
45
+ expected: false,
46
+ },
47
+ {
48
+ bitmask: BigInt(0b1010),
49
+ position: 1,
50
+ expected: true,
51
+ },
52
+ {
53
+ bitmask: BigInt(0b1010),
54
+ position: 0,
55
+ expected: false,
56
+ },
57
+ ])("Should check if a bitmask has a specific bit set in position $position", ({ bitmask, position, expected }) => {
58
+ const check = BitmaskUtils_1.BitmaskUtils.encodeFromPosition(position);
59
+ const result = BitmaskUtils_1.BitmaskUtils.checkBitmask(bitmask, check);
60
+ expect(result).toStrictEqual(expected);
61
+ });
62
+ it("Should check if a bitmask has a specific bit set with big numbers", () => {
63
+ const bitmask = BitmaskUtils_1.BitmaskUtils.encodeFromPosition(100);
64
+ const result = BitmaskUtils_1.BitmaskUtils.checkBitmask(bitmask, bitmask);
65
+ expect(result).toStrictEqual(true);
66
+ });
67
+ it("Should check if a bitmask has a specific bit set with enormous numbers", () => {
68
+ const bitmask = BitmaskUtils_1.BitmaskUtils.encodeFromPosition(500);
69
+ const result = BitmaskUtils_1.BitmaskUtils.checkBitmask(bitmask, bitmask);
70
+ expect(result).toStrictEqual(true);
71
+ });
72
+ });
73
+ describe("combineBitmasks", () => {
74
+ it.each([
75
+ {
76
+ bitmasks: [BigInt(0b1000), BigInt(0b0100)],
77
+ expected: BigInt(0b1100),
78
+ },
79
+ {
80
+ bitmasks: [BigInt(0b1000), BigInt(0b0100), BigInt(0b0010)],
81
+ expected: BigInt(0b1110),
82
+ },
83
+ {
84
+ bitmasks: [
85
+ BigInt(0b1000),
86
+ BigInt(0b0100),
87
+ BigInt(0b0010),
88
+ BigInt(0b0001),
89
+ ],
90
+ expected: BigInt(0b1111),
91
+ },
92
+ {
93
+ bitmasks: [
94
+ BitmaskUtils_1.BitmaskUtils.encodeFromPosition(100),
95
+ BitmaskUtils_1.BitmaskUtils.encodeFromPosition(200),
96
+ BitmaskUtils_1.BitmaskUtils.encodeFromPosition(300),
97
+ BitmaskUtils_1.BitmaskUtils.encodeFromPosition(400),
98
+ ],
99
+ expected: BitmaskUtils_1.BitmaskUtils.encodeFromPosition(100) |
100
+ BitmaskUtils_1.BitmaskUtils.encodeFromPosition(200) |
101
+ BitmaskUtils_1.BitmaskUtils.encodeFromPosition(300) |
102
+ BitmaskUtils_1.BitmaskUtils.encodeFromPosition(400),
103
+ },
104
+ ])("Should combine multiple bitmasks into one", ({ bitmasks, expected }) => {
105
+ const result = BitmaskUtils_1.BitmaskUtils.combineBitmasks(bitmasks);
106
+ expect(result).toStrictEqual(expected);
107
+ });
108
+ });
109
+ describe("decodeFromBase64", () => {
110
+ it.each([
111
+ { base64: "MA==", expected: BigInt(0b0) },
112
+ { base64: "MQ==", expected: BigInt(0b1) },
113
+ { base64: "Mg==", expected: BigInt(0b10) },
114
+ { base64: "NjY3", expected: BigInt(0b1010011011) },
115
+ {
116
+ base64: "MTI2NzY1MDYwMDIyODIyOTQwMTQ5NjcwMzIwNTM3Ng==",
117
+ expected: BitmaskUtils_1.BitmaskUtils.encodeFromPosition(100),
118
+ },
119
+ ])("Should decode a base64 encoded bitmask for $base64", ({ base64, expected }) => {
120
+ const result = BitmaskUtils_1.BitmaskUtils.decodeFromBase64(base64);
121
+ expect(result).toStrictEqual(expected);
122
+ });
123
+ });
124
+ describe("encodeToBase64", () => {
125
+ it.each([
126
+ { bitmask: BigInt(0b0), expected: "MA==" },
127
+ { bitmask: BigInt(0b1), expected: "MQ==" },
128
+ { bitmask: BigInt(0b10), expected: "Mg==" },
129
+ { bitmask: BigInt(0b1010011011), expected: "NjY3" },
130
+ {
131
+ bitmask: BitmaskUtils_1.BitmaskUtils.encodeFromPosition(100),
132
+ expected: "MTI2NzY1MDYwMDIyODIyOTQwMTQ5NjcwMzIwNTM3Ng==",
133
+ },
134
+ ])("Should encode a bitmask to a base64 string for $bitmask", ({ bitmask, expected }) => {
135
+ const result = BitmaskUtils_1.BitmaskUtils.encodeToBase64(bitmask);
136
+ expect(result).toStrictEqual(expected);
137
+ });
138
+ it("Should encode and decode a bitmask from and to a base64 string", () => {
139
+ const bitmask = BigInt(0b1010011011);
140
+ const base64 = BitmaskUtils_1.BitmaskUtils.encodeToBase64(bitmask);
141
+ const result = BitmaskUtils_1.BitmaskUtils.decodeFromBase64(base64);
142
+ expect(result).toStrictEqual(bitmask);
143
+ });
144
+ it("Should encode and decode a bitmask from and to a base64 string with enormous numbers", () => {
145
+ const bitmask = BitmaskUtils_1.BitmaskUtils.encodeFromPosition(500);
146
+ const base64 = BitmaskUtils_1.BitmaskUtils.encodeToBase64(bitmask);
147
+ const result = BitmaskUtils_1.BitmaskUtils.decodeFromBase64(base64);
148
+ expect(result).toStrictEqual(bitmask);
149
+ });
150
+ });
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AAC,MAAM,CAAC,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;QACxC,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
package/dist/JWTUser.d.ts CHANGED
@@ -1,5 +1,7 @@
1
+ import { IPermissionService } from "./PermissionService";
1
2
  import { IJWTUser, JWTPayload } from "./types";
2
3
  export declare class JWTUser implements IJWTUser {
4
+ #private;
3
5
  id: string;
4
6
  employeeReference: string | null;
5
7
  organizationReference: string;
@@ -7,6 +9,14 @@ export declare class JWTUser implements IJWTUser {
7
9
  roles: string[];
8
10
  permissions: string;
9
11
  segmentReference: string | null;
12
+ /**
13
+ * Instantiate the permission service for all instances of JWTUser
14
+ */
15
+ static setPermissionService(service: IPermissionService): void;
16
+ /**
17
+ * @throws {PermissionServiceNotInstantiated}
18
+ */
19
+ static getPermissionService(): IPermissionService;
10
20
  constructor({ id, employeeReference, organizationReference, roles, permissions, segmentReference, }: IJWTUser);
11
21
  static fromPayload(payload: JWTPayload): JWTUser;
12
22
  toTokenPayload(): JWTPayload;
@@ -23,9 +33,55 @@ export declare class JWTUser implements IJWTUser {
23
33
  hasRoles(validateRoles: string | string[]): boolean;
24
34
  /**
25
35
  * Check if user has permission to perform an action
26
- * @note - not implemented, will be implemented in the next release
27
36
  * @param permissionCode - permission code to check
28
37
  * @returns boolean
38
+ * @throws {PermissionDefinitionNotFound}
39
+ * @throws {PermissionServiceNotInstantiated}
40
+ */
41
+ hasPermission(permissionCode: string): Promise<boolean>;
42
+ /**
43
+ * Throws an error if the user does not have the required permission
44
+ * @param permissionCode
45
+ * @throws {NoPermissionError}
46
+ * @throws {PermissionDefinitionNotFound}
47
+ * @throws {PermissionServiceNotInstantiated}
48
+ */
49
+ validatePermission(permissionCode: string): Promise<void>;
50
+ /**
51
+ * Check if user has all permissions to perform an action
52
+ * @param permissionCodes - permission codes to check
53
+ * @returns boolean
54
+ * @throws {PermissionDefinitionNotFound}
55
+ * @throws {PermissionServiceNotInstantiated}
56
+ */
57
+ hasEveryPermission(permissionCodes: string[]): Promise<boolean>;
58
+ /**
59
+ * Throws an error if the user does not have all permissions
60
+ * @param permissionCode
61
+ * @throws {NoPermissionError}
62
+ * @throws {PermissionDefinitionNotFound}
63
+ * @throws {PermissionServiceNotInstantiated}
64
+ */
65
+ validateEveryPermission(permissionCodes: string[]): Promise<void>;
66
+ /**
67
+ * Check if user has at least one permission
68
+ * @param permissionCodes - permission codes to check
69
+ * @returns boolean
70
+ * @throws {PermissionDefinitionNotFound}
71
+ * @throws {PermissionServiceNotInstantiated}
72
+ */
73
+ hasSomePermission(permissionCodes: string[]): Promise<boolean>;
74
+ /**
75
+ * Throws an error if the user does not have at least one of the permissions
76
+ * @param permissionCode
77
+ * @throws {NoPermissionError}
78
+ * @throws {PermissionDefinitionNotFound}
79
+ * @throws {PermissionServiceNotInstantiated}
80
+ */
81
+ validateSomePermission(permissionCodes: string[]): Promise<void>;
82
+ /**
83
+ * @throws {PermissionDefinitionNotFound}
84
+ * @throws {PermissionServiceNotInstantiated}
29
85
  */
30
- hasPermission(_permissionCode: string): boolean;
86
+ private getPermissionDefinition;
31
87
  }
package/dist/JWTUser.js CHANGED
@@ -1,6 +1,31 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
12
+ if (kind === "m") throw new TypeError("Private method is not writable");
13
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
14
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
15
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
16
+ };
17
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
18
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
19
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
20
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
21
+ };
22
+ var _a, _JWTUser_permissionService;
2
23
  Object.defineProperty(exports, "__esModule", { value: true });
3
24
  exports.JWTUser = void 0;
25
+ const BitmaskUtils_1 = require("./BitmaskUtils");
26
+ const PermissionNotFound_1 = require("./errors/PermissionNotFound");
27
+ const PermissionServiceNotInstantiated_1 = require("./errors/PermissionServiceNotInstantiated");
28
+ const NoPermissionError_1 = require("./errors/NoPermissionError");
4
29
  class JWTUser {
5
30
  constructor({ id, employeeReference, organizationReference, roles, permissions, segmentReference, }) {
6
31
  this.id = id;
@@ -10,6 +35,21 @@ class JWTUser {
10
35
  this.permissions = permissions;
11
36
  this.segmentReference = segmentReference;
12
37
  }
38
+ /**
39
+ * Instantiate the permission service for all instances of JWTUser
40
+ */
41
+ static setPermissionService(service) {
42
+ __classPrivateFieldSet(JWTUser, _a, service, "f", _JWTUser_permissionService);
43
+ }
44
+ /**
45
+ * @throws {PermissionServiceNotInstantiated}
46
+ */
47
+ static getPermissionService() {
48
+ if (!__classPrivateFieldGet(JWTUser, _a, "f", _JWTUser_permissionService)) {
49
+ throw new PermissionServiceNotInstantiated_1.PermissionServiceNotInstantiated();
50
+ }
51
+ return __classPrivateFieldGet(JWTUser, _a, "f", _JWTUser_permissionService);
52
+ }
13
53
  static fromPayload(payload) {
14
54
  return new JWTUser({
15
55
  id: payload.sub,
@@ -50,13 +90,128 @@ class JWTUser {
50
90
  }
51
91
  /**
52
92
  * Check if user has permission to perform an action
53
- * @note - not implemented, will be implemented in the next release
54
93
  * @param permissionCode - permission code to check
55
94
  * @returns boolean
95
+ * @throws {PermissionDefinitionNotFound}
96
+ * @throws {PermissionServiceNotInstantiated}
97
+ */
98
+ hasPermission(permissionCode) {
99
+ return __awaiter(this, void 0, void 0, function* () {
100
+ const definition = yield this.getPermissionDefinition(permissionCode);
101
+ const checkBitmask = BitmaskUtils_1.BitmaskUtils.encodeFromPosition(definition.position);
102
+ const permissionsBitmask = BitmaskUtils_1.BitmaskUtils.decodeFromBase64(this.permissions);
103
+ return BitmaskUtils_1.BitmaskUtils.checkBitmask(permissionsBitmask, checkBitmask);
104
+ });
105
+ }
106
+ /**
107
+ * Throws an error if the user does not have the required permission
108
+ * @param permissionCode
109
+ * @throws {NoPermissionError}
110
+ * @throws {PermissionDefinitionNotFound}
111
+ * @throws {PermissionServiceNotInstantiated}
112
+ */
113
+ validatePermission(permissionCode) {
114
+ return __awaiter(this, void 0, void 0, function* () {
115
+ const hasPermission = yield this.hasPermission(permissionCode);
116
+ if (!hasPermission) {
117
+ throw new NoPermissionError_1.NoPermissionError([permissionCode]);
118
+ }
119
+ });
120
+ }
121
+ /**
122
+ * Check if user has all permissions to perform an action
123
+ * @param permissionCodes - permission codes to check
124
+ * @returns boolean
125
+ * @throws {PermissionDefinitionNotFound}
126
+ * @throws {PermissionServiceNotInstantiated}
127
+ */
128
+ hasEveryPermission(permissionCodes) {
129
+ return __awaiter(this, void 0, void 0, function* () {
130
+ const permissionsNotMet = [];
131
+ for (const permissionCode of permissionCodes) {
132
+ const hasPermission = yield this.hasPermission(permissionCode);
133
+ if (!hasPermission) {
134
+ permissionsNotMet.push(permissionCode);
135
+ }
136
+ }
137
+ return permissionsNotMet.length === 0;
138
+ });
139
+ }
140
+ /**
141
+ * Throws an error if the user does not have all permissions
142
+ * @param permissionCode
143
+ * @throws {NoPermissionError}
144
+ * @throws {PermissionDefinitionNotFound}
145
+ * @throws {PermissionServiceNotInstantiated}
146
+ */
147
+ validateEveryPermission(permissionCodes) {
148
+ return __awaiter(this, void 0, void 0, function* () {
149
+ const permissionsNotMet = [];
150
+ for (const permissionCode of permissionCodes) {
151
+ const hasPermission = yield this.hasPermission(permissionCode);
152
+ if (!hasPermission) {
153
+ permissionsNotMet.push(permissionCode);
154
+ }
155
+ }
156
+ if (permissionsNotMet.length > 0) {
157
+ throw new NoPermissionError_1.NoPermissionError(permissionsNotMet);
158
+ }
159
+ });
160
+ }
161
+ /**
162
+ * Check if user has at least one permission
163
+ * @param permissionCodes - permission codes to check
164
+ * @returns boolean
165
+ * @throws {PermissionDefinitionNotFound}
166
+ * @throws {PermissionServiceNotInstantiated}
56
167
  */
57
- hasPermission(_permissionCode) {
58
- throw new Error("Not implemented");
168
+ hasSomePermission(permissionCodes) {
169
+ return __awaiter(this, void 0, void 0, function* () {
170
+ for (const permissionCode of permissionCodes) {
171
+ const hasPermission = yield this.hasPermission(permissionCode);
172
+ if (hasPermission) {
173
+ return true;
174
+ }
175
+ }
176
+ return false;
177
+ });
178
+ }
179
+ /**
180
+ * Throws an error if the user does not have at least one of the permissions
181
+ * @param permissionCode
182
+ * @throws {NoPermissionError}
183
+ * @throws {PermissionDefinitionNotFound}
184
+ * @throws {PermissionServiceNotInstantiated}
185
+ */
186
+ validateSomePermission(permissionCodes) {
187
+ return __awaiter(this, void 0, void 0, function* () {
188
+ const hasSomePermission = yield this.hasSomePermission(permissionCodes);
189
+ if (!hasSomePermission) {
190
+ throw new NoPermissionError_1.NoPermissionError(permissionCodes);
191
+ }
192
+ });
193
+ }
194
+ /**
195
+ * @throws {PermissionDefinitionNotFound}
196
+ * @throws {PermissionServiceNotInstantiated}
197
+ */
198
+ getPermissionDefinition(permissionCode) {
199
+ return __awaiter(this, void 0, void 0, function* () {
200
+ const definitions = yield JWTUser.getPermissionService().getPermissions();
201
+ const definition = definitions.find((def) => def.code === permissionCode);
202
+ if (!definition) {
203
+ throw new PermissionNotFound_1.PermissionDefinitionNotFound(permissionCode);
204
+ }
205
+ return definition;
206
+ });
59
207
  }
60
208
  }
61
209
  exports.JWTUser = JWTUser;
210
+ _a = JWTUser;
211
+ /**
212
+ * Static reference to the permission service
213
+ * This is used to make sure that the permission service is only instantiated once
214
+ * and can be used by all instances of JWTUser
215
+ */
216
+ _JWTUser_permissionService = { value: null };
62
217
  //# sourceMappingURL=JWTUser.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"JWTUser.js","sourceRoot":"","sources":["../src/JWTUser.ts"],"names":[],"mappings":";;;AAEA,MAAa,OAAO;IAelB,YAAY,EACV,EAAE,EACF,iBAAiB,EACjB,qBAAqB,EACrB,KAAK,EACL,WAAW,EACX,gBAAgB,GACP;QACT,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;QACb,IAAI,CAAC,iBAAiB,GAAG,iBAAiB,CAAC;QAC3C,IAAI,CAAC,qBAAqB,GAAG,qBAAqB,CAAC;QACnD,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAC/B,IAAI,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;IAC3C,CAAC;IAED,MAAM,CAAC,WAAW,CAAC,OAAmB;QACpC,OAAO,IAAI,OAAO,CAAC;YACjB,EAAE,EAAE,OAAO,CAAC,GAAG;YACf,iBAAiB,EAAE,OAAO,CAAC,GAAG;YAC9B,qBAAqB,EAAE,OAAO,CAAC,GAAG;YAClC,KAAK,EAAE,OAAO,CAAC,GAAG;YAClB,WAAW,EAAE,OAAO,CAAC,IAAI;YACzB,gBAAgB,EAAE,OAAO,CAAC,GAAG,IAAI,IAAI;SACtC,CAAC,CAAC;IACL,CAAC;IAEM,cAAc;QACnB,OAAO;YACL,GAAG,EAAE,IAAI,CAAC,EAAE;YACZ,GAAG,EAAE,IAAI,CAAC,iBAAiB;YAC3B,GAAG,EAAE,IAAI,CAAC,qB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E;oBAClB,iBAAiB,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;iBACxC;aACF;YACD,IAAI,iBAAiB,CAAC,MAAM,GAAG,CAAC,EAAE;gBAChC,MAAM,IAAI,qCAAiB,CAAC,iBAAiB,CAAC,CAAC;aAChD;QACH,CAAC;KAAA;IAED;;;;;;OAMG;IACU,iBAAiB,CAAC,eAAyB;;YACtD,KAAK,MAAM,cAAc,IAAI,eAAe,EAAE;gBAC5C,MAAM,aAAa,GAAG,MAAM,IAAI,CAAC,aAAa,CAAC,cAAc,CAAC,CAAC;gBAC/D,IAAI,aAAa,EAAE;oBACjB,OAAO,IAAI,CAAC;iBACb;aACF;YACD,OAAO,KAAK,CAAC;QACf,CAAC;KAAA;IAED;;;;;;OAMG;IACU,sBAAsB,CACjC,eAAyB;;YAEzB,MAAM,iBAAiB,GAAG,MAAM,IAAI,CAAC,iBAAiB,CAAC,eAAe,CAAC,CAAC;YACxE,IAAI,CAAC,iBAAiB,EAAE;gBACtB,MAAM,IAAI,qCAAiB,CAAC,eAAe,CAAC,CAAC;aAC9C;QACH,CAAC;KAAA;IAED;;;OAGG;IACW,uBAAuB,CACnC,cAAsB;;YAEtB,MAAM,WAAW,GAAG,MAAM,OAAO,CAAC,oBAAoB,EAAE,CAAC,cAAc,EAAE,CAAC;YAC1E,MAAM,UAAU,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,KAAK,cAAc,CAAC,CAAC;YAC1E,IAAI,CAAC,UAAU,EAAE;gBACf,MAAM,IAAI,iDAA4B,CAAC,cAAc,CAAC,CAAC;aACxD;YACD,OAAO,UAAU,CAAC;QACpB,CAAC;KAAA;;AAnNH,0BAoNC;;AArMC;;;;GAIG;AACH,sCAAuD,IAAI,EAAC,CAAA"}
@@ -0,0 +1,26 @@
1
+ import { ILogger } from "@alanszp/logger";
2
+ import { Permission } from "./types";
3
+ import { PermissionsResolutionFunction } from "./axiosPermissionsResolutionFactory";
4
+ export interface IPermissionService {
5
+ getPermissions(): Promise<Permission[]>;
6
+ }
7
+ export declare class PermissionService implements IPermissionService {
8
+ #private;
9
+ private readonly logger;
10
+ constructor(logger: ILogger, permissionsResolutionFn: PermissionsResolutionFunction);
11
+ /**
12
+ * Retrieve all pages from a paginated {@link ListResult} request
13
+ * @todo this could be generalized, but tricky to do so because of the binding of the fetcher function
14
+ */
15
+ private getAllPagesFromPaginatedRequest;
16
+ hasCachedPermissions(): boolean;
17
+ /**
18
+ * Retrieve the permissions definitions
19
+ * @returns {Promise<Permission[]>}
20
+ */
21
+ getPermissions(checkCache?: boolean): Promise<Permission[]>;
22
+ isPermissionsCacheReady(): Promise<boolean>;
23
+ reloadPermissionCache(): Promise<void>;
24
+ startPermissionObserver(permissionRefetchInMs?: number): void;
25
+ stopPermissionObserver(): void;
26
+ }