Skip to content

Commit 240a3ce

Browse files
committed
getAccessToken thread safe
1 parent 2a553e7 commit 240a3ce

3 files changed

Lines changed: 17 additions & 13 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# **Changelog for PG3 Python Interface**
22

3+
### 3.3.17
4+
- getAccessToken is now thread safe
5+
36
### 3.3.16
47
- Enhanced webhook support
58

udi_interface/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = '3.3.16'
1+
__version__ = '3.3.17'
22
__description__ = 'UDI Python Interface for Polyglot version 3'
33
__url__ = 'https://github.com/UniversalDevicesInc/udi_python_interface'
44
__author__ = 'Universal Devices Inc.'

udi_interface/oauth.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def __init__(self, polyglot):
3232

3333
# Mutex to protect _oauthConfig
3434
self._lock = threading.Lock()
35+
self._token_lock = threading.Lock()
3536

3637
# This is the result of the getAll we get on startup.
3738
def customNsHandler(self, key, data):
@@ -126,22 +127,22 @@ def _oAuthTokensRefresh(self):
126127
# Gets the access token, and refresh if necessary
127128
# Should be called only after config is done
128129
def getAccessToken(self):
129-
# Make sure we have received tokens before attempting to renew
130+
with self._token_lock:
131+
# Make sure we have received tokens before attempting to renew
132+
if self._oauthTokens is not None and self._oauthTokens.get('refresh_token'):
133+
expiry = self._oauthTokens.get('expiry')
130134

131-
if self._oauthTokens is not None and self._oauthTokens.get('refresh_token'):
132-
expiry = self._oauthTokens.get('expiry')
135+
# If expired or expiring in less than 60 seconds, refresh
136+
if expiry is None or datetime.fromisoformat(expiry) - timedelta(seconds=60) < datetime.now():
133137

134-
# If expired or expiring in less than 60 seconds, refresh
135-
if expiry is None or datetime.fromisoformat(expiry) - timedelta(seconds=60) < datetime.now():
138+
LOGGER.info(f"Access tokens: Token is expired since {expiry}. Initiating refresh.")
139+
self._oAuthTokensRefresh()
140+
else:
141+
LOGGER.info(f"Access tokens: Token is still valid until {expiry}, no need to refresh")
136142

137-
LOGGER.info(f"Access tokens: Token is expired since {expiry}. Initiating refresh.")
138-
self._oAuthTokensRefresh()
143+
return self._oauthTokens.get('access_token')
139144
else:
140-
LOGGER.info(f"Access tokens: Token is still valid until {expiry}, no need to refresh")
141-
142-
return self._oauthTokens.get('access_token')
143-
else:
144-
raise ValueError('Access token is not available')
145+
raise ValueError('Access token is not available')
145146

146147
# This will update the oAuth settings with the changes in update
147148
def updateOauthSettings(self, update):

0 commit comments

Comments
 (0)