OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # coding: utf-8 | 2 # coding: utf-8 |
3 # | 3 # |
4 # Copyright 2007 Google Inc. | 4 # Copyright 2007 Google Inc. |
5 # | 5 # |
6 # Licensed under the Apache License, Version 2.0 (the "License"); | 6 # Licensed under the Apache License, Version 2.0 (the "License"); |
7 # you may not use this file except in compliance with the License. | 7 # you may not use this file except in compliance with the License. |
8 # You may obtain a copy of the License at | 8 # You may obtain a copy of the License at |
9 # | 9 # |
10 # http://www.apache.org/licenses/LICENSE-2.0 | 10 # http://www.apache.org/licenses/LICENSE-2.0 |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
282 def _CreateRequest(self, url, data=None): | 282 def _CreateRequest(self, url, data=None): |
283 """Creates a new urllib request.""" | 283 """Creates a new urllib request.""" |
284 LOGGER.debug("Creating request for: '%s' with payload:\n%s", url, data) | 284 LOGGER.debug("Creating request for: '%s' with payload:\n%s", url, data) |
285 req = urllib2.Request(url, data=data, headers={"Accept": "text/plain"}) | 285 req = urllib2.Request(url, data=data, headers={"Accept": "text/plain"}) |
286 if self.host_override: | 286 if self.host_override: |
287 req.add_header("Host", self.host_override) | 287 req.add_header("Host", self.host_override) |
288 for key, value in self.extra_headers.iteritems(): | 288 for key, value in self.extra_headers.iteritems(): |
289 req.add_header(key, value) | 289 req.add_header(key, value) |
290 return req | 290 return req |
291 | 291 |
292 def _GetAuthToken(self, email, password): | 292 def _GetAuthToken(self, email, password, internal=False): |
293 """Uses ClientLogin to authenticate the user, returning an auth token. | 293 """Uses ClientLogin to authenticate the user, returning an auth token. |
294 | 294 |
295 Args: | 295 Args: |
296 email: The user's email address | 296 email: The user's email address |
297 password: The user's password | 297 password: The user's password |
298 | 298 |
299 Raises: | 299 Raises: |
300 ClientLoginError: If there was an error authenticating with ClientLogin. | 300 ClientLoginError: If there was an error authenticating with ClientLogin. |
301 HTTPError: If there was some other form of HTTP error. | 301 HTTPError: If there was some other form of HTTP error. |
302 | 302 |
303 Returns: | 303 Returns: |
304 The authentication token returned by ClientLogin. | 304 The authentication token returned by ClientLogin. |
305 """ | 305 """ |
306 account_type = self.account_type | 306 account_type = self.account_type |
307 if self.host.endswith(".google.com"): | 307 if self.host.endswith(".google.com"): |
308 # Needed for use inside Google. | 308 # Needed for use inside Google. |
309 account_type = "HOSTED" | 309 account_type = "HOSTED" |
310 service = ('ClientLogin') if not internal else ('ClientAuth') | |
310 req = self._CreateRequest( | 311 req = self._CreateRequest( |
311 url="https://www.google.com/accounts/ClientLogin", | 312 url="https://www.google.com/accounts/%s" % (service,), |
312 data=urllib.urlencode({ | 313 data=urllib.urlencode({ |
313 "Email": email, | 314 "Email": email, |
314 "Passwd": password, | 315 "Passwd": password, |
315 "service": "ah", | 316 "service": "ah", |
316 "source": "rietveld-codereview-upload", | 317 "source": "rietveld-codereview-upload", |
317 "accountType": account_type, | 318 "accountType": account_type, |
318 }), | 319 }), |
319 ) | 320 ) |
320 try: | 321 try: |
321 response = self.opener.open(req) | 322 response = self.opener.open(req) |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
366 3) We pass the auth token to /_ah/login on the server to obtain an | 367 3) We pass the auth token to /_ah/login on the server to obtain an |
367 authentication cookie. If login was successful, it tries to redirect | 368 authentication cookie. If login was successful, it tries to redirect |
368 us to the URL we provided. | 369 us to the URL we provided. |
369 | 370 |
370 If we attempt to access the upload API without first obtaining an | 371 If we attempt to access the upload API without first obtaining an |
371 authentication cookie, it returns a 401 response (or a 302) and | 372 authentication cookie, it returns a 401 response (or a 302) and |
372 directs us to authenticate ourselves with ClientLogin. | 373 directs us to authenticate ourselves with ClientLogin. |
373 """ | 374 """ |
374 for i in range(3): | 375 for i in range(3): |
375 credentials = self.auth_function() | 376 credentials = self.auth_function() |
377 | |
378 # Try external, then internal. | |
379 exc = None | |
376 try: | 380 try: |
377 auth_token = self._GetAuthToken(credentials[0], credentials[1]) | 381 auth_token = self._GetAuthToken(credentials[0], credentials[1]) |
378 except ClientLoginError, e: | 382 except urllib2.HTTPError: |
383 try: | |
384 auth_token = self._GetAuthToken(credentials[0], credentials[1], | |
385 internal=True) | |
386 except ClientLoginError, e: | |
387 exc = e | |
388 if exc: | |
379 print >> sys.stderr, '' | 389 print >> sys.stderr, '' |
380 if e.reason == "BadAuthentication": | 390 if e.reason == "BadAuthentication": |
Vadim Sh.
2015/04/07 17:31:10
this things may be different for ClientAuth. Try e
| |
381 if e.info == "InvalidSecondFactor": | 391 if e.info == "InvalidSecondFactor": |
382 print >> sys.stderr, ( | 392 print >> sys.stderr, ( |
383 "Use an application-specific password instead " | 393 "Use an application-specific password instead " |
384 "of your regular account password.\n" | 394 "of your regular account password.\n" |
385 "See http://www.google.com/" | 395 "See http://www.google.com/" |
386 "support/accounts/bin/answer.py?answer=185833") | 396 "support/accounts/bin/answer.py?answer=185833") |
387 else: | 397 else: |
388 print >> sys.stderr, "Invalid username or password." | 398 print >> sys.stderr, "Invalid username or password." |
389 elif e.reason == "CaptchaRequired": | 399 elif e.reason == "CaptchaRequired": |
390 print >> sys.stderr, ( | 400 print >> sys.stderr, ( |
(...skipping 11 matching lines...) Expand all Loading... | |
402 elif e.reason == "AccountDisabled": | 412 elif e.reason == "AccountDisabled": |
403 print >> sys.stderr, "The user account has been disabled." | 413 print >> sys.stderr, "The user account has been disabled." |
404 break | 414 break |
405 elif e.reason == "ServiceDisabled": | 415 elif e.reason == "ServiceDisabled": |
406 print >> sys.stderr, ("The user's access to the service has been " | 416 print >> sys.stderr, ("The user's access to the service has been " |
407 "disabled.") | 417 "disabled.") |
408 elif e.reason == "ServiceUnavailable": | 418 elif e.reason == "ServiceUnavailable": |
409 print >> sys.stderr, "The service is not available; try again later." | 419 print >> sys.stderr, "The service is not available; try again later." |
410 else: | 420 else: |
411 # Unknown error. | 421 # Unknown error. |
412 raise | 422 raise exc |
413 print >> sys.stderr, '' | 423 print >> sys.stderr, '' |
414 continue | 424 continue |
415 self._GetAuthCookie(auth_token) | 425 self._GetAuthCookie(auth_token) |
416 return | 426 return |
417 | 427 |
418 def Send(self, request_path, payload=None, | 428 def Send(self, request_path, payload=None, |
419 content_type="application/octet-stream", | 429 content_type="application/octet-stream", |
420 timeout=None, | 430 timeout=None, |
421 extra_headers=None, | 431 extra_headers=None, |
422 **kwargs): | 432 **kwargs): |
(...skipping 2298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2721 os.environ['LC_ALL'] = 'C' | 2731 os.environ['LC_ALL'] = 'C' |
2722 RealMain(sys.argv) | 2732 RealMain(sys.argv) |
2723 except KeyboardInterrupt: | 2733 except KeyboardInterrupt: |
2724 print | 2734 print |
2725 StatusUpdate("Interrupted.") | 2735 StatusUpdate("Interrupted.") |
2726 sys.exit(1) | 2736 sys.exit(1) |
2727 | 2737 |
2728 | 2738 |
2729 if __name__ == "__main__": | 2739 if __name__ == "__main__": |
2730 main() | 2740 main() |
OLD | NEW |