mirror of
https://github.com/rembo10/headphones.git
synced 2026-07-20 07:54:01 +01:00
Dropping trailing whitespaces
This commit is contained in:
+60
-60
@@ -85,11 +85,11 @@ def generate_verifier(length=8):
|
||||
|
||||
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
|
||||
@@ -97,7 +97,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.
|
||||
@@ -125,12 +125,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
|
||||
@@ -175,7 +175,7 @@ class Token(object):
|
||||
|
||||
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.
|
||||
"""
|
||||
@@ -188,7 +188,7 @@ class Token(object):
|
||||
if self.callback_confirmed is not None:
|
||||
data['oauth_callback_confirmed'] = self.callback_confirmed
|
||||
return urllib.urlencode(data)
|
||||
|
||||
|
||||
@staticmethod
|
||||
def from_string(s):
|
||||
"""Deserializes a token from a string like one returned by
|
||||
@@ -209,7 +209,7 @@ class Token(object):
|
||||
try:
|
||||
secret = params['oauth_token_secret'][0]
|
||||
except Exception:
|
||||
raise ValueError("'oauth_token_secret' not found in "
|
||||
raise ValueError("'oauth_token_secret' not found in "
|
||||
"OAuth request.")
|
||||
|
||||
token = Token(key, secret)
|
||||
@@ -225,45 +225,45 @@ 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.
|
||||
|
||||
|
||||
"""
|
||||
|
||||
|
||||
http_method = HTTP_METHOD
|
||||
http_url = None
|
||||
version = VERSION
|
||||
|
||||
|
||||
def __init__(self, method=HTTP_METHOD, url=None, parameters=None):
|
||||
if method is not None:
|
||||
self.method = method
|
||||
|
||||
|
||||
if url is not None:
|
||||
self.url = url
|
||||
|
||||
|
||||
if parameters is not None:
|
||||
self.update(parameters)
|
||||
|
||||
|
||||
@setter
|
||||
def url(self, value):
|
||||
parts = urlparse.urlparse(value)
|
||||
@@ -280,33 +280,33 @@ class Request(dict):
|
||||
|
||||
value = '%s://%s%s' % (scheme, netloc, path)
|
||||
self.__dict__['url'] = value
|
||||
|
||||
|
||||
@setter
|
||||
def method(self, value):
|
||||
self.__dict__['method'] = value.upper()
|
||||
|
||||
|
||||
def _get_timestamp_nonce(self):
|
||||
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.iteritems()
|
||||
return dict([(k, v) for k, v in self.iteritems()
|
||||
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()
|
||||
oauth_params = ((k, v) for k, v in self.items()
|
||||
if k.startswith('oauth_'))
|
||||
stringy_params = ((k, escape(str(v))) for k, v in oauth_params)
|
||||
header_params = ('%s="%s"' % (k, v) for k, v in stringy_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}
|
||||
|
||||
|
||||
def to_postdata(self):
|
||||
"""Serialize as post data for a POST request."""
|
||||
return self.encode_postdata(self)
|
||||
@@ -327,7 +327,7 @@ class Request(dict):
|
||||
raise Error('Parameter not found: %s' % parameter)
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
def get_normalized_parameters(self):
|
||||
"""Return a string that contains the parameters that must be signed."""
|
||||
items = [(k, v) for k, v in self.items() if k != 'oauth_signature']
|
||||
@@ -337,7 +337,7 @@ class Request(dict):
|
||||
# (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')
|
||||
|
||||
|
||||
def sign_request(self, signature_method, consumer, token):
|
||||
"""Set the signature parameter to the result of sign."""
|
||||
|
||||
@@ -349,24 +349,24 @@ class Request(dict):
|
||||
|
||||
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.randint(0, 100000000))
|
||||
|
||||
|
||||
@classmethod
|
||||
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 and 'Authorization' in headers:
|
||||
auth_header = headers['Authorization']
|
||||
@@ -380,57 +380,57 @@ class Request(dict):
|
||||
except:
|
||||
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.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):
|
||||
if not parameters:
|
||||
parameters = {}
|
||||
|
||||
|
||||
defaults = {
|
||||
'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
|
||||
|
||||
|
||||
return Request(http_method, http_url, parameters)
|
||||
|
||||
|
||||
@classmethod
|
||||
def from_token_and_callback(cls, token, callback=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
|
||||
|
||||
|
||||
if callback:
|
||||
parameters['oauth_callback'] = callback
|
||||
|
||||
|
||||
return cls(http_method, http_url, parameters)
|
||||
|
||||
|
||||
@staticmethod
|
||||
def _split_header(header):
|
||||
"""Turn Authorization: header into parameters."""
|
||||
@@ -447,7 +447,7 @@ class Request(dict):
|
||||
# Remove quotes and unescape the value.
|
||||
params[param_parts[0]] = urllib.unquote(param_parts[1].strip('\"'))
|
||||
return params
|
||||
|
||||
|
||||
@staticmethod
|
||||
def _split_url_string(param_str):
|
||||
"""Turn URL string into parameters."""
|
||||
@@ -460,7 +460,7 @@ class Request(dict):
|
||||
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.
|
||||
@@ -536,7 +536,7 @@ class Server(object):
|
||||
if not valid:
|
||||
key, base = signature_method.signing_base(request, consumer, token)
|
||||
|
||||
raise Error('Invalid signature. Expected signature base '
|
||||
raise Error('Invalid signature. Expected signature base '
|
||||
'string: %s' % base)
|
||||
|
||||
built = signature_method.sign(request, consumer, token)
|
||||
@@ -567,7 +567,7 @@ class Client(httplib2.Http):
|
||||
self.token = token
|
||||
self.method = SignatureMethod_HMAC_SHA1()
|
||||
|
||||
httplib2.Http.__init__(self, cache=cache, timeout=timeout,
|
||||
httplib2.Http.__init__(self, cache=cache, timeout=timeout,
|
||||
proxy_info=proxy_info)
|
||||
|
||||
def set_signature_method(self, method):
|
||||
@@ -576,10 +576,10 @@ class Client(httplib2.Http):
|
||||
|
||||
self.method = method
|
||||
|
||||
def request(self, uri, method="GET", body=None, headers=None,
|
||||
def request(self, uri, method="GET", body=None, headers=None,
|
||||
redirections=httplib2.DEFAULT_MAX_REDIRECTS, connection_type=None,
|
||||
force_auth_header=False):
|
||||
|
||||
|
||||
if not isinstance(headers, dict):
|
||||
headers = {}
|
||||
|
||||
@@ -587,7 +587,7 @@ class Client(httplib2.Http):
|
||||
parameters = dict(parse_qsl(body))
|
||||
elif method == "GET":
|
||||
parsed = urlparse.urlparse(uri)
|
||||
parameters = parse_qs(parsed.query)
|
||||
parameters = parse_qs(parsed.query)
|
||||
else:
|
||||
parameters = None
|
||||
|
||||
@@ -614,14 +614,14 @@ class Client(httplib2.Http):
|
||||
# don't call update twice.
|
||||
headers.update(req.to_header())
|
||||
|
||||
return httplib2.Http.request(self, uri, method=method, body=body,
|
||||
headers=headers, redirections=redirections,
|
||||
return httplib2.Http.request(self, uri, method=method, body=body,
|
||||
headers=headers, redirections=redirections,
|
||||
connection_type=connection_type)
|
||||
|
||||
|
||||
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
|
||||
@@ -657,7 +657,7 @@ class SignatureMethod(object):
|
||||
|
||||
class SignatureMethod_HMAC_SHA1(SignatureMethod):
|
||||
name = 'HMAC-SHA1'
|
||||
|
||||
|
||||
def signing_base(self, request, consumer, token):
|
||||
sig = (
|
||||
escape(request.method),
|
||||
|
||||
Reference in New Issue
Block a user