/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/transport/http/_urllib2_wrappers.py

  • Committer: Jelmer Vernooij
  • Date: 2009-02-25 14:36:59 UTC
  • mfrom: (4048 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4049.
  • Revision ID: jelmer@samba.org-20090225143659-vx6cbqtmyicuzfyf
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
# ensure that.
48
48
 
49
49
import httplib
 
50
try:
 
51
    import kerberos
 
52
except ImportError:
 
53
    have_kerberos = False
 
54
else:
 
55
    have_kerberos = True
50
56
import socket
51
57
import urllib
52
58
import urllib2
251
257
        # Preserve our preciousss
252
258
        sock = self.sock
253
259
        self.sock = None
254
 
        # Let httplib.HTTPConnection do its housekeeping 
 
260
        # Let httplib.HTTPConnection do its housekeeping
255
261
        self.close()
256
262
        # Restore our preciousss
257
263
        self.sock = sock
365
371
 
366
372
    def __init__(self, request):
367
373
        """Constructor
368
 
        
 
374
 
369
375
        :param request: the first request sent to the proxied host, already
370
376
            processed by the opener (i.e. proxied_host is already set).
371
377
        """
692
698
                        connect.proxied_host, self.host))
693
699
            # Housekeeping
694
700
            connection.cleanup_pipe()
695
 
            # Establish the connection encryption 
 
701
            # Establish the connection encryption
696
702
            connection.connect_to_origin()
697
703
            # Propagate the connection to the original request
698
704
            request.connection = connection
943
949
    preventively set authentication headers after the first
944
950
    successful authentication.
945
951
 
946
 
    This can be used for http and proxy, as well as for basic and
 
952
    This can be used for http and proxy, as well as for basic, negotiate and
947
953
    digest authentications.
948
954
 
949
955
    This provides an unified interface for all authentication handlers
1063
1069
        (digest's nonce is an example, digest's nonce_count is a
1064
1070
        *counter-example*). Such parameters must be updated by
1065
1071
        using the update_auth() method.
1066
 
        
 
1072
 
1067
1073
        :param header: The authentication header sent by the server.
1068
1074
        :param auth: The auth parameters already known. They may be
1069
1075
             updated.
1143
1149
    https_request = http_request # FIXME: Need test
1144
1150
 
1145
1151
 
 
1152
class NegotiateAuthHandler(AbstractAuthHandler):
 
1153
    """A authentication handler that handles WWW-Authenticate: Negotiate.
 
1154
 
 
1155
    At the moment this handler supports just Kerberos. In the future,
 
1156
    NTLM support may also be added.
 
1157
    """
 
1158
 
 
1159
    handler_order = 480
 
1160
 
 
1161
    def auth_match(self, header, auth):
 
1162
        scheme = header.lower()
 
1163
        if scheme != 'negotiate':
 
1164
            return False
 
1165
        self.update_auth(auth, 'scheme', scheme)
 
1166
        resp = self._auth_match_kerberos(auth)
 
1167
        if resp is None:
 
1168
            return False
 
1169
        # Optionally should try to authenticate using NTLM here
 
1170
        self.update_auth(auth, 'negotiate_response', resp)
 
1171
        return True
 
1172
 
 
1173
    def _auth_match_kerberos(self, auth):
 
1174
        """Try to create a GSSAPI response for authenticating against a host."""
 
1175
        if not have_kerberos:
 
1176
            return None
 
1177
        ret, vc = kerberos.authGSSClientInit("HTTP@%(host)s" % auth)
 
1178
        if ret < 1:
 
1179
            trace.warning('Unable to create GSSAPI context for %s: %d',
 
1180
                auth['host'], ret)
 
1181
            return None
 
1182
        ret = kerberos.authGSSClientStep(vc, "")
 
1183
        if ret < 0:
 
1184
            trace.mutter('authGSSClientStep failed: %d', ret)
 
1185
            return None
 
1186
        return kerberos.authGSSClientResponse(vc)
 
1187
 
 
1188
    def build_auth_header(self, auth, request):
 
1189
        return "Negotiate %s" % auth['negotiate_response']
 
1190
 
 
1191
    def auth_params_reusable(self, auth):
 
1192
        # If the auth scheme is known, it means a previous
 
1193
        # authentication was successful, all information is
 
1194
        # available, no further checks are needed.
 
1195
        return (auth.get('scheme', None) == 'negotiate' and
 
1196
                auth.get('negotiate_response', None) is not None)
 
1197
 
 
1198
 
1146
1199
class BasicAuthHandler(AbstractAuthHandler):
1147
1200
    """A custom basic authentication handler."""
1148
1201
 
1368
1421
    """Custom proxy basic authentication handler"""
1369
1422
 
1370
1423
 
 
1424
class HTTPNegotiateAuthHandler(NegotiateAuthHandler, HTTPAuthHandler):
 
1425
    """Custom http negotiate authentication handler"""
 
1426
 
 
1427
 
 
1428
class ProxyNegotiateAuthHandler(NegotiateAuthHandler, ProxyAuthHandler):
 
1429
    """Custom proxy negotiate authentication handler"""
 
1430
 
 
1431
 
1371
1432
class HTTPErrorProcessor(urllib2.HTTPErrorProcessor):
1372
1433
    """Process HTTP error responses.
1373
1434
 
1432
1493
            ProxyHandler(),
1433
1494
            HTTPBasicAuthHandler(),
1434
1495
            HTTPDigestAuthHandler(),
 
1496
            HTTPNegotiateAuthHandler(),
1435
1497
            ProxyBasicAuthHandler(),
1436
1498
            ProxyDigestAuthHandler(),
 
1499
            ProxyNegotiateAuthHandler(),
1437
1500
            HTTPHandler,
1438
1501
            HTTPSHandler,
1439
1502
            HTTPDefaultErrorHandler,