code formatting (black)

This commit is contained in:
adam j hartz 2025-06-06 14:18:36 -04:00
commit 71c710f662
Signed by: hz
GPG key ID: B63A0A59F5C82493
5 changed files with 284 additions and 209 deletions

View file

@ -46,15 +46,15 @@ from ._compat import urlparse
from ._compat import urlunparse
from ._version import __version__
OAUTH_VERSION = '1.0' # Hi Blaine!
HTTP_METHOD = 'GET'
SIGNATURE_METHOD = 'PLAINTEXT'
OAUTH_VERSION = "1.0" # Hi Blaine!
HTTP_METHOD = "GET"
SIGNATURE_METHOD = "PLAINTEXT"
class Error(RuntimeError):
"""Generic exception class."""
def __init__(self, message='OAuth error occurred.'):
def __init__(self, message="OAuth error occurred."):
self._message = message
@property
@ -70,15 +70,14 @@ class MissingSignature(Error):
pass
def build_authenticate_header(realm=''):
def build_authenticate_header(realm=""):
"""Optional WWW-Authenticate header (401 error)"""
return {'WWW-Authenticate': 'OAuth realm="%s"' % realm}
return {"WWW-Authenticate": 'OAuth realm="%s"' % realm}
def build_xoauth_string(url, consumer, token=None):
"""Build an XOAUTH string for use in SMTP/IMPA authentication."""
request = Request.from_consumer_and_token(consumer, token,
"GET", url)
request = Request.from_consumer_and_token(consumer, token, "GET", url)
signing_method = SignatureMethod_HMAC_SHA1()
request.sign_request(signing_method, consumer, token)
@ -88,30 +87,39 @@ def build_xoauth_string(url, consumer, token=None):
if v is not None:
params.append('%s="%s"' % (k, escape(v)))
return "%s %s %s" % ("GET", url, ','.join(params))
return "%s %s %s" % ("GET", url, ",".join(params))
def to_unicode(s):
""" Convert to unicode, raise exception with instructive error
message if s is not unicode, ascii, or utf-8. """
"""Convert to unicode, raise exception with instructive error
message if s is not unicode, ascii, or utf-8."""
if not isinstance(s, TEXT):
if not isinstance(s, bytes):
raise TypeError('You are required to pass either unicode or '
'bytes here, not: %r (%s)' % (type(s), s))
raise TypeError(
"You are required to pass either unicode or "
"bytes here, not: %r (%s)" % (type(s), s)
)
try:
s = s.decode('utf-8')
s = s.decode("utf-8")
except UnicodeDecodeError as le:
raise TypeError('You are required to pass either a unicode '
'object or a utf-8-enccoded bytes string here. '
'You passed a bytes object which contained '
'non-utf-8: %r. The UnicodeDecodeError that '
'resulted from attempting to interpret it as '
'utf-8 was: %s'
% (s, le,))
raise TypeError(
"You are required to pass either a unicode "
"object or a utf-8-enccoded bytes string here. "
"You passed a bytes object which contained "
"non-utf-8: %r. The UnicodeDecodeError that "
"resulted from attempting to interpret it as "
"utf-8 was: %s"
% (
s,
le,
)
)
return s
def to_utf8(s):
return to_unicode(s).encode('utf-8')
return to_unicode(s).encode("utf-8")
def to_unicode_if_string(s):
if isinstance(s, STRING_TYPES):
@ -119,12 +127,14 @@ def to_unicode_if_string(s):
else:
return s
def to_utf8_if_string(s):
if isinstance(s, STRING_TYPES):
return to_utf8(s)
else:
return s
def to_unicode_optional_iterator(x):
"""
Raise TypeError if x is a str containing non-utf8 bytes or if x is
@ -136,10 +146,11 @@ def to_unicode_optional_iterator(x):
try:
l = list(x)
except TypeError as e:
assert 'is not iterable' in str(e)
assert "is not iterable" in str(e)
return x
else:
return [ to_unicode(e) for e in l ]
return [to_unicode(e) for e in l]
def to_utf8_optional_iterator(x):
"""
@ -152,16 +163,18 @@ def to_utf8_optional_iterator(x):
try:
l = list(x)
except TypeError as e:
assert 'is not iterable' in str(e)
assert "is not iterable" in str(e)
return x
else:
return [ to_utf8_if_string(e) for e in l ]
return [to_utf8_if_string(e) for e in l]
def escape(s):
"""Escape a URL including any /."""
if not isinstance(s, bytes):
s = s.encode('utf-8')
return quote(s, safe='~')
s = s.encode("utf-8")
return quote(s, safe="~")
def generate_timestamp():
"""Get seconds since epoch (UTC)."""
@ -170,21 +183,21 @@ def generate_timestamp():
def generate_nonce(length=8):
"""Generate pseudorandom number."""
return ''.join([str(random.SystemRandom().randint(0, 9)) for i in range(length)])
return "".join([str(random.SystemRandom().randint(0, 9)) for i in range(length)])
def generate_verifier(length=8):
"""Generate pseudorandom number."""
return ''.join([str(random.SystemRandom().randint(0, 9)) for i in range(length)])
return "".join([str(random.SystemRandom().randint(0, 9)) for i in range(length)])
class Consumer(object):
"""A consumer of OAuth-protected services.
The OAuth consumer is a "third-party" service that wants to access
protected resources from an OAuth service provider on behalf of an end
user. It's kind of the OAuth client.
Usually a consumer must be registered with the service provider by the
developer of the consumer software. As part of that process, the service
provider gives the consumer a *key* and a *secret* with which the consumer
@ -192,7 +205,7 @@ class Consumer(object):
key in each request to identify itself, but will use its secret only when
signing requests, to prove that the request is from that particular
registered consumer.
Once registered, the consumer can then use its consumer credentials to ask
the service provider for a request token, kicking off the OAuth
authorization process.
@ -209,8 +222,7 @@ class Consumer(object):
raise ValueError("Key and secret must be set.")
def __str__(self):
data = {'oauth_consumer_key': self.key,
'oauth_consumer_secret': self.secret}
data = {"oauth_consumer_key": self.key, "oauth_consumer_secret": self.secret}
return urlencode(data)
@ -218,12 +230,12 @@ class Consumer(object):
class Token(object):
"""An OAuth credential used to request authorization or a protected
resource.
Tokens in OAuth comprise a *key* and a *secret*. The key is included in
requests to identify the token being used, but the secret is used only in
the signature, to prove that the requester is who the server gave the
token to.
When first negotiating the authorization, the consumer asks for a *request
token* that the live user authorizes with the service provider. The
consumer then exchanges the request token for an *access token* that can
@ -245,7 +257,7 @@ class Token(object):
def set_callback(self, callback):
self.callback = callback
self.callback_confirmed = 'true'
self.callback_confirmed = "true"
def set_verifier(self, verifier=None):
if verifier is not None:
@ -259,28 +271,27 @@ class Token(object):
parts = urlparse(self.callback)
scheme, netloc, path, params, query, fragment = parts[:6]
if query:
query = '%s&oauth_verifier=%s' % (query, self.verifier)
query = "%s&oauth_verifier=%s" % (query, self.verifier)
else:
query = 'oauth_verifier=%s' % self.verifier
return urlunparse((scheme, netloc, path, params,
query, fragment))
query = "oauth_verifier=%s" % self.verifier
return urlunparse((scheme, netloc, path, params, query, fragment))
return self.callback
def to_string(self):
"""Returns this token as a plain string, suitable for storage.
The resulting string includes the token's secret, so you should never
send or store this string where a third party can read it.
"""
items = [
('oauth_token', self.key),
('oauth_token_secret', self.secret),
("oauth_token", self.key),
("oauth_token_secret", self.secret),
]
if self.callback_confirmed is not None:
items.append(('oauth_callback_confirmed', self.callback_confirmed))
items.append(("oauth_callback_confirmed", self.callback_confirmed))
return urlencode(items)
@staticmethod
def from_string(s):
"""Deserializes a token from a string like one returned by
@ -294,19 +305,18 @@ class Token(object):
raise ValueError("Invalid parameter string.")
try:
key = params['oauth_token'][0]
key = params["oauth_token"][0]
except Exception:
raise ValueError("'oauth_token' not found in OAuth request.")
try:
secret = params['oauth_token_secret'][0]
secret = params["oauth_token_secret"][0]
except Exception:
raise ValueError("'oauth_token_secret' not found in "
"OAuth request.")
raise ValueError("'oauth_token_secret' not found in " "OAuth request.")
token = Token(key, secret)
try:
token.callback_confirmed = params['oauth_callback_confirmed'][0]
token.callback_confirmed = params["oauth_callback_confirmed"][0]
except KeyError:
pass # 1.0, no callback confirmed.
return token
@ -317,35 +327,40 @@ class Token(object):
def setter(attr):
name = attr.__name__
def getter(self):
try:
return self.__dict__[name]
except KeyError:
raise AttributeError(name)
def deleter(self):
del self.__dict__[name]
return property(getter, attr, deleter)
class Request(dict):
"""The parameters and information for an HTTP request, suitable for
authorizing with OAuth credentials.
When a consumer wants to access a service's protected resources, it does
so using a signed HTTP request identifying itself (the consumer) with its
key, and providing an access token authorized by the end user to access
those resources.
"""
version = OAUTH_VERSION
def __init__(self, method=HTTP_METHOD, url=None, parameters=None,
body=b'', is_form_encoded=False):
def __init__(
self,
method=HTTP_METHOD,
url=None,
parameters=None,
body=b"",
is_form_encoded=False,
):
if url is not None:
self.url = to_unicode(url)
self.method = method
@ -360,61 +375,59 @@ class Request(dict):
@setter
def url(self, value):
self.__dict__['url'] = value
self.__dict__["url"] = value
if value is not None:
scheme, netloc, path, query, fragment = urlsplit(value)
# Exclude default port numbers.
if scheme == 'http' and netloc[-3:] == ':80':
if scheme == "http" and netloc[-3:] == ":80":
netloc = netloc[:-3]
elif scheme == 'https' and netloc[-4:] == ':443':
elif scheme == "https" and netloc[-4:] == ":443":
netloc = netloc[:-4]
if scheme not in ('http', 'https'):
if scheme not in ("http", "https"):
raise ValueError("Unsupported URL %s (%s)." % (value, scheme))
# Normalized URL excludes params, query, and fragment.
self.normalized_url = urlunsplit((scheme, netloc, path, None, None))
else:
self.normalized_url = None
self.__dict__['url'] = None
self.__dict__["url"] = None
@setter
def method(self, value):
self.__dict__['method'] = value.upper()
self.__dict__["method"] = value.upper()
def _get_timestamp_nonce(self):
return self['oauth_timestamp'], self['oauth_nonce']
return self["oauth_timestamp"], self["oauth_nonce"]
def get_nonoauth_parameters(self):
"""Get any non-OAuth parameters."""
return dict([(k, v) for k, v in self.items()
if not k.startswith('oauth_')])
def to_header(self, realm=''):
return dict([(k, v) for k, v in self.items() if not k.startswith("oauth_")])
def to_header(self, realm=""):
"""Serialize as a header for an HTTPAuth request."""
oauth_params = ((k, v) for k, v in self.items()
if k.startswith('oauth_'))
oauth_params = ((k, v) for k, v in self.items() if k.startswith("oauth_"))
stringy_params = ((k, escape(v)) for k, v in oauth_params)
header_params = ('%s="%s"' % (k, v) for k, v in stringy_params)
params_header = ', '.join(header_params)
params_header = ", ".join(header_params)
auth_header = 'OAuth realm="%s"' % realm
if params_header:
auth_header = "%s, %s" % (auth_header, params_header)
return {'Authorization': auth_header}
return {"Authorization": auth_header}
def to_postdata(self):
"""Serialize as post data for a POST request."""
items = []
for k, v in sorted(self.items()): # predictable for testing
items.append((k.encode('utf-8'), to_utf8_optional_iterator(v)))
for k, v in sorted(self.items()): # predictable for testing
items.append((k.encode("utf-8"), to_utf8_optional_iterator(v)))
# tell urlencode to deal with sequence values and map them correctly
# to resulting querystring. for example self["k"] = ["v1", "v2"] will
# result in 'k=v1&k=v2' and not k=%5B%27v1%27%2C+%27v2%27%5D
return urlencode(items, True).replace('+', '%20')
return urlencode(items, True).replace("+", "%20")
def to_url(self):
"""Serialize as a URL for a GET request."""
base_url = urlparse(self.url)
@ -444,7 +457,7 @@ class Request(dict):
def get_parameter(self, parameter):
ret = self.get(parameter)
if ret is None:
raise Error('Parameter not found: %s' % parameter)
raise Error("Parameter not found: %s" % parameter)
return ret
@ -452,7 +465,7 @@ class Request(dict):
"""Return a string that contains the parameters that must be signed."""
items = []
for key, value in self.items():
if key == 'oauth_signature':
if key == "oauth_signature":
continue
# 1.0a/9.1.1 states that kvp must be sorted by key, then by value,
# so we unpack sequence values into multiple items for sorting.
@ -462,16 +475,23 @@ class Request(dict):
try:
value = list(value)
except TypeError as e:
assert 'is not iterable' in str(e)
assert "is not iterable" in str(e)
items.append((to_utf8_if_string(key), to_utf8_if_string(value)))
else:
items.extend((to_utf8_if_string(key), to_utf8_if_string(item)) for item in value)
items.extend(
(to_utf8_if_string(key), to_utf8_if_string(item))
for item in value
)
# Include any query string parameters from the provided URL
query = urlparse(self.url)[4]
url_items = self._split_url_string(query).items()
url_items = [(to_utf8(k), to_utf8_optional_iterator(v)) for k, v in url_items if k != 'oauth_signature' ]
url_items = [
(to_utf8(k), to_utf8_optional_iterator(v))
for k, v in url_items
if k != "oauth_signature"
]
items.extend(url_items)
items.sort()
@ -480,7 +500,7 @@ class Request(dict):
# spec draft 7, section 3.6
# (http://tools.ietf.org/html/draft-hammer-oauth-07#section-3.6)
# Spaces must be encoded with "%20" instead of "+"
return encoded_str.replace('+', '%20').replace('%7E', '~')
return encoded_str.replace("+", "%20").replace("%7E", "~")
def sign_request(self, signature_method, consumer, token):
"""Set the signature parameter to the result of sign."""
@ -492,133 +512,154 @@ class Request(dict):
# oauth_body_hash parameter on requests with form-encoded
# request bodies."
if not self.body:
self.body = ''
self['oauth_body_hash'] = base64.b64encode(sha1(to_utf8(self.body)).digest())
self.body = ""
self["oauth_body_hash"] = base64.b64encode(
sha1(to_utf8(self.body)).digest()
)
if 'oauth_consumer_key' not in self:
self['oauth_consumer_key'] = consumer.key
if "oauth_consumer_key" not in self:
self["oauth_consumer_key"] = consumer.key
if token and 'oauth_token' not in self:
self['oauth_token'] = token.key
if token and "oauth_token" not in self:
self["oauth_token"] = token.key
self["oauth_signature_method"] = signature_method.name
self["oauth_signature"] = signature_method.sign(self, consumer, token)
self['oauth_signature_method'] = signature_method.name
self['oauth_signature'] = signature_method.sign(self, consumer, token)
@classmethod
def make_timestamp(cls):
"""Get seconds since epoch (UTC)."""
return str(int(time.time()))
@classmethod
def make_nonce(cls):
"""Generate pseudorandom number."""
return str(random.SystemRandom().randint(0, 100000000))
@classmethod
def from_request(cls, http_method, http_url, headers=None, parameters=None,
query_string=None):
def from_request(
cls, http_method, http_url, headers=None, parameters=None, query_string=None
):
"""Combines multiple parameter sources."""
if parameters is None:
parameters = {}
# Headers
if headers:
auth_header = None
for k, v in headers.items():
if k.lower() == 'authorization' or \
k.upper() == 'HTTP_AUTHORIZATION':
if k.lower() == "authorization" or k.upper() == "HTTP_AUTHORIZATION":
auth_header = v
# Check that the authorization header is OAuth.
if auth_header and auth_header[:6] == 'OAuth ':
if auth_header and auth_header[:6] == "OAuth ":
auth_header = auth_header[6:]
try:
# Get the parameters from the header.
header_params = cls._split_header(auth_header)
parameters.update(header_params)
except:
raise Error('Unable to parse OAuth parameters from '
'Authorization header.')
raise Error(
"Unable to parse OAuth parameters from " "Authorization header."
)
# GET or POST query string.
if query_string:
query_params = cls._split_url_string(query_string)
parameters.update(query_params)
# URL parameters.
param_str = urlparse(http_url)[4] # query
param_str = urlparse(http_url)[4] # query
url_params = cls._split_url_string(param_str)
parameters.update(url_params)
if parameters:
return cls(http_method, http_url, parameters)
return None
@classmethod
def from_consumer_and_token(cls, consumer, token=None,
http_method=HTTP_METHOD, http_url=None, parameters=None,
body=b'', is_form_encoded=False):
def from_consumer_and_token(
cls,
consumer,
token=None,
http_method=HTTP_METHOD,
http_url=None,
parameters=None,
body=b"",
is_form_encoded=False,
):
if not parameters:
parameters = {}
defaults = {
'oauth_consumer_key': consumer.key,
'oauth_timestamp': cls.make_timestamp(),
'oauth_nonce': cls.make_nonce(),
'oauth_version': cls.version,
"oauth_consumer_key": consumer.key,
"oauth_timestamp": cls.make_timestamp(),
"oauth_nonce": cls.make_nonce(),
"oauth_version": cls.version,
}
defaults.update(parameters)
parameters = defaults
if token:
parameters['oauth_token'] = token.key
parameters["oauth_token"] = token.key
if token.verifier:
parameters['oauth_verifier'] = token.verifier
return cls(http_method, http_url, parameters, body=body,
is_form_encoded=is_form_encoded)
parameters["oauth_verifier"] = token.verifier
return cls(
http_method,
http_url,
parameters,
body=body,
is_form_encoded=is_form_encoded,
)
@classmethod
def from_token_and_callback(cls, token, callback=None,
http_method=HTTP_METHOD, http_url=None, parameters=None):
def from_token_and_callback(
cls,
token,
callback=None,
http_method=HTTP_METHOD,
http_url=None,
parameters=None,
):
if not parameters:
parameters = {}
parameters['oauth_token'] = token.key
parameters["oauth_token"] = token.key
if callback:
parameters['oauth_callback'] = callback
parameters["oauth_callback"] = callback
return cls(http_method, http_url, parameters)
@staticmethod
def _split_header(header):
"""Turn Authorization: header into parameters."""
params = {}
parts = header.split(',')
parts = header.split(",")
for param in parts:
# Ignore realm parameter.
if param.find('realm') > -1:
if param.find("realm") > -1:
continue
# Remove whitespace.
param = param.strip()
# Split key-value.
param_parts = param.split('=', 1)
param_parts = param.split("=", 1)
# Remove quotes and unescape the value.
params[param_parts[0]] = unquote(param_parts[1].strip('\"'))
params[param_parts[0]] = unquote(param_parts[1].strip('"'))
return params
@staticmethod
def _split_url_string(param_str):
"""Turn URL string into parameters."""
if not PY3:
# If passed unicode with quoted UTF8, Python2's parse_qs leaves
# mojibake'd uniocde after unquoting, so encode first.
param_str = b(param_str, 'utf-8')
param_str = b(param_str, "utf-8")
parameters = parse_qs(param_str, keep_blank_values=True)
for k, v in parameters.items():
if len(v) == 1:
@ -651,33 +692,48 @@ class Client(httplib2.Http):
self.method = method
def request(self, uri, method="GET", body=b'', headers=None,
redirections=httplib2.DEFAULT_MAX_REDIRECTS, connection_type=None):
DEFAULT_POST_CONTENT_TYPE = 'application/x-www-form-urlencoded'
def request(
self,
uri,
method="GET",
body=b"",
headers=None,
redirections=httplib2.DEFAULT_MAX_REDIRECTS,
connection_type=None,
):
DEFAULT_POST_CONTENT_TYPE = "application/x-www-form-urlencoded"
if not isinstance(headers, dict):
headers = {}
if method == "POST":
headers['Content-Type'] = headers.get('Content-Type',
DEFAULT_POST_CONTENT_TYPE)
headers["Content-Type"] = headers.get(
"Content-Type", DEFAULT_POST_CONTENT_TYPE
)
is_form_encoded = \
headers.get('Content-Type') == 'application/x-www-form-urlencoded'
is_form_encoded = (
headers.get("Content-Type") == "application/x-www-form-urlencoded"
)
if is_form_encoded and body:
parameters = parse_qs(body)
else:
parameters = None
req = Request.from_consumer_and_token(self.consumer,
token=self.token, http_method=method, http_url=uri,
parameters=parameters, body=body, is_form_encoded=is_form_encoded)
req = Request.from_consumer_and_token(
self.consumer,
token=self.token,
http_method=method,
http_url=uri,
parameters=parameters,
body=body,
is_form_encoded=is_form_encoded,
)
req.sign_request(self.method, self.consumer, self.token)
scheme, netloc, path, params, query, fragment = urlparse(uri)
realm = urlunparse((scheme, netloc, '', None, None, None))
realm = urlunparse((scheme, netloc, "", None, None, None))
if is_form_encoded:
body = req.to_postdata()
@ -686,21 +742,27 @@ class Client(httplib2.Http):
else:
headers.update(req.to_header(realm=realm))
return httplib2.Http.request(self, uri, method=method, body=body,
headers=headers, redirections=redirections,
connection_type=connection_type)
return httplib2.Http.request(
self,
uri,
method=method,
body=body,
headers=headers,
redirections=redirections,
connection_type=connection_type,
)
class Server(object):
"""A skeletal implementation of a service provider, providing protected
resources to requests from authorized consumers.
This class implements the logic to check requests for authorization. You
can use it with your web server or web framework to protect certain
resources with OAuth.
"""
timestamp_threshold = 300 # In seconds, five minutes.
timestamp_threshold = 300 # In seconds, five minutes.
version = OAUTH_VERSION
signature_methods = None
@ -719,20 +781,20 @@ class Server(object):
parameters = request.get_nonoauth_parameters()
return parameters
def build_authenticate_header(self, realm=''):
def build_authenticate_header(self, realm=""):
"""Optional support for the authenticate header."""
return {'WWW-Authenticate': 'OAuth realm="%s"' % realm}
return {"WWW-Authenticate": 'OAuth realm="%s"' % realm}
def _check_version(self, request):
"""Verify the correct version of the request for this server."""
version = self._get_version(request)
if version and version != self.version:
raise Error('OAuth version %s not supported.' % str(version))
raise Error("OAuth version %s not supported." % str(version))
def _get_version(self, request):
"""Return the version of the request for this server."""
try:
version = request.get_parameter('oauth_version')
version = request.get_parameter("oauth_version")
except:
version = OAUTH_VERSION
@ -740,7 +802,7 @@ class Server(object):
def _get_signature_method(self, request):
"""Figure out the signature with some defaults."""
signature_method = request.get('oauth_signature_method')
signature_method = request.get("oauth_signature_method")
if signature_method is None:
signature_method = SIGNATURE_METHOD
@ -748,21 +810,22 @@ class Server(object):
# Get the signature method object.
return self.signature_methods[signature_method]
except KeyError:
signature_method_names = ', '.join(self.signature_methods.keys())
raise Error('Signature method %s not supported try one of the '
'following: %s'
% (signature_method, signature_method_names))
signature_method_names = ", ".join(self.signature_methods.keys())
raise Error(
"Signature method %s not supported try one of the "
"following: %s" % (signature_method, signature_method_names)
)
def _check_signature(self, request, consumer, token):
timestamp, nonce = request._get_timestamp_nonce()
self._check_timestamp(timestamp)
signature_method = self._get_signature_method(request)
signature = request.get('oauth_signature')
signature = request.get("oauth_signature")
if signature is None:
raise MissingSignature('Missing oauth_signature.')
raise MissingSignature("Missing oauth_signature.")
if isinstance(signature, str):
signature = signature.encode('ascii', 'ignore')
signature = signature.encode("ascii", "ignore")
# Validate the signature.
valid = signature_method.check(request, consumer, token, signature)
@ -770,8 +833,9 @@ class Server(object):
if not valid:
key, base = signature_method.signing_base(request, consumer, token)
raise Error('Invalid signature. Expected signature base '
'string: %s' % base)
raise Error(
"Invalid signature. Expected signature base " "string: %s" % base
)
def _check_timestamp(self, timestamp):
"""Verify that timestamp is recentish."""
@ -779,21 +843,23 @@ class Server(object):
now = int(time.time())
lapsed = now - timestamp
if lapsed > self.timestamp_threshold:
raise Error('Expired timestamp: given %d and now %s has a '
'greater difference than threshold %d' % (timestamp, now,
self.timestamp_threshold))
raise Error(
"Expired timestamp: given %d and now %s has a "
"greater difference than threshold %d"
% (timestamp, now, self.timestamp_threshold)
)
class SignatureMethod(object):
"""A way of signing requests.
The OAuth protocol lets consumers and service providers pick a way to sign
requests. This interface shows the methods expected by the other `oauth`
modules for signing requests. Subclass it and implement its methods to
provide a new way to sign requests.
"""
def signing_base(self, request, consumer, token): #pragma NO COVER
def signing_base(self, request, consumer, token): # pragma NO COVER
"""Calculates the string that needs to be signed.
This method returns a 2-tuple containing the starting key for the
@ -803,7 +869,7 @@ class SignatureMethod(object):
"""
raise NotImplementedError
def sign(self, request, consumer, token): #pragma NO COVER
def sign(self, request, consumer, token): # pragma NO COVER
"""Returns the signature for the given request, based on the consumer
and token also provided.
@ -821,10 +887,10 @@ class SignatureMethod(object):
class SignatureMethod_HMAC_SHA1(SignatureMethod):
name = 'HMAC-SHA1'
name = "HMAC-SHA1"
def signing_base(self, request, consumer, token):
if (not hasattr(request, 'normalized_url') or request.normalized_url is None):
if not hasattr(request, "normalized_url") or request.normalized_url is None:
raise ValueError("Base URL for request is not set.")
sig = (
@ -833,11 +899,11 @@ class SignatureMethod_HMAC_SHA1(SignatureMethod):
escape(request.get_normalized_parameters()),
)
key = '%s&' % escape(consumer.secret)
key = "%s&" % escape(consumer.secret)
if token:
key += escape(token.secret)
raw = '&'.join(sig)
return key.encode('ascii'), raw.encode('ascii')
raw = "&".join(sig)
return key.encode("ascii"), raw.encode("ascii")
def sign(self, request, consumer, token):
"""Builds the base signature string."""
@ -851,16 +917,16 @@ class SignatureMethod_HMAC_SHA1(SignatureMethod):
class SignatureMethod_PLAINTEXT(SignatureMethod):
name = 'PLAINTEXT'
name = "PLAINTEXT"
def signing_base(self, request, consumer, token):
"""Concatenates the consumer key and secret with the token's
secret."""
sig = '%s&' % escape(consumer.secret)
sig = "%s&" % escape(consumer.secret)
if token:
sig = sig + escape(token.secret)
return sig, sig
def sign(self, request, consumer, token):
key, raw = self.signing_base(request, consumer, token)
return raw.encode('utf8')
return raw.encode("utf8")

