@@ -205,8 +205,8 @@ def __str__(self):
205
205
return self .to_string ()
206
206
207
207
208
- def setter (setter ):
209
- name = setter .__name__
208
+ def setter (attr ):
209
+ name = attr .__name__
210
210
211
211
def getter (self ):
212
212
try :
@@ -217,7 +217,7 @@ def getter(self):
217
217
def deleter (self ):
218
218
del self .__dict__ [name ]
219
219
220
- return property (getter , setter , deleter )
220
+ return property (getter , attr , deleter )
221
221
222
222
223
223
class Request (dict ):
@@ -291,6 +291,11 @@ def to_postdata(self):
291
291
def to_url (self ):
292
292
"""Serialize as a URL for a GET request."""
293
293
return '%s?%s' % (self .url , self .to_postdata ())
294
+
295
+ def get_parameter (self , parameter ):
296
+ ret = self .get (parameter )
297
+ if ret is None :
298
+ raise Error ('Parameter not found: %s' % parameter )
294
299
295
300
def get_normalized_parameters (self ):
296
301
"""Return a string that contains the parameters that must be signed."""
@@ -416,73 +421,22 @@ class Server(object):
416
421
This class implements the logic to check requests for authorization. You
417
422
can use it with your web server or web framework to protect certain
418
423
resources with OAuth.
419
-
420
- As this class has no knowledge of how your application stores data, you
421
- have to give it an object it can use to load OAuth objects. Implement a
422
- subclass of `oauth.interface.DataStore` for your storage system and supply
423
- it to the `Server` instance as `data_store`.
424
424
"""
425
425
426
426
timestamp_threshold = 300 # In seconds, five minutes.
427
427
version = VERSION
428
428
signature_methods = None
429
- data_store = None
430
429
431
- def __init__ (self , data_store = None , signature_methods = None ):
432
- self .data_store = data_store
430
+ def __init__ (self , signature_methods = None ):
433
431
self .signature_methods = signature_methods or {}
434
432
435
- def set_data_store (self , data_store ):
436
- self .data_store = data_store
437
-
438
- def get_data_store (self ):
439
- return self .data_store
440
-
441
433
def add_signature_method (self , signature_method ):
442
434
self .signature_methods [signature_method .name ] = signature_method
443
435
return self .signature_methods
444
436
445
- def fetch_request_token (self , oauth_request ):
446
- """Processes a request_token request and returns the
447
- request token on success.
448
- """
449
- try :
450
- # Get the request token for authorization.
451
- token = self ._get_token (oauth_request , 'request' )
452
- except Error :
453
- # No token required for the initial token request.
454
- version = self ._get_version (oauth_request )
455
- consumer = self ._get_consumer (oauth_request )
456
- try :
457
- callback = self .get_callback (oauth_request )
458
- except Error :
459
- callback = None # 1.0, no callback specified.
460
- self ._check_signature (oauth_request , consumer , None )
461
- # Fetch a new token.
462
- token = self .data_store .fetch_request_token (consumer , callback )
463
- return token
464
-
465
- def fetch_access_token (self , oauth_request ):
466
- """Processes an access_token request and returns the
467
- access token on success.
468
- """
469
- version = self ._get_version (oauth_request )
470
- consumer = self ._get_consumer (oauth_request )
471
- try :
472
- verifier = self ._get_verifier (oauth_request )
473
- except Error :
474
- verifier = None
475
- # Get the request token.
476
- token = self ._get_token (oauth_request , 'request' )
477
- self ._check_signature (oauth_request , consumer , token )
478
- new_token = self .data_store .fetch_access_token (consumer ,
479
- token , verifier )
480
-
481
- return new_token
482
-
483
- def verify_request (self , oauth_request ):
437
+ def verify_request (self , request , consumer , token ):
484
438
"""Verifies an api call and checks all the parameters."""
485
- # -> consumer and token
439
+
486
440
version = self ._get_version (oauth_request )
487
441
consumer = self ._get_consumer (oauth_request )
488
442
# Get the access token.
@@ -491,33 +445,26 @@ def verify_request(self, oauth_request):
491
445
parameters = oauth_request .get_nonoauth_parameters ()
492
446
return consumer , token , parameters
493
447
494
- def authorize_token (self , token , user ):
495
- """Authorize a request token."""
496
- return self .data_store .authorize_request_token (token , user )
497
-
498
- def get_callback (self , oauth_request ):
499
- """Get the callback URL."""
500
- return oauth_request .get_parameter ('oauth_callback' )
501
-
502
448
def build_authenticate_header (self , realm = '' ):
503
449
"""Optional support for the authenticate header."""
504
450
return {'WWW-Authenticate' : 'OAuth realm="%s"' % realm }
505
451
506
- def _get_version (self , oauth_request ):
452
+ def _get_version (self , request ):
507
453
"""Verify the correct version request for this server."""
508
454
try :
509
- version = oauth_request .get_parameter ('oauth_version' )
455
+ version = request .get_parameter ('oauth_version' )
510
456
except :
511
457
version = VERSION
458
+
512
459
if version and version != self .version :
513
460
raise Error ('OAuth version %s not supported.' % str (version ))
461
+
514
462
return version
515
463
516
- def _get_signature_method (self , oauth_request ):
464
+ def _get_signature_method (self , request ):
517
465
"""Figure out the signature with some defaults."""
518
466
try :
519
- signature_method = oauth_request .get_parameter (
520
- 'oauth_signature_method' )
467
+ signature_method = request .get_parameter ('oauth_signature_method' )
521
468
except :
522
469
signature_method = SIGNATURE_METHOD
523
470
try :
@@ -530,48 +477,29 @@ def _get_signature_method(self, oauth_request):
530
477
531
478
return signature_method
532
479
533
- def _get_consumer (self , oauth_request ):
534
- consumer_key = oauth_request .get_parameter ('oauth_consumer_key' )
535
- consumer = self .data_store .lookup_consumer (consumer_key )
536
- if not consumer :
537
- raise Error ('Invalid consumer.' )
538
- return consumer
539
-
540
- def _get_token (self , oauth_request , token_type = 'access' ):
541
- """Try to find the token for the provided request token key."""
542
- token_field = oauth_request .get_parameter ('oauth_token' )
543
- token = self .data_store .lookup_token (token_type , token_field )
544
- if not token :
545
- raise Error ('Invalid %s token: %s' % (token_type , token_field ))
546
- return token
547
-
548
- def _get_verifier (self , oauth_request ):
549
- return oauth_request .get_parameter ('oauth_verifier' )
480
+ def _get_verifier (self , request ):
481
+ return request .get_parameter ('oauth_verifier' )
550
482
551
- def _check_signature (self , oauth_request , consumer , token ):
552
- timestamp , nonce = oauth_request ._get_timestamp_nonce ()
483
+ def _check_signature (self , request , consumer , token ):
484
+ timestamp , nonce = request ._get_timestamp_nonce ()
553
485
self ._check_timestamp (timestamp )
554
- self ._check_nonce (consumer , token , nonce )
555
- signature_method = self ._get_signature_method (oauth_request )
486
+ signature_method = self ._get_signature_method (request )
556
487
557
488
try :
558
- signature = oauth_request .get_parameter ('oauth_signature' )
489
+ signature = request .get_parameter ('oauth_signature' )
559
490
except :
560
491
raise Error ('Missing signature.' )
561
492
562
493
# Validate the signature.
563
- valid_sig = signature_method .check_signature (oauth_request , consumer ,
564
- token , signature )
494
+ valid = signature_method .check (request , consumer , token , signature )
565
495
566
- if not valid_sig :
567
- key , base = signature_method .signing_base (
568
- oauth_request , consumer , token )
496
+ if not valid :
497
+ key , base = signature_method .signing_base (request , consumer , token )
569
498
570
499
raise Error ('Invalid signature. Expected signature base '
571
500
'string: %s' % base )
572
501
573
- built = signature_method .sign (oauth_request ,
574
- consumer , token )
502
+ built = signature_method .sign (request , consumer , token )
575
503
576
504
def _check_timestamp (self , timestamp ):
577
505
"""Verify that timestamp is recentish."""
@@ -583,12 +511,6 @@ def _check_timestamp(self, timestamp):
583
511
'greater difference than threshold %d' %
584
512
(timestamp , now , self .timestamp_threshold ))
585
513
586
- def _check_nonce (self , consumer , token , nonce ):
587
- """Verify that the nonce is uniqueish."""
588
- nonce = self .data_store .lookup_nonce (consumer , token , nonce )
589
- if nonce :
590
- raise Error ('Nonce already used: %s' % str (nonce ))
591
-
592
514
593
515
class Client (object ):
594
516
"""OAuthClient is a worker to attempt to execute a request."""
@@ -618,42 +540,6 @@ def access_resource(self, oauth_request):
618
540
raise NotImplementedError
619
541
620
542
621
- class DataStore (object ):
622
- """A database abstraction used to lookup consumers and tokens.
623
-
624
- To use your backend store with the `oauth` module, implement a subclass of
625
- this class that performs its methods using your database or storage
626
- system. Then, when using `oauth.Server`, supply it with an instance of
627
- your custom `DataStore` class to have objects stored in natively in your
628
- own data store.
629
-
630
- """
631
-
632
- def lookup_consumer (self , key ):
633
- """-> OAuthConsumer."""
634
- raise NotImplementedError
635
-
636
- def lookup_token (self , oauth_consumer , token_type , token_token ):
637
- """-> OAuthToken."""
638
- raise NotImplementedError
639
-
640
- def lookup_nonce (self , oauth_consumer , oauth_token , nonce ):
641
- """-> OAuthToken."""
642
- raise NotImplementedError
643
-
644
- def fetch_request_token (self , oauth_consumer , oauth_callback ):
645
- """-> OAuthToken."""
646
- raise NotImplementedError
647
-
648
- def fetch_access_token (self , oauth_consumer , oauth_token , oauth_verifier ):
649
- """-> OAuthToken."""
650
- raise NotImplementedError
651
-
652
- def authorize_request_token (self , oauth_token , user ):
653
- """-> OAuthToken."""
654
- raise NotImplementedError
655
-
656
-
657
543
class SignatureMethod (object ):
658
544
"""A way of signing requests.
659
545
@@ -748,7 +634,6 @@ def sign(self, request, consumer, token):
748
634
OAuthRequest = Request
749
635
OAuthServer = Server
750
636
OAuthClient = Client
751
- OAuthDataStore = DataStore
752
637
OAuthSignatureMethod = SignatureMethod
753
638
OAuthSignatureMethod_HMAC_SHA1 = SignatureMethod_HMAC_SHA1
754
639
OAuthSignatureMethod_PLAINTEXT = SignatureMethod_PLAINTEXT
0 commit comments