Merge pull request #372 from wilford/master
Adds token_expiry to the Devshell credential.
This commit is contained in:
@@ -14,6 +14,7 @@
|
|||||||
|
|
||||||
"""OAuth 2.0 utitilies for Google Developer Shell environment."""
|
"""OAuth 2.0 utitilies for Google Developer Shell environment."""
|
||||||
|
|
||||||
|
import datetime
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import socket
|
import socket
|
||||||
@@ -21,6 +22,10 @@ import socket
|
|||||||
from oauth2client._helpers import _to_bytes
|
from oauth2client._helpers import _to_bytes
|
||||||
from oauth2client import client
|
from oauth2client import client
|
||||||
|
|
||||||
|
# Expose utcnow() at module level to allow for
|
||||||
|
# easier testing (by replacing with a stub).
|
||||||
|
_UTCNOW = datetime.datetime.utcnow
|
||||||
|
|
||||||
DEVSHELL_ENV = 'DEVSHELL_CLIENT_PORT'
|
DEVSHELL_ENV = 'DEVSHELL_CLIENT_PORT'
|
||||||
|
|
||||||
|
|
||||||
@@ -52,6 +57,7 @@ class CredentialInfoResponse(object):
|
|||||||
* Index 0 - user email
|
* Index 0 - user email
|
||||||
* Index 1 - default project ID. None if the project context is not known.
|
* Index 1 - default project ID. None if the project context is not known.
|
||||||
* Index 2 - OAuth2 access token. None if there is no valid auth context.
|
* Index 2 - OAuth2 access token. None if there is no valid auth context.
|
||||||
|
* Index 3 - Seconds until the access token expires. None if not present.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, json_string):
|
def __init__(self, json_string):
|
||||||
@@ -63,6 +69,7 @@ class CredentialInfoResponse(object):
|
|||||||
self.user_email = pbl[0] if pbl_len > 0 else None
|
self.user_email = pbl[0] if pbl_len > 0 else None
|
||||||
self.project_id = pbl[1] if pbl_len > 1 else None
|
self.project_id = pbl[1] if pbl_len > 1 else None
|
||||||
self.access_token = pbl[2] if pbl_len > 2 else None
|
self.access_token = pbl[2] if pbl_len > 2 else None
|
||||||
|
self.expires_in = pbl[3] if pbl_len > 3 else None
|
||||||
|
|
||||||
|
|
||||||
def _SendRecv():
|
def _SendRecv():
|
||||||
@@ -117,6 +124,12 @@ class DevshellCredentials(client.GoogleCredentials):
|
|||||||
def _refresh(self, http_request):
|
def _refresh(self, http_request):
|
||||||
self.devshell_response = _SendRecv()
|
self.devshell_response = _SendRecv()
|
||||||
self.access_token = self.devshell_response.access_token
|
self.access_token = self.devshell_response.access_token
|
||||||
|
expires_in = self.devshell_response.expires_in
|
||||||
|
if expires_in is not None:
|
||||||
|
delta = datetime.timedelta(seconds=expires_in)
|
||||||
|
self.token_expiry = _UTCNOW() + delta
|
||||||
|
else:
|
||||||
|
self.token_expiry = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def user_email(self):
|
def user_email(self):
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
|
|
||||||
"""Tests for oauth2client.devshell."""
|
"""Tests for oauth2client.devshell."""
|
||||||
|
|
||||||
|
import datetime
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import socket
|
import socket
|
||||||
@@ -22,6 +23,7 @@ import unittest
|
|||||||
|
|
||||||
import mock
|
import mock
|
||||||
|
|
||||||
|
from oauth2client import devshell
|
||||||
from oauth2client._helpers import _from_bytes
|
from oauth2client._helpers import _from_bytes
|
||||||
from oauth2client._helpers import _to_bytes
|
from oauth2client._helpers import _to_bytes
|
||||||
from oauth2client.client import save_to_well_known_file
|
from oauth2client.client import save_to_well_known_file
|
||||||
@@ -33,6 +35,16 @@ from oauth2client.devshell import DEVSHELL_ENV
|
|||||||
from oauth2client.devshell import DevshellCredentials
|
from oauth2client.devshell import DevshellCredentials
|
||||||
from oauth2client.devshell import NoDevshellServer
|
from oauth2client.devshell import NoDevshellServer
|
||||||
|
|
||||||
|
# A dummy value to use for the expires_in field
|
||||||
|
# in CredentialInfoResponse.
|
||||||
|
EXPIRES_IN = 1000
|
||||||
|
DEFAULT_CREDENTIAL_JSON = json.dumps([
|
||||||
|
'joe@example.com',
|
||||||
|
'fooproj',
|
||||||
|
'sometoken',
|
||||||
|
EXPIRES_IN
|
||||||
|
])
|
||||||
|
|
||||||
|
|
||||||
class TestCredentialInfoResponse(unittest.TestCase):
|
class TestCredentialInfoResponse(unittest.TestCase):
|
||||||
|
|
||||||
@@ -51,16 +63,20 @@ class TestCredentialInfoResponse(unittest.TestCase):
|
|||||||
self.assertEqual(info_response.user_email, None)
|
self.assertEqual(info_response.user_email, None)
|
||||||
self.assertEqual(info_response.project_id, None)
|
self.assertEqual(info_response.project_id, None)
|
||||||
self.assertEqual(info_response.access_token, None)
|
self.assertEqual(info_response.access_token, None)
|
||||||
|
self.assertEqual(info_response.expires_in, None)
|
||||||
|
|
||||||
def test_constructor_full_list(self):
|
def test_constructor_full_list(self):
|
||||||
user_email = 'user_email'
|
user_email = 'user_email'
|
||||||
project_id = 'project_id'
|
project_id = 'project_id'
|
||||||
access_token = 'access_token'
|
access_token = 'access_token'
|
||||||
json_string = json.dumps([user_email, project_id, access_token])
|
expires_in = 1
|
||||||
|
json_string = json.dumps(
|
||||||
|
[user_email, project_id, access_token, expires_in])
|
||||||
info_response = CredentialInfoResponse(json_string)
|
info_response = CredentialInfoResponse(json_string)
|
||||||
self.assertEqual(info_response.user_email, user_email)
|
self.assertEqual(info_response.user_email, user_email)
|
||||||
self.assertEqual(info_response.project_id, project_id)
|
self.assertEqual(info_response.project_id, project_id)
|
||||||
self.assertEqual(info_response.access_token, access_token)
|
self.assertEqual(info_response.access_token, access_token)
|
||||||
|
self.assertEqual(info_response.expires_in, expires_in)
|
||||||
|
|
||||||
|
|
||||||
class Test_SendRecv(unittest.TestCase):
|
class Test_SendRecv(unittest.TestCase):
|
||||||
@@ -106,8 +122,7 @@ class _AuthReferenceServer(threading.Thread):
|
|||||||
|
|
||||||
def __init__(self, response=None):
|
def __init__(self, response=None):
|
||||||
super(_AuthReferenceServer, self).__init__(None)
|
super(_AuthReferenceServer, self).__init__(None)
|
||||||
self.response = (response or
|
self.response = response or DEFAULT_CREDENTIAL_JSON
|
||||||
'["joe@example.com", "fooproj", "sometoken"]')
|
|
||||||
self.bad_request = False
|
self.bad_request = False
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
@@ -195,12 +210,19 @@ class DevshellCredentialsTests(unittest.TestCase):
|
|||||||
creds = DevshellCredentials()
|
creds = DevshellCredentials()
|
||||||
self.assertEquals(None, creds.refresh_token)
|
self.assertEquals(None, creds.refresh_token)
|
||||||
|
|
||||||
def test_reads_credentials(self):
|
@mock.patch.object(devshell, '_UTCNOW')
|
||||||
|
def test_reads_credentials(self, utcnow):
|
||||||
|
NOW = datetime.datetime(1992, 12, 31)
|
||||||
|
utcnow.return_value = NOW
|
||||||
with _AuthReferenceServer():
|
with _AuthReferenceServer():
|
||||||
creds = DevshellCredentials()
|
creds = DevshellCredentials()
|
||||||
self.assertEqual('joe@example.com', creds.user_email)
|
self.assertEqual('joe@example.com', creds.user_email)
|
||||||
self.assertEqual('fooproj', creds.project_id)
|
self.assertEqual('fooproj', creds.project_id)
|
||||||
self.assertEqual('sometoken', creds.access_token)
|
self.assertEqual('sometoken', creds.access_token)
|
||||||
|
self.assertEqual(
|
||||||
|
NOW + datetime.timedelta(seconds=EXPIRES_IN),
|
||||||
|
creds.token_expiry)
|
||||||
|
utcnow.assert_called_once_with()
|
||||||
|
|
||||||
def test_handles_skipped_fields(self):
|
def test_handles_skipped_fields(self):
|
||||||
with _AuthReferenceServer('["joe@example.com"]'):
|
with _AuthReferenceServer('["joe@example.com"]'):
|
||||||
@@ -208,6 +230,7 @@ class DevshellCredentialsTests(unittest.TestCase):
|
|||||||
self.assertEqual('joe@example.com', creds.user_email)
|
self.assertEqual('joe@example.com', creds.user_email)
|
||||||
self.assertEqual(None, creds.project_id)
|
self.assertEqual(None, creds.project_id)
|
||||||
self.assertEqual(None, creds.access_token)
|
self.assertEqual(None, creds.access_token)
|
||||||
|
self.assertEqual(None, creds.token_expiry)
|
||||||
|
|
||||||
def test_handles_tiny_response(self):
|
def test_handles_tiny_response(self):
|
||||||
with _AuthReferenceServer('[]'):
|
with _AuthReferenceServer('[]'):
|
||||||
@@ -218,7 +241,7 @@ class DevshellCredentialsTests(unittest.TestCase):
|
|||||||
|
|
||||||
def test_handles_ignores_extra_fields(self):
|
def test_handles_ignores_extra_fields(self):
|
||||||
with _AuthReferenceServer(
|
with _AuthReferenceServer(
|
||||||
'["joe@example.com", "fooproj", "sometoken", "extra"]'):
|
'["joe@example.com", "fooproj", "sometoken", 1, "extra"]'):
|
||||||
creds = DevshellCredentials()
|
creds = DevshellCredentials()
|
||||||
self.assertEqual('joe@example.com', creds.user_email)
|
self.assertEqual('joe@example.com', creds.user_email)
|
||||||
self.assertEqual('fooproj', creds.project_id)
|
self.assertEqual('fooproj', creds.project_id)
|
||||||
|
|||||||
Reference in New Issue
Block a user