@felloh-org/lambda-wrapper 1.1.45 → 1.1.46

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 = {
@@ -0,0 +1,137 @@
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
+ * Check user role and organisation access
68
+ * @param organisationID
69
+ * @param roles
70
+ * @returns {Promise<boolean>}
71
+ */
72
+
73
+
74
+ async checkUserRoles(organisationID = null, roles = []) {
75
+ await this.initialise(); // If the organisation is not null, then check the user organisation permissions
76
+
77
+ if (organisationID !== null) {
78
+ // Fetch the users organisations that they have access to
79
+ const organisations = await this.fetchUserOrganisations(this.user); // Loop through the organisations and try to find
80
+
81
+ let organisationFound = false;
82
+
83
+ for (const organisationEntity of organisations) {
84
+ if (organisationEntity.id === organisationID) {
85
+ organisationFound = true;
86
+ }
87
+ } // Throw an error if the user does not have the organisation
88
+
89
+
90
+ if (organisationFound === false) {
91
+ const notFoundResponse = new _response.default(null, 401);
92
+ notFoundResponse.addError({
93
+ title: 'Unauthorized',
94
+ message: 'You do not have sufficient privileges to access data for this organisation',
95
+ documentation_url: 'https://developers.felloh.com/errors#error-responses',
96
+ type: 'unauthorized',
97
+ code: 'unauthorized.organisation'
98
+ });
99
+ throw notFoundResponse;
100
+ }
101
+ } // If the roles are not null, then check the users roles
102
+
103
+
104
+ if (roles.length >= 1) {
105
+ // Fetch the users assigned roles
106
+ const userRoles = await this.userRoleRepository.createQueryBuilder('user_roles').leftJoinAndSelect('user_roles.role', 'role').where('user_id = :userID', {
107
+ userID: this.user.id
108
+ }).getMany(); // Loop through the roles and determine match count
109
+
110
+ let roleCount = 0;
111
+
112
+ for (const userRole of userRoles) {
113
+ if (roles.indexOf(userRole.role.machine_name) !== -1) {
114
+ roleCount += 1;
115
+ }
116
+ } // Throw an error if the user does not have all the roles
117
+
118
+
119
+ if (roleCount !== roles.length) {
120
+ const notFoundResponse = new _response.default(null, 401);
121
+ notFoundResponse.addError({
122
+ title: 'Unauthorized',
123
+ message: 'You do not have sufficient privileges to be able to facilitate this request',
124
+ documentation_url: 'https://developers.felloh.com/errors#error-responses',
125
+ type: 'unauthorized',
126
+ code: 'unauthorized.roles'
127
+ });
128
+ throw notFoundResponse;
129
+ }
130
+ }
131
+
132
+ return true;
133
+ }
134
+
135
+ }
136
+
137
+ exports.default = AuthenticationService;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@felloh-org/lambda-wrapper",
3
- "version": "1.1.45",
3
+ "version": "1.1.46",
4
4
  "description": "Lambda wrapper for all Felloh Serverless Projects",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {