Skip to content

Commit

Permalink
Removing unnecessary persists
Browse files Browse the repository at this point in the history
  • Loading branch information
MiksIr committed Jun 10, 2020
1 parent 45a8987 commit 02bc005
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/auth/auth_gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def validate_user(self, user, request_handler):
return False
access_token = access_token.decode("utf-8")

self.clean_and_persist_sessions()
self.validate_sessions()

if self.user_states.get(user) is None:
LOGGER.debug("User %s not found in state" % user)
Expand Down Expand Up @@ -195,20 +195,24 @@ def logout(self, user, request_handler):

def clean_sessions(self):
now = time.time()
changed = False
for user_data in list(self.user_states.keys()):
if self.session_expire and (self.user_states[user_data]['visit'] + self.session_expire) < now:
LOGGER.info("User %s removed because session expired" % user_data)
del self.user_states[user_data]
changed = True
continue
if self.user_states[user_data]['state'] is None or self.user_states[user_data]['state'] != "active":
LOGGER.info("User %s removed because state '%s' != 'active'" %
(user_data, self.user_states[user_data]['state']))
del self.user_states[user_data]
changed = True
continue
return changed

def clean_and_persist_sessions(self):
self.clean_sessions()
if self.gitlab_dump:
def validate_sessions(self, force_persist=False):
changed = self.clean_sessions()
if self.gitlab_dump and (changed or force_persist):
self.persist_session()

def persist_session(self):
Expand Down Expand Up @@ -236,7 +240,7 @@ def update_groups(self, user, access_token):
self.user_states[user]['updating'] = False
self.user_states[user]['updated'] = now
self.user_states[user]['visit'] = now
self.clean_and_persist_sessions()
self.validate_sessions(force_persist=True)

@gen.coroutine
def update_user(self, user, access_token):
Expand All @@ -251,7 +255,7 @@ def update_user(self, user, access_token):
self.user_states[user]['updating'] = False
self.user_states[user]['updated'] = now
self.user_states[user]['visit'] = now
self.clean_and_persist_sessions()
self.validate_sessions(force_persist=True)
return

@gen.coroutine
Expand Down Expand Up @@ -311,7 +315,7 @@ def read_user(self, code, request_handler):
user_response['updating'] = False
oauth_access_token = user_response.pop('access_token')
self.user_states[user_response['email']] = user_response
self.clean_and_persist_sessions()
self.validate_sessions(force_persist=True)
request_handler.set_secure_cookie('token', oauth_access_token)

return user_response['email']
Expand Down

0 comments on commit 02bc005

Please sign in to comment.