Commits

ben...@localhost.nymphormation.net  committed 98cd67b

add authentification management to curl http client. In the mean time
add testadd authentification management to curl http client. In the mean
time add testss

  • Participants
  • Parent commits 6b01fb6

Comments (0)

Files changed (5)

File restclient/http.py

             raise RuntimeError('Cannot find pycurl library')
 
         self.timeout = timeout
-        self.follow_redirects = True
+        self._credentials = {}
+
+    def add_credentials(self, user, password):
+        self._credentials = {
+                "user": user,
+                "password": password
+        }
+
+    def _get_credentials(self):
+        return self._credentials
 
     def _parseHeaders(self, status_and_headers):
         status_and_headers.seek(0)
             c.setopt(pycurl.WRITEFUNCTION, data.write)
             c.setopt(pycurl.HEADERFUNCTION, header.write)
             c.setopt(pycurl.URL ,url)
-            if self.follow_redirects:
-                c.setopt(pycurl.FOLLOWLOCATION, 1)
-                c.setopt(pycurl.MAXREDIRS, 5)
+            c.setopt(pycurl.FOLLOWLOCATION, 1)
+            c.setopt(pycurl.MAXREDIRS, 5)
 
-            #c.setopt(pycurl.VERBOSE, 1)
+            auth = self._get_credentials()
+            user = auth.get('user', None)
+            password = auth.get('password', None)
+            if user is not None:
+                # accept any auth methode
+                c.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_ANY)
+                c.setopt(pycurl.PROXYAUTH, pycurl.HTTPAUTH_ANY)
+                userpass = user + ':'
+                if password is not None: # '' is a valid password
+                    userpass += password
+                c.setopt(pycurl.USERPWD, userpass)
+            #.setopt(pycurl.VERBOSE, 1)
 
             if headers:
                 c.setopt(pycurl.HTTPHEADER,

File restclient/rest.py

     request.
     """
 
-class Unauthorised(Exception):
+class Unauthorized(Exception):
     """Exception raised when a 401 HTTP error is received in response to a
     request.
     """

File tests/_server_test.py

             else:
                 self.error_Response()
         elif path == "/auth":
-            if self.headers.get('authorization') != self.auth:
+            extra_headers = [('Content-type', 'text/plain')]
+
+            if not 'Authorization' in self.headers:
                 realm = "test"
-                self.send_response(401)
-                self.send_header('WWW-Authenticate', 'Basic realm="%s"' % realm)
-                self.end_headers()
-                return
-
+                extra_headers.append(('WWW-Authenticate', 'Basic realm="%s"' % realm))
+                self._respond(401, extra_headers, "")
+            else:
+                auth = self.headers['Authorization'][len('Basic')+1:]
+                auth = base64.b64decode(auth).split(':')
+                self._respond(200, extra_headers, "ok")
         else:
             self._respond(404, 
-                    [('Content-type', 'text/plain')], "Not Found" )
+                [('Content-type', 'text/plain')], "Not Found" )
 
 
     def do_POST(self):

File tests/clients_test.py

 class CurlHTTPClientTestCase(HTTPClientTestCase):
     httpclient = CurlHTTPClient()
 
+    def testAuth(self):
+        httpclient = CurlHTTPClient()
+        httpclient.add_credentials("test", "test")
+        
+        res = Resource(self.url, httpclient)
+        result = res.get('/auth')
+        self.assert_(res.status_code == 200)
+
+
+
 class HTTPLib2HTTPClientTestCase(HTTPClientTestCase):
     httpclient = HTTPLib2HTTPClient() 
 

File tests/resource_test.py

         httpclient = Urllib2HTTPClient(auth_handler)
         
         res = Resource(self.url, httpclient)
-        
+        result = res.get('/auth')
+        self.assert_(res.status_code == 200) 
     
 if __name__ == '__main__':
     from _server_test import run_server_test