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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 2) We use ClientLogin to obtain an AUTH token for the user | 365 2) We use ClientLogin to obtain an AUTH token for the user |
365 (see http://code.google.com/apis/accounts/AuthForInstalledApps.html). | 366 (see http://code.google.com/apis/accounts/AuthForInstalledApps.html). |
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 """ |
| 375 INTERNAL_ERROR_MAP = { |
| 376 "badauth": "BadAuthentication", |
| 377 "cr": "CaptchaRequired", |
| 378 "adel": "AccountDeleted", |
| 379 "adis": "AccountDisabled", |
| 380 "sdis": "ServiceDisabled", |
| 381 "ire": "ServiceUnavailable", |
| 382 } |
| 383 |
374 for i in range(3): | 384 for i in range(3): |
375 credentials = self.auth_function() | 385 credentials = self.auth_function() |
| 386 |
| 387 # Try external, then internal. |
| 388 e = None |
376 try: | 389 try: |
377 auth_token = self._GetAuthToken(credentials[0], credentials[1]) | 390 auth_token = self._GetAuthToken(credentials[0], credentials[1]) |
378 except ClientLoginError, e: | 391 except urllib2.HTTPError: |
| 392 try: |
| 393 auth_token = self._GetAuthToken(credentials[0], credentials[1], |
| 394 internal=True) |
| 395 except ClientLoginError, exc: |
| 396 e = exc |
| 397 if e: |
379 print >> sys.stderr, '' | 398 print >> sys.stderr, '' |
| 399 if internal: |
| 400 e.reason = INTERNAL_ERROR_MAP.get(e.reason, e.reason) |
380 if e.reason == "BadAuthentication": | 401 if e.reason == "BadAuthentication": |
381 if e.info == "InvalidSecondFactor": | 402 if e.info == "InvalidSecondFactor": |
382 print >> sys.stderr, ( | 403 print >> sys.stderr, ( |
383 "Use an application-specific password instead " | 404 "Use an application-specific password instead " |
384 "of your regular account password.\n" | 405 "of your regular account password.\n" |
385 "See http://www.google.com/" | 406 "See http://www.google.com/" |
386 "support/accounts/bin/answer.py?answer=185833") | 407 "support/accounts/bin/answer.py?answer=185833") |
387 else: | 408 else: |
388 print >> sys.stderr, "Invalid username or password." | 409 print >> sys.stderr, "Invalid username or password." |
389 elif e.reason == "CaptchaRequired": | 410 elif e.reason == "CaptchaRequired": |
(...skipping 12 matching lines...) Expand all Loading... |
402 elif e.reason == "AccountDisabled": | 423 elif e.reason == "AccountDisabled": |
403 print >> sys.stderr, "The user account has been disabled." | 424 print >> sys.stderr, "The user account has been disabled." |
404 break | 425 break |
405 elif e.reason == "ServiceDisabled": | 426 elif e.reason == "ServiceDisabled": |
406 print >> sys.stderr, ("The user's access to the service has been " | 427 print >> sys.stderr, ("The user's access to the service has been " |
407 "disabled.") | 428 "disabled.") |
408 elif e.reason == "ServiceUnavailable": | 429 elif e.reason == "ServiceUnavailable": |
409 print >> sys.stderr, "The service is not available; try again later." | 430 print >> sys.stderr, "The service is not available; try again later." |
410 else: | 431 else: |
411 # Unknown error. | 432 # Unknown error. |
412 raise | 433 raise e |
413 print >> sys.stderr, '' | 434 print >> sys.stderr, '' |
414 continue | 435 continue |
415 self._GetAuthCookie(auth_token) | 436 self._GetAuthCookie(auth_token) |
416 return | 437 return |
417 | 438 |
418 def Send(self, request_path, payload=None, | 439 def Send(self, request_path, payload=None, |
419 content_type="application/octet-stream", | 440 content_type="application/octet-stream", |
420 timeout=None, | 441 timeout=None, |
421 extra_headers=None, | 442 extra_headers=None, |
422 **kwargs): | 443 **kwargs): |
(...skipping 2298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2721 os.environ['LC_ALL'] = 'C' | 2742 os.environ['LC_ALL'] = 'C' |
2722 RealMain(sys.argv) | 2743 RealMain(sys.argv) |
2723 except KeyboardInterrupt: | 2744 except KeyboardInterrupt: |
2724 print | 2745 print |
2725 StatusUpdate("Interrupted.") | 2746 StatusUpdate("Interrupted.") |
2726 sys.exit(1) | 2747 sys.exit(1) |
2727 | 2748 |
2728 | 2749 |
2729 if __name__ == "__main__": | 2750 if __name__ == "__main__": |
2730 main() | 2751 main() |
OLD | NEW |