diff options
author | JoonCheol Park <jooncheol@gmail.com> | 2012-10-11 02:31:44 +0900 |
---|---|---|
committer | JoonCheol Park <jooncheol@gmail.com> | 2012-10-12 06:02:35 +0900 |
commit | e98607248eec2b149d84efe944c12cbef419b82e (patch) | |
tree | 5a0e8ea0fc7f7b5c55ea6cb725a49e656afdef82 | |
parent | 2f6ab7f5b8f21b2dbfe9ae102674bd1694e94f03 (diff) | |
download | git-repo-e98607248eec2b149d84efe944c12cbef419b82e.tar.gz |
Support HTTP authentication using user input as fallback
If repo could not find authentication credentials from ~/.netrc, this
patch tries to get user and password from user's console input. This
could be a good choice if user doesn't want to save his plain password
in ~/.netrc or if user doesn't know about the netrc usage.
The user will be prompted only if authentication infomation does not
exist in the password manager. Since main.py firstly loads auth
infomation from ~/.netrc, this will be executed only as fallback
mechanism.
Example:
$ repo upload .
Upload project xxx/ to remote branch master:
branch yyy ( 1 commit, ...):
to https://review.zzz.com/gerrit/ (y/N)? y
(repo may try to access to https://review.zzz.com/gerrit/ssh_info and
will get the 401 HTTP Basic Authentication response from server. If no
authentication info in ~/.netrc, This patch will ask username/passwd)
Authorization Required (Message from Web Server)
User: pororo
Password:
....
[OK ] xxx/
Change-Id: Ia348a4609ac40060d9093c7dc8d7c2560020455a
-rwxr-xr-x | main.py | 24 |
1 files changed, 24 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 getpass | ||
25 | import netrc | 26 | import netrc |
26 | import optparse | 27 | import optparse |
27 | import os | 28 | import os |
@@ -276,7 +277,25 @@ class _UserAgentHandler(urllib2.BaseHandler): | |||
276 | req.add_header('User-Agent', _UserAgent()) | 277 | req.add_header('User-Agent', _UserAgent()) |
277 | return req | 278 | return req |
278 | 279 | ||
280 | def _AddPasswordFromUserInput(handler, msg, req): | ||
281 | # If repo could not find auth info from netrc, try to get it from user input | ||
282 | url = req.get_full_url() | ||
283 | user, password = handler.passwd.find_user_password(None, url) | ||
284 | if user is None: | ||
285 | print msg | ||
286 | try: | ||
287 | user = raw_input('User: ') | ||
288 | password = getpass.getpass() | ||
289 | except KeyboardInterrupt: | ||
290 | return | ||
291 | handler.passwd.add_password(None, url, user, password) | ||
292 | |||
279 | class _BasicAuthHandler(urllib2.HTTPBasicAuthHandler): | 293 | class _BasicAuthHandler(urllib2.HTTPBasicAuthHandler): |
294 | def http_error_401(self, req, fp, code, msg, headers): | ||
295 | _AddPasswordFromUserInput(self, msg, req) | ||
296 | return urllib2.HTTPBasicAuthHandler.http_error_401( | ||
297 | self, req, fp, code, msg, headers) | ||
298 | |||
280 | def http_error_auth_reqed(self, authreq, host, req, headers): | 299 | def http_error_auth_reqed(self, authreq, host, req, headers): |
281 | try: | 300 | try: |
282 | old_add_header = req.add_header | 301 | old_add_header = req.add_header |
@@ -295,6 +314,11 @@ class _BasicAuthHandler(urllib2.HTTPBasicAuthHandler): | |||
295 | raise | 314 | raise |
296 | 315 | ||
297 | class _DigestAuthHandler(urllib2.HTTPDigestAuthHandler): | 316 | class _DigestAuthHandler(urllib2.HTTPDigestAuthHandler): |
317 | def http_error_401(self, req, fp, code, msg, headers): | ||
318 | _AddPasswordFromUserInput(self, msg, req) | ||
319 | return urllib2.HTTPDigestAuthHandler.http_error_401( | ||
320 | self, req, fp, code, msg, headers) | ||
321 | |||
298 | def http_error_auth_reqed(self, auth_header, host, req, headers): | 322 | def http_error_auth_reqed(self, auth_header, host, req, headers): |
299 | try: | 323 | try: |
300 | old_add_header = req.add_header | 324 | old_add_header = req.add_header |