@villedemontreal/jwt-validator 5.7.7

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 (104) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +313 -0
  3. package/dist/scripts/index.d.ts +6 -0
  4. package/dist/scripts/index.js +16 -0
  5. package/dist/scripts/index.js.map +1 -0
  6. package/dist/scripts/lint.d.ts +6 -0
  7. package/dist/scripts/lint.js +18 -0
  8. package/dist/scripts/lint.js.map +1 -0
  9. package/dist/scripts/lintFix.d.ts +6 -0
  10. package/dist/scripts/lintFix.js +21 -0
  11. package/dist/scripts/lintFix.js.map +1 -0
  12. package/dist/scripts/showCoverage.d.ts +13 -0
  13. package/dist/scripts/showCoverage.js +40 -0
  14. package/dist/scripts/showCoverage.js.map +1 -0
  15. package/dist/scripts/test.d.ts +13 -0
  16. package/dist/scripts/test.js +29 -0
  17. package/dist/scripts/test.js.map +1 -0
  18. package/dist/scripts/testUnits.d.ts +15 -0
  19. package/dist/scripts/testUnits.js +95 -0
  20. package/dist/scripts/testUnits.js.map +1 -0
  21. package/dist/scripts/watch.d.ts +14 -0
  22. package/dist/scripts/watch.js +96 -0
  23. package/dist/scripts/watch.js.map +1 -0
  24. package/dist/src/config/configs.d.ts +88 -0
  25. package/dist/src/config/configs.js +123 -0
  26. package/dist/src/config/configs.js.map +1 -0
  27. package/dist/src/config/constants.d.ts +56 -0
  28. package/dist/src/config/constants.js +66 -0
  29. package/dist/src/config/constants.js.map +1 -0
  30. package/dist/src/config/init.d.ts +15 -0
  31. package/dist/src/config/init.js +48 -0
  32. package/dist/src/config/init.js.map +1 -0
  33. package/dist/src/index.d.ts +10 -0
  34. package/dist/src/index.js +32 -0
  35. package/dist/src/index.js.map +1 -0
  36. package/dist/src/jwtValidator.d.ts +21 -0
  37. package/dist/src/jwtValidator.js +129 -0
  38. package/dist/src/jwtValidator.js.map +1 -0
  39. package/dist/src/jwtValidator.test.d.ts +1 -0
  40. package/dist/src/jwtValidator.test.js +500 -0
  41. package/dist/src/jwtValidator.test.js.map +1 -0
  42. package/dist/src/middleware/jwtMiddleware.d.ts +7 -0
  43. package/dist/src/middleware/jwtMiddleware.js +27 -0
  44. package/dist/src/middleware/jwtMiddleware.js.map +1 -0
  45. package/dist/src/models/customError.d.ts +11 -0
  46. package/dist/src/models/customError.js +38 -0
  47. package/dist/src/models/customError.js.map +1 -0
  48. package/dist/src/models/expressRequest.d.ts +15 -0
  49. package/dist/src/models/expressRequest.js +17 -0
  50. package/dist/src/models/expressRequest.js.map +1 -0
  51. package/dist/src/models/gluuUserType.d.ts +9 -0
  52. package/dist/src/models/gluuUserType.js +14 -0
  53. package/dist/src/models/gluuUserType.js.map +1 -0
  54. package/dist/src/models/jwtPayload.d.ts +30 -0
  55. package/dist/src/models/jwtPayload.js +19 -0
  56. package/dist/src/models/jwtPayload.js.map +1 -0
  57. package/dist/src/models/pagination.d.ts +16 -0
  58. package/dist/src/models/pagination.js +16 -0
  59. package/dist/src/models/pagination.js.map +1 -0
  60. package/dist/src/models/publicKey.d.ts +29 -0
  61. package/dist/src/models/publicKey.js +13 -0
  62. package/dist/src/models/publicKey.js.map +1 -0
  63. package/dist/src/repositories/cachedPublicKeyRepository.d.ts +53 -0
  64. package/dist/src/repositories/cachedPublicKeyRepository.js +102 -0
  65. package/dist/src/repositories/cachedPublicKeyRepository.js.map +1 -0
  66. package/dist/src/repositories/publicKeyRepository.d.ts +19 -0
  67. package/dist/src/repositories/publicKeyRepository.js +44 -0
  68. package/dist/src/repositories/publicKeyRepository.js.map +1 -0
  69. package/dist/src/userValidator.d.ts +30 -0
  70. package/dist/src/userValidator.js +35 -0
  71. package/dist/src/userValidator.js.map +1 -0
  72. package/dist/src/userValidator.test.d.ts +1 -0
  73. package/dist/src/userValidator.test.js +251 -0
  74. package/dist/src/userValidator.test.js.map +1 -0
  75. package/dist/src/utils/jwtMock.d.ts +31 -0
  76. package/dist/src/utils/jwtMock.js +221 -0
  77. package/dist/src/utils/jwtMock.js.map +1 -0
  78. package/dist/src/utils/logger.d.ts +11 -0
  79. package/dist/src/utils/logger.js +54 -0
  80. package/dist/src/utils/logger.js.map +1 -0
  81. package/dist/src/utils/testingConfigurations.d.ts +7 -0
  82. package/dist/src/utils/testingConfigurations.js +16 -0
  83. package/dist/src/utils/testingConfigurations.js.map +1 -0
  84. package/package.json +82 -0
  85. package/src/config/configs.ts +145 -0
  86. package/src/config/constants.ts +83 -0
  87. package/src/config/init.ts +58 -0
  88. package/src/index.ts +15 -0
  89. package/src/jwtValidator.test.ts +607 -0
  90. package/src/jwtValidator.ts +162 -0
  91. package/src/middleware/jwtMiddleware.ts +33 -0
  92. package/src/models/customError.ts +37 -0
  93. package/src/models/expressRequest.ts +27 -0
  94. package/src/models/gluuUserType.ts +9 -0
  95. package/src/models/jwtPayload.ts +58 -0
  96. package/src/models/pagination.ts +26 -0
  97. package/src/models/publicKey.ts +33 -0
  98. package/src/repositories/cachedPublicKeyRepository.ts +121 -0
  99. package/src/repositories/publicKeyRepository.ts +75 -0
  100. package/src/userValidator.test.ts +279 -0
  101. package/src/userValidator.ts +54 -0
  102. package/src/utils/jwtMock.ts +243 -0
  103. package/src/utils/logger.ts +60 -0
  104. package/src/utils/testingConfigurations.ts +12 -0
