diff options
author | jortel <devnull@localhost> | 2009-07-10 17:16:58 +0000 |
---|---|---|
committer | jortel <devnull@localhost> | 2009-07-10 17:16:58 +0000 |
commit | 5ffe2ed15ebaa351e0f394c875878a84932c004f (patch) | |
tree | ceb22d67dcc8620d48b5b57995b7141a30be1b10 /suds/transport | |
parent | adcecea134ea985159138b871365ae4f70633137 (diff) | |
download | suds-5ffe2ed15ebaa351e0f394c875878a84932c004f.tar.gz |
Add HttpAuthenticated to transport.http.py. This class provides a primiative http authentication that adds http Authentication: header to all requests instead of waiting for a 401 response.
Diffstat (limited to 'suds/transport')
-rw-r--r-- | suds/transport/http.py | 24 | ||||
-rw-r--r-- | suds/transport/https.py | 21 |
2 files changed, 33 insertions, 12 deletions
diff --git a/suds/transport/http.py b/suds/transport/http.py index 9f77154..c805230 100644 --- a/suds/transport/http.py +++ b/suds/transport/http.py @@ -19,6 +19,7 @@ Contains transport interface (classes) and reference implementation. """ import urllib2 as u2 +import base64 from suds.transport import * from urlparse import urlparse from cookielib import CookieJar @@ -29,7 +30,8 @@ log = getLogger(__name__) class HttpTransport(Transport): """ - urllib2 transport implementation. + HTTP transport using urllib2. Provided basic http transport + that provides for cookies, proxies but no authentication. """ def __init__(self, **kwargs): @@ -105,3 +107,23 @@ class HttpTransport(Transport): return protocol = u2request.type u2request.set_proxy(proxy, protocol) + + +class HttpAuthenticated(HttpTransport): + """ + Provides basic http authentication for servers that don't follow + the specified challenge / response model. This implementation + appends the I{Authorization} http header with base64 encoded + credentials on every http request. + """ + + def send(self, request): + credentials = self.credentials() + if not (None in credentials): + encoded = base64.encodestring(':'.join(credentials)) + basic = 'Basic %s' % encoded[:-1] + request.headers['Authorization'] = basic + return HttpTransport.send(self, request) + + def credentials(self): + return (self.options.username, self.options.password)
\ No newline at end of file diff --git a/suds/transport/https.py b/suds/transport/https.py index 592c718..a3370bd 100644 --- a/suds/transport/https.py +++ b/suds/transport/https.py @@ -28,7 +28,9 @@ log = getLogger(__name__) class HttpAuthenticated(HttpTransport): """ - Provides basic http authentication. + Provides basic http authentication that follows the RFC-2617 specification. + As defined by specifications, credentials are provided to the server + upon request (HTTP/1.0 401 Authorization Required) by the server only. @ivar pm: The password manager. @ivar handler: The authentication handler. """ @@ -56,15 +58,12 @@ class HttpAuthenticated(HttpTransport): self.urlopener = u2.build_opener(self.handler) def open(self, request): - self.__addcredentials(request) + credentials = self.credentials() + if not (None in credentials): + u = credentials[0] + p = credentials[1] + self.pm.add_password(None, request.url, u, p) return HttpTransport.open(self, request) - - def send(self, request): - self.__addcredentials(request) - return HttpTransport.send(self, request) - def __addcredentials(self, request): - user = self.options.username - pwd = self.options.password - if user is not None: - self.pm.add_password(None, request.url, user, pwd)
\ No newline at end of file + def credentials(self): + return (self.options.username, self.options.password)
\ No newline at end of file |