Skip to content

Commit

Permalink
carry token expiry into Credentials object (googleapis#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
andytzeng committed Nov 6, 2017
1 parent 84252c4 commit f5bb83d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
6 changes: 5 additions & 1 deletion google_auth_oauthlib/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
.. _requests-oauthlib: http://requests-oauthlib.readthedocs.io/en/stable/
"""

import datetime
import json

import google.oauth2.credentials
Expand Down Expand Up @@ -128,10 +129,13 @@ def credentials_from_session(session, client_config=None):
'There is no access token for this session, did you call '
'fetch_token?')

return google.oauth2.credentials.Credentials(
credentials = google.oauth2.credentials.Credentials(
session.token['access_token'],
refresh_token=session.token.get('refresh_token'),
token_uri=client_config.get('token_uri'),
client_id=client_config.get('client_id'),
client_secret=client_config.get('client_secret'),
scopes=session.scope)
credentials.expiry = datetime.datetime.fromtimestamp(
session.token['expires_at'])
return credentials
11 changes: 8 additions & 3 deletions tests/test_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import concurrent.futures
import datetime
from functools import partial
import json
import os
Expand Down Expand Up @@ -133,12 +134,14 @@ def test_fetch_token(self, instance):
def test_credentials(self, instance):
instance.oauth2session.token = {
'access_token': mock.sentinel.access_token,
'refresh_token': mock.sentinel.refresh_token
'refresh_token': mock.sentinel.refresh_token,
'expires_at': 643969200.0
}

credentials = instance.credentials

assert credentials.token == mock.sentinel.access_token
assert credentials.expiry == datetime.datetime(1990, 5, 29, 8, 20, 0)
assert credentials._refresh_token == mock.sentinel.refresh_token
assert (credentials._client_id ==
CLIENT_SECRETS_INFO['web']['client_id'])
Expand All @@ -150,7 +153,8 @@ def test_credentials(self, instance):
def test_authorized_session(self, instance):
instance.oauth2session.token = {
'access_token': mock.sentinel.access_token,
'refresh_token': mock.sentinel.refresh_token
'refresh_token': mock.sentinel.refresh_token,
'expires_at': 643969200.0
}

session = instance.authorized_session()
Expand All @@ -172,7 +176,8 @@ def mock_fetch_token(self, instance):
def set_token(*args, **kwargs):
instance.oauth2session.token = {
'access_token': mock.sentinel.access_token,
'refresh_token': mock.sentinel.refresh_token
'refresh_token': mock.sentinel.refresh_token,
'expires_at': 643969200.0
}

fetch_token_patch = mock.patch.object(
Expand Down
5 changes: 4 additions & 1 deletion tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import datetime
import json
import os

Expand Down Expand Up @@ -73,13 +74,15 @@ def session():
def test_credentials_from_session(session):
session.token = {
'access_token': mock.sentinel.access_token,
'refresh_token': mock.sentinel.refresh_token
'refresh_token': mock.sentinel.refresh_token,
'expires_at': 643969200.0
}

credentials = helpers.credentials_from_session(
session, CLIENT_SECRETS_INFO['web'])

assert credentials.token == mock.sentinel.access_token
assert credentials.expiry == datetime.datetime(1990, 5, 29, 8, 20, 0)
assert credentials._refresh_token == mock.sentinel.refresh_token
assert credentials._client_id == CLIENT_SECRETS_INFO['web']['client_id']
assert (credentials._client_secret ==
Expand Down

0 comments on commit f5bb83d

Please sign in to comment.