package/package.json ADDED
@@ -0,0 +1,82 @@
1
+ {
2
+ "name": "@villedemontreal/jwt-validator",
3
+ "version": "5.7.7",
4
+ "description": "Module to validate JWT (JSON Web Tokens)",
5
+ "main": "dist/src/index.js",
6
+ "typings": "dist/src",
7
+ "files": [
8
+ "src",
9
+ "dist"
10
+ ],
11
+ "scripts": {
12
+ "start": "node run test",
13
+ "test": "node run test",
14
+ "compile": "node run compile",
15
+ "prepare": "node run compile",
16
+ "prepublishOnly": "node run test",
17
+ "lint": "node run lint",
18
+ "lint-fix": "node run lint-fix",
19
+ "tslint": "node run tslint",
20
+ "tslint-fix": "node run tslint-fix",
21
+ "prettier": "node run prettier",
22
+ "prettier-fix": "node run prettier-fix",
23
+ "watch": "node run watch"
24
+ },
25
+ "keywords": [
26
+ "mtl",
27
+ "jwt",
28
+ "jwt-verification",
29
+ "security",
30
+ "authentication"
31
+ ],
32
+ "author": "Ville de Montréal",
33
+ "license": "MIT",
34
+ "dependencies": {
35
+ "@types/app-root-path": "1.2.4",
36
+ "@types/express": "4.17.13",
37
+ "@types/http-status-codes": "1.2.0",
38
+ "@types/jsonwebtoken": "8.5.9",
39
+ "@types/lodash": "4.14.184",
40
+ "@types/nock": "10.0.3",
41
+ "@types/node": "18.7.14",
42
+ "@types/request": "2.48.8",
43
+ "@types/request-promise-native": "1.0.18",
44
+ "@types/superagent": "4.1.15",
45
+ "@typescript-eslint/eslint-plugin": "5.36.1",
46
+ "@typescript-eslint/parser": "5.36.1",
47
+ "@villedemontreal/general-utils": "5.16.7",
48
+ "@villedemontreal/http-request": "7.4.5",
49
+ "@villedemontreal/logger": "6.5.7",
50
+ "app-root-path": "3.1.0",
51
+ "http-header-fields-typed": "1.3.0",
52
+ "http-status-codes": "2.2.0",
53
+ "jsonwebtoken": "8.5.1",
54
+ "lodash": "4.17.21",
55
+ "moment": "2.29.4",
56
+ "nock": "13.2.9",
57
+ "superagent": "7.1.6"
58
+ },
59
+ "devDependencies": {
60
+ "@types/chai": "4.3.3",
61
+ "@types/fs-extra": "9.0.13",
62
+ "@types/mocha": "9.1.1",
63
+ "@types/sinon": "10.0.13",
64
+ "@types/validator": "13.7.6",
65
+ "@villedemontreal/scripting": "2.1.6",
66
+ "chai": "4.3.6",
67
+ "eslint": "8.23.0",
68
+ "eslint-config-prettier": "8.5.0",
69
+ "eslint-plugin-prettier": "4.2.1",
70
+ "fs-extra": "10.1.0",
71
+ "mocha": "9.2.2",
72
+ "mocha-jenkins-reporter": "0.4.7",
73
+ "mock-express-request": "0.2.2",
74
+ "node-mocks-http": "1.11.0",
75
+ "node-notifier": "10.0.1",
76
+ "nyc": "15.1.0",
77
+ "sinon": "14.0.0",
78
+ "superagent-mocker": "0.5.2",
79
+ "typescript": "4.8.2",
80
+ "validator": "13.7.0"
81
+ }
82
+ }
@@ -0,0 +1,145 @@
1
+ import { ILogger } from '@villedemontreal/logger';
2
+ import * as os from 'os';
3
+ import * as path from 'path';
4
+ import { constants } from './constants';
5
+
6
+ /**
7
+ * Lib configs
8
+ */
9
+ export class Configs {
10
+ public isWindows: boolean;
11
+ public libRoot: string;
12
+ /**
13
+ * The host to query the public keys
14
+ */
15
+ private _host: string;
16
+ /**
17
+ * The endpoint to query the public keys
18
+ */
19
+ private _endpoint: string = constants.default.endpoint;
20
+ /**
21
+ * The parameters to query the public keys
22
+ */
23
+ private _fetchKeysParameters: string = constants.default.fetchKeysParameters;
24
+ /**
25
+ * Cache duration
26
+ */
27
+ private _cacheDuration: number = constants.default.cacheDuration;
28
+
29
+ private _loggerCreator: (name: string) => ILogger;
30
+ private _correlationIdProvider: () => string;
31
+
32
+ constructor() {
33
+ this.libRoot = path.normalize(__dirname + '/../../..');
34
+ this.isWindows = os.platform() === 'win32';
35
+ }
36
+
37
+ /**
38
+ * Get the host to query the public keys
39
+ * @return {string} host
40
+ */
41
+ public getHost() {
42
+ if (!this._host) {
43
+ throw new Error(`The "host" must be set!`);
44
+ }
45
+ return this._host;
46
+ }
47
+
48
+ /**
49
+ * Get the endpoint to query the public keys
50
+ * @return {string} endpoint
51
+ */
52
+ public getEndpoint() {
53
+ return this._endpoint;
54
+ }
55
+
56
+ /**
57
+ * Get the parameters to query the public keys
58
+ * @return {string} fetchKeysParameters
59
+ */
60
+ public getFetchKeysParameters() {
61
+ return this._fetchKeysParameters;
62
+ }
63
+
64
+ /**
65
+ * Get the cache duration in seconds
66
+ * @return {number} cacheDuration
67
+ */
68
+ public getCacheDuration() {
69
+ return this._cacheDuration;
70
+ }
71
+
72
+ /**
73
+ * Set the host to query the public keys
74
+ * @param {string} host
75
+ * @return
76
+ */
77
+ public setHost(host: string) {
78
+ this._host = host;
79
+ }
80
+
81
+ /**
82
+ * Set the endpoint to query the public keys
83
+ * @param {string} endpoint
84
+ * @return
85
+ */
86
+ public setEndpoint(endpoint: string) {
87
+ this._endpoint = endpoint;
88
+ }
89
+
90
+ /**
91
+ * Set the paramters to query the public keys
92
+ * @param {string} fetchKeysParameters
93
+ * @return
94
+ */
95
+ public setFetchKeysParameters(fetchKeysParameters: string) {
96
+ this._fetchKeysParameters = fetchKeysParameters;
97
+ }
98
+
99
+ /**
100
+ * Get the cache duration in seconds
101
+ * @param {number} cacheDuration
102
+ * @return
103
+ */
104
+ public setCacheDuration(cacheDuration: number) {
105
+ this._cacheDuration = cacheDuration;
106
+ }
107
+
108
+ /**
109
+ * The Logger creator
110
+ */
111
+ get loggerCreator(): (name: string) => ILogger {
112
+ if (!this._loggerCreator) {
113
+ throw new Error(`The Logger Creator HAS to be set as a configuration`);
114
+ }
115
+ return this._loggerCreator;
116
+ }
117
+
118
+ /**
119
+ * Sets the Logger creator.
120
+ */
121
+ public setLoggerCreator(loggerCreator: (name: string) => ILogger) {
122
+ this._loggerCreator = loggerCreator;
123
+ }
124
+
125
+ /**
126
+ * Sets the Correlation Id provider.
127
+ */
128
+ public setCorrelationIdProvider(correlationIdProvider: () => string) {
129
+ this._correlationIdProvider = correlationIdProvider;
130
+ }
131
+
132
+ /**
133
+ * The Correlation Id provider
134
+ */
135
+ get correlationIdProvider(): () => string {
136
+ if (!this._correlationIdProvider) {
137
+ throw new Error(
138
+ `The Correlation Id provider HAS to be set as a configuration! Please call the init(...) fonction first.`
139
+ );
140
+ }
141
+ return this._correlationIdProvider;
142
+ }
143
+ }
144
+
145
+ export const configs = new Configs();
@@ -0,0 +1,83 @@
1
+ // ==========================================
2
+ // Application constants
3
+ // ==========================================
4
+ import { path as appRoot } from 'app-root-path';
5
+ import * as path from 'path';
6
+
7
+ /**
8
+ * Library constants
9
+ */
10
+ export class Constants {
11
+ /**
12
+ * The library root. When this library is used
13
+ * as a dependency in a project, the "libRoot"
14
+ * will be the path to the dependency folder,
15
+ * inside the "node_modules".
16
+ */
17
+ public libRoot: string;
18
+
19
+ /**
20
+ * The app root. When this library is used
21
+ * as a dependency in a project, the "appRoot"
22
+ * will be the path to the root project!
23
+ */
24
+ public appRoot: string;
25
+
26
+ constructor() {
27
+ // From the "dist/src/config" folder
28
+ this.libRoot = path.normalize(__dirname + '/../../..');
29
+ this.appRoot = appRoot;
30
+ }
31
+
32
+ /**
33
+ * Errors related constants
34
+ */
35
+ get errors() {
36
+ return {
37
+ codes: {
38
+ // Main Code
39
+ INVALID_HEADER: 'invalidHeader',
40
+ ACCOUNT_ALREADY_EXISTS: 'accountAlreadyExists',
41
+ ACCOUNT_NOT_FOUND: 'accountNotFound',
42
+ ACCOUNT_ALREADY_VERIFIED: 'accountAlreadyVerified',
43
+ INVALID_AUTHORIZATION_HEADER: 'invalidAuthorizationHeader',
44
+ INVALID_JWT: 'invalidJWT',
45
+ CODE_NOT_FOUND: 'codeNotFound',
46
+ CODE_EXPIRED: 'codeExpired',
47
+ PHONE_NOT_FOUND: 'phoneNotFound',
48
+ UNABLE_TO_GET_PUBLIC_KEY: 'unableToGetPublicKey',
49
+
50
+ // Value Code
51
+ NULL_VALUE: 'nullValue',
52
+ INVALID_VALUE: 'invalidValue',
53
+ INVALID_EMAIL_VALUE: 'invalidEmailValue',
54
+ UNAUTHORIZED_ACCESS: 'unauthorizedAccess',
55
+
56
+ // Information Code
57
+ TEST_REMAINING: 'testRemaining',
58
+ },
59
+ };
60
+ }
61
+
62
+ /**
63
+ * Extra values that we can add to the original Express request.
64
+ */
65
+ get requestExtraVariables() {
66
+ return {
67
+ JWT: 'jwt',
68
+ };
69
+ }
70
+
71
+ /**
72
+ * Default values
73
+ */
74
+ get default() {
75
+ return {
76
+ endpoint: '/api/security/v1/keys',
77
+ fetchKeysParameters: 'state=active&state=revoked&offset=0&limit=25',
78
+ cacheDuration: 60 * 5,
79
+ };
80
+ }
81
+ }
82
+
83
+ export const constants: Constants = new Constants();
@@ -0,0 +1,58 @@
1
+ import { init as initHttpUtils } from '@villedemontreal/http-request';
2
+ import { ILogger } from '@villedemontreal/logger';
3
+ import { configs } from './configs';
4
+ import { constants } from './constants';
5
+
6
+ let libIsInited = false;
7
+
8
+ /**
9
+ * Inits the library.
10
+ */
11
+ export function init(
12
+ loggerCreator: (name: string) => ILogger,
13
+ correlationIdProvider: () => string,
14
+ host: string,
15
+ endpoint: string = constants.default.endpoint,
16
+ fetchKeysParameters: string = constants.default.fetchKeysParameters,
17
+ cacheDuration: number = constants.default.cacheDuration,
18
+ urlCaseSensitive = false
19
+ ): void {
20
+ if (!loggerCreator) {
21
+ throw new Error(`The Logger Creator is required.`);
22
+ }
23
+ configs.setLoggerCreator(loggerCreator);
24
+
25
+ if (!correlationIdProvider) {
26
+ throw new Error(`The Correlation Id provider is required.`);
27
+ }
28
+ configs.setCorrelationIdProvider(correlationIdProvider);
29
+
30
+ configs.setHost(host);
31
+ configs.setEndpoint(endpoint);
32
+ configs.setFetchKeysParameters(fetchKeysParameters);
33
+ configs.setCacheDuration(cacheDuration);
34
+
35
+ // ==========================================
36
+ // Inits the Http Utils library!
37
+ // ==========================================
38
+ initHttpUtils(loggerCreator, correlationIdProvider, urlCaseSensitive);
39
+
40
+ // ==========================================
41
+ // Set as being "properly initialized".
42
+ // At the very end of the "init()" function!
43
+ // ==========================================
44
+ libIsInited = true;
45
+ }
46
+
47
+ /**
48
+ * Is the library properly initialized?
49
+ *
50
+ * This function MUST be named "isInited()"!
51
+ * Code using this library may loop over all its "@villedemontreal"
52
+ * dependencies and, if one of those exports a "isInited" fonction,
53
+ * it will enforce that the lib has been properly initialized before
54
+ * starting...
55
+ */
56
+ export function isInited(): boolean {
57
+ return libIsInited;
58
+ }
package/src/index.ts ADDED
@@ -0,0 +1,15 @@
1
+ export * from './config/constants';
2
+ // ==========================================
3
+ // We do not export the configs instance itself,
4
+ // only the "init()" method, so we can define
5
+ // which are the required parameters.
6
+ // ==========================================
7
+ export * from './config/init';
8
+ export * from './jwtValidator';
9
+ export * from './middleware/jwtMiddleware';
10
+ export * from './models/expressRequest';
11
+ export * from './models/gluuUserType';
12
+ export * from './models/jwtPayload';
13
+ export * from './models/publicKey';
14
+ export * from './userValidator';
15
+ export * from './utils/jwtMock';