Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 10 additions & 21 deletions auth/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@

from dataclasses import dataclass
from datetime import datetime
import json
from typing import Any, Dict, Type, TypeVar
from typing import Any, Dict, Mapping, Type, TypeVar, Union

import pytz
from dateutil.relativedelta import relativedelta
from google.auth.transport import requests
from google.oauth2 import credentials as oauth

from auth import decorators
from .credentials_helpers import encode_key

from .abstract_datastore import AbstractDatastore
from .credentials_helpers import encode_key
from .exceptions import CredentialsError


Expand All @@ -36,7 +35,6 @@ class ProjectCredentials(object):
client_secret: str



class Credentials(object):
"""Credentials.

Expand Down Expand Up @@ -97,7 +95,7 @@ def token_details(self) -> Dict[str, Any]:
return self.datastore.get_document(id=encode_key(self._email))

def store_credentials(self,
creds: oauth.Credentials) -> None:
creds: Union[oauth.Credentials, Mapping[str, Any]]) -> None:
"""Stores the credentials.

This function uses the datastore to store the user credentials for later.
Expand All @@ -110,8 +108,11 @@ def store_credentials(self,
"""
if self._email:
key = encode_key(self._email)
json_creds = json.loads(creds.to_json())
self.datastore.update_document(id=key, new_data=json_creds)

if isinstance(creds, oauth.Credentials):
self.datastore.update_document(id=key, new_data=creds.to_json())
else:
self.datastore.update_document(id=key, new_data=creds)

def _refresh_credentials(self, creds: oauth.Credentials) -> None:
"""Refreshes the Google OAuth credentials.
Expand Down Expand Up @@ -139,27 +140,15 @@ def credentials(self) -> oauth.Credentials:
expiry = self._to_utc(
datetime.now().astimezone(pytz.utc) + relativedelta(minutes=30))
if token := self.token_details:
if token.get('access_token'):
# This handles old-style credential storages.
creds = oauth.Credentials.from_authorized_user_info({
'token': token['access_token'],
'refresh_token': token['refresh_token'],
'client_id': self.project_credentials.client_id,
'client_secret': self.project_credentials.client_secret,
})

else:
creds = \
oauth.Credentials.from_authorized_user_info(token)
creds = oauth.Credentials.from_authorized_user_info(token)

if creds.expired:
creds.expiry = expiry
self._refresh_credentials(creds=creds)

else:
creds = None
raise CredentialsError(
message='credentials not found', email=self._email)
raise CredentialsError(message='credentials not found', email=self._email)

return creds

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "python-oauth-token-manager"
version = "0.2.7"
version = "0.3.0"
authors = [{ name = "David Harcombe", email = "david.harcombe@gmail.com" }]
description = "API for managing stored OAuth credentials."
readme = "README.md"
Expand Down