aboutsummaryrefslogtreecommitdiff
path: root/gn2/wqflask/oauth2/client.py
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2024-07-31 15:27:59 -0500
committerAlexander_Kabui2024-08-28 15:02:46 +0300
commit2ff7cf9ff8640328c5849c5ff07bd0113a303856 (patch)
treee0fb37f43efda2307bd8563a152b59fe66c80538 /gn2/wqflask/oauth2/client.py
parenta1eb080eca9611262f1a3fa4463d40653d6899c2 (diff)
downloadgenenetwork2-2ff7cf9ff8640328c5849c5ff07bd0113a303856.tar.gz
Synchronise token refreshes
The application can be run in a multi-threaded server, leading to a situation where the multiple threads attempt to get a new JWT using the exact same refresh token. This synchronises the various threads ensuring only a single thread is able to retrieve the new JWT that all the rest of the threads then use.
Diffstat (limited to 'gn2/wqflask/oauth2/client.py')
-rw-r--r--gn2/wqflask/oauth2/client.py37
1 files changed, 33 insertions, 4 deletions
diff --git a/gn2/wqflask/oauth2/client.py b/gn2/wqflask/oauth2/client.py
index 770777b5..0d4615e8 100644
--- a/gn2/wqflask/oauth2/client.py
+++ b/gn2/wqflask/oauth2/client.py
@@ -1,5 +1,7 @@
"""Common oauth2 client utilities."""
import json
+import time
+import random
import requests
from typing import Optional
from urllib.parse import urljoin
@@ -38,10 +40,36 @@ def oauth2_client():
def __update_token__(token, refresh_token=None, access_token=None):
"""Update the token when refreshed."""
session.set_user_token(token)
+ return token
- def __client__(token) -> OAuth2Session:
+ def __validate_token__(token):
_jwt = jwt.decode(token["access_token"],
app.config["AUTH_SERVER_SSL_PUBLIC_KEY"])
+ return token
+
+ def __delay__():
+ """Do a tiny delay."""
+ time.sleep(random.choice(tuple(i/1000.0 for i in range(0,100))))
+
+ def __refresh_token__(token):
+ """Synchronise token refresh."""
+ if session.is_token_expired():
+ __delay__()
+ if session.is_token_refreshing():
+ while session.is_token_refreshing():
+ __delay__()
+ _token = session.user_token().either(None, lambda _tok: _tok)
+ return _token
+
+ session.toggle_token_refreshing()
+ _client = __client__(token)
+ _client.get(urljoin(authserver_uri(), "auth/user/"))
+ session.toggle_token_refreshing()
+ return _client.token
+
+ return token
+
+ def __client__(token) -> OAuth2Session:
client = OAuth2Session(
oauth2_clientid(),
oauth2_clientsecret(),
@@ -51,9 +79,10 @@ def oauth2_client():
token=token,
update_token=__update_token__)
return client
- return session.user_token().either(
- lambda _notok: __client__(None),
- lambda token: __client__(token))
+ return session.user_token().then(__validate_token__).then(
+ __refresh_token__).either(
+ lambda _notok: __client__(None),
+ lambda token: __client__(token))
def __no_token__(_err) -> Left:
"""Handle situation where request is attempted with no token."""