]>
Commit | Line | Data |
---|---|---|
3930cc7b MV |
1 | try: |
2 | import urllib.parse as urllib_parse | |
dd648a25 | 3 | from base64 import encodebytes |
3930cc7b | 4 | except ImportError: |
84d17de6 | 5 | import urllib as urllib_parse |
dd648a25 | 6 | from base64 import encodestring as encodebytes |
568331a9 | 7 | |
1cc9ab0b MV |
8 | class Auth(object): |
9 | """ | |
10 | ABC for Authenticator objects. | |
11 | """ | |
12 | ||
568331a9 MH |
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.""" | |
aec68959 | 17 | raise NotImplementedError() |
568331a9 MH |
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.""" | |
aec68959 | 22 | raise NotImplementedError() |
568331a9 | 23 | |
dd648a25 MV |
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 | } | |
1cc9ab0b | 43 | |
aec68959 | 44 | class NoAuth(Auth): |
1cc9ab0b MV |
45 | """ |
46 | No authentication authenticator. | |
47 | """ | |
d20da7f3 MV |
48 | def __init__(self): |
49 | pass | |
50 | ||
aec68959 | 51 | def encode_params(self, base_url, method, params): |
3930cc7b | 52 | return urllib_parse.urlencode(params) |
aec68959 | 53 | |
d20da7f3 MV |
54 | def generate_headers(self): |
55 | return {} |