@sdflc/backend-helpers 1.0.55 → 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.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.config;
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 (context.authorization && jwtSecret) {
561
- const verifyResult = (0, userToken_1.verifyUserToken)(context.authorization, jwtSecret, verifyTokenArgs);
563
+ if (token && jwtSecret) {
564
+ const verifyResult = (0, userToken_1.verifyUserToken)(token, jwtSecret, verifyTokenArgs);
562
565
  if (verifyResult.didFail()) {
563
566
  return [false, verifyResult.getErrorSummary('')];
564
567
  }
565
- const token = verifyResult.getDataFirst();
566
- context.accountId = token.accountId;
567
- context.roleId = token.roleId;
568
- context.userId = token.userId;
569
- context.spaceId = token.spaceId || context.spaceId;
570
- context.lang = token.lang || context.requestedLang;
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) {
@@ -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,
@@ -8,4 +8,5 @@ export interface CreateUserTokenArgs {
8
8
  header?: any;
9
9
  keyid?: any;
10
10
  expiresIn?: number;
11
+ useFingerprint?: boolean;
11
12
  }
@@ -0,0 +1,4 @@
1
+ export interface CreateUserTokenResult {
2
+ token: string;
3
+ fingerprint: string;
4
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -9,4 +9,5 @@ export interface VerifyUserTokenArgs {
9
9
  maxAge?: number;
10
10
  clockTimestamp?: number;
11
11
  nonce?: string;
12
+ fingreprint?: string;
12
13
  }
@@ -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';
@@ -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) => OpResult;
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;
@@ -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 token = jsonwebtoken_1.default.sign(params, secret, {
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, fingreprint, } = 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 != fingreprint) {
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sdflc/backend-helpers",
3
- "version": "1.0.55",
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",