@villedemontreal/jwt-validator 5.9.1 → 5.9.2
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.
|
@@ -3,12 +3,40 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.tokenTransformationMiddleware = void 0;
|
|
4
4
|
const general_utils_1 = require("@villedemontreal/general-utils");
|
|
5
5
|
const http_header_fields_typed_1 = require("http-header-fields-typed");
|
|
6
|
+
const _ = require("lodash");
|
|
6
7
|
const constants_1 = require("../config/constants");
|
|
7
8
|
const customError_1 = require("../models/customError");
|
|
8
9
|
const logger_1 = require("../utils/logger");
|
|
9
10
|
const superagent = require("superagent");
|
|
10
|
-
const _regexAccessToken = /([a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12})/;
|
|
11
11
|
const logger = (0, logger_1.createLogger)('Token transformation middleware');
|
|
12
|
+
/** Regex to test the UUID format of the Authorization header */
|
|
13
|
+
const _regexUuidAccessToken = /([a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12})/;
|
|
14
|
+
/** Regex to test the JWT format of the Authorization header */
|
|
15
|
+
const _regexJwtAccessToken = /([a-zA-Z0-9_=]+)\.([a-zA-Z0-9_=]+)\.([a-zA-Z0-9_\-+/=]+)$/;
|
|
16
|
+
/**
|
|
17
|
+
* Validate the access_token format from authorization header and return it.
|
|
18
|
+
*
|
|
19
|
+
* @param {string} authHeader
|
|
20
|
+
* @return {*} {string}
|
|
21
|
+
*/
|
|
22
|
+
const getAccessTokenFromHeader = (authHeader) => {
|
|
23
|
+
if (authHeader.split(' ')[0] !== 'Bearer') {
|
|
24
|
+
logger.warning('The authorization header is not "Bearer" type.');
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
const accessTokenUuidRegExpArray = _regexUuidAccessToken.exec(authHeader);
|
|
28
|
+
const accessTokenJwtRegExpArray = _regexJwtAccessToken.exec(authHeader);
|
|
29
|
+
if (_.isNil(accessTokenUuidRegExpArray) && _.isNil(accessTokenJwtRegExpArray)) {
|
|
30
|
+
logger.warning('Could not find a valid access token from the authorization header');
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
if (!_.isNil(accessTokenJwtRegExpArray)) {
|
|
34
|
+
return accessTokenJwtRegExpArray[0];
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
return accessTokenUuidRegExpArray[0];
|
|
38
|
+
}
|
|
39
|
+
};
|
|
12
40
|
/**
|
|
13
41
|
* Token transformation Middleware. It will generate extended jwt
|
|
14
42
|
* in exchange for an access token.
|
|
@@ -21,22 +49,16 @@ const tokenTransformationMiddleware = (config) => {
|
|
|
21
49
|
// Validate the authorization header
|
|
22
50
|
const authHeader = req.get(http_header_fields_typed_1.default.AUTHORIZATION);
|
|
23
51
|
if (general_utils_1.utils.isBlank(authHeader)) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
message: 'authorization header is empty',
|
|
28
|
-
});
|
|
52
|
+
logger.warning('The authorization header is empty.');
|
|
53
|
+
next();
|
|
54
|
+
return;
|
|
29
55
|
}
|
|
30
56
|
// Extract the access token value from the authorization header
|
|
31
|
-
const
|
|
32
|
-
if (
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
target: 'access_token',
|
|
36
|
-
message: 'could not find a valid access token from the authorization header',
|
|
37
|
-
});
|
|
57
|
+
const accessToken = getAccessTokenFromHeader(authHeader);
|
|
58
|
+
if (_.isNil(accessToken)) {
|
|
59
|
+
next();
|
|
60
|
+
return;
|
|
38
61
|
}
|
|
39
|
-
const accessToken = accessTokenRegExpArray[1];
|
|
40
62
|
// Call the service endpoint to exchange the access token for a extended jwt
|
|
41
63
|
superagent
|
|
42
64
|
.post(config.service.uri)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tokenTransformationMiddleware.js","sourceRoot":"","sources":["../../../src/middleware/tokenTransformationMiddleware.ts"],"names":[],"mappings":";;;AAAA,kEAAuD;AAEvD,uEAA6D;AAC7D,mDAAgD;AAEhD,
|
|
1
|
+
{"version":3,"file":"tokenTransformationMiddleware.js","sourceRoot":"","sources":["../../../src/middleware/tokenTransformationMiddleware.ts"],"names":[],"mappings":";;;AAAA,kEAAuD;AAEvD,uEAA6D;AAC7D,4BAA4B;AAC5B,mDAAgD;AAEhD,uDAA8D;AAC9D,4CAA+C;AAC/C,yCAA0C;AAE1C,MAAM,MAAM,GAAG,IAAA,qBAAY,EAAC,iCAAiC,CAAC,CAAC;AAE/D,gEAAgE;AAChE,MAAM,qBAAqB,GAAG,gEAAgE,CAAC;AAC/F,+DAA+D;AAC/D,MAAM,oBAAoB,GAAG,2DAA2D,CAAC;AAEzF;;;;;GAKG;AACH,MAAM,wBAAwB,GAAG,CAAC,UAAkB,EAAiB,EAAE;IACrE,IAAI,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,QAAQ,EAAE;QACzC,MAAM,CAAC,OAAO,CAAC,gDAAgD,CAAC,CAAC;QACjE,OAAO,IAAI,CAAC;KACb;IACD,MAAM,0BAA0B,GAAG,qBAAqB,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IAC1E,MAAM,yBAAyB,GAAG,oBAAoB,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IACxE,IAAI,CAAC,CAAC,KAAK,CAAC,0BAA0B,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,yBAAyB,CAAC,EAAE;QAC7E,MAAM,CAAC,OAAO,CAAC,mEAAmE,CAAC,CAAC;QACpF,OAAO,IAAI,CAAC;KACb;IACD,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,yBAAyB,CAAC,EAAE;QACvC,OAAO,yBAAyB,CAAC,CAAC,CAAC,CAAC;KACrC;SAAM;QACL,OAAO,0BAA0B,CAAC,CAAC,CAAC,CAAC;KACtC;AACH,CAAC,CAAC;A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E,CAAC,GAAG,UAAU,GAAG,EAAE,CAAC;oBACjF,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,OAAO,EAAE,iBAAiB,CAAC,CAAC;oBAC7C,IAAI,EAAE,CAAC;iBACR;qBAAM;oBACL,MAAM,GAAG,GAAG,IAAA,mCAAqB,EAAC;wBAChC,IAAI,EAAE,qBAAS,CAAC,MAAM,CAAC,KAAK,CAAC,UAAU;wBACvC,MAAM,EAAE,KAAK;wBACb,OAAO,EAAE,0DAA0D;qBACpE,CAAC,CAAC;oBACH,IAAI,CAAC,GAAG,CAAC,CAAC;iBACX;YACH,CAAC,CAAC;iBACD,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;SAC9B;QAAC,OAAO,GAAG,EAAE;YACZ,IAAI,CAAC,GAAG,CAAC,CAAC;SACX;IACH,CAAC,CAAC;AACJ,CAAC,CAAC;AAxDW,QAAA,6BAA6B,iCAwDxC"}
|
package/package.json
CHANGED
|
@@ -1,16 +1,44 @@
|
|
|
1
1
|
import { utils } from '@villedemontreal/general-utils';
|
|
2
2
|
import * as express from 'express';
|
|
3
3
|
import httpHeaderFieldsTyped from 'http-header-fields-typed';
|
|
4
|
+
import * as _ from 'lodash';
|
|
4
5
|
import { constants } from '../config/constants';
|
|
5
6
|
import { ITokenTtransformationMiddlewareConfig } from '../config/tokenTransformationMiddlewareConfig';
|
|
6
|
-
import {
|
|
7
|
+
import { createInvalidJwtError } from '../models/customError';
|
|
7
8
|
import { createLogger } from '../utils/logger';
|
|
8
9
|
import superagent = require('superagent');
|
|
9
10
|
|
|
10
|
-
const _regexAccessToken = /([a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12})/;
|
|
11
|
-
|
|
12
11
|
const logger = createLogger('Token transformation middleware');
|
|
13
12
|
|
|
13
|
+
/** Regex to test the UUID format of the Authorization header */
|
|
14
|
+
const _regexUuidAccessToken = /([a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12})/;
|
|
15
|
+
/** Regex to test the JWT format of the Authorization header */
|
|
16
|
+
const _regexJwtAccessToken = /([a-zA-Z0-9_=]+)\.([a-zA-Z0-9_=]+)\.([a-zA-Z0-9_\-+/=]+)$/;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Validate the access_token format from authorization header and return it.
|
|
20
|
+
*
|
|
21
|
+
* @param {string} authHeader
|
|
22
|
+
* @return {*} {string}
|
|
23
|
+
*/
|
|
24
|
+
const getAccessTokenFromHeader = (authHeader: string): string | null => {
|
|
25
|
+
if (authHeader.split(' ')[0] !== 'Bearer') {
|
|
26
|
+
logger.warning('The authorization header is not "Bearer" type.');
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
const accessTokenUuidRegExpArray = _regexUuidAccessToken.exec(authHeader);
|
|
30
|
+
const accessTokenJwtRegExpArray = _regexJwtAccessToken.exec(authHeader);
|
|
31
|
+
if (_.isNil(accessTokenUuidRegExpArray) && _.isNil(accessTokenJwtRegExpArray)) {
|
|
32
|
+
logger.warning('Could not find a valid access token from the authorization header');
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
if (!_.isNil(accessTokenJwtRegExpArray)) {
|
|
36
|
+
return accessTokenJwtRegExpArray[0];
|
|
37
|
+
} else {
|
|
38
|
+
return accessTokenUuidRegExpArray[0];
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
14
42
|
/**
|
|
15
43
|
* Token transformation Middleware. It will generate extended jwt
|
|
16
44
|
* in exchange for an access token.
|
|
@@ -25,25 +53,19 @@ export const tokenTransformationMiddleware: (
|
|
|
25
53
|
return (req: express.Request, res: express.Response, next: express.NextFunction): void => {
|
|
26
54
|
try {
|
|
27
55
|
// Validate the authorization header
|
|
28
|
-
const authHeader
|
|
56
|
+
const authHeader = req.get(httpHeaderFieldsTyped.AUTHORIZATION);
|
|
29
57
|
if (utils.isBlank(authHeader)) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
message: 'authorization header is empty',
|
|
34
|
-
});
|
|
58
|
+
logger.warning('The authorization header is empty.');
|
|
59
|
+
next();
|
|
60
|
+
return;
|
|
35
61
|
}
|
|
36
62
|
|
|
37
63
|
// Extract the access token value from the authorization header
|
|
38
|
-
const
|
|
39
|
-
if (
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
target: 'access_token',
|
|
43
|
-
message: 'could not find a valid access token from the authorization header',
|
|
44
|
-
});
|
|
64
|
+
const accessToken = getAccessTokenFromHeader(authHeader);
|
|
65
|
+
if (_.isNil(accessToken)) {
|
|
66
|
+
next();
|
|
67
|
+
return;
|
|
45
68
|
}
|
|
46
|
-
const accessToken = accessTokenRegExpArray[1];
|
|
47
69
|
|
|
48
70
|
// Call the service endpoint to exchange the access token for a extended jwt
|
|
49
71
|
superagent
|