From: Jessamyn Smith Date: Mon, 20 Apr 2015 17:07:46 +0000 (-0400) Subject: If invalid values are passed for secrets, raise an exception with a helpful error... X-Git-Tag: twitter-1.17.0~2^2~1 X-Git-Url: https://jfr.im/git/z_archive/twitter.git/commitdiff_plain/5722b73f1daae67bac2374950d6b17f8ed6d59ac If invalid values are passed for secrets, raise an exception with a helpful error message rather than allowing code to fail later. --- diff --git a/twitter/auth.py b/twitter/auth.py index 77633ff..45f1f4c 100644 --- a/twitter/auth.py +++ b/twitter/auth.py @@ -5,6 +5,7 @@ except ImportError: import urllib as urllib_parse from base64 import encodestring as encodebytes + class Auth(object): """ ABC for Authenticator objects. @@ -21,6 +22,7 @@ class Auth(object): by the authentication scheme in use.""" raise NotImplementedError() + class UserPassAuth(Auth): """ Basic auth authentication using email/username and @@ -41,6 +43,7 @@ class UserPassAuth(Auth): .encode('utf8')).strip(b'\n') } + class NoAuth(Auth): """ No authentication authenticator. @@ -53,3 +56,7 @@ class NoAuth(Auth): def generate_headers(self): return {} + + +class MissingCredentialsError(Exception): + pass diff --git a/twitter/oauth.py b/twitter/oauth.py index 2df5ff0..b0d7f41 100644 --- a/twitter/oauth.py +++ b/twitter/oauth.py @@ -57,7 +57,7 @@ import hashlib import hmac import base64 -from .auth import Auth +from .auth import Auth, MissingCredentialsError def write_token_file(filename, oauth_token, oauth_token_secret): @@ -92,6 +92,10 @@ class OAuth(Auth): self.consumer_key = consumer_key self.consumer_secret = consumer_secret + if token_secret is None or consumer_secret is None: + raise MissingCredentialsError( + 'You must supply strings for token_secret and consumer_secret, not None.') + def encode_params(self, base_url, method, params): params = params.copy() diff --git a/twitter/oauth2.py b/twitter/oauth2.py index 6c69c2b..fb5fefe 100644 --- a/twitter/oauth2.py +++ b/twitter/oauth2.py @@ -31,7 +31,7 @@ except ImportError: from urllib import quote, urlencode from base64 import b64encode -from .auth import Auth +from .auth import Auth, MissingCredentialsError def write_bearer_token_file(filename, oauth2_bearer_token): """ @@ -89,7 +89,3 @@ class OAuth2(Auth): ).encode('utf8') } return headers - - -class MissingCredentialsError(Exception): - pass