From c143d96fab487f6c5cd533a01324ecc0d3a1e558 Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Sat, 13 Jan 2024 16:20:19 +0100 Subject: [PATCH] use persited auth_state --- jupyter/base/values/values.yaml | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/jupyter/base/values/values.yaml b/jupyter/base/values/values.yaml index 3ab0224..54b08d7 100644 --- a/jupyter/base/values/values.yaml +++ b/jupyter/base/values/values.yaml @@ -15,25 +15,28 @@ hub: from oauthenticator.generic import GenericOAuthenticator def post_auth_hook(authenticator, handler, authentication): user = authentication['auth_state']['oauth_user']['ocs']['data']['id'] - auth_state = authentication['auth_state'] - auth_state['token_expires'] = time.time() + auth_state['token_response']['expires_in'] - authenticator.user_dict[user] = auth_state + authentication['auth_state']['token_expires'] = time.time() + auth_state['token_response']['expires_in'] return authentication class NextcloudOAuthenticator(GenericOAuthenticator): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.user_dict = {} - def pre_spawn_start(self, user, spawner): + async def pre_spawn_start(self, user, spawner): super().pre_spawn_start(user, spawner) - access_token = self.user_dict[user.name]['access_token'] + auth_state = await user.get_auth_state() + if not auth_state: + return + access_token = auth_state['access_token'] spawner.environment['NEXTCLOUD_ACCESS_TOKEN'] = access_token async def refresh_user(self, user, handler=None): - try: - access_token = self.user_dict[user.name]['access_token'] - refresh_token = self.user_dict[user.name]['refresh_token'] - token_response = self.user_dict[user.name]['token_response'] + auth_state = await user.get_auth_state() + if not auth_state: + return False + access_token = auth_state['access_token'] + refresh_token = auth_state['refresh_token'] + token_response = name]['token_response'] now = time.time() expires = self.user_dict[user.name]['token_expires'] if now >= expires: