]> jfr.im git - z_archive/twitter.git/blobdiff - twitter/auth.py
Version 1.17.0
[z_archive/twitter.git] / twitter / auth.py
index 9aad691711439a2c964e1bf8c85e7e1b39a9d005..45f1f4c001ae21df31a0aa904a5aad06aafaee97 100644 (file)
@@ -1,5 +1,10 @@
-import urllib.request, urllib.parse, urllib.error
-from base64 import encodestring
+try:
+    import urllib.parse as urllib_parse
+    from base64 import encodebytes
+except ImportError:
+    import urllib as urllib_parse
+    from base64 import encodestring as encodebytes
+
 
 class Auth(object):
     """
@@ -18,6 +23,27 @@ class Auth(object):
         raise NotImplementedError()
 
 
+class UserPassAuth(Auth):
+    """
+    Basic auth authentication using email/username and
+    password. Deprecated.
+    """
+    def __init__(self, username, password):
+        self.username = username
+        self.password = password
+
+    def encode_params(self, base_url, method, params):
+        # We could consider automatically converting unicode to utf8 strings
+        # before encoding...
+        return urllib_parse.urlencode(params)
+
+    def generate_headers(self):
+        return {b"Authorization": b"Basic " + encodebytes(
+                ("%s:%s" %(self.username, self.password))
+                .encode('utf8')).strip(b'\n')
+                }
+
+
 class NoAuth(Auth):
     """
     No authentication authenticator.
@@ -26,7 +52,11 @@ class NoAuth(Auth):
         pass
 
     def encode_params(self, base_url, method, params):
-        return urllib.parse.urlencode(params)
+        return urllib_parse.urlencode(params)
 
     def generate_headers(self):
         return {}
+
+
+class MissingCredentialsError(Exception):
+    pass