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