Compare commits

...

2 commits

Author SHA1 Message Date
Micke Nordin 755b165302
Try to sync with refresh token 2024-01-15 11:08:27 +01:00
Micke Nordin e92dd6bd10
Cleaner debug 2024-01-15 10:14:07 +01:00

View file

@ -12,6 +12,7 @@ hub:
extraConfig: extraConfig:
oauthCode: | oauthCode: |
import time import time
from datetime import datetime
from oauthenticator.generic import GenericOAuthenticator from oauthenticator.generic import GenericOAuthenticator
def post_auth_hook(authenticator, handler, authentication): def post_auth_hook(authenticator, handler, authentication):
@ -32,22 +33,29 @@ hub:
if not auth_state: if not auth_state:
return return
access_token = auth_state['access_token'] access_token = auth_state['access_token']
access_token = auth_state['refresh_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):
debug = 'NEXTCLOUD_DEBUG_OAUTH' in os.environ
auth_state = await user.get_auth_state() auth_state = await user.get_auth_state()
if not auth_state: if not auth_state:
print(f'auth_state missing for {user}') if debug:
print(f'auth_state missing for {user}')
return False return False
access_token = auth_state['access_token'] access_token = auth_state['access_token']
refresh_token = auth_state['refresh_token'] refresh_token = auth_state['refresh_token']
token_response = auth_state['token_response'] token_response = auth_state['token_response']
now = time.time() now = time.time()
now_hr = datetime.fromtimestamp(now)
expires = auth_state['token_expires'] expires = auth_state['token_expires']
expires_hr = datetime.fromtimestamp(expires)
if now >= expires: if now >= expires:
print(f'Time is: {now}, token expired: {expires}') if debug:
print(f'Time is: {now_hr}, token expired: {expires_hr}')
return False return False
print(f'Time is: {now}, token expires: {expires}') if debug:
print(f'Time is: {now_hr}, token expires: {expires_hr}')
return True return True
c.JupyterHub.authenticator_class = NextcloudOAuthenticator c.JupyterHub.authenticator_class = NextcloudOAuthenticator
@ -66,6 +74,7 @@ hub:
c.NextcloudOAuthenticator.post_auth_hook = post_auth_hook c.NextcloudOAuthenticator.post_auth_hook = post_auth_hook
extraEnv: extraEnv:
NEXTCLOUD_DEBUG_OAUTH: "yes"
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: JUPYTERHUB_CRYPT_KEY: