8000 Raise a helpful exception when trying to refresh credentials without … · sktt/google-auth-library-python@118c048 · GitHub
[go: up one dir, main page]

8000 Skip to content

Commit 118c048

Browse files
authored
Raise a helpful exception when trying to refresh credentials without a refresh token (googleapis#262)
1 parent ce54cc3 commit 118c048

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

google/oauth2/credentials.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
from google.auth import _helpers
4040
from google.auth import credentials
41+
from google.auth import exceptions
4142
from google.oauth2 import _client
4243

4344

@@ -120,6 +121,15 @@ def requires_scopes(self):
120121

121122
@_helpers.copy_docstring(credentials.Credentials)
122123
def refresh(self, request):
124+
if (self._refresh_token is None or
125+
self._token_uri is None or
126+
self._client_id is None or
127+
self._client_secret is None):
128+
raise exceptions.RefreshError(
129+
'The credentials do not contain the necessary fields need to '
130+
'refresh the access token. You must specify refresh_token, '
131+
'token_uri, client_id, and client_secret.')
132+
123133
access_token, refresh_token, expiry, grant_response = (
124134
_client.refresh_grant(
125135
request, self._token_uri, self._refresh_token, self._client_id,

tests/oauth2/test_credentials.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
import os
1818

1919
import mock
20+
import pytest
2021

2122
from google.auth import _helpers
23+
from google.auth import exceptions
2224
from google.auth import transport
2325
from google.oauth2 import credentials
2426

@@ -95,6 +97,16 @@ def test_refresh_success(self, unused_utcnow, refresh_grant):
9597
# expired)
9698
assert credentials.valid
9799

100+
def test_refresh_no_refresh_token(self):
101+
request = mock.create_autospec(transport.Request)
102+
credentials_ = credentials.Credentials(
103+
token=None, refresh_token=None)
104+
105+
with pytest.raises(exceptions.RefreshError, match='necessary fields'):
106+
credentials_.refresh(request)
107+
108+
request.assert_not_called()
109+
98110
def test_from_authorized_user_info(self):
99111
info = AUTH_USER_INFO.copy()
100112

0 commit comments

Comments
 (0)
0