| 1 |
"""HTTP Authentication and Proxy support. |
| 2 |
|
| 3 |
|
| 4 |
Copyright 2006 John J. Lee <[email protected]> |
| 5 |
|
| 6 |
This code is free software; you can redistribute it and/or modify it under |
| 7 |
the terms of the BSD or ZPL 2.1 licenses (see the file LICENSE |
| 8 |
included with the distribution). |
| 9 |
|
| 10 |
""" |
| 11 |
|
| 12 |
from __future__ import absolute_import |
| 13 |
from ._urllib2_fork import HTTPPasswordMgr |
| 14 |
from .polyglot import is_string, iteritems |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class HTTPProxyPasswordMgr(HTTPPasswordMgr): |
| 19 |
|
| 20 |
|
| 21 |
def add_password(self, realm, uri, user, passwd): |
| 22 |
|
| 23 |
if uri is None or is_string(uri): |
| 24 |
uris = [uri] |
| 25 |
else: |
| 26 |
uris = uri |
| 27 |
passwd_by_domain = self.passwd.setdefault(realm, {}) |
| 28 |
for uri in uris: |
| 29 |
for default_port in True, False: |
| 30 |
reduced_uri = self.reduce_uri(uri, default_port) |
| 31 |
passwd_by_domain[reduced_uri] = (user, passwd) |
| 32 |
|
| 33 |
def find_user_password(self, realm, authuri): |
| 34 |
attempts = [(realm, authuri), (None, authuri)] |
| 35 |
|
| 36 |
|
| 37 |
for default_uri in False, True: |
| 38 |
for realm, authuri in attempts: |
| 39 |
authinfo_by_domain = self.passwd.get(realm, {}) |
| 40 |
for default_port in True, False: |
| 41 |
reduced_authuri = self.reduce_uri(authuri, default_port) |
| 42 |
for uri, authinfo in iteritems(authinfo_by_domain): |
| 43 |
if uri is None and not default_uri: |
| 44 |
continue |
| 45 |
if self.is_suburi(uri, reduced_authuri): |
| 46 |
return authinfo |
| 47 |
user, password = None, None |
| 48 |
|
| 49 |
if user is not None: |
| 50 |
break |
| 51 |
return user, password |
| 52 |
|
| 53 |
def reduce_uri(self, uri, default_port=True): |
| 54 |
if uri is None: |
| 55 |
return None |
| 56 |
return HTTPPasswordMgr.reduce_uri(self, uri, default_port) |
| 57 |
|
| 58 |
def is_suburi(self, base, test): |
| 59 |
if base is None: |
| 60 |
|
| 61 |
hostport, path = test |
| 62 |
base = (hostport, "/") |
| 63 |
return HTTPPasswordMgr.is_suburi(self, base, test) |
| 64 |
|
| 65 |
|
| 66 |
class HTTPSClientCertMgr(HTTPPasswordMgr): |
| 67 |
|
| 68 |
|
| 69 |
def add_key_cert(self, uri, key_file, cert_file): |
| 70 |
self.add_password(None, uri, key_file, cert_file) |
| 71 |
|
| 72 |
def find_key_cert(self, authuri): |
| 73 |
return HTTPPasswordMgr.find_user_password(self, None, authuri) |