@sdflc/backend-helpers 1.0.55 → 1.0.57
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.js +12 -9
- package/build/apiContext.js +5 -3
- 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 +1 -1
- package/build/userToken.js +9 -4
- package/package.json +1 -1
package/build/BaseCore.js
CHANGED
|
@@ -548,26 +548,29 @@ class BaseCore {
|
|
|
548
548
|
}
|
|
549
549
|
// eslint-disable-next-line
|
|
550
550
|
authenticate(params, opt) {
|
|
551
|
+
var _a, _b;
|
|
551
552
|
return __awaiter(this, void 0, void 0, function* () {
|
|
552
553
|
//const { accessRights } = params || {};
|
|
553
|
-
const { context } = this.
|
|
554
|
+
const { context, req } = this.getContext();
|
|
554
555
|
let authByToken = false;
|
|
555
556
|
const [spaceResult, space] = yield this.findSpace();
|
|
556
557
|
if (spaceResult !== true) {
|
|
557
558
|
return spaceResult;
|
|
558
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]) || '';
|
|
559
562
|
const { jwtSecret, verifyTokenArgs } = yield this.getJwtTokenSecretAndSettings(params);
|
|
560
|
-
if (
|
|
561
|
-
const verifyResult = (0, userToken_1.verifyUserToken)(
|
|
563
|
+
if (token && jwtSecret) {
|
|
564
|
+
const verifyResult = (0, userToken_1.verifyUserToken)(token, jwtSecret, Object.assign(Object.assign({}, verifyTokenArgs), { fingerprint }));
|
|
562
565
|
if (verifyResult.didFail()) {
|
|
563
566
|
return [false, verifyResult.getErrorSummary('')];
|
|
564
567
|
}
|
|
565
|
-
const
|
|
566
|
-
context.accountId =
|
|
567
|
-
context.roleId =
|
|
568
|
-
context.userId =
|
|
569
|
-
context.spaceId =
|
|
570
|
-
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;
|
|
571
574
|
authByToken = true;
|
|
572
575
|
}
|
|
573
576
|
if (!authByToken) {
|
package/build/apiContext.js
CHANGED
|
@@ -52,9 +52,7 @@ exports.createApiContext = createApiContext;
|
|
|
52
52
|
*/
|
|
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
|
-
const authorization = req.headers[constants_1.HEADERS.AUTHORIZATION]
|
|
56
|
-
? req.headers[constants_1.HEADERS.AUTHORIZATION].replace('Bearer ', '')
|
|
57
|
-
: undefined;
|
|
55
|
+
const authorization = (req.headers[constants_1.HEADERS.AUTHORIZATION] || '').replace('Bearer ', '');
|
|
58
56
|
const spaceId = req.headers[constants_1.HEADERS.SPACE_ID];
|
|
59
57
|
const apiKey = req.headers[constants_1.HEADERS.API_KEY];
|
|
60
58
|
const managementApiKey = req.headers[constants_1.HEADERS.MANAGEMENT_API_KEY];
|
|
@@ -67,6 +65,8 @@ const createContextDefault = (args) => __awaiter(void 0, void 0, void 0, functio
|
|
|
67
65
|
const acceptLanguage = req.headers[constants_1.HEADERS.ACCEPT_LANGUAGE];
|
|
68
66
|
const languages = (0, utils_1.extractLanguages)(acceptLanguage);
|
|
69
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 };
|
|
70
70
|
const requestId = (0, uuid_1.v4)();
|
|
71
71
|
const db = new Database_1.Database({
|
|
72
72
|
knexConfig,
|
|
@@ -88,6 +88,8 @@ const createContextDefault = (args) => __awaiter(void 0, void 0, void 0, functio
|
|
|
88
88
|
userId,
|
|
89
89
|
req,
|
|
90
90
|
res,
|
|
91
|
+
headers,
|
|
92
|
+
cookies,
|
|
91
93
|
};
|
|
92
94
|
return yield (0, exports.createApiContext)({
|
|
93
95
|
cores,
|
|
@@ -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,5 +1,5 @@
|
|
|
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 createUserToken: (params: UserTokenParams, secret: string, opt: CreateUserTokenArgs) =>
|
|
4
|
+
export declare const createUserToken: (params: UserTokenParams, secret: string, opt: CreateUserTokenArgs) => any;
|
|
5
5
|
export declare const verifyUserToken: (token: string, secret: string, args?: VerifyUserTokenArgs) => OpResult;
|
package/build/userToken.js
CHANGED
|
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
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");
|
|
9
10
|
let defaultJwtAlgorithm = 'HS256';
|
|
10
11
|
const setDefaultJwtAlgorithm = (algorithm) => {
|
|
@@ -12,11 +13,12 @@ const setDefaultJwtAlgorithm = (algorithm) => {
|
|
|
12
13
|
};
|
|
13
14
|
exports.setDefaultJwtAlgorithm = setDefaultJwtAlgorithm;
|
|
14
15
|
const createUserToken = (params, secret, opt) => {
|
|
15
|
-
const { algorithm, audience, issuer, jwtid, subject, noTimestamp, header, keyid, expiresIn } = opt || {};
|
|
16
|
+
const { algorithm, audience, issuer, jwtid, subject, noTimestamp, header, keyid, expiresIn, useFingerprint } = opt || {};
|
|
16
17
|
if (!params || !secret) {
|
|
17
18
|
return api_helpers_1.OpResult.fail(api_helpers_1.OP_RESULT_CODES.VALIDATION_FAILED, null, 'Failed to create user JWT');
|
|
18
19
|
}
|
|
19
|
-
const
|
|
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, {
|
|
20
22
|
algorithm: algorithm || defaultJwtAlgorithm,
|
|
21
23
|
audience,
|
|
22
24
|
issuer,
|
|
@@ -27,11 +29,11 @@ const createUserToken = (params, secret, opt) => {
|
|
|
27
29
|
keyid,
|
|
28
30
|
expiresIn: expiresIn && !isNaN(expiresIn) ? expiresIn : undefined,
|
|
29
31
|
});
|
|
30
|
-
return api_helpers_1.OpResult.ok(token);
|
|
32
|
+
return api_helpers_1.OpResult.ok({ token, fingerprint });
|
|
31
33
|
};
|
|
32
34
|
exports.createUserToken = createUserToken;
|
|
33
35
|
const verifyUserToken = (token, secret, args) => {
|
|
34
|
-
const { algorithms, audience, issuer, ignoreExpiration, ignoreNotBefore, subject, clockTolerance, maxAge, clockTimestamp, nonce, } = args || {};
|
|
36
|
+
const { algorithms, audience, issuer, ignoreExpiration, ignoreNotBefore, subject, clockTolerance, maxAge, clockTimestamp, nonce, fingerprint, } = args || {};
|
|
35
37
|
if (!args) {
|
|
36
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');
|
|
37
39
|
}
|
|
@@ -48,6 +50,9 @@ const verifyUserToken = (token, secret, args) => {
|
|
|
48
50
|
clockTimestamp,
|
|
49
51
|
nonce,
|
|
50
52
|
});
|
|
53
|
+
if ((obj === null || obj === void 0 ? void 0 : obj.fingerprint) && obj.fingerprint != fingerprint) {
|
|
54
|
+
return api_helpers_1.OpResult.fail(api_helpers_1.OP_RESULT_CODES.UNAUTHORIZED, null, 'User unauthenticated');
|
|
55
|
+
}
|
|
51
56
|
return api_helpers_1.OpResult.ok(obj);
|
|
52
57
|
}
|
|
53
58
|
catch (ex) {
|
package/package.json
CHANGED