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

Source Code for Module restkit.filters

  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 base64 
  7  import re 
  8  try: 
  9      from urlparse import parse_qsl 
 10  except ImportError: 
 11      from cgi import parse_qsl 
 12  from urlparse import urlunparse 
 13   
 14  from .oauth2 import Request, SignatureMethod_HMAC_SHA1 
 15   
16 -class BasicAuth(object):
17 """ Simple filter to manage basic authentification""" 18
19 - def __init__(self, username, password):
20 self.credentials = (username, password)
21
22 - def on_request(self, request):
23 encode = base64.b64encode("%s:%s" % self.credentials) 24 request.headers['Authorization'] = 'Basic %s' % encode
25
26 -def validate_consumer(consumer):
27 """ validate a consumer agains oauth2.Consumer object """ 28 if not hasattr(consumer, "key"): 29 raise ValueError("Invalid consumer.") 30 return consumer
31
32 -def validate_token(token):
33 """ validate a token agains oauth2.Token object """ 34 if token is not None and not hasattr(token, "key"): 35 raise ValueError("Invalid token.") 36 return token
37 38
39 -class OAuthFilter(object):
40 """ oauth filter """ 41
42 - def __init__(self, path, consumer, token=None, method=None):
43 """ Init OAuthFilter 44 45 :param path: path or regexp. * mean all path on wicth oauth can be 46 applied. 47 :param consumer: oauth consumer, instance of oauth2.Consumer 48 :param token: oauth token, instance of oauth2.Token 49 :param method: oauth signature method 50 51 token and method signature are optionnals. Consumer should be an 52 instance of `oauth2.Consumer`, token an instance of `oauth2.Toke` 53 signature method an instance of `oauth2.SignatureMethod`. 54 55 """ 56 57 if path.endswith('*'): 58 self.match = re.compile("%s.*" % path.rsplit('*', 1)[0]) 59 else: 60 self.match = re.compile("%s$" % path) 61 self.consumer = validate_consumer(consumer) 62 self.token = validate_token(token) 63 self.method = method or SignatureMethod_HMAC_SHA1()
64
65 - def on_path(self, request):
66 path = request.parsed_url.path or "/" 67 return (self.match.match(path) is not None)
68
69 - def on_request(self, request):
70 if not self.on_path(request): 71 return 72 73 params = {} 74 form = False 75 parsed_url = request.parsed_url 76 77 if request.body and request.body is not None: 78 ctype = request.headers.iget('content-type') 79 if ctype is not None and \ 80 ctype.startswith('application/x-www-form-urlencoded'): 81 # we are in a form try to get oauth params from here 82 form = True 83 params = dict(parse_qsl(request.body)) 84 85 # update params from quey parameters 86 params.update(parse_qsl(parsed_url.query)) 87 88 raw_url = urlunparse((parsed_url.scheme, parsed_url.netloc, 89 parsed_url.path, '', '', '')) 90 91 oauth_req = Request.from_consumer_and_token(self.consumer, 92 token=self.token, http_method=request.method, 93 http_url=raw_url, parameters=params) 94 95 oauth_req.sign_request(self.method, self.consumer, self.token) 96 97 if form: 98 request.body = oauth_req.to_postdata() 99 request.headers['Content-Length'] = len(request.body) 100 elif request.method in ('GET', 'HEAD'): 101 request.original_url = request.url 102 request.url = oauth_req.to_url() 103 else: 104 oauth_headers = oauth_req.to_header() 105 request.headers.update(oauth_headers)
106