Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Commit

Permalink
Remove unneeded contrib.appengine exception
Browse files Browse the repository at this point in the history
* Duplicated InvalidClientSecretsError - Removed
* Internal only InvalidXsrfTokenError - Added message
  • Loading branch information
pferate committed Jul 11, 2016
1 parent f25b7ab commit 24621b5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
21 changes: 11 additions & 10 deletions oauth2client/contrib/appengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,7 @@ def _safe_html(s):
return cgi.escape(s, quote=1).replace("'", ''')


class InvalidClientSecretsError(Exception):
"""The client_secrets.json file is malformed or missing required fields."""


class InvalidXsrfTokenError(Exception):
class _InvalidXsrfTokenError(Exception):
"""The XSRF token is invalid or expired."""


Expand Down Expand Up @@ -477,15 +473,15 @@ def _parse_state_value(state, user):
user: google.appengine.api.users.User, The current user.
Raises:
InvalidXsrfTokenError: if the XSRF token is invalid.
_InvalidXsrfTokenError: if the XSRF token is invalid.
Returns:
The redirect URI.
"""
uri, token = state.rsplit(':', 1)
if not xsrfutil.validate_token(xsrf_secret_key(), token, user.user_id(),
action_id=uri):
raise InvalidXsrfTokenError()
raise _InvalidXsrfTokenError('Invalid XSRF Token')

return uri

Expand Down Expand Up @@ -813,8 +809,13 @@ def get(self):
decorator._credentials_class, None,
decorator._credentials_property_name,
user=user).put(credentials)
redirect_uri = _parse_state_value(
str(self.request.get('state')), user)
try:
redirect_uri = _parse_state_value(
str(self.request.get('state')), user)
except _InvalidXsrfTokenError as e:
self.response.out.write(
'The authorization request failed: %s' % e)
return

if (decorator._token_response_param and
credentials.token_response):
Expand Down Expand Up @@ -885,7 +886,7 @@ def __init__(self, filename, scope, message=None, cache=None, **kwargs):
cache=cache)
if client_type not in (clientsecrets.TYPE_WEB,
clientsecrets.TYPE_INSTALLED):
raise InvalidClientSecretsError(
raise clientsecrets.InvalidClientSecretsError(
"OAuth2Decorator doesn't support this OAuth 2.0 flow.")

constructor_kwargs = dict(kwargs)
Expand Down
6 changes: 2 additions & 4 deletions tests/contrib/test_appengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@
from oauth2client.contrib.appengine import CredentialsNDBModel
from oauth2client.contrib.appengine import CredentialsProperty
from oauth2client.contrib.appengine import FlowProperty
from oauth2client.contrib.appengine import (
InvalidClientSecretsError as AppEngineInvalidClientSecretsError)
from oauth2client.contrib.appengine import OAuth2Decorator
from oauth2client.contrib.appengine import OAuth2DecoratorFromClientSecrets
from oauth2client.contrib.appengine import oauth2decorator_from_clientsecrets
Expand Down Expand Up @@ -923,7 +921,7 @@ def test_decorator_from_client_secrets_bad_type(self):
'oauth2client.contrib.appengine.clientsecrets.loadfile')
with loadfile_patch as loadfile_mock:
loadfile_mock.return_value = ('badtype', None)
with self.assertRaises(AppEngineInvalidClientSecretsError):
with self.assertRaises(InvalidClientSecretsError):
OAuth2DecoratorFromClientSecrets(
'doesntmatter.json',
scope=['foo_scope', 'bar_scope'])
Expand Down Expand Up @@ -1079,5 +1077,5 @@ def test_build_and_parse_state(self):
self.assertEqual(
'https://example.org',
appengine._parse_state_value(state, UserMock()))
with self.assertRaises(appengine.InvalidXsrfTokenError):
with self.assertRaises(appengine._InvalidXsrfTokenError):
appengine._parse_state_value(state[1:], UserMock())

0 comments on commit 24621b5

Please sign in to comment.