Compare commits

..

2 commits

Author SHA1 Message Date
Micke Nordin c143d96fab
use persited auth_state 2024-01-13 16:20:19 +01:00
Micke Nordin c4c4ff2f31
Allow authstate to be persisted 2024-01-13 16:09:47 +01:00

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:
@ -42,6 +45,7 @@ hub:
print(f'Time is: {now}, token expires: {expires}') print(f'Time is: {now}, token expires: {expires}')
return True return True
except KeyError: except KeyError:
print(f'No auth_state available for user: {user.name}')
return False return False
c.JupyterHub.authenticator_class = NextcloudOAuthenticator c.JupyterHub.authenticator_class = NextcloudOAuthenticator
@ -62,6 +66,11 @@ hub:
extraEnv: extraEnv:
NEXTCLOUD_HOST: sunet.drive.test.sunet.se NEXTCLOUD_HOST: sunet.drive.test.sunet.se
JUPYTER_HOST: jupyter.drive.test.sunet.se JUPYTER_HOST: jupyter.drive.test.sunet.se
JUPYTERHUB_CRYPT_KEY:
valueFrom:
secretKeyRef:
name: jupyterhub-secrets
key: crypt-key
NEXTCLOUD_CLIENT_ID: NEXTCLOUD_CLIENT_ID:
valueFrom: valueFrom:
secretKeyRef: secretKeyRef: