Skip to content

Commit 35aba71

Browse files
committed
PEP8 compliance.
1 parent 0a60198 commit 35aba71

File tree

1 file changed

+55
-10
lines changed

1 file changed

+55
-10
lines changed

oauth/__init__.py

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
class Error(RuntimeError):
4040
"""Generic exception class."""
41+
4142
def __init__(self, message='OAuth error occured.'):
4243
self._message = message
4344

@@ -110,6 +111,7 @@ def __init__(self, key, secret):
110111
if self.key is None or self.secret is None:
111112
raise ValueError("Key and secret must be set.")
112113

114+
113115
class Token(object):
114116
"""An OAuth credential used to request authorization or a protected
115117
resource.
@@ -197,7 +199,8 @@ def from_string(s):
197199
try:
198200
secret = params['oauth_token_secret'][0]
199201
except Exception:
200-
raise ValueError("'oauth_token_secret' not found in OAuth request.")
202+
raise ValueError("'oauth_token_secret' not found in "
203+
"OAuth request.")
201204

202205
token = Token(key, secret)
203206
try:
@@ -209,6 +212,7 @@ def from_string(s):
209212
def __str__(self):
210213
return self.to_string()
211214

215+
212216
def setter(setter):
213217
name = setter.__name__
214218

@@ -223,6 +227,7 @@ def deleter(self):
223227

224228
return property(getter, setter, deleter)
225229

230+
226231
class Request(dict):
227232

228233
"""The parameters and information for an HTTP request, suitable for
@@ -270,11 +275,13 @@ def _get_timestamp_nonce(self):
270275

271276
def get_nonoauth_parameters(self):
272277
"""Get any non-OAuth parameters."""
273-
return dict([(k, v) for k, v in self.iteritems() if not k.startswith('oauth_')])
278+
return dict([(k, v) for k, v in self.iteritems()
279+
if not k.startswith('oauth_')])
274280

275281
def to_header(self, realm=''):
276282
"""Serialize as a header for an HTTPAuth request."""
277-
oauth_params = ((k, v) for k, v in self.iteritems() if k.startswith('oauth_'))
283+
oauth_params = ((k, v) for k, v in self.iteritems()
284+
if k.startswith('oauth_'))
278285
stringy_params = ((k, escape(str(v))) for k, v in oauth_params)
279286
header_params = ('%s="%s"' % (k, v) for k, v in stringy_params)
280287
params_header = ', '.join(header_params)
@@ -371,7 +378,8 @@ def from_consumer_and_token(cls, oauth_consumer, token=None,
371378
return OAuthRequest(http_method, http_url, parameters)
372379

373380
@classmethod
374-
def from_token_and_callback(cls, token, callback=None, http_method=HTTP_METHOD,
381+
def from_token_and_callback(cls, token, callback=None,
382+
http_method=HTTP_METHOD,
375383
http_url=None, parameters=None):
376384
if not parameters:
377385
parameters = {}
@@ -410,7 +418,20 @@ def _split_url_string(param_str):
410418

411419

412420
class Server(object):
413-
"""A worker to check the validity of a request against a data store."""
421+
"""A skeletal implementation of a service provider, providing protected
422+
resources to requests from authorized consumers.
423+
424+
This class implements the logic to check requests for authorization. You
425+
can use it with your web server or web framework to protect certain
426+
resources with OAuth.
427+
428+
As this class has no knowledge of how your application stores data, you
429+
have to give it an object it can use to load OAuth objects. Implement a
430+
subclass of `oauth.interface.DataStore` for your storage system and supply
431+
it to the `Server` instance as `data_store`.
432+
433+
"""
434+
414435
timestamp_threshold = 300 # In seconds, five minutes.
415436
version = VERSION
416437
signature_methods = None
@@ -463,7 +484,9 @@ def fetch_access_token(self, oauth_request):
463484
# Get the request token.
464485
token = self._get_token(oauth_request, 'request')
465486
self._check_signature(oauth_request, consumer, token)
466-
new_token = self.data_store.fetch_access_token(consumer, token, verifier)
487+
new_token = self.data_store.fetch_access_token(consumer,
488+
token, verifier)
489+
467490
return new_token
468491

469492
def verify_request(self, oauth_request):
@@ -539,19 +562,25 @@ def _check_signature(self, oauth_request, consumer, token):
539562
self._check_timestamp(timestamp)
540563
self._check_nonce(consumer, token, nonce)
541564
signature_method = self._get_signature_method(oauth_request)
565+
542566
try:
543567
signature = oauth_request.get_parameter('oauth_signature')
544568
except:
545569
raise Error('Missing signature.')
570+
546571
# Validate the signature.
547572
valid_sig = signature_method.check_signature(oauth_request, consumer,
548573
token, signature)
574+
549575
if not valid_sig:
550576
key, base = signature_method.build_signature_base_string(
551577
oauth_request, consumer, token)
578+
552579
raise Error('Invalid signature. Expected signature base '
553580
'string: %s' % base)
554-
built = signature_method.build_signature(oauth_request, consumer, token)
581+
582+
built = signature_method.build_signature(oauth_request,
583+
consumer, token)
555584

556585
def _check_timestamp(self, timestamp):
557586
"""Verify that timestamp is recentish."""
@@ -599,7 +628,15 @@ def access_resource(self, oauth_request):
599628

600629

601630
class DataStore(object):
602-
"""A database abstraction used to lookup consumers and tokens."""
631+
"""A database abstraction used to lookup consumers and tokens.
632+
633+
To use your backend store with the `oauth` module, implement a subclass of
634+
this class that performs its methods using your database or storage
635+
system. Then, when using `oauth.Server`, supply it with an instance of
636+
your custom `DataStore` class to have objects stored in natively in your
637+
own data store.
638+
639+
"""
603640

604641
def lookup_consumer(self, key):
605642
"""-> OAuthConsumer."""
@@ -627,12 +664,20 @@ def authorize_request_token(self, oauth_token, user):
627664

628665

629666
class SignatureMethod(object):
630-
"""A strategy class that implements a signature method."""
667+
"""A way of signing requests.
668+
669+
The OAuth protocol lets consumers and service providers pick a way to sign
670+
requests. This interface shows the methods expected by the other `oauth`
671+
modules for signing requests. Subclass it and implement its methods to
672+
provide a new way to sign requests.
673+
"""
674+
631675
def get_name(self):
632676
"""-> str."""
633677
raise NotImplementedError
634678

635-
def build_signature_base_string(self, oauth_request, oauth_consumer, oauth_token):
679+
def build_signature_base_string(self, oauth_request,
680+
oauth_consumer, oauth_token):
636681
"""-> str key, str raw."""
637682
raise NotImplementedError
638683

0 commit comments

Comments
 (0)