/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: Andrew Bennetts
  • Date: 2009-02-20 06:16:22 UTC
  • mfrom: (4023 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4027.
  • Revision ID: andrew.bennetts@canonical.com-20090220061622-te9miq29rlfqiwwv
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
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
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,