Skip to content

Commit

Permalink
Merge pull request #51 from sserrata/token-caching
Browse files Browse the repository at this point in the history
Add support for caching access tokens
  • Loading branch information
sserrata authored Jun 18, 2018
2 parents b4bc342 + e50ff92 commit 487b860
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
4 changes: 3 additions & 1 deletion pancloud/adapters/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ def remove_profile(self, profile=None):
pass

@abstractmethod
def write_credentials(self, credentials=None, profile=None):
def write_credentials(self, credentials=None, profile=None,
cache_token=None):
"""Write credentials.
Write credentials to store.
Args:
cache_token (bool): If ``True``, stores ``access_token`` in token store. Defaults to ``True``.
credentials (class): Read-only credentials.
profile (str): Credentials profile. Defaults to ``'default'``.
Expand Down
6 changes: 5 additions & 1 deletion pancloud/adapters/tinydb_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ def remove_profile(self, profile=None):
with self.db:
return self.db.remove(self.query.profile == profile)

def write_credentials(self, credentials=None, profile=None):
def write_credentials(self, credentials=None, profile=None,
cache_token=None):
"""Write credentials.
Write credentials to credentials file. Performs ``upsert``.
Args:
cache_token (bool): If ``True``, stores ``access_token`` in token store. Defaults to ``True``.
credentials (class): Read-only credentials.
profile (str): Credentials profile. Defaults to ``'default'``.
Expand All @@ -83,6 +85,8 @@ def write_credentials(self, credentials=None, profile=None):
'client_secret': credentials.client_secret,
'refresh_token': credentials.refresh_token
}
if cache_token:
d.update({'access_token': credentials.access_token})
with self.lock:
return self.db.upsert(
d, self.query.profile == profile
Expand Down
24 changes: 17 additions & 7 deletions pancloud/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
class Credentials(object):
"""An Application Framework credentials object."""

def __init__(self, auth_base_url=None, client_id=None, client_secret=None,
instance_id=None, profile=None, redirect_uri=None,
region=None, refresh_token=None, scope=None,
storage_adapter=None, token_url=None, token_revoke_url=None,
**kwargs):
def __init__(self, auth_base_url=None, cache_token=True,
client_id=None, client_secret=None, instance_id=None,
profile=None, redirect_uri=None, region=None,
refresh_token=None, scope=None, storage_adapter=None,
token_url=None, token_revoke_url=None, **kwargs):
"""Persist Session() and credentials attributes.
Built on top of the ``Requests`` library, ``Credentials`` is an
Expand Down Expand Up @@ -65,6 +65,7 @@ def __init__(self, auth_base_url=None, client_id=None, client_secret=None,
"""
self.access_token_ = None
self.auth_base_url = auth_base_url or BASE_URL
self.cache_token_ = cache_token
self.client_id_ = client_id
self.client_secret_ = client_secret
self.environ = os.environ
Expand Down Expand Up @@ -106,6 +107,10 @@ def access_token(self):
"""Get access_token"""
return self.access_token_

@property
def cache_token(self):
return self.cache_token_

@property
def client_id(self):
"""Get client_id"""
Expand Down Expand Up @@ -239,7 +244,11 @@ def get_credentials(self):
class: Read-only credentials.
"""
access_token = self.access_token_
if self.cache_token:
access_token = self._resolve_credential(
'access_token') or self.access_token_
else:
access_token = self.access_token_
client_id = self.client_id_ or self._resolve_credential(
'client_id')
client_secret = self.client_secret_ or self._resolve_credential(
Expand Down Expand Up @@ -358,5 +367,6 @@ def write_credentials(self):
"""
c = self.get_credentials()
return self.storage().write_credentials(
credentials=c, profile=self.profile
credentials=c, profile=self.profile,
cache_token=self.cache_token_
)
5 changes: 5 additions & 0 deletions pancloud/httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ def request(self, **kwargs):

# Prepare and send the Request() and return Response()
try:
if credentials:
if credentials.cache_token:
h = self._apply_credentials(
k['headers'], credentials)
k['headers'] = h
r = self._send_request(
enforce_json, method, raise_for_status, url, **k
)
Expand Down

0 comments on commit 487b860

Please sign in to comment.