Skip to content

Commit

Permalink
Add support for expirying OAuth Tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
s7clarke10 authored Jul 10, 2023
1 parent 08014b1 commit 8b72d00
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions tap_rest_api_msdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class RestApiStream(RESTStream):

# Intialise self.http_auth used by prepare_request
http_auth = None
# Cache the authenticator using a Smart Singleton pattern
_authenticator = None

@property
def url_base(self) -> Any:
Expand All @@ -29,7 +31,6 @@ def url_base(self) -> Any:


@property
@cached
def authenticator(self) -> Any:
"""Calls an appropriate SDK Authentication method based on the the set auth_method
which is set via the config.
Expand All @@ -39,9 +40,8 @@ def authenticator(self) -> Any:
Note 1: Each auth method requires certain configuration to be present see README.md
for each auth methods configuration requirements.
Note 2: The cached decorator will have all calls to the authenticator use the
same instance logic to speed up processing. TODO: Examine if this affects OAuth2
callbacks and if logic is required for callback for expiring AWS STS tokens.
Note 2: Using Singleton Pattern on the autenticator for caching with a check
if an OAuth Token has expired and needs to be refreshed.
Raises:
ValueError: if the auth_method is unknown.
Expand All @@ -50,11 +50,16 @@ def authenticator(self) -> Any:
A SDK Authenticator or APIAuthenticatorBase if no auth_method supplied.
"""

stream_authenticator = select_authenticator(self)

if stream_authenticator:
return stream_authenticator
else:
return APIAuthenticatorBase(stream=self)

auth_method = self.config.get("auth_method",None)

if not self._authenticator:
self._authenticator = select_authenticator(self)
if not self._authenticator:
# No Auth Method, use default Authenticator
self._authenticator = APIAuthenticatorBase(stream=self)
elif auth_method == 'oauth':
if not self._authenticator.is_token_valid():
# Obtain a new OAuth token as it has expired
self._authenticator = select_authenticator(self)

return self._authenticator

0 comments on commit 8b72d00

Please sign in to comment.