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 exceptions
Browse files Browse the repository at this point in the history
* Duplicated InvalidClientSecretsError - Removed
* Internal only InvalidXsrfTokenError - Refactored to ValueError
  • Loading branch information
pferate committed Jun 30, 2016
1 parent 267bbc5 commit 28186d0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
23 changes: 10 additions & 13 deletions oauth2client/contrib/appengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,6 @@ 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):
"""The XSRF token is invalid or expired."""


class SiteXsrfSecretKey(db.Model):
"""Storage for the sites XSRF secret key.
Expand Down Expand Up @@ -477,15 +469,15 @@ def _parse_state_value(state, user):
user: google.appengine.api.users.User, The current user.
Raises:
InvalidXsrfTokenError: if the XSRF token is invalid.
ValueError: 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 ValueError('Invalid XSRF Token')

return uri

Expand Down Expand Up @@ -813,8 +805,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 ValueError 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 +882,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):
with loadfile_patch as loadfile_mock:
loadfile_mock.return_value = ('badtype', None)
self.assertRaises(
AppEngineInvalidClientSecretsError,
InvalidClientSecretsError,
OAuth2DecoratorFromClientSecrets,
'doesntmatter.json',
scope=['foo_scope', 'bar_scope'])
Expand Down Expand Up @@ -1079,7 +1077,7 @@ def test_build_and_parse_state(self):
self.assertEqual(
'https://example.org',
appengine._parse_state_value(state, UserMock()))
self.assertRaises(appengine.InvalidXsrfTokenError,
self.assertRaises(ValueError,
appengine._parse_state_value, state[1:], UserMock())


Expand Down

0 comments on commit 28186d0

Please sign in to comment.