-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthControllerV3.js
More file actions
355 lines (315 loc) · 13.2 KB
/
authControllerV3.js
File metadata and controls
355 lines (315 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
'use strict';
//
// authControllerV3.js
// Handle security and authorization checks
// Tapis V3
//
// VDJServer Analysis Portal
// VDJ Web API service
// https://vdjserver.org
//
// Copyright (C) 2020 The University of Texas Southwestern Medical Center
//
// Author: Scott Christley <scott.christley@utsouthwestern.edu>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
var AuthController = {};
module.exports = AuthController;
// Processing
var tapisSettings = require('./tapisSettings');
var tapisIO = tapisSettings.get_default_tapis();
var config = tapisSettings.config;
var ServiceAccount = tapisIO.serviceAccount;
var webhookIO = require('./webhookIO');
// Extract token from header
AuthController.extractToken = function(req) {
const context = 'AuthController.extractToken';
//config.log.info(context, req['headers']);
// extract the token from the authorization header
if (! req['headers']['authorization']) {
let msg = 'missing authorization header';
msg = config.log.error(context, msg);
webhookIO.postToSlack(msg);
return false;
}
var fields = req['headers']['authorization'].split(' ');
if (fields.length != 2) {
let msg = 'invalid authorization header: ' + req['headers']['authorization'];
msg = config.log.error(context, msg);
webhookIO.postToSlack(msg);
return false;
}
if (fields[0].toLowerCase() != 'bearer') {
let msg = 'invalid authorization header: ' + req['headers']['authorization'];
msg = config.log.error(context, msg);
webhookIO.postToSlack(msg);
return false;
}
return fields[1];
}
//
// Security handlers, these are called by the openapi
// middleware. Return true if authentication is valid,
// otherwise return false. The middleware will throw
// a generic 401 error, which the errorMiddleware returns
// to the client
//
// Verify a Tapis token
// Sets the associated user profile for the token in req.user
AuthController.userAuthorization = function(req, scopes, definition) {
const context = 'AuthController.userAuthorization';
//config.log.info(context, 'start');
var token = AuthController.extractToken(req);
if (!token) return false;
// get my profile and username from the token
// return a promise
return tapisIO.getTapisUserProfile(token, 'me')
.then(function(userProfile) {
//config.log.info(context, JSON.stringify(userProfile));
// save the user profile
req['user'] = userProfile['result'];
// service account does not need the verification record
if (req['user']['username'] == tapisSettings.serviceAccountKey) return true;
// TODO: user verification needs to be re-worked
return true;
// // now check that the user account has been verified
// return tapisIO.getUserVerificationMetadata(req['user']['username'])
// .then(function(userVerificationMetadata) {
// if (userVerificationMetadata && userVerificationMetadata[0] && userVerificationMetadata[0].value.isVerified === true) {
// // valid
// return true;
// }
// else {
// var msg = 'access by unverified user: ' + req['user']['username'];
// msg = config.log.error(context, msg);
// webhookIO.postToSlack(msg);
// return false;
// }
// });
})
.catch(function(error) {
var msg = 'invalid token: ' + token + ', error: ' + error;
msg = config.log.error(context, msg);
webhookIO.postToSlack(msg);
return false;
});
}
// Requires the user account to have admin privileges.
// Currently, only the service account has that.
AuthController.adminAuthorization = function(req, scopes, definition) {
const context = 'AuthController.adminAuthorization';
//config.log.info(context, 'start');
var token = AuthController.extractToken(req);
if (!token) return false;
// get my profile and username from the token
// return a promise
return tapisIO.getTapisUserProfile(token, 'me')
.then(function(userProfile) {
// save the user profile
req['user'] = userProfile['result'];
if (req['user']['username'] == tapisSettings.serviceAccountKey) {
// valid
config.log.info(context, 'admin access by service account: ' + req['user']['username']
+ ', route: ' + JSON.stringify(req.route.path), true);
return true;
}
else if (tapisSettings.adminAccountKeys && tapisSettings.adminAccountKeys.length > 0
&& tapisSettings.adminAccountKeys.indexOf(req['user']['username']) >= 0) {
// valid
config.log.info(context, 'admin access by authorized user: ' + req['user']['username']
+ ', route: ' + JSON.stringify(req.route.path), true);
return true;
} else {
var msg = 'access by unauthorized user: ' + req['user']['username']
+ ', route: ' + JSON.stringify(req.route.path);
msg = config.log.error(context, msg);
webhookIO.postToSlack(msg);
return false;
}
})
.catch(function(error) {
var msg = 'invalid token: ' + token + ', error: ' + error;
msg = config.log.error(context, msg);
webhookIO.postToSlack(msg);
return false;
});
}
// Verify a user has access to project
AuthController.projectAuthorization = function(req, scopes, definition) {
const context = 'AuthController.projectAuthorization';
//config.log.info(context, 'start');
var token = AuthController.extractToken(req);
if (!token) return false;
// check body and params for project uuid
var project_uuid;
if (req.body) project_uuid = req.body.project_uuid;
if (project_uuid == undefined)
if (req.params) project_uuid = req.params.project_uuid;
if (project_uuid == undefined) {
var msg = 'missing project uuid, route ' + JSON.stringify(req.route.path);
msg = config.log.error(context, msg);
webhookIO.postToSlack(msg);
return false;
}
// verify the user token
// return a promise
return AuthController.userAuthorization(req, scopes, definition)
.then(function(result) {
if (!result) return result;
// verify the user has access to project
return tapisIO.getAllProjectMetadata(req['user']['username'], project_uuid);
})
.then(function(projectMetadata) {
//config.log.info(context, JSON.stringify(projectMetadata));
// make sure its project metadata and not some random uuid
// TODO: should disallow old VDJServer V1 projects at some point
if (projectMetadata && (projectMetadata.length == 1) && ((projectMetadata[0].name == 'private_project') || (projectMetadata[0].name == 'public_project') || (projectMetadata[0].name == 'project') || (projectMetadata[0].name == 'archived_project'))) {
// save the project metadata
req['project_metadata'] = projectMetadata[0];
return true;
}
else {
return Promise.reject(new Error('invalid project metadata'));
}
})
.catch(function(error) {
var msg = 'project: ' + project_uuid + ', route: '
+ JSON.stringify(req.route.path) + ', error: ' + error;
msg = config.log.error(context, msg);
webhookIO.postToSlack(msg);
return false;
});
}
// Write access to project
AuthController.projectWriteAuthorization = async function(req, scopes, definition) {
const context = 'AuthController.projectWriteAuthorization';
// call default permission check
var result = await AuthController.projectAuthorization(req, scopes, definition);
if (!result) return result;
// archived_project and public_project should be excluded.
if (req['project_metadata']['name'] == 'archived_project') {
let msg = 'Archived projects cannot be modified. Unarchive the project first.';
msg = config.log.error(context, msg);
webhookIO.postToSlack(msg);
return false;
}
if (req['project_metadata']['name'] == 'public_project') {
let msg = 'Published (public) projects cannot be modified. Unpublish the project first.';
msg = config.log.error(context, msg);
webhookIO.postToSlack(msg);
return false;
}
// only private_project can be written
if (req['project_metadata']['name'] != 'private_project') {
let msg = 'Unknown project type: ' + req['project_metadata']['name'];
msg = config.log.error(context, msg);
webhookIO.postToSlack(msg);
return false;
}
// otherwise all good
return true;
}
// Unarchive access to project, as an archived_project does not allow write access
AuthController.projectUnarchiveAuthorization = async function(req, scopes, definition) {
const context = 'AuthController.projectUnarchiveAuthorization';
// call default permission check
var result = await AuthController.projectAuthorization(req, scopes, definition);
if (!result) return result;
// only archived_project
if (req['project_metadata']['name'] == 'archived_project') {
// all good, any project user can unarchive
return true;
} else {
let msg = 'Unable to unarchive an invalid project type: ' + req['project_metadata']['name'];
msg = config.log.error(context, msg);
webhookIO.postToSlack(msg);
return false;
}
}
// Unpublish access to project, as a public_project does not allow write access
AuthController.projectUnpublishAuthorization = async function(req, scopes, definition) {
const context = 'AuthController.projectUnpublishAuthorization';
// call default permission check
var result = await AuthController.projectAuthorization(req, scopes, definition);
if (!result) return result;
// only public_project
if (req['project_metadata']['name'] == 'public_project') {
// all good, any project user can unpublish
return true;
} else {
let msg = 'Unable to unpublish an invalid project type: ' + req['project_metadata']['name'];
msg = config.log.error(context, msg);
webhookIO.postToSlack(msg);
return false;
}
}
//
// verify a valid and active username account
//
AuthController.verifyUser = function(username) {
const context = 'AuthController.verifyUser';
if (username == undefined) return false;
if (!username) return false;
// return a promise
return tapisIO.getUserProfile(username)
.then(function(userProfileMetadata) {
// just check user profile
if (userProfileMetadata && userProfileMetadata[0])
return true;
else
return false;
// if (userVerificationMetadata && userVerificationMetadata[0] && userVerificationMetadata[0].value.isVerified === true) {
// // valid
// return true;
// }
// else {
// return false;
// }
})
.catch(function(error) {
var msg = 'error validating user: ' + username + ', error ' + error;
msg = config.log.error(context, msg);
webhookIO.postToSlack(msg);
return false;
})
;
}
/* TODO: is this needed?
//
// verify user has access to metadata entry
//
AuthController.verifyMetadataAccess = function(uuid, accessToken, username) {
const context = 'AuthController.verifyMetadataAccess';
if (uuid == undefined) return false;
if (accessToken == undefined) return false;
if (username == undefined) return false;
return tapisIO.getMetadataPermissionsForUser(accessToken, uuid, username)
.then(function(metadataPermissions) {
// we can read the metadata, but do we have write permission?
if (metadataPermissions && metadataPermissions.permission.write)
return true;
else {
return false;
}
})
.catch(function(error) {
var msg = 'uuid: ' + uuid
+ ', error validating user: ' + username + ', error ' + error;
msg = AuthController.config.log.error(context, msg);
webhookIO.postToSlack(msg);
return false;
});
}
*/