View file

@ -1,30 +1,35 @@
try:
TEXT = unicode
except NameError: #pragma NO COVER Py3k
except NameError: # pragma NO COVER Py3k
PY3 = True
TEXT = str
STRING_TYPES = (str, bytes)
def b(x, encoding='ascii'):
def b(x, encoding="ascii"):
return bytes(x, encoding)
else: #pragma NO COVER Python2
else: # pragma NO COVER Python2
PY3 = False
STRING_TYPES = (unicode, bytes)
def b(x, encoding='ascii'):
def b(x, encoding="ascii"):
if isinstance(x, unicode):
x = x.encode(encoding)
return x
def u(x, encoding='ascii'):
if isinstance(x, TEXT): #pragma NO COVER
def u(x, encoding="ascii"):
if isinstance(x, TEXT): # pragma NO COVER
return x
try:
return x.decode(encoding)
except AttributeError: #pragma NO COVER
raise ValueError('WTF: %s' % x)
except AttributeError: # pragma NO COVER
raise ValueError("WTF: %s" % x)
try:
import urlparse
except ImportError: #pragma NO COVER Py3k
except ImportError: # pragma NO COVER Py3k
from urllib.parse import parse_qs
from urllib.parse import parse_qsl
from urllib.parse import quote
@ -35,7 +40,7 @@ except ImportError: #pragma NO COVER Py3k
from urllib.parse import urlunsplit
from urllib.parse import urlparse
from urllib.parse import urlunparse
else: #pragma NO COVER Python2
else: # pragma NO COVER Python2
from urlparse import parse_qs
from urlparse import parse_qsl
from urllib import quote
@ -45,4 +50,5 @@ else: #pragma NO COVER Python2
from urlparse import urlunsplit
from urlparse import urlparse
from urlparse import urlunparse
unquote_to_bytes = unquote

