@sdflc/backend-helpers 1.0.57 → 1.0.58

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.
@@ -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,10 @@ 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[]>;
57
61
  }
58
62
  export { BaseCore };
package/build/BaseCore.js CHANGED
@@ -546,19 +546,30 @@ class BaseCore {
546
546
  return [true, space];
547
547
  });
548
548
  }
549
+ extractCredentials(args) {
550
+ var _a, _b;
551
+ return __awaiter(this, void 0, void 0, function* () {
552
+ const { req, space, authorization } = args;
553
+ const token = ((_a = req.cookies) === null || _a === void 0 ? void 0 : _a[space.cookieName]) || authorization;
554
+ const fingerprint = ((_b = req.cookies) === null || _b === void 0 ? void 0 : _b[space.fingerprintCookieName]) || '';
555
+ return {
556
+ token,
557
+ fingerprint,
558
+ };
559
+ });
560
+ }
549
561
  // eslint-disable-next-line
550
562
  authenticate(params, opt) {
551
- var _a, _b;
552
563
  return __awaiter(this, void 0, void 0, function* () {
553
564
  //const { accessRights } = params || {};
554
- const { context, req } = this.getContext();
565
+ const context = this.getContext();
566
+ const { req, authorization } = context;
555
567
  let authByToken = false;
556
568
  const [spaceResult, space] = yield this.findSpace();
557
569
  if (spaceResult !== true) {
558
570
  return spaceResult;
559
571
  }
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]) || '';
572
+ const { token, fingerprint } = yield this.extractCredentials({ req, authorization, space });
562
573
  const { jwtSecret, verifyTokenArgs } = yield this.getJwtTokenSecretAndSettings(params);
563
574
  if (token && jwtSecret) {
564
575
  const verifyResult = (0, userToken_1.verifyUserToken)(token, jwtSecret, Object.assign(Object.assign({}, verifyTokenArgs), { fingerprint }));
@@ -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
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sdflc/backend-helpers",
3
- "version": "1.0.57",
3
+ "version": "1.0.58",
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",