@felloh-org/lambda-wrapper 1.1.45 → 1.1.48

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"));
@@ -13,15 +15,19 @@ var _logger = _interopRequireDefault(require("../service/logger"));
13
15
 
14
16
  var _request = _interopRequireDefault(require("../service/request"));
15
17
 
18
+ var _user = _interopRequireDefault(require("../service/user"));
19
+
16
20
  var _warehouse = _interopRequireDefault(require("../service/warehouse"));
17
21
 
18
22
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
23
 
20
24
  const DEFINITIONS = {
25
+ AUTHENTICATION: 'AUTHENTICATION',
21
26
  EVENT_BRIDGE: 'EVENT_BRIDGE',
22
27
  HTTP: 'HTTP',
23
28
  LOGGER: 'LOGGER',
24
29
  REQUEST: 'REQUEST',
30
+ USER: 'USER',
25
31
  WAREHOUSE: 'WAREHOUSE'
26
32
  };
27
33
  exports.DEFINITIONS = DEFINITIONS;
@@ -30,7 +36,9 @@ const Dependencies = {
30
36
  [DEFINITIONS.HTTP]: _http.default,
31
37
  [DEFINITIONS.LOGGER]: _logger.default,
32
38
  [DEFINITIONS.REQUEST]: _request.default,
33
- [DEFINITIONS.WAREHOUSE]: _warehouse.default
39
+ [DEFINITIONS.WAREHOUSE]: _warehouse.default,
40
+ [DEFINITIONS.AUTHENTICATION]: _authentication.default,
41
+ [DEFINITIONS.USER]: _user.default
34
42
  };
35
43
  exports.Dependencies = Dependencies;
36
44
  var _default = {
@@ -0,0 +1,161 @@
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 = await di.get(_dependencies.DEFINITIONS.USER).getUser();
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 = null) {
74
+ await this.initialise();
75
+ const roles = await this.userRoleRepository.createQueryBuilder('user_roles').leftJoinAndSelect('user_roles.role', 'role').where('user_id = :userID', {
76
+ userID: userID !== null ? userID : this.user.id
77
+ }).getMany();
78
+ return roles.map(role => role.role.machine_name);
79
+ }
80
+ /**
81
+ * Check to see if user has a role
82
+ * @param role
83
+ * @param userID
84
+ * @returns {Promise<boolean>}
85
+ */
86
+
87
+
88
+ async hasRole(role, userID = null) {
89
+ const roles = await this.fetchUserRoles(userID);
90
+ return roles.indexOf(role) !== -1;
91
+ }
92
+ /**
93
+ * Check user role and organisation access
94
+ * @param organisationID
95
+ * @param roles
96
+ * @returns {Promise<boolean>}
97
+ */
98
+
99
+
100
+ async checkUserRoles(organisationID = null, roles = []) {
101
+ await this.initialise(); // If the organisation is not null, then check the user organisation permissions
102
+
103
+ if (organisationID !== null) {
104
+ // Fetch the users organisations that they have access to
105
+ const organisations = await this.fetchUserOrganisations(this.user); // Loop through the organisations and try to find
106
+
107
+ let organisationFound = false;
108
+
109
+ for (const organisationEntity of organisations) {
110
+ if (organisationEntity.id === organisationID) {
111
+ organisationFound = true;
112
+ }
113
+ } // Throw an error if the user does not have the organisation
114
+
115
+
116
+ if (organisationFound === false) {
117
+ const notFoundResponse = new _response.default(null, 401);
118
+ notFoundResponse.addError({
119
+ title: 'Unauthorized',
120
+ message: 'You do not have sufficient privileges to access data for this organisation',
121
+ documentation_url: 'https://developers.felloh.com/errors#error-responses',
122
+ type: 'unauthorized',
123
+ code: 'unauthorized.organisation'
124
+ });
125
+ throw notFoundResponse;
126
+ }
127
+ } // If the roles are not null, then check the users roles
128
+
129
+
130
+ if (roles.length >= 1) {
131
+ // Fetch the users assigned roles
132
+ const userRoles = await this.fetchUserRoles(this.user.id); // Loop through the roles and determine match count
133
+
134
+ let roleCount = 0;
135
+
136
+ for (const userRole of userRoles) {
137
+ if (roles.indexOf(userRole) !== -1) {
138
+ roleCount += 1;
139
+ }
140
+ } // Throw an error if the user does not have all the roles
141
+
142
+
143
+ if (roleCount !== roles.length) {
144
+ const notFoundResponse = new _response.default(null, 401);
145
+ notFoundResponse.addError({
146
+ title: 'Unauthorized',
147
+ message: 'You do not have sufficient privileges to be able to facilitate this request',
148
+ documentation_url: 'https://developers.felloh.com/errors#error-responses',
149
+ type: 'unauthorized',
150
+ code: 'unauthorized.roles'
151
+ });
152
+ throw notFoundResponse;
153
+ }
154
+ }
155
+
156
+ return true;
157
+ }
158
+
159
+ }
160
+
161
+ exports.default = AuthenticationService;
@@ -0,0 +1,59 @@
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 _user = _interopRequireDefault(require("../entity/user/user"));
13
+
14
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
+
16
+ // eslint-disable-next-line import/no-named-as-default,import/no-named-as-default-member
17
+
18
+ /**
19
+ * User Service class
20
+ */
21
+ class User extends _dependencyAware.default {
22
+ constructor(di) {
23
+ super(di);
24
+ this.initialised = false;
25
+ const that = this;
26
+
27
+ this.initialise = async () => {
28
+ if (that.initialised === false) {
29
+ const warehouse = di.get(_dependencies.DEFINITIONS.WAREHOUSE);
30
+ that.connection = await warehouse.connect();
31
+ this.initialised = true;
32
+ this.userRepository = await that.connection.getRepository(_user.default);
33
+ }
34
+ };
35
+ }
36
+ /**
37
+ * Fetch
38
+ * @returns {Promise<*>}
39
+ */
40
+
41
+
42
+ async getUser() {
43
+ await this.initialise();
44
+
45
+ if (typeof process.env.IS_OFFLINE !== 'undefined') {
46
+ const sub = this.di.getEvent().requestContext.authorizer.claims.sub;
47
+ return this.userRepository.findOne({
48
+ cognito_sub: sub
49
+ }, {
50
+ relations: ['organisations']
51
+ });
52
+ }
53
+
54
+ return this.di.getEvent().requestContext.authorizer.user;
55
+ }
56
+
57
+ }
58
+
59
+ exports.default = User;
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.48",
4
4
  "description": "Lambda wrapper for all Felloh Serverless Projects",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {