@sdflc/backend-helpers 1.0.57 → 1.0.59
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 +8 -1
- package/build/BaseCore.js +63 -5
- package/build/apiContext.js +2 -0
- package/build/constants.d.ts +7 -1
- package/build/constants.js +11 -1
- package/build/interfaces/CreateUserTokenArgs.d.ts +0 -1
- package/build/interfaces/ExtractCredentialsArgs.d.ts +14 -0
- package/build/interfaces/ExtractCredentialsArgs.js +2 -0
- package/build/interfaces/UserTokenParams.d.ts +1 -0
- package/build/interfaces/index.d.ts +1 -0
- package/build/userToken.d.ts +3 -0
- package/build/userToken.js +25 -9
- package/package.json +1 -1
package/build/BaseCore.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { OpResult } from '@sdflc/api-helpers';
|
|
2
2
|
import { Logger, MeasureTool } from '@sdflc/utils';
|
|
3
3
|
import { Database } from './Database';
|
|
4
|
-
import { BaseCorePropsInterface, BaseCoreActionsInterface, CoreActionInterface, JwtTokenSecretAndSettings, ApiContext } from './interfaces';
|
|
4
|
+
import { BaseCorePropsInterface, BaseCoreActionsInterface, CoreActionInterface, JwtTokenSecretAndSettings, ApiContext, ExtractCredentialsArgs } from './interfaces';
|
|
5
5
|
declare class BaseCore {
|
|
6
6
|
protected measureTool: MeasureTool;
|
|
7
7
|
protected config: BaseCorePropsInterface;
|
|
@@ -53,6 +53,13 @@ declare class BaseCore {
|
|
|
53
53
|
*/
|
|
54
54
|
getJwtTokenSecretAndSettings(params: any): Promise<JwtTokenSecretAndSettings>;
|
|
55
55
|
findSpace(): Promise<any[]>;
|
|
56
|
+
extractCredentials(args: ExtractCredentialsArgs): Promise<{
|
|
57
|
+
token: any;
|
|
58
|
+
fingerprint: any;
|
|
59
|
+
}>;
|
|
56
60
|
authenticate(params: any, opt?: BaseCoreActionsInterface): Promise<any[]>;
|
|
61
|
+
processRemovedRecord(record: any): any;
|
|
62
|
+
addRemovedModifierToFields(fields: string | string[]): any;
|
|
63
|
+
setOrderNo(args: any): Promise<void>;
|
|
57
64
|
}
|
|
58
65
|
export { BaseCore };
|
package/build/BaseCore.js
CHANGED
|
@@ -12,6 +12,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.BaseCore = void 0;
|
|
13
13
|
const api_helpers_1 = require("@sdflc/api-helpers");
|
|
14
14
|
const utils_1 = require("@sdflc/utils");
|
|
15
|
+
const lodash_1 = require("lodash");
|
|
16
|
+
const constants_1 = require("./constants");
|
|
15
17
|
const userToken_1 = require("./userToken");
|
|
16
18
|
const defaultValidate = ( /*params: any, opt?: BaseCoreActionsInterface*/) => {
|
|
17
19
|
return [true, {}];
|
|
@@ -546,22 +548,33 @@ class BaseCore {
|
|
|
546
548
|
return [true, space];
|
|
547
549
|
});
|
|
548
550
|
}
|
|
551
|
+
extractCredentials(args) {
|
|
552
|
+
var _a, _b;
|
|
553
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
554
|
+
const { req, space, authorization } = args;
|
|
555
|
+
const token = ((_a = req.cookies) === null || _a === void 0 ? void 0 : _a[space.cookieName]) || authorization;
|
|
556
|
+
const fingerprint = ((_b = req.cookies) === null || _b === void 0 ? void 0 : _b[space.fingerprintCookieName]) || '';
|
|
557
|
+
return {
|
|
558
|
+
token,
|
|
559
|
+
fingerprint,
|
|
560
|
+
};
|
|
561
|
+
});
|
|
562
|
+
}
|
|
549
563
|
// eslint-disable-next-line
|
|
550
564
|
authenticate(params, opt) {
|
|
551
|
-
var _a, _b;
|
|
552
565
|
return __awaiter(this, void 0, void 0, function* () {
|
|
553
566
|
//const { accessRights } = params || {};
|
|
554
|
-
const
|
|
567
|
+
const context = this.getContext();
|
|
568
|
+
const { req, authorization } = context;
|
|
555
569
|
let authByToken = false;
|
|
556
570
|
const [spaceResult, space] = yield this.findSpace();
|
|
557
571
|
if (spaceResult !== true) {
|
|
558
572
|
return spaceResult;
|
|
559
573
|
}
|
|
560
|
-
const token
|
|
561
|
-
const fingerprint = ((_b = req.cookies) === null || _b === void 0 ? void 0 : _b[space.fingerprintCookieName]) || '';
|
|
574
|
+
const { token, fingerprint } = yield this.extractCredentials({ req, authorization, space });
|
|
562
575
|
const { jwtSecret, verifyTokenArgs } = yield this.getJwtTokenSecretAndSettings(params);
|
|
563
576
|
if (token && jwtSecret) {
|
|
564
|
-
const verifyResult = (0, userToken_1.verifyUserToken)(token, jwtSecret, Object.assign(Object.assign({}, verifyTokenArgs), { fingerprint }));
|
|
577
|
+
const verifyResult = (0, userToken_1.verifyUserToken)(token, jwtSecret, Object.assign(Object.assign({}, verifyTokenArgs), { algorithms: (verifyTokenArgs === null || verifyTokenArgs === void 0 ? void 0 : verifyTokenArgs.algorithms) || [(0, userToken_1.getDefaultJwtAlgorithm)()], audience: (verifyTokenArgs === null || verifyTokenArgs === void 0 ? void 0 : verifyTokenArgs.audience) || space.id, issuer: (verifyTokenArgs === null || verifyTokenArgs === void 0 ? void 0 : verifyTokenArgs.issuer) || (0, userToken_1.getDefaultJwtIssuer)(), fingerprint }));
|
|
565
578
|
if (verifyResult.didFail()) {
|
|
566
579
|
return [false, verifyResult.getErrorSummary('')];
|
|
567
580
|
}
|
|
@@ -584,5 +597,50 @@ class BaseCore {
|
|
|
584
597
|
return [true, ''];
|
|
585
598
|
});
|
|
586
599
|
}
|
|
600
|
+
processRemovedRecord(record) {
|
|
601
|
+
if (record && typeof record.id === 'string') {
|
|
602
|
+
record.id = record.id.split('~')[0];
|
|
603
|
+
}
|
|
604
|
+
return record;
|
|
605
|
+
}
|
|
606
|
+
addRemovedModifierToFields(fields) {
|
|
607
|
+
const obj = {};
|
|
608
|
+
if (!fields) {
|
|
609
|
+
this.logger.log(`The 'addRemovedModifierToFields' method expected its fields parameter to be a field name or an array of table field names but got:`, fields);
|
|
610
|
+
return obj;
|
|
611
|
+
}
|
|
612
|
+
(0, lodash_1.castArray)(fields).forEach((field) => {
|
|
613
|
+
obj[field] = this.getDb().raw(`${field} || '~removed~' || uuid_generate_v4()`);
|
|
614
|
+
});
|
|
615
|
+
return obj;
|
|
616
|
+
}
|
|
617
|
+
setOrderNo(args) {
|
|
618
|
+
var _a;
|
|
619
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
620
|
+
const { params, filter, gateway } = args || {};
|
|
621
|
+
if (typeof params !== 'object') {
|
|
622
|
+
throw new Error(`The 'setOrderNo' expected the parameter to have 'params' object but got: ${JSON.stringify(params)} `);
|
|
623
|
+
}
|
|
624
|
+
if (typeof gateway !== 'object') {
|
|
625
|
+
throw new Error(`The 'setOrderNo' expected the parameter to have 'gateway' object but got: ${JSON.stringify(gateway)} `);
|
|
626
|
+
}
|
|
627
|
+
if (typeof gateway.list !== 'function') {
|
|
628
|
+
throw new Error(`The 'setOrderNo' expected the 'gateway' object to have 'list' method but got: ${JSON.stringify(gateway.list)} `);
|
|
629
|
+
}
|
|
630
|
+
if (!params.orderNo) {
|
|
631
|
+
const lastRecord = yield gateway.list({
|
|
632
|
+
params: filter,
|
|
633
|
+
pagination: { pageSize: 1, sortBy: [{ name: constants_1.FIELDS.ORDER_NO, order: constants_1.ORDER_BYS.DESC }] },
|
|
634
|
+
});
|
|
635
|
+
if (lastRecord.length > 0) {
|
|
636
|
+
const order = new utils_1.StrOrderHelpers({
|
|
637
|
+
start: ((_a = lastRecord[0]) === null || _a === void 0 ? void 0 : _a.orderNo) || constants_1.DEFAULT_ORDER_NO,
|
|
638
|
+
step: constants_1.DEFAULT_ORDER_NO_STEP,
|
|
639
|
+
});
|
|
640
|
+
params.orderNo = order.increase();
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
});
|
|
644
|
+
}
|
|
587
645
|
}
|
|
588
646
|
exports.BaseCore = BaseCore;
|
package/build/apiContext.js
CHANGED
|
@@ -53,6 +53,7 @@ exports.createApiContext = createApiContext;
|
|
|
53
53
|
const createContextDefault = (args) => __awaiter(void 0, void 0, void 0, function* () {
|
|
54
54
|
const { knexConfig, isProduction, logger, doAuth, spaceGw, interserviceApiKey, jwtSecret, verifyTokenArgs, dbSchema, cores, gateways, req, res, extendContext, getCoreContext, getGatewayContext, } = args || {};
|
|
55
55
|
const authorization = (req.headers[constants_1.HEADERS.AUTHORIZATION] || '').replace('Bearer ', '');
|
|
56
|
+
const fingerprint = req.headers[constants_1.HEADERS.FINGERPRINT] || '';
|
|
56
57
|
const spaceId = req.headers[constants_1.HEADERS.SPACE_ID];
|
|
57
58
|
const apiKey = req.headers[constants_1.HEADERS.API_KEY];
|
|
58
59
|
const managementApiKey = req.headers[constants_1.HEADERS.MANAGEMENT_API_KEY];
|
|
@@ -83,6 +84,7 @@ const createContextDefault = (args) => __awaiter(void 0, void 0, void 0, functio
|
|
|
83
84
|
languages,
|
|
84
85
|
requestedLang,
|
|
85
86
|
authorization,
|
|
87
|
+
fingerprint,
|
|
86
88
|
accountId,
|
|
87
89
|
roleId,
|
|
88
90
|
userId,
|
package/build/constants.d.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
declare const FIELDS: any;
|
|
2
|
+
declare const ORDER_BYS: {
|
|
3
|
+
ASC: string;
|
|
4
|
+
DESC: string;
|
|
5
|
+
};
|
|
2
6
|
declare const HEADERS: any;
|
|
3
7
|
declare const ID_FIELD: any;
|
|
4
|
-
|
|
8
|
+
declare const DEFAULT_ORDER_NO = "10000000";
|
|
9
|
+
declare const DEFAULT_ORDER_NO_STEP = "1000";
|
|
10
|
+
export { ID_FIELD, FIELDS, ORDER_BYS, HEADERS, DEFAULT_ORDER_NO, DEFAULT_ORDER_NO_STEP };
|
package/build/constants.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.HEADERS = exports.FIELDS = exports.ID_FIELD = void 0;
|
|
3
|
+
exports.DEFAULT_ORDER_NO_STEP = exports.DEFAULT_ORDER_NO = exports.HEADERS = exports.ORDER_BYS = exports.FIELDS = exports.ID_FIELD = void 0;
|
|
4
4
|
const FIELDS = {
|
|
5
5
|
ID: 'id',
|
|
6
6
|
STATUS: 'status',
|
|
@@ -25,8 +25,14 @@ const FIELDS = {
|
|
|
25
25
|
PHONE: 'phone',
|
|
26
26
|
};
|
|
27
27
|
exports.FIELDS = FIELDS;
|
|
28
|
+
const ORDER_BYS = {
|
|
29
|
+
ASC: 'asc',
|
|
30
|
+
DESC: 'desc',
|
|
31
|
+
};
|
|
32
|
+
exports.ORDER_BYS = ORDER_BYS;
|
|
28
33
|
const HEADERS = {
|
|
29
34
|
AUTHORIZATION: 'authorization',
|
|
35
|
+
FINGERPRINT: 'x-fingerprint',
|
|
30
36
|
SPACE_ID: 'x-space-id',
|
|
31
37
|
API_KEY: 'x-api-key',
|
|
32
38
|
MANAGEMENT_API_KEY: 'x-management-api-key',
|
|
@@ -42,3 +48,7 @@ const HEADERS = {
|
|
|
42
48
|
exports.HEADERS = HEADERS;
|
|
43
49
|
const ID_FIELD = FIELDS.ID;
|
|
44
50
|
exports.ID_FIELD = ID_FIELD;
|
|
51
|
+
const DEFAULT_ORDER_NO = '10000000';
|
|
52
|
+
exports.DEFAULT_ORDER_NO = DEFAULT_ORDER_NO;
|
|
53
|
+
const DEFAULT_ORDER_NO_STEP = '1000';
|
|
54
|
+
exports.DEFAULT_ORDER_NO_STEP = DEFAULT_ORDER_NO_STEP;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface ExtractCredentialsArgs {
|
|
2
|
+
/**
|
|
3
|
+
* Req is a request object of the Express library
|
|
4
|
+
*/
|
|
5
|
+
req: any;
|
|
6
|
+
/**
|
|
7
|
+
* Space is an object with configuration where to get data from
|
|
8
|
+
*/
|
|
9
|
+
space: any;
|
|
10
|
+
/**
|
|
11
|
+
* Authorization is a token from the Authorization header
|
|
12
|
+
*/
|
|
13
|
+
authorization?: string;
|
|
14
|
+
}
|
|
@@ -22,3 +22,4 @@ export { BaseCoreDoingWhatInterface } from './BaseCoreDoingWhatInterface';
|
|
|
22
22
|
export { JwtTokenSecretAndSettings } from './JwtTokenSecretAndSettings';
|
|
23
23
|
export { CommonValidatorArgs } from './CommonValidatorArgs';
|
|
24
24
|
export { CreateUserTokenResult } from './CreateUserTokenResult';
|
|
25
|
+
export { ExtractCredentialsArgs } from './ExtractCredentialsArgs';
|
package/build/userToken.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { OpResult } from '@sdflc/api-helpers';
|
|
2
2
|
import { UserTokenParams, CreateUserTokenArgs, VerifyUserTokenArgs } from './interfaces';
|
|
3
3
|
export declare const setDefaultJwtAlgorithm: (algorithm: string) => void;
|
|
4
|
+
export declare const getDefaultJwtAlgorithm: () => string;
|
|
5
|
+
export declare const setDefaultJwtIssuer: (issuer: string) => void;
|
|
6
|
+
export declare const getDefaultJwtIssuer: () => string;
|
|
4
7
|
export declare const createUserToken: (params: UserTokenParams, secret: string, opt: CreateUserTokenArgs) => any;
|
|
5
8
|
export declare const verifyUserToken: (token: string, secret: string, args?: VerifyUserTokenArgs) => OpResult;
|
package/build/userToken.js
CHANGED
|
@@ -3,33 +3,49 @@ 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 = exports.setDefaultJwtAlgorithm = void 0;
|
|
6
|
+
exports.verifyUserToken = exports.createUserToken = exports.getDefaultJwtIssuer = exports.setDefaultJwtIssuer = exports.getDefaultJwtAlgorithm = exports.setDefaultJwtAlgorithm = void 0;
|
|
7
7
|
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
8
8
|
const uuid_1 = require("uuid");
|
|
9
9
|
const api_helpers_1 = require("@sdflc/api-helpers");
|
|
10
10
|
let defaultJwtAlgorithm = 'HS256';
|
|
11
|
+
let defaultIssuer = 'microservices-auth';
|
|
11
12
|
const setDefaultJwtAlgorithm = (algorithm) => {
|
|
12
13
|
defaultJwtAlgorithm = algorithm;
|
|
13
14
|
};
|
|
14
15
|
exports.setDefaultJwtAlgorithm = setDefaultJwtAlgorithm;
|
|
16
|
+
const getDefaultJwtAlgorithm = () => {
|
|
17
|
+
return defaultJwtAlgorithm;
|
|
18
|
+
};
|
|
19
|
+
exports.getDefaultJwtAlgorithm = getDefaultJwtAlgorithm;
|
|
20
|
+
const setDefaultJwtIssuer = (issuer) => {
|
|
21
|
+
defaultIssuer = issuer;
|
|
22
|
+
};
|
|
23
|
+
exports.setDefaultJwtIssuer = setDefaultJwtIssuer;
|
|
24
|
+
const getDefaultJwtIssuer = () => {
|
|
25
|
+
return defaultIssuer;
|
|
26
|
+
};
|
|
27
|
+
exports.getDefaultJwtIssuer = getDefaultJwtIssuer;
|
|
15
28
|
const createUserToken = (params, secret, opt) => {
|
|
16
|
-
const { algorithm, audience, issuer, jwtid,
|
|
29
|
+
const { algorithm, audience, issuer, jwtid, noTimestamp, header, keyid, expiresIn, useFingerprint } = opt || {};
|
|
17
30
|
if (!params || !secret) {
|
|
18
31
|
return api_helpers_1.OpResult.fail(api_helpers_1.OP_RESULT_CODES.VALIDATION_FAILED, null, 'Failed to create user JWT');
|
|
19
32
|
}
|
|
20
|
-
|
|
21
|
-
|
|
33
|
+
if (useFingerprint) {
|
|
34
|
+
const fingerprint = (0, uuid_1.v4)();
|
|
35
|
+
params['fingerprint'] = fingerprint;
|
|
36
|
+
}
|
|
37
|
+
const token = jsonwebtoken_1.default.sign(params, secret, {
|
|
22
38
|
algorithm: algorithm || defaultJwtAlgorithm,
|
|
23
39
|
audience,
|
|
24
|
-
issuer,
|
|
40
|
+
issuer: issuer || defaultIssuer,
|
|
25
41
|
jwtid,
|
|
26
|
-
subject,
|
|
27
|
-
noTimestamp,
|
|
42
|
+
subject: params.userId,
|
|
43
|
+
noTimestamp: noTimestamp !== null && noTimestamp !== void 0 ? noTimestamp : false,
|
|
28
44
|
header,
|
|
29
45
|
keyid,
|
|
30
46
|
expiresIn: expiresIn && !isNaN(expiresIn) ? expiresIn : undefined,
|
|
31
47
|
});
|
|
32
|
-
return api_helpers_1.OpResult.ok({ token, fingerprint });
|
|
48
|
+
return api_helpers_1.OpResult.ok({ token, fingerprint: params.fingerprint });
|
|
33
49
|
};
|
|
34
50
|
exports.createUserToken = createUserToken;
|
|
35
51
|
const verifyUserToken = (token, secret, args) => {
|
|
@@ -41,7 +57,7 @@ const verifyUserToken = (token, secret, args) => {
|
|
|
41
57
|
const obj = jsonwebtoken_1.default.verify(token, secret, {
|
|
42
58
|
algorithms: algorithms || [defaultJwtAlgorithm],
|
|
43
59
|
audience,
|
|
44
|
-
issuer,
|
|
60
|
+
issuer: issuer || defaultIssuer,
|
|
45
61
|
ignoreExpiration,
|
|
46
62
|
ignoreNotBefore,
|
|
47
63
|
subject,
|
package/package.json
CHANGED