@sdflc/backend-helpers 1.0.53 → 1.0.56
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.
- package/build/BaseCore.d.ts +2 -1
- package/build/BaseCore.js +27 -12
- package/build/apiContext.js +11 -5
- package/build/checkItHelpers.d.ts +1 -1
- package/build/configBulder.d.ts +1 -1
- package/build/dbTableHelpers.d.ts +1 -1
- package/build/index.d.ts +19 -19
- package/build/index.js +19 -56
- package/build/interfaces/ApiContextInterface.d.ts +1 -0
- package/build/interfaces/CreateUserTokenArgs.d.ts +1 -0
- package/build/interfaces/CreateUserTokenResult.d.ts +4 -0
- package/build/interfaces/CreateUserTokenResult.js +2 -0
- package/build/interfaces/VerifyUserTokenArgs.d.ts +1 -0
- package/build/interfaces/index.d.ts +1 -0
- package/build/userToken.d.ts +3 -2
- package/build/userToken.js +20 -7
- package/package.json +25 -25
package/build/BaseCore.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { OpResult } from '@sdflc/api-helpers';
|
|
1
2
|
import { Logger, MeasureTool } from '@sdflc/utils';
|
|
2
3
|
import { Database } from './Database';
|
|
3
4
|
import { BaseCorePropsInterface, BaseCoreActionsInterface, CoreActionInterface, JwtTokenSecretAndSettings, ApiContext } from './interfaces';
|
|
@@ -13,7 +14,7 @@ declare class BaseCore {
|
|
|
13
14
|
getValidators(): any;
|
|
14
15
|
success(data: any, code?: number): any;
|
|
15
16
|
failure(code: number, errorMsg: string, data?: any): any;
|
|
16
|
-
validationFailure(ex: any): any;
|
|
17
|
+
validationFailure(ex: OpResult | any): any;
|
|
17
18
|
exceptionFailure(ex: any, doingWhat: string | any, data?: any): any;
|
|
18
19
|
checkResult(args: any): Promise<void>;
|
|
19
20
|
runAction(opt: CoreActionInterface): Promise<any>;
|
package/build/BaseCore.js
CHANGED
|
@@ -73,8 +73,20 @@ class BaseCore {
|
|
|
73
73
|
}
|
|
74
74
|
validationFailure(ex) {
|
|
75
75
|
const result = new api_helpers_1.OpResult({ code: api_helpers_1.OP_RESULT_CODES.VALIDATION_FAILED });
|
|
76
|
-
|
|
77
|
-
result.
|
|
76
|
+
if (ex instanceof api_helpers_1.OpResult) {
|
|
77
|
+
return result.toJS();
|
|
78
|
+
}
|
|
79
|
+
Object.keys((ex === null || ex === void 0 ? void 0 : ex.errors) || {}).forEach((key) => {
|
|
80
|
+
const obj = ex.errors[key];
|
|
81
|
+
if (Array.isArray(obj.errors)) {
|
|
82
|
+
// if we have multiple rules failures the add them all
|
|
83
|
+
obj.errors.forEach((item) => {
|
|
84
|
+
result.addError(key, item.message);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
result.addError(key, obj.message);
|
|
89
|
+
}
|
|
78
90
|
});
|
|
79
91
|
return result.toJS();
|
|
80
92
|
}
|
|
@@ -126,7 +138,7 @@ class BaseCore {
|
|
|
126
138
|
// this.measureTool.threadBlockTime(doingWhat, ({ name, durationMs }: any) => {
|
|
127
139
|
// this.logger.debug(`The ${name} blocked thread for the time of ${durationMs} ms`);
|
|
128
140
|
// });
|
|
129
|
-
this.logger.
|
|
141
|
+
this.logger.debug(`The ${doingWhat} started:`, args);
|
|
130
142
|
this.measureTool.start();
|
|
131
143
|
let step = doingWhat || '';
|
|
132
144
|
const setStep = (stepName) => {
|
|
@@ -536,26 +548,29 @@ class BaseCore {
|
|
|
536
548
|
}
|
|
537
549
|
// eslint-disable-next-line
|
|
538
550
|
authenticate(params, opt) {
|
|
551
|
+
var _a, _b;
|
|
539
552
|
return __awaiter(this, void 0, void 0, function* () {
|
|
540
553
|
//const { accessRights } = params || {};
|
|
541
|
-
const { context } = this.
|
|
554
|
+
const { context, req } = this.getContext();
|
|
542
555
|
let authByToken = false;
|
|
543
556
|
const [spaceResult, space] = yield this.findSpace();
|
|
544
557
|
if (spaceResult !== true) {
|
|
545
558
|
return spaceResult;
|
|
546
559
|
}
|
|
560
|
+
const token = ((_a = req.cookies) === null || _a === void 0 ? void 0 : _a[space.cookieName]) || context.authorization;
|
|
561
|
+
const fingerPrint = ((_b = req.cookies) === null || _b === void 0 ? void 0 : _b[space.fingerprintCookieName]) || '';
|
|
547
562
|
const { jwtSecret, verifyTokenArgs } = yield this.getJwtTokenSecretAndSettings(params);
|
|
548
|
-
if (
|
|
549
|
-
const verifyResult = (0, userToken_1.verifyUserToken)(
|
|
563
|
+
if (token && jwtSecret) {
|
|
564
|
+
const verifyResult = (0, userToken_1.verifyUserToken)(token, jwtSecret, verifyTokenArgs);
|
|
550
565
|
if (verifyResult.didFail()) {
|
|
551
566
|
return [false, verifyResult.getErrorSummary('')];
|
|
552
567
|
}
|
|
553
|
-
const
|
|
554
|
-
context.accountId =
|
|
555
|
-
context.roleId =
|
|
556
|
-
context.userId =
|
|
557
|
-
context.spaceId =
|
|
558
|
-
context.lang =
|
|
568
|
+
const tokenData = verifyResult.getDataFirst();
|
|
569
|
+
context.accountId = tokenData.accountId;
|
|
570
|
+
context.roleId = tokenData.roleId;
|
|
571
|
+
context.userId = tokenData.userId;
|
|
572
|
+
context.spaceId = tokenData.spaceId || context.spaceId;
|
|
573
|
+
context.lang = tokenData.lang || context.requestedLang;
|
|
559
574
|
authByToken = true;
|
|
560
575
|
}
|
|
561
576
|
if (!authByToken) {
|
package/build/apiContext.js
CHANGED
|
@@ -20,7 +20,7 @@ const constants_1 = require("./constants");
|
|
|
20
20
|
* @returns {object} A context object with all available cores
|
|
21
21
|
*/
|
|
22
22
|
const createApiContext = (args) => __awaiter(void 0, void 0, void 0, function* () {
|
|
23
|
-
const { cores, gateways, req, getCoreContext, getGatewayContext, extendContext, requestId: inRequestId } = args || {};
|
|
23
|
+
const { cores, gateways, req, res, getCoreContext, getGatewayContext, extendContext, requestId: inRequestId, } = args || {};
|
|
24
24
|
const requestId = inRequestId || (0, uuid_1.v4)();
|
|
25
25
|
const coreContext = typeof getCoreContext === 'function' ? yield getCoreContext({ req }) : {};
|
|
26
26
|
const gatewayContext = typeof getGatewayContext === 'function' ? yield getGatewayContext({ req }) : {};
|
|
@@ -30,6 +30,7 @@ const createApiContext = (args) => __awaiter(void 0, void 0, void 0, function* (
|
|
|
30
30
|
gateways: {},
|
|
31
31
|
usedKeyPrefixes: {},
|
|
32
32
|
req,
|
|
33
|
+
res,
|
|
33
34
|
};
|
|
34
35
|
context.cores = Object.keys(cores || {}).reduce((acc, key) => {
|
|
35
36
|
acc[(0, lodash_1.camelCase)(key)] = new cores[key](Object.assign(Object.assign({}, coreContext), { context }));
|
|
@@ -50,10 +51,8 @@ exports.createApiContext = createApiContext;
|
|
|
50
51
|
* @returns {object} A context object with all available cores
|
|
51
52
|
*/
|
|
52
53
|
const createContextDefault = (args) => __awaiter(void 0, void 0, void 0, function* () {
|
|
53
|
-
const { knexConfig, isProduction, logger, doAuth, spaceGw, interserviceApiKey, jwtSecret, verifyTokenArgs, dbSchema, cores, gateways, req, extendContext, getCoreContext, getGatewayContext, } = args || {};
|
|
54
|
-
const authorization = req.headers[constants_1.HEADERS.AUTHORIZATION]
|
|
55
|
-
? req.headers[constants_1.HEADERS.AUTHORIZATION].replace('Bearer ', '')
|
|
56
|
-
: undefined;
|
|
54
|
+
const { knexConfig, isProduction, logger, doAuth, spaceGw, interserviceApiKey, jwtSecret, verifyTokenArgs, dbSchema, cores, gateways, req, res, extendContext, getCoreContext, getGatewayContext, } = args || {};
|
|
55
|
+
const authorization = (req.headers[constants_1.HEADERS.AUTHORIZATION] || '').replace('Bearer ', '');
|
|
57
56
|
const spaceId = req.headers[constants_1.HEADERS.SPACE_ID];
|
|
58
57
|
const apiKey = req.headers[constants_1.HEADERS.API_KEY];
|
|
59
58
|
const managementApiKey = req.headers[constants_1.HEADERS.MANAGEMENT_API_KEY];
|
|
@@ -66,6 +65,8 @@ const createContextDefault = (args) => __awaiter(void 0, void 0, void 0, functio
|
|
|
66
65
|
const acceptLanguage = req.headers[constants_1.HEADERS.ACCEPT_LANGUAGE];
|
|
67
66
|
const languages = (0, utils_1.extractLanguages)(acceptLanguage);
|
|
68
67
|
const requestedLang = req.headers[constants_1.HEADERS.REQUESTED_LANGUAGE] || (languages === null || languages === void 0 ? void 0 : languages[0]) || 'en';
|
|
68
|
+
const headers = req.headers || { noHeadersSetup: true };
|
|
69
|
+
const cookies = req.cookies || { noCookiesSetup: true };
|
|
69
70
|
const requestId = (0, uuid_1.v4)();
|
|
70
71
|
const db = new Database_1.Database({
|
|
71
72
|
knexConfig,
|
|
@@ -85,11 +86,16 @@ const createContextDefault = (args) => __awaiter(void 0, void 0, void 0, functio
|
|
|
85
86
|
accountId,
|
|
86
87
|
roleId,
|
|
87
88
|
userId,
|
|
89
|
+
req,
|
|
90
|
+
res,
|
|
91
|
+
headers,
|
|
92
|
+
cookies,
|
|
88
93
|
};
|
|
89
94
|
return yield (0, exports.createApiContext)({
|
|
90
95
|
cores,
|
|
91
96
|
gateways,
|
|
92
97
|
req,
|
|
98
|
+
res,
|
|
93
99
|
requestId,
|
|
94
100
|
extendContext: ({ context, req }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
95
101
|
context.db = db;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { OpResult } from '@sdflc/api-helpers';
|
|
2
|
-
export declare const checkArray: (arr: any, validator: any, prefix?: string
|
|
2
|
+
export declare const checkArray: (arr: any, validator: any, prefix?: string) => OpResult;
|
|
3
3
|
export declare const createErrorOnCheckError: (checkItResult: any) => OpResult;
|
package/build/configBulder.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { BuildConfigOptions } from './interfaces';
|
|
2
|
-
export declare const buildConfig: (dirName: string, opt?: BuildConfigOptions
|
|
2
|
+
export declare const buildConfig: (dirName: string, opt?: BuildConfigOptions) => any;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DbFieldAddDefaultInterface } from './interfaces';
|
|
2
|
-
export declare const dbFieldAddDefaults: (table: any, opt?: DbFieldAddDefaultInterface
|
|
2
|
+
export declare const dbFieldAddDefaults: (table: any, opt?: DbFieldAddDefaultInterface) => void;
|
|
3
3
|
export declare const addOrderNo: (table: any, opt?: any) => any;
|
|
4
4
|
export declare const addAmount: (table: any, fieldName: string) => any;
|
|
5
5
|
export declare const addSpaceId: (table: any, opt?: any) => any;
|
package/build/index.d.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
export
|
|
6
|
-
export
|
|
7
|
-
export
|
|
8
|
-
export
|
|
9
|
-
export
|
|
10
|
-
export
|
|
11
|
-
export
|
|
12
|
-
export
|
|
13
|
-
export
|
|
14
|
-
export
|
|
15
|
-
export
|
|
16
|
-
export
|
|
17
|
-
export
|
|
18
|
-
export
|
|
19
|
-
export
|
|
1
|
+
export * from './constants';
|
|
2
|
+
export * from './interfaces';
|
|
3
|
+
export * from './configBulder';
|
|
4
|
+
export * from './Database';
|
|
5
|
+
export * from './dbHelpers';
|
|
6
|
+
export * from './DbMigratorAndSeeder';
|
|
7
|
+
export * from './dbSeeder';
|
|
8
|
+
export * from './knexConfigBuilder';
|
|
9
|
+
export * from './redisClient';
|
|
10
|
+
export * from './checkItHelpers';
|
|
11
|
+
export * from './DataCache';
|
|
12
|
+
export * from './BaseGateway';
|
|
13
|
+
export * from './BaseCore';
|
|
14
|
+
export * from './apiContext';
|
|
15
|
+
export * from './BaseController';
|
|
16
|
+
export * from './dbTableHelpers';
|
|
17
|
+
export * from './resolversHelpers';
|
|
18
|
+
export * from './userToken';
|
|
19
|
+
export * from './passwordHelpers';
|
|
20
20
|
export * from './commonValidators';
|
package/build/index.js
CHANGED
|
@@ -14,60 +14,23 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
var dbSeeder_1 = require("./dbSeeder");
|
|
37
|
-
Object.defineProperty(exports, "dbSeed", { enumerable: true, get: function () { return dbSeeder_1.dbSeed; } });
|
|
38
|
-
var knexConfigBuilder_1 = require("./knexConfigBuilder");
|
|
39
|
-
Object.defineProperty(exports, "buildKnexConfig", { enumerable: true, get: function () { return knexConfigBuilder_1.buildKnexConfig; } });
|
|
40
|
-
var redisClient_1 = require("./redisClient");
|
|
41
|
-
Object.defineProperty(exports, "createRedisClient", { enumerable: true, get: function () { return redisClient_1.createRedisClient; } });
|
|
42
|
-
var checkItHelpers_1 = require("./checkItHelpers");
|
|
43
|
-
Object.defineProperty(exports, "checkArray", { enumerable: true, get: function () { return checkItHelpers_1.checkArray; } });
|
|
44
|
-
Object.defineProperty(exports, "createErrorOnCheckError", { enumerable: true, get: function () { return checkItHelpers_1.createErrorOnCheckError; } });
|
|
45
|
-
var DataCache_1 = require("./DataCache");
|
|
46
|
-
Object.defineProperty(exports, "DataCache", { enumerable: true, get: function () { return DataCache_1.DataCache; } });
|
|
47
|
-
var BaseGateway_1 = require("./BaseGateway");
|
|
48
|
-
Object.defineProperty(exports, "BaseGateway", { enumerable: true, get: function () { return BaseGateway_1.BaseGateway; } });
|
|
49
|
-
var BaseCore_1 = require("./BaseCore");
|
|
50
|
-
Object.defineProperty(exports, "BaseCore", { enumerable: true, get: function () { return BaseCore_1.BaseCore; } });
|
|
51
|
-
var apiContext_1 = require("./apiContext");
|
|
52
|
-
Object.defineProperty(exports, "createApiContext", { enumerable: true, get: function () { return apiContext_1.createApiContext; } });
|
|
53
|
-
Object.defineProperty(exports, "createContextDefault", { enumerable: true, get: function () { return apiContext_1.createContextDefault; } });
|
|
54
|
-
var BaseController_1 = require("./BaseController");
|
|
55
|
-
Object.defineProperty(exports, "BaseController", { enumerable: true, get: function () { return BaseController_1.BaseController; } });
|
|
56
|
-
var dbTableHelpers_1 = require("./dbTableHelpers");
|
|
57
|
-
Object.defineProperty(exports, "dbFieldAddDefaults", { enumerable: true, get: function () { return dbTableHelpers_1.dbFieldAddDefaults; } });
|
|
58
|
-
Object.defineProperty(exports, "addOrderNo", { enumerable: true, get: function () { return dbTableHelpers_1.addOrderNo; } });
|
|
59
|
-
Object.defineProperty(exports, "addAmount", { enumerable: true, get: function () { return dbTableHelpers_1.addAmount; } });
|
|
60
|
-
Object.defineProperty(exports, "addSpaceId", { enumerable: true, get: function () { return dbTableHelpers_1.addSpaceId; } });
|
|
61
|
-
Object.defineProperty(exports, "addUserId", { enumerable: true, get: function () { return dbTableHelpers_1.addUserId; } });
|
|
62
|
-
Object.defineProperty(exports, "addAccountId", { enumerable: true, get: function () { return dbTableHelpers_1.addAccountId; } });
|
|
63
|
-
Object.defineProperty(exports, "addLang", { enumerable: true, get: function () { return dbTableHelpers_1.addLang; } });
|
|
64
|
-
Object.defineProperty(exports, "defaultFields", { enumerable: true, get: function () { return dbTableHelpers_1.defaultFields; } });
|
|
65
|
-
var resolversHelpers_1 = require("./resolversHelpers");
|
|
66
|
-
Object.defineProperty(exports, "buildDefaultResolvers", { enumerable: true, get: function () { return resolversHelpers_1.buildDefaultResolvers; } });
|
|
67
|
-
var userToken_1 = require("./userToken");
|
|
68
|
-
Object.defineProperty(exports, "createUserToken", { enumerable: true, get: function () { return userToken_1.createUserToken; } });
|
|
69
|
-
Object.defineProperty(exports, "verifyUserToken", { enumerable: true, get: function () { return userToken_1.verifyUserToken; } });
|
|
70
|
-
var passwordHelpers_1 = require("./passwordHelpers");
|
|
71
|
-
Object.defineProperty(exports, "getSaltAndHash", { enumerable: true, get: function () { return passwordHelpers_1.getSaltAndHash; } });
|
|
72
|
-
Object.defineProperty(exports, "checkPassword", { enumerable: true, get: function () { return passwordHelpers_1.checkPassword; } });
|
|
17
|
+
__exportStar(require("./constants"), exports);
|
|
18
|
+
__exportStar(require("./interfaces"), exports);
|
|
19
|
+
__exportStar(require("./configBulder"), exports);
|
|
20
|
+
__exportStar(require("./Database"), exports);
|
|
21
|
+
__exportStar(require("./dbHelpers"), exports);
|
|
22
|
+
__exportStar(require("./DbMigratorAndSeeder"), exports);
|
|
23
|
+
__exportStar(require("./dbSeeder"), exports);
|
|
24
|
+
__exportStar(require("./knexConfigBuilder"), exports);
|
|
25
|
+
__exportStar(require("./redisClient"), exports);
|
|
26
|
+
__exportStar(require("./checkItHelpers"), exports);
|
|
27
|
+
__exportStar(require("./DataCache"), exports);
|
|
28
|
+
__exportStar(require("./BaseGateway"), exports);
|
|
29
|
+
__exportStar(require("./BaseCore"), exports);
|
|
30
|
+
__exportStar(require("./apiContext"), exports);
|
|
31
|
+
__exportStar(require("./BaseController"), exports);
|
|
32
|
+
__exportStar(require("./dbTableHelpers"), exports);
|
|
33
|
+
__exportStar(require("./resolversHelpers"), exports);
|
|
34
|
+
__exportStar(require("./userToken"), exports);
|
|
35
|
+
__exportStar(require("./passwordHelpers"), exports);
|
|
73
36
|
__exportStar(require("./commonValidators"), exports);
|
|
@@ -21,3 +21,4 @@ export { BaseCoreActionsInterface } from './BaseCoreActionsInterface';
|
|
|
21
21
|
export { BaseCoreDoingWhatInterface } from './BaseCoreDoingWhatInterface';
|
|
22
22
|
export { JwtTokenSecretAndSettings } from './JwtTokenSecretAndSettings';
|
|
23
23
|
export { CommonValidatorArgs } from './CommonValidatorArgs';
|
|
24
|
+
export { CreateUserTokenResult } from './CreateUserTokenResult';
|
package/build/userToken.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { OpResult } from '@sdflc/api-helpers';
|
|
2
2
|
import { UserTokenParams, CreateUserTokenArgs, VerifyUserTokenArgs } from './interfaces';
|
|
3
|
-
export declare const
|
|
4
|
-
export declare const
|
|
3
|
+
export declare const setDefaultJwtAlgorithm: (algorithm: string) => void;
|
|
4
|
+
export declare const createUserToken: (params: UserTokenParams, secret: string, opt: CreateUserTokenArgs) => any;
|
|
5
|
+
export declare const verifyUserToken: (token: string, secret: string, args?: VerifyUserTokenArgs) => OpResult;
|
package/build/userToken.js
CHANGED
|
@@ -3,16 +3,23 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.verifyUserToken = exports.createUserToken = void 0;
|
|
6
|
+
exports.verifyUserToken = exports.createUserToken = exports.setDefaultJwtAlgorithm = void 0;
|
|
7
7
|
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
8
|
+
const uuid_1 = require("uuid");
|
|
8
9
|
const api_helpers_1 = require("@sdflc/api-helpers");
|
|
10
|
+
let defaultJwtAlgorithm = 'HS256';
|
|
11
|
+
const setDefaultJwtAlgorithm = (algorithm) => {
|
|
12
|
+
defaultJwtAlgorithm = algorithm;
|
|
13
|
+
};
|
|
14
|
+
exports.setDefaultJwtAlgorithm = setDefaultJwtAlgorithm;
|
|
9
15
|
const createUserToken = (params, secret, opt) => {
|
|
10
|
-
const { algorithm, audience, issuer, jwtid, subject, noTimestamp, header, keyid, expiresIn } = opt || {};
|
|
16
|
+
const { algorithm, audience, issuer, jwtid, subject, noTimestamp, header, keyid, expiresIn, useFingerprint } = opt || {};
|
|
11
17
|
if (!params || !secret) {
|
|
12
18
|
return api_helpers_1.OpResult.fail(api_helpers_1.OP_RESULT_CODES.VALIDATION_FAILED, null, 'Failed to create user JWT');
|
|
13
19
|
}
|
|
14
|
-
const
|
|
15
|
-
|
|
20
|
+
const fingerprint = useFingerprint ? (0, uuid_1.v4)() : undefined;
|
|
21
|
+
const token = jsonwebtoken_1.default.sign(useFingerprint ? Object.assign(Object.assign({}, params), { fingerprint }) : params, secret, {
|
|
22
|
+
algorithm: algorithm || defaultJwtAlgorithm,
|
|
16
23
|
audience,
|
|
17
24
|
issuer,
|
|
18
25
|
jwtid,
|
|
@@ -22,17 +29,17 @@ const createUserToken = (params, secret, opt) => {
|
|
|
22
29
|
keyid,
|
|
23
30
|
expiresIn: expiresIn && !isNaN(expiresIn) ? expiresIn : undefined,
|
|
24
31
|
});
|
|
25
|
-
return api_helpers_1.OpResult.ok(token);
|
|
32
|
+
return api_helpers_1.OpResult.ok({ token, fingerprint });
|
|
26
33
|
};
|
|
27
34
|
exports.createUserToken = createUserToken;
|
|
28
35
|
const verifyUserToken = (token, secret, args) => {
|
|
29
|
-
const { algorithms, audience, issuer, ignoreExpiration, ignoreNotBefore, subject, clockTolerance, maxAge, clockTimestamp, nonce, } = args || {};
|
|
36
|
+
const { algorithms, audience, issuer, ignoreExpiration, ignoreNotBefore, subject, clockTolerance, maxAge, clockTimestamp, nonce, fingreprint, } = args || {};
|
|
30
37
|
if (!args) {
|
|
31
38
|
return api_helpers_1.OpResult.fail(api_helpers_1.OP_RESULT_CODES.VALIDATION_FAILED, null, 'Cannot verify user token as there are no parameters specified');
|
|
32
39
|
}
|
|
33
40
|
try {
|
|
34
41
|
const obj = jsonwebtoken_1.default.verify(token, secret, {
|
|
35
|
-
algorithms: algorithms || [
|
|
42
|
+
algorithms: algorithms || [defaultJwtAlgorithm],
|
|
36
43
|
audience,
|
|
37
44
|
issuer,
|
|
38
45
|
ignoreExpiration,
|
|
@@ -43,9 +50,15 @@ const verifyUserToken = (token, secret, args) => {
|
|
|
43
50
|
clockTimestamp,
|
|
44
51
|
nonce,
|
|
45
52
|
});
|
|
53
|
+
if ((obj === null || obj === void 0 ? void 0 : obj.fingerprint) && obj.fingerprint != fingreprint) {
|
|
54
|
+
return api_helpers_1.OpResult.fail(api_helpers_1.OP_RESULT_CODES.UNAUTHORIZED, null, 'User unauthenticated');
|
|
55
|
+
}
|
|
46
56
|
return api_helpers_1.OpResult.ok(obj);
|
|
47
57
|
}
|
|
48
58
|
catch (ex) {
|
|
59
|
+
if (ex.name === 'TokenExpiredError') {
|
|
60
|
+
return api_helpers_1.OpResult.fail(api_helpers_1.OP_RESULT_CODES.EXPIRED, null, 'User authorization has expired');
|
|
61
|
+
}
|
|
49
62
|
return api_helpers_1.OpResult.fail(api_helpers_1.OP_RESULT_CODES.UNAUTHORIZED, null, 'User unauthenticated');
|
|
50
63
|
}
|
|
51
64
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sdflc/backend-helpers",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.56",
|
|
4
4
|
"description": "Set of helper functions and classes to simplify building backend apps within @sdflc ecosystem",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"types": "build/index.d.ts",
|
|
@@ -23,45 +23,45 @@
|
|
|
23
23
|
"author": "Sergey Demidenko",
|
|
24
24
|
"license": "ISC",
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@sdflc/api-helpers": "^1.1.
|
|
27
|
-
"@sdflc/utils": "^1.0.
|
|
28
|
-
"bcrypt": "^5.0
|
|
26
|
+
"@sdflc/api-helpers": "^1.1.10",
|
|
27
|
+
"@sdflc/utils": "^1.0.22",
|
|
28
|
+
"bcrypt": "^5.1.0",
|
|
29
29
|
"bluebird": "^3.7.2",
|
|
30
30
|
"checkit": "^0.7.0",
|
|
31
|
-
"dataloader": "^2.
|
|
32
|
-
"dayjs": "^1.11.
|
|
31
|
+
"dataloader": "^2.1.0",
|
|
32
|
+
"dayjs": "^1.11.6",
|
|
33
33
|
"dotenv-safe": "^8.2.0",
|
|
34
|
-
"jest": "^27.5.1",
|
|
35
34
|
"json-stable-stringify": "^1.0.1",
|
|
36
35
|
"jsonwebtoken": "^8.5.1",
|
|
37
|
-
"knex": "^
|
|
36
|
+
"knex": "^2.3.0",
|
|
38
37
|
"lodash": "^4.17.21",
|
|
39
|
-
"pg": "^8.
|
|
38
|
+
"pg": "^8.8.0",
|
|
40
39
|
"redis": "^4.0.6",
|
|
41
|
-
"
|
|
42
|
-
"typescript": "^4.6.3",
|
|
43
|
-
"uuid": "^8.3.2"
|
|
40
|
+
"uuid": "^9.0.0"
|
|
44
41
|
},
|
|
45
42
|
"devDependencies": {
|
|
46
43
|
"@types/bcrypt": "^5.0.0",
|
|
47
|
-
"@types/bluebird": "^3.5.
|
|
44
|
+
"@types/bluebird": "^3.5.37",
|
|
48
45
|
"@types/dotenv-safe": "^8.1.2",
|
|
46
|
+
"@types/jest": "^29.2.0",
|
|
49
47
|
"@types/json-stable-stringify": "^1.0.34",
|
|
50
|
-
"@types/jsonwebtoken": "^8.5.
|
|
48
|
+
"@types/jsonwebtoken": "^8.5.9",
|
|
51
49
|
"@types/lodash": "^4.14.181",
|
|
52
|
-
"@types/node": "^
|
|
53
|
-
"@types/redis": "^4.0.11",
|
|
50
|
+
"@types/node": "^18.11.7",
|
|
54
51
|
"@types/uuid": "^8.3.4",
|
|
55
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
56
|
-
"@typescript-eslint/parser": "^5.
|
|
57
|
-
"eslint": "^8.
|
|
52
|
+
"@typescript-eslint/eslint-plugin": "^5.41.0",
|
|
53
|
+
"@typescript-eslint/parser": "^5.41.0",
|
|
54
|
+
"eslint": "^8.26.0",
|
|
58
55
|
"eslint-config-prettier": "^8.5.0",
|
|
59
|
-
"eslint-plugin-jest": "^
|
|
60
|
-
"eslint-plugin-prettier": "^4.
|
|
61
|
-
"husky": "^
|
|
62
|
-
"
|
|
56
|
+
"eslint-plugin-jest": "^27.1.3",
|
|
57
|
+
"eslint-plugin-prettier": "^4.2.1",
|
|
58
|
+
"husky": "^8.0.1",
|
|
59
|
+
"jest": "^29.2.2",
|
|
60
|
+
"prettier": "^2.7.1",
|
|
61
|
+
"rimraf": "^3.0.2",
|
|
63
62
|
"run-script-os": "^1.1.6",
|
|
64
|
-
"ts-jest": "^
|
|
65
|
-
"ts-node": "^10.
|
|
63
|
+
"ts-jest": "^29.0.3",
|
|
64
|
+
"ts-node": "^10.9.1",
|
|
65
|
+
"typescript": "^4.8.4"
|
|
66
66
|
}
|
|
67
67
|
}
|