use persited auth_state

This commit is contained in:
Micke Nordin 2024-01-13 16:20:19 +01:00
parent c4c4ff2f31
commit c143d96fab
Signed by untrusted user: Micke
GPG key ID: 0DA0A7A5708FE257

View file

@ -15,25 +15,28 @@ hub:
from oauthenticator.generic import GenericOAuthenticator from oauthenticator.generic import GenericOAuthenticator
def post_auth_hook(authenticator, handler, authentication): def post_auth_hook(authenticator, handler, authentication):
user = authentication['auth_state']['oauth_user']['ocs']['data']['id'] user = authentication['auth_state']['oauth_user']['ocs']['data']['id']
auth_state = authentication['auth_state'] authentication['auth_state']['token_expires'] = time.time() + auth_state['token_response']['expires_in']
auth_state['token_expires'] = time.time() + auth_state['token_response']['expires_in']
authenticator.user_dict[user] = auth_state
return authentication return authentication
class NextcloudOAuthenticator(GenericOAuthenticator): class NextcloudOAuthenticator(GenericOAuthenticator):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.user_dict = {} self.user_dict = {}
def pre_spawn_start(self, user, spawner): async def pre_spawn_start(self, user, spawner):
super().pre_spawn_start(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 spawner.environment['NEXTCLOUD_ACCESS_TOKEN'] = access_token
async def refresh_user(self, user, handler=None): async def refresh_user(self, user, handler=None):
try: auth_state = await user.get_auth_state()
access_token = self.user_dict[user.name]['access_token'] if not auth_state:
refresh_token = self.user_dict[user.name]['refresh_token'] return False
token_response = self.user_dict[user.name]['token_response'] access_token = auth_state['access_token']
refresh_token = auth_state['refresh_token']
token_response = name]['token_response']
now = time.time() now = time.time()
expires = self.user_dict[user.name]['token_expires'] expires = self.user_dict[user.name]['token_expires']
if now >= expires: if now >= expires: