diff options
author | Shawn O. Pearce <sop@google.com> | 2011-09-19 10:04:23 -0700 |
---|---|---|
committer | Shawn O. Pearce <sop@google.com> | 2011-09-19 14:52:32 -0700 |
commit | bd0312a4841e9488cf43ae4afb3b58d44eebbbb1 (patch) | |
tree | bb0a60c3068ceb640d9618cb8d7afa716b9609c0 | |
parent | 334851e4b6390f4c78e463b977003f1d967c88ed (diff) | |
download | git-repo-bd0312a4841e9488cf43ae4afb3b58d44eebbbb1.tar.gz |
Support ~/.netrc for HTTP Basic authentication
If repo tries to access a URL over HTTP and the user needs to
authenticate, offer a match from ~/.netrc. This matches behavior
with the Git command line client.
Change-Id: I803f3c5d562177ea0330941350cff3cc1e1bef08
Signed-off-by: Shawn O. Pearce <sop@google.com>
-rwxr-xr-x | main.py | 12 |
1 files changed, 12 insertions, 0 deletions
@@ -22,6 +22,7 @@ if __name__ == '__main__': | |||
22 | del sys.argv[-1] | 22 | del sys.argv[-1] |
23 | del magic | 23 | del magic |
24 | 24 | ||
25 | import netrc | ||
25 | import optparse | 26 | import optparse |
26 | import os | 27 | import os |
27 | import re | 28 | import re |
@@ -254,6 +255,17 @@ class _UserAgentHandler(urllib2.BaseHandler): | |||
254 | def init_http(): | 255 | def init_http(): |
255 | handlers = [_UserAgentHandler()] | 256 | handlers = [_UserAgentHandler()] |
256 | 257 | ||
258 | mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() | ||
259 | try: | ||
260 | n = netrc.netrc() | ||
261 | for host in n.hosts: | ||
262 | p = n.hosts[host] | ||
263 | mgr.add_password(None, 'http://%s/' % host, p[0], p[2]) | ||
264 | mgr.add_password(None, 'https://%s/' % host, p[0], p[2]) | ||
265 | except netrc.NetrcParseError: | ||
266 | pass | ||
267 | handlers.append(urllib2.HTTPBasicAuthHandler(mgr)) | ||
268 | |||
257 | if 'http_proxy' in os.environ: | 269 | if 'http_proxy' in os.environ: |
258 | url = os.environ['http_proxy'] | 270 | url = os.environ['http_proxy'] |
259 | handlers.append(urllib2.ProxyHandler({'http': url, 'https': url})) | 271 | handlers.append(urllib2.ProxyHandler({'http': url, 'https': url})) |