View file

@ -3,17 +3,16 @@
manual_verstr = "1.9"
auto_build_num = "0.post1"
verstr = manual_verstr + "." + auto_build_num
try:
from pyutil.version_class import Version as pyutil_Version
except (ImportError, ValueError): #pragma NO COVER
except (ImportError, ValueError): # pragma NO COVER
# Maybe there is no pyutil installed.
from distutils.version import LooseVersion as distutils_Version
__version__ = distutils_Version(verstr)
else: #pragma NO COVER
else: # pragma NO COVER
__version__ = pyutil_Version(verstr)

View file

@ -36,5 +36,6 @@ class IMAP4_SSL(imaplib.IMAP4_SSL):
if token is not None and not isinstance(token, oauth2.Token):
raise ValueError("Invalid token.")
imaplib.IMAP4_SSL.authenticate(self, 'XOAUTH',
lambda x: oauth2.build_xoauth_string(url, consumer, token))
imaplib.IMAP4_SSL.authenticate(
self, "XOAUTH", lambda x: oauth2.build_xoauth_string(url, consumer, token)
)

View file

@ -37,5 +37,8 @@ class SMTP(smtplib.SMTP):
if token is not None and not isinstance(token, oauth2.Token):
raise ValueError("Invalid token.")
self.docmd('AUTH', 'XOAUTH %s' % \
base64.b64encode(oauth2.build_xoauth_string(url, consumer, token)))
self.docmd(
"AUTH",
"XOAUTH %s"
% base64.b64encode(oauth2.build_xoauth_string(url, consumer, token)),
)