OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright 2007 Google Inc. | 3 # Copyright 2007 Google Inc. |
4 # | 4 # |
5 # Licensed under the Apache License, Version 2.0 (the "License"); | 5 # Licensed under the Apache License, Version 2.0 (the "License"); |
6 # you may not use this file except in compliance with the License. | 6 # you may not use this file except in compliance with the License. |
7 # You may obtain a copy of the License at | 7 # You may obtain a copy of the License at |
8 # | 8 # |
9 # http://www.apache.org/licenses/LICENSE-2.0 | 9 # http://www.apache.org/licenses/LICENSE-2.0 |
10 # | 10 # |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 def _CreateRequest(self, url, data=None): | 211 def _CreateRequest(self, url, data=None): |
212 """Creates a new urllib request.""" | 212 """Creates a new urllib request.""" |
213 logging.debug("Creating request for: '%s' with payload:\n%s", url, data) | 213 logging.debug("Creating request for: '%s' with payload:\n%s", url, data) |
214 req = urllib2.Request(url, data=data) | 214 req = urllib2.Request(url, data=data) |
215 if self.host_override: | 215 if self.host_override: |
216 req.add_header("Host", self.host_override) | 216 req.add_header("Host", self.host_override) |
217 for key, value in self.extra_headers.iteritems(): | 217 for key, value in self.extra_headers.iteritems(): |
218 req.add_header(key, value) | 218 req.add_header(key, value) |
219 return req | 219 return req |
220 | 220 |
221 def _GetAuthToken(self, email, password): | 221 def _GetAuthToken(self, host, email, password): |
222 """Uses ClientLogin to authenticate the user, returning an auth token. | 222 """Uses ClientLogin to authenticate the user, returning an auth token. |
223 | 223 |
224 Args: | 224 Args: |
| 225 host: Host to get a token against. |
225 email: The user's email address | 226 email: The user's email address |
226 password: The user's password | 227 password: The user's password |
227 | 228 |
228 Raises: | 229 Raises: |
229 ClientLoginError: If there was an error authenticating with ClientLogin. | 230 ClientLoginError: If there was an error authenticating with ClientLogin. |
230 HTTPError: If there was some other form of HTTP error. | 231 HTTPError: If there was some other form of HTTP error. |
231 | 232 |
232 Returns: | 233 Returns: |
233 The authentication token returned by ClientLogin. | 234 The authentication token returned by ClientLogin. |
234 """ | 235 """ |
235 account_type = self.account_type | 236 account_type = self.account_type |
236 if self.host.endswith(".google.com"): | 237 if host.endswith(".google.com"): |
237 # Needed for use inside Google. | 238 # Needed for use inside Google. |
238 account_type = "HOSTED" | 239 account_type = "HOSTED" |
239 req = self._CreateRequest( | 240 req = self._CreateRequest( |
240 url="https://www.google.com/accounts/ClientLogin", | 241 url="https://www.google.com/accounts/ClientLogin", |
241 data=urllib.urlencode({ | 242 data=urllib.urlencode({ |
242 "Email": email, | 243 "Email": email, |
243 "Passwd": password, | 244 "Passwd": password, |
244 "service": "ah", | 245 "service": "ah", |
245 "source": "rietveld-codereview-upload", | 246 "source": "rietveld-codereview-upload", |
246 "accountType": account_type, | 247 "accountType": account_type, |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 authentication cookie. If login was successful, it tries to redirect | 310 authentication cookie. If login was successful, it tries to redirect |
310 us to the URL we provided. | 311 us to the URL we provided. |
311 | 312 |
312 If we attempt to access the upload API without first obtaining an | 313 If we attempt to access the upload API without first obtaining an |
313 authentication cookie, it returns a 401 response (or a 302) and | 314 authentication cookie, it returns a 401 response (or a 302) and |
314 directs us to authenticate ourselves with ClientLogin. | 315 directs us to authenticate ourselves with ClientLogin. |
315 """ | 316 """ |
316 for i in range(3): | 317 for i in range(3): |
317 credentials = self.auth_function() | 318 credentials = self.auth_function() |
318 try: | 319 try: |
319 auth_token = self._GetAuthToken(credentials[0], credentials[1]) | 320 auth_token = self._GetAuthToken(host, credentials[0], credentials[1]) |
320 except ClientLoginError, e: | 321 except ClientLoginError, e: |
321 if e.reason == "BadAuthentication": | 322 if e.reason == "BadAuthentication": |
322 print >>sys.stderr, "Invalid username or password." | 323 print >>sys.stderr, "Invalid username or password." |
323 continue | 324 continue |
324 if e.reason == "CaptchaRequired": | 325 if e.reason == "CaptchaRequired": |
325 print >>sys.stderr, ( | 326 print >>sys.stderr, ( |
326 "Please go to\n" | 327 "Please go to\n" |
327 "https://www.google.com/accounts/DisplayUnlockCaptcha\n" | 328 "https://www.google.com/accounts/DisplayUnlockCaptcha\n" |
328 "and verify you are a human. Then try again.\n" | 329 "and verify you are a human. Then try again.\n" |
329 "If you are using a Google Apps account the URL is:\n" | 330 "If you are using a Google Apps account the URL is:\n" |
(...skipping 1443 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1773 try: | 1774 try: |
1774 RealMain(sys.argv) | 1775 RealMain(sys.argv) |
1775 except KeyboardInterrupt: | 1776 except KeyboardInterrupt: |
1776 print | 1777 print |
1777 StatusUpdate("Interrupted.") | 1778 StatusUpdate("Interrupted.") |
1778 sys.exit(1) | 1779 sys.exit(1) |
1779 | 1780 |
1780 | 1781 |
1781 if __name__ == "__main__": | 1782 if __name__ == "__main__": |
1782 main() | 1783 main() |
OLD | NEW |