Debian Codegraph @

mechanize/_auth.py

raw symbols SCIP
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 # TODO: stop deriving from HTTPPasswordMgr
18 class HTTPProxyPasswordMgr(HTTPPasswordMgr):
19 # has default realm and host/port
20
21 def add_password(self, realm, uri, user, passwd):
22 # uri could be a single URI or a sequence
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 # bleh, want default realm to take precedence over default
36 # URI/authority, hence this outer loop
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 # default to the proxy's host/port
61 hostport, path = test
62 base = (hostport, "/")
63 return HTTPPasswordMgr.is_suburi(self, base, test)
64
65
66 class HTTPSClientCertMgr(HTTPPasswordMgr):
67 # implementation inheritance: this is not a proper subclass
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)