Package restkit :: Package filters :: Module oauth2
[hide private]
[frames] | no frames]

Source Code for Module restkit.filters.oauth2

  1  # -*- coding: utf-8 - 
  2  # 
  3  # This file is part of restkit released under the MIT license.  
  4  # See the NOTICE for more information. 
  5   
  6  import re 
  7  import urlparse 
  8  try: 
  9      from urlparse import parse_qsl, urlunparse 
 10  except ImportError: 
 11      from cgi import parse_qsl, urlunparse 
 12       
 13  from restkit.util.oauth2 import Consumer, Request, SignatureMethod_HMAC_SHA1,\ 
 14  Token 
 15   
16 -def validate_consumer(consumer):
17 """ validate a consumer agains oauth2.Consumer object """ 18 if not isinstance(consumer, Consumer): 19 raise ValueError("Invalid consumer.") 20 return consumer
21
22 -def validate_token(token):
23 """ validate a token agains oauth2.Token object """ 24 if token is not None and not isinstance(token, Token): 25 raise ValueError("Invalid token.") 26 return token
27 28
29 -class OAuthFilter(object):
30
31 - def __init__(self, path, consumer, token=None, method=None):
32 """ Init OAuthFilter 33 34 :param path: path or regexp. * mean all path on wicth oauth can be 35 applied. 36 :param consumer: oauth consumer, instance of oauth2.Consumer 37 :param token: oauth token, instance of oauth2.Token 38 :param method: oauth signature method 39 40 token and method signature are optionnals. Consumer should be an 41 instance of `oauth2.Consumer`, token an instance of `oauth2.Toke` 42 signature method an instance of `oauth2.SignatureMethod`. 43 44 """ 45 46 if path.endswith('*'): 47 self.match = re.compile("%s.*" % path.rsplit('*', 1)[0]) 48 else: 49 self.match = re.compile("%s$" % path) 50 self.consumer = validate_consumer(consumer) 51 self.token = validate_token(token) 52 self.method = method or SignatureMethod_HMAC_SHA1()
53
54 - def on_path(self, req):
55 path = req.uri.path or "/" 56 return (self.match.match(path) is not None)
57
58 - def on_request(self, req, tries):
59 if tries < 2: 60 return 61 62 if not self.on_path(req): 63 return 64 65 headers = dict(req.headers) 66 params = {} 67 form = False 68 if req.body and req.body is not None: 69 ctype = headers.get('Content-Type') 70 if ctype is not None and \ 71 ctype.startswith('application/x-www-form-urlencoded'): 72 # we are in a form try to get oauth params from here 73 form = True 74 params = dict(parse_qsl(req.body)) 75 76 # update params from quey parameters 77 params.update(parse_qsl(req.uri.query)) 78 79 raw_url = urlunparse((req.uri.scheme, req.uri.netloc, 80 req.uri.path, '', '', '')) 81 82 oauth_req = Request.from_consumer_and_token(self.consumer, 83 token=self.token, http_method=req.method, 84 http_url=raw_url, parameters=params) 85 86 oauth_req.sign_request(self.method, self.consumer, self.token) 87 88 if form: 89 req.body = oauth_req.to_postdata() 90 elif req.method in ('GET', 'HEAD'): 91 req.url = req.final_url = oauth_req.to_url() 92 req.uri = urlparse.urlparse(req.url) 93 else: 94 oauth_headers = oauth_req.to_header() 95 for k, v in list(oauth_headers.items()): 96 if not isinstance(v, basestring): 97 v = str(v) 98 req.headers.append((k.title(), v))
99