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