diff options
| author | Shawn O. Pearce <sop@google.com> | 2008-11-04 10:40:43 -0800 |
|---|---|---|
| committer | Shawn O. Pearce <sop@google.com> | 2008-11-04 10:49:04 -0800 |
| commit | d3c388391e11aff0b26ecf19c8cb668a9629ef5a (patch) | |
| tree | 0b25adaa167e99f06fd38e1da8dea82c65252ae7 /codereview/proto_client.py | |
| parent | 2450a2987afcf3d01a4d43fffe590b02880d9aa7 (diff) | |
| download | git-repo-d3c388391e11aff0b26ecf19c8cb668a9629ef5a.tar.gz | |
Update proto_client to notify the user when auth cookies are accessed
Signed-off-by: Shawn O. Pearce <sop@google.com>
Diffstat (limited to 'codereview/proto_client.py')
| -rwxr-xr-x | codereview/proto_client.py | 58 |
1 files changed, 39 insertions, 19 deletions
diff --git a/codereview/proto_client.py b/codereview/proto_client.py index a51fcd06..b58cf6a8 100755 --- a/codereview/proto_client.py +++ b/codereview/proto_client.py | |||
| @@ -20,6 +20,7 @@ import md5 | |||
| 20 | import os | 20 | import os |
| 21 | import random | 21 | import random |
| 22 | import socket | 22 | import socket |
| 23 | import sys | ||
| 23 | import time | 24 | import time |
| 24 | import urllib | 25 | import urllib |
| 25 | import urllib2 | 26 | import urllib2 |
| @@ -29,6 +30,38 @@ from froofle.protobuf.service import RpcChannel | |||
| 29 | from froofle.protobuf.service import RpcController | 30 | from froofle.protobuf.service import RpcController |
| 30 | from need_retry_pb2 import RetryRequestLaterResponse; | 31 | from need_retry_pb2 import RetryRequestLaterResponse; |
| 31 | 32 | ||
| 33 | _cookie_jars = {} | ||
| 34 | |||
| 35 | def _open_jar(path): | ||
| 36 | auth = False | ||
| 37 | |||
| 38 | if path is None: | ||
| 39 | c = cookielib.CookieJar() | ||
| 40 | else: | ||
| 41 | c = _cookie_jars.get(path) | ||
| 42 | if c is None: | ||
| 43 | c = cookielib.MozillaCookieJar(path) | ||
| 44 | |||
| 45 | if os.path.exists(path): | ||
| 46 | try: | ||
| 47 | c.load() | ||
| 48 | auth = True | ||
| 49 | except (cookielib.LoadError, IOError): | ||
| 50 | pass | ||
| 51 | |||
| 52 | if auth: | ||
| 53 | print >>sys.stderr, \ | ||
| 54 | 'Loaded authentication cookies from %s' \ | ||
| 55 | % path | ||
| 56 | else: | ||
| 57 | os.close(os.open(path, os.O_CREAT, 0600)) | ||
| 58 | os.chmod(path, 0600) | ||
| 59 | _cookie_jars[path] = c | ||
| 60 | else: | ||
| 61 | auth = True | ||
| 62 | return c, auth | ||
| 63 | |||
| 64 | |||
| 32 | class ClientLoginError(urllib2.HTTPError): | 65 | class ClientLoginError(urllib2.HTTPError): |
| 33 | """Raised to indicate an error authenticating with ClientLogin.""" | 66 | """Raised to indicate an error authenticating with ClientLogin.""" |
| 34 | 67 | ||
| @@ -269,6 +302,9 @@ class HttpRpc(RpcChannel): | |||
| 269 | self._GetAuthCookie(auth_token) | 302 | self._GetAuthCookie(auth_token) |
| 270 | self.authenticated = True | 303 | self.authenticated = True |
| 271 | if self.cookie_file is not None: | 304 | if self.cookie_file is not None: |
| 305 | print >>sys.stderr, \ | ||
| 306 | 'Saving authentication cookies to %s' \ | ||
| 307 | % self.cookie_file | ||
| 272 | self.cookie_jar.save() | 308 | self.cookie_jar.save() |
| 273 | return | 309 | return |
| 274 | 310 | ||
| @@ -337,24 +373,8 @@ class HttpRpc(RpcChannel): | |||
| 337 | opener.add_handler(urllib2.HTTPDefaultErrorHandler()) | 373 | opener.add_handler(urllib2.HTTPDefaultErrorHandler()) |
| 338 | opener.add_handler(urllib2.HTTPSHandler()) | 374 | opener.add_handler(urllib2.HTTPSHandler()) |
| 339 | opener.add_handler(urllib2.HTTPErrorProcessor()) | 375 | opener.add_handler(urllib2.HTTPErrorProcessor()) |
| 340 | if self.cookie_file is not None: | 376 | |
| 341 | self.cookie_jar = cookielib.MozillaCookieJar(self.cookie_file) | 377 | self.cookie_jar, \ |
| 342 | if os.path.exists(self.cookie_file): | 378 | self.authenticated = _open_jar(self.cookie_file) |
| 343 | try: | ||
| 344 | self.cookie_jar.load() | ||
| 345 | self.authenticated = True | ||
| 346 | except (cookielib.LoadError, IOError): | ||
| 347 | # Failed to load cookies - just ignore them. | ||
| 348 | pass | ||
| 349 | else: | ||
| 350 | # Create an empty cookie file with mode 600 | ||
| 351 | fd = os.open(self.cookie_file, os.O_CREAT, 0600) | ||
| 352 | os.close(fd) | ||
| 353 | # Always chmod the cookie file | ||
| 354 | os.chmod(self.cookie_file, 0600) | ||
| 355 | else: | ||
| 356 | # Don't save cookies across runs of update.py. | ||
| 357 | self.cookie_jar = cookielib.CookieJar() | ||
| 358 | opener.add_handler(urllib2.HTTPCookieProcessor(self.cookie_jar)) | 379 | opener.add_handler(urllib2.HTTPCookieProcessor(self.cookie_jar)) |
| 359 | return opener | 380 | return opener |
| 360 | |||
