-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathusermanager.py
479 lines (382 loc) · 16.3 KB
/
usermanager.py
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
import datetime
import json
import logging
import uuid
import flask
import flask_security
import requests
# from authlib.oauth2.rfc6749 import OAuth2Token
from authlib.integrations.flask_client import OAuth
from flask_login import current_user
from mxcubecore import HardwareRepository as HWR
from mxcubecore.model.lims_session import LimsSessionManager
from mxcubeweb.core.components.component_base import ComponentBase
from mxcubeweb.core.models.usermodels import User
from mxcubeweb.core.util.convertutils import convert_to_dict
from mxcubeweb.core.util.networkutils import (
is_local_host,
remote_addr,
)
class BaseUserManager(ComponentBase):
def __init__(self, app, config):
super().__init__(app, config)
self.oauth_client = OAuth(app=app.server.flask)
self.oauth_client.register(
name="keycloak",
client_id=self.app.CONFIG.sso.CLIENT_ID,
client_secret=self.app.CONFIG.sso.CLIENT_SECRET,
server_metadata_url=self.app.CONFIG.sso.META_DATA_URI,
client_kwargs={
"scope": "openid email profile",
"code_challenge_method": "S256", # enable PKCE
},
)
def get_observers(self):
return [
user
for user in User.query.all()
if ((not user.in_control) and user.is_authenticated and user.is_active)
]
def get_operator(self):
user = None
for _u in User.query.all():
if _u.in_control:
user = _u
break
return user
def is_operator(self):
return getattr(current_user, "in_control", False)
def active_logged_in_users(self, exclude_inhouse=False):
self.update_active_users()
if exclude_inhouse:
users = [
_u.username for _u in User.query.all() if _u.active and not _u.isstaff
]
else:
users = [_u.username for _u in User.query.all() if _u.active]
return users
def get_user(self, username):
user = None
for _u in User.query.all():
if _u.username == username:
user = _u
return user
def set_operator(self, username):
user = None
for _u in User.query.all():
if _u.username == username:
self.db_set_in_control(_u, True)
user = _u
else:
self.db_set_in_control(_u, False)
return user
def update_active_users(self):
for _u in User.query.all():
if (
_u.active
and _u.last_request_timestamp
and (datetime.datetime.now() - _u.last_request_timestamp)
> flask.current_app.permanent_session_lifetime
):
logging.getLogger("HWR.MX3").info(
f"Logged out inactive user {_u.username}"
)
self.app.server.user_datastore.deactivate_user(_u)
self.app.server.emit(
"userChanged", room=_u.socketio_session_id, namespace="/hwr"
)
self.app.server.emit("observersChanged", namespace="/hwr")
def update_operator(self, new_login=False):
active_in_control = False
for _u in User.query.all():
if _u.is_authenticated and _u.in_control:
active_in_control = True
else:
self.db_set_in_control(_u, False)
# If new login and new observer login, clear nickname
# so that the user get an opertunity to set one
if new_login:
current_user.nickname = ""
# If no user is currently in control set this user to be
# in control
if not active_in_control:
if not HWR.beamline.lims.is_user_login_type():
# current_user.nickname = self.app.lims.get_proposal(current_user)
current_user.fullname = HWR.beamline.lims.get_full_user_name()
current_user.nickname = HWR.beamline.lims.get_user_name()
else:
current_user.nickname = current_user.username
self.db_set_in_control(current_user, True)
# Set active proposal to that of the active user
for _u in User.query.all():
if _u.is_authenticated and _u.in_control:
if not HWR.beamline.lims.is_user_login_type():
# In principle there is no need for doing so..
self.app.lims.select_session(
self.app.lims.get_session_manager().active_session.proposal_name
) # The username is the proposal
elif _u.selected_proposal is not None:
self.app.lims.select_session(_u.selected_proposal)
def is_inhouse_user(self, user_id):
user_id_list = [
"%s%s" % (code, number)
for (code, number) in HWR.beamline.session.in_house_users
]
return user_id in user_id_list
# Abstract method to be implemented by concrete implementation
def _login(self, login_id, password) -> LimsSessionManager:
pass
def sso_validate(self) -> str:
try:
token_response = self.oauth_client.keycloak.authorize_access_token()
username = token_response["userinfo"]["preferred_username"]
token = token_response["access_token"]
except Exception as e:
raise e
else:
self.login(username, token, sso_data=token_response)
def sso_token_expired(self) -> bool:
res = json.loads(
requests.post(
self.app.CONFIG.sso.TOKEN_INFO_URI,
headers={"Authorization": "Bearer %s" % current_user.token},
data={
"grant_type": "refresh_token",
"refresh_token": current_user.refresh_token,
},
).json()
)
return "access_token" not in res
def handle_sso_logout(self):
if current_user.is_anonymous:
if self.sso_token_expired():
self.signout()
def login(self, login_id: str, password: str, sso_data: dict = {}):
try:
sessionManager: LimsSessionManager = self._login(login_id, password)
except BaseException as e:
logging.getLogger("MX3.HWR").error(str(e))
raise e
else:
if "sid" not in flask.session:
flask.session["sid"] = str(uuid.uuid4())
# Making sure that the session of any in active users are invalideted
# before calling login
self.update_active_users()
user = self.db_create_user(login_id, password, sessionManager, sso_data)
self.app.server.user_datastore.activate_user(user)
flask_security.login_user(user, remember=False)
# Important to make flask_security user tracking work
self.app.server.security.datastore.commit()
address = self.app.sample_changer.get_loaded_sample()
# If A sample is mounted (and not already marked as such),
# get sample changer contents and add mounted sample to the queue
if not self.app.sample_changer.get_current_sample() and address:
self.app.sample_changer.get_sample_list()
self.update_operator(new_login=True)
msg = "User %s signed in" % user.username
logging.getLogger("MX3.HWR").info(msg)
# Abstract method to be implemented by concrete implementation
def _signout(self):
pass
def signout(self):
self._signout()
user = current_user
# If operator logs out clear queue and sample list
if self.is_operator():
self.app.queue.clear_queue()
HWR.beamline.sample_view.clear_all()
self.app.lims.init_sample_list()
self.app.queue.init_queue_settings()
if hasattr(HWR.beamline.session, "clear_session"):
HWR.beamline.session.clear_session()
self.app.CURRENTLY_MOUNTED_SAMPLE = ""
self.db_set_in_control(current_user, False)
msg = "User %s signed out" % user.username
logging.getLogger("MX3.HWR").info(msg)
self.app.server.user_datastore.deactivate_user(user)
flask_security.logout_user()
self.app.server.emit("observersChanged", namespace="/hwr")
def is_authenticated(self):
return current_user.is_authenticated()
def force_signout_user(self, username):
user = self.get_user(username)
if not user.in_control or current_user.is_anonymous:
socketio_sid = user.socketio_session_id
self.app.server.user_datastore.delete_user(user)
self.app.server.user_datastore.commit()
self.app.server.emit("forceSignout", room=socketio_sid, namespace="/hwr")
def login_info(self):
# update_operator will update the login status of current_user, and make
# sure that the is_anonymous has the correct value.
# Update operator calls lims.select_session that raises an exception if
# there are no valid LIMS sessions.
try:
self.update_operator()
except Exception:
pass
if not current_user.is_anonymous:
session_manager: LimsSessionManager = self.app.lims.get_session_manager()
login_type = (
"User" if HWR.beamline.lims.is_user_login_type() else "Proposal"
)
res = {
"synchrotronName": HWR.beamline.session.synchrotron_name,
"beamlineName": HWR.beamline.session.beamline_name,
"loggedIn": True,
"loginType": login_type,
"limsName": [item.dict() for item in HWR.beamline.lims.get_lims_name()],
"proposalList": [
session.__dict__ for session in session_manager.sessions
],
"rootPath": HWR.beamline.session.get_base_image_directory(),
"user": current_user.todict(),
"useSSO": self.app.CONFIG.sso.USE_SSO,
}
res["selectedProposal"] = "%s%s" % (
HWR.beamline.session.proposal_code,
HWR.beamline.session.proposal_number,
)
res["selectedProposalID"] = HWR.beamline.session.proposal_id
else:
logging.getLogger("MX3.HWR").info("Logged out")
res = {"loggedIn": False, "useSSO": self.app.CONFIG.sso.USE_SSO}
return res
def update_user(self, user):
self.app.server.user_datastore.put(user)
self.app.server.user_datastore.commit()
def _get_configured_roles(self, user):
roles = set()
_ihs = ["%s%s" % prop for prop in HWR.beamline.session.in_house_users]
if self.config.inhouse_is_staff and user in _ihs:
roles.add("staff")
for _u in self.config.users:
if _u.username == user:
roles.add(_u.role)
break
return list(roles)
def db_create_user(
self, user: str, password: str, lims_data: LimsSessionManager, sso_data: dict
):
sid = flask.session["sid"]
user_datastore = self.app.server.user_datastore
username = HWR.beamline.lims.get_user_name()
fullname = HWR.beamline.lims.get_full_user_name()
# if HWR.beamline.lims.loginType.lower() == "user":
# username = f"{user}"
# Make sure that the roles staff and incontrol always
# exists
if not user_datastore.find_role("staff"):
user_datastore.create_role(name="staff")
user_datastore.create_role(name="incontrol")
self.app.server.user_datastore.commit()
_u = user_datastore.find_user(username=username)
if not _u:
if not HWR.beamline.lims.is_user_login_type():
selected_proposal = user
else:
selected_proposal = None
user_datastore.create_user(
username=username,
fullname=fullname,
password="",
nickname=user,
session_id=sid,
selected_proposal=selected_proposal,
limsdata=lims_data.json(),
refresh_token=sso_data.get("refresh_token", str(uuid.uuid4())),
token=sso_data.get("token", str(uuid.uuid4())),
roles=self._get_configured_roles(user),
)
else:
_u.limsdata = lims_data.json() # json.dumps(lims_data)
_u.refresh_token = sso_data.get("refresh_token", str(uuid.uuid4()))
_u.token = sso_data.get("token", str(uuid.uuid4()))
user_datastore.append_roles(_u, self._get_configured_roles(user))
self.app.server.user_datastore.commit()
return user_datastore.find_user(username=username)
def db_set_in_control(self, user, control):
user_datastore = self.app.server.user_datastore
if control:
for _u in User.query.all():
if _u.username == user.username:
_u.in_control = True
else:
_u.in_control = False
user_datastore.put(_u)
else:
_u = user_datastore.find_user(username=user.username)
_u.in_control = control
user_datastore.put(_u)
self.app.server.user_datastore.commit()
class UserManager(BaseUserManager):
def __init__(self, app, config):
super().__init__(app, config)
def _debug(self, msg: str):
logging.getLogger("HWR").debug(msg)
def _login(self, login_id: str, password: str) -> LimsSessionManager:
self._debug("_login. login_id=%s" % login_id)
try:
session_manager: LimsSessionManager = HWR.beamline.lims.login(
login_id, password, is_local_host()
)
except Exception as e:
logging.getLogger("MX3.HWR").error(e)
raise e
self._debug(
"_login. proposal_tuple retrieved. Sessions=%s "
% str(len(session_manager.sessions))
)
inhouse = self.is_inhouse_user(login_id)
active_users = self.active_logged_in_users()
if login_id in active_users:
if current_user.is_anonymous:
self.force_signout_user(login_id)
else:
if current_user.username == login_id:
raise Exception("You are already logged in")
else:
raise Exception(
"Login rejected, you are already logged in"
" somewhere else\nand Another user is already"
" logged in"
)
# Only allow in-house log-in from local host
if inhouse and not (inhouse and is_local_host()):
raise Exception("In-house only allowed from localhost")
# Only allow other users to log-in if they are from the same proposal
if (
active_users
and (login_id not in [p.split("-")[0] for p in active_users])
and not HWR.beamline.lims.is_user_login_type()
):
raise Exception("Another user is already logged in")
# Only allow if no one else is logged in
if not current_user.is_anonymous:
if (
active_users
and current_user.username != login_id
and HWR.beamline.lims.is_user_login_type()
):
raise Exception("Another user is already logged in")
# Only allow local login when remote is disabled
if not self.app.ALLOW_REMOTE and not is_local_host():
raise Exception("Remote access disabled")
return session_manager
def _signout(self):
if self.app.CONFIG.sso.LOGOUT_URI:
requests.post(
self.app.CONFIG.sso.LOGOUT_URI,
data={
"client_id": self.app.CONFIG.sso.CLIENT_ID,
"client_secret": self.app.CONFIG.sso.CLIENT_SECRET,
"refresh_token": current_user.refresh_token,
},
)
class SSOUserManager(BaseUserManager):
def __init__(self, app, config):
super().__init__(app, config)
def _login(self, login_id: str, password: str, sso: bool):
return {"status": {"code": "ok", "msg": ""}}
def _signout(self):
pass