@felloh-org/lambda-wrapper 1.1.44 → 1.1.47

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.
@@ -5,6 +5,8 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = exports.Dependencies = exports.DEFINITIONS = void 0;
7
7
 
8
+ var _authentication = _interopRequireDefault(require("../service/authentication"));
9
+
8
10
  var _eventBridge = _interopRequireDefault(require("../service/event-bridge"));
9
11
 
10
12
  var _http = _interopRequireDefault(require("../service/http"));
@@ -18,6 +20,7 @@ var _warehouse = _interopRequireDefault(require("../service/warehouse"));
18
20
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
21
 
20
22
  const DEFINITIONS = {
23
+ AUTHENTICATION: 'AUTHENTICATION',
21
24
  EVENT_BRIDGE: 'EVENT_BRIDGE',
22
25
  HTTP: 'HTTP',
23
26
  LOGGER: 'LOGGER',
@@ -30,7 +33,8 @@ const Dependencies = {
30
33
  [DEFINITIONS.HTTP]: _http.default,
31
34
  [DEFINITIONS.LOGGER]: _logger.default,
32
35
  [DEFINITIONS.REQUEST]: _request.default,
33
- [DEFINITIONS.WAREHOUSE]: _warehouse.default
36
+ [DEFINITIONS.WAREHOUSE]: _warehouse.default,
37
+ [DEFINITIONS.AUTHENTICATION]: _authentication.default
34
38
  };
35
39
  exports.Dependencies = Dependencies;
36
40
  var _default = {
@@ -6,6 +6,9 @@ module.exports = class paymentProviderPaymentMethod1658483667045 {
6
6
  async up(queryRunner) {
7
7
  await queryRunner.query(`ALTER TABLE "payment"."payment_provider" ADD "payment_method" character varying(20) NOT NULL DEFAULT 'UNKNOWN'`);
8
8
  await queryRunner.query(`ALTER TABLE "payment"."payment_provider" ALTER COLUMN "name" SET NOT NULL`);
9
+ await queryRunner.query(`UPDATE "payment"."payment_provider" SET "payment_method" = 'CARD' WHERE "name" = 'TOTAL_PROCESSING'`);
10
+ await queryRunner.query(`UPDATE "payment"."payment_provider" SET "payment_method" = 'OPEN_BANKING' WHERE "name" = 'NUAPAY'`);
11
+ await queryRunner.query(`UPDATE "payment"."payment_provider" SET "payment_method" = 'CARD' WHERE "name" = 'NUVEI'`);
9
12
  }
10
13
 
11
14
  async down(queryRunner) {
@@ -0,0 +1,149 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _dependencies = require("../config/dependencies");
9
+
10
+ var _dependencyAware = _interopRequireDefault(require("../dependency-injection/dependency-aware"));
11
+
12
+ var _organisation = _interopRequireDefault(require("../entity/user/organisation"));
13
+
14
+ var _userRole = _interopRequireDefault(require("../entity/user/user-role"));
15
+
16
+ var _response = _interopRequireDefault(require("../model/response"));
17
+
18
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
+
20
+ // eslint-disable-next-line import/no-named-as-default,import/no-named-as-default-member
21
+ // eslint-disable-next-line import/no-named-as-default,import/no-named-as-default-member
22
+
23
+ /**
24
+ * Authentication Service class
25
+ */
26
+ class AuthenticationService extends _dependencyAware.default {
27
+ constructor(di) {
28
+ super(di);
29
+ this.initialised = false;
30
+ const that = this;
31
+
32
+ this.initialise = async () => {
33
+ if (that.initialised === false) {
34
+ const warehouse = di.get(_dependencies.DEFINITIONS.WAREHOUSE);
35
+ that.connection = await warehouse.connect();
36
+ this.initialised = true;
37
+ this.userRoleRepository = await that.connection.getRepository(_userRole.default);
38
+ this.organisationRepository = await that.connection.getRepository(_organisation.default);
39
+ this.user = JSON.parse(di.getEvent().requestContext.authorizer.user);
40
+ }
41
+ };
42
+ }
43
+ /**
44
+ * Fetch a users organisations
45
+ * @param user
46
+ * @returns {Promise<*[]>}
47
+ */
48
+
49
+
50
+ async fetchUserOrganisations(user) {
51
+ await this.initialise();
52
+ const response = [];
53
+
54
+ for await (const organisation of user.organisations) {
55
+ const orgs = await this.organisationRepository.findDescendants(organisation, {
56
+ relations: ['parent']
57
+ });
58
+
59
+ for (const org of orgs) {
60
+ response.push(org);
61
+ }
62
+ }
63
+
64
+ return response;
65
+ }
66
+ /**
67
+ * Fetch roles for a user
68
+ * @param userID
69
+ * @returns {Promise<any[]>}
70
+ */
71
+
72
+
73
+ async fetchUserRoles(userID) {
74
+ await this.initialise();
75
+ const roles = await this.userRoleRepository.createQueryBuilder('user_roles').leftJoinAndSelect('user_roles.role', 'role').where('user_id = :userID', {
76
+ userID
77
+ }).getMany();
78
+ return roles.map(role => role.role.machine_name);
79
+ }
80
+ /**
81
+ * Check user role and organisation access
82
+ * @param organisationID
83
+ * @param roles
84
+ * @returns {Promise<boolean>}
85
+ */
86
+
87
+
88
+ async checkUserRoles(organisationID = null, roles = []) {
89
+ await this.initialise(); // If the organisation is not null, then check the user organisation permissions
90
+
91
+ if (organisationID !== null) {
92
+ // Fetch the users organisations that they have access to
93
+ const organisations = await this.fetchUserOrganisations(this.user); // Loop through the organisations and try to find
94
+
95
+ let organisationFound = false;
96
+
97
+ for (const organisationEntity of organisations) {
98
+ if (organisationEntity.id === organisationID) {
99
+ organisationFound = true;
100
+ }
101
+ } // Throw an error if the user does not have the organisation
102
+
103
+
104
+ if (organisationFound === false) {
105
+ const notFoundResponse = new _response.default(null, 401);
106
+ notFoundResponse.addError({
107
+ title: 'Unauthorized',
108
+ message: 'You do not have sufficient privileges to access data for this organisation',
109
+ documentation_url: 'https://developers.felloh.com/errors#error-responses',
110
+ type: 'unauthorized',
111
+ code: 'unauthorized.organisation'
112
+ });
113
+ throw notFoundResponse;
114
+ }
115
+ } // If the roles are not null, then check the users roles
116
+
117
+
118
+ if (roles.length >= 1) {
119
+ // Fetch the users assigned roles
120
+ const userRoles = await this.fetchUserRoles(this.user.id); // Loop through the roles and determine match count
121
+
122
+ let roleCount = 0;
123
+
124
+ for (const userRole of userRoles) {
125
+ if (roles.indexOf(userRole) !== -1) {
126
+ roleCount += 1;
127
+ }
128
+ } // Throw an error if the user does not have all the roles
129
+
130
+
131
+ if (roleCount !== roles.length) {
132
+ const notFoundResponse = new _response.default(null, 401);
133
+ notFoundResponse.addError({
134
+ title: 'Unauthorized',
135
+ message: 'You do not have sufficient privileges to be able to facilitate this request',
136
+ documentation_url: 'https://developers.felloh.com/errors#error-responses',
137
+ type: 'unauthorized',
138
+ code: 'unauthorized.roles'
139
+ });
140
+ throw notFoundResponse;
141
+ }
142
+ }
143
+
144
+ return true;
145
+ }
146
+
147
+ }
148
+
149
+ exports.default = AuthenticationService;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@felloh-org/lambda-wrapper",
3
- "version": "1.1.44",
3
+ "version": "1.1.47",
4
4
  "description": "Lambda wrapper for all Felloh Serverless Projects",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {