]> jfr.im git - z_archive/twitter.git/blob - twitter/auth.py
Merge branch 'master' into jordan_patch
[z_archive/twitter.git] / twitter / auth.py
1 import urllib
2 from base64 import encodestring
3
4 class Auth(object):
5 """
6 ABC for Authenticator objects.
7 """
8
9 def encode_params(self, base_url, method, params):
10 """Encodes parameters for a request suitable for including in a URL
11 or POST body. This method may also add new params to the request
12 if required by the authentication scheme in use."""
13 raise NotImplementedError
14
15 def generate_headers(self):
16 """Generates headers which should be added to the request if required
17 by the authentication scheme in use."""
18 raise NotImplementedError
19
20
21 class UserPassAuth(Auth):
22 """
23 Basic auth authentication using email/username and
24 password. Deprecated.
25 """
26 def __init__(self, username, password):
27 self.username = username
28 self.password = password
29
30 def encode_params(self, base_url, method, params):
31 # We could consider automatically converting unicode to utf8 strings
32 # before encoding...
33 return urllib.urlencode(params)
34
35 def generate_headers(self):
36 return {"Authorization": "Basic " + encodestring("%s:%s" %(
37 self.username, self.password)).strip('\n')}
38
39 class NoAuth(UserPassAuth):
40 """
41 No authentication authenticator.
42 """
43 def __init__(self):
44 pass
45
46 def generate_headers(self):
47 return {}