OLD | NEW |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Google OAuth2 related functions.""" | 5 """Google OAuth2 related functions.""" |
6 | 6 |
7 import BaseHTTPServer | 7 import BaseHTTPServer |
8 import collections | 8 import collections |
9 import datetime | 9 import datetime |
10 import functools | 10 import functools |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 webserver_port=None, | 104 webserver_port=None, |
105 refresh_token_json=None): | 105 refresh_token_json=None): |
106 """Returns new instance of AuthConfig. | 106 """Returns new instance of AuthConfig. |
107 | 107 |
108 If some config option is None, it will be set to a reasonable default value. | 108 If some config option is None, it will be set to a reasonable default value. |
109 This function also acts as an authoritative place for default values of | 109 This function also acts as an authoritative place for default values of |
110 corresponding command line options. | 110 corresponding command line options. |
111 """ | 111 """ |
112 default = lambda val, d: val if val is not None else d | 112 default = lambda val, d: val if val is not None else d |
113 return AuthConfig( | 113 return AuthConfig( |
114 default(use_oauth2, _should_use_oauth2()), | 114 default(use_oauth2, True), |
115 default(save_cookies, True), | 115 default(save_cookies, True), |
116 default(use_local_webserver, not _is_headless()), | 116 default(use_local_webserver, not _is_headless()), |
117 default(webserver_port, 8090), | 117 default(webserver_port, 8090), |
118 default(refresh_token_json, '')) | 118 default(refresh_token_json, '')) |
119 | 119 |
120 | 120 |
121 def add_auth_options(parser, default_config=None): | 121 def add_auth_options(parser, default_config=None): |
122 """Appends OAuth related options to OptionParser.""" | 122 """Appends OAuth related options to OptionParser.""" |
123 default_config = default_config or make_auth_config() | 123 default_config = default_config or make_auth_config() |
124 parser.auth_group = optparse.OptionGroup(parser, 'Auth options') | 124 parser.auth_group = optparse.OptionGroup(parser, 'Auth options') |
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
474 credentials.token_expiry - datetime.datetime.utcnow()) | 474 credentials.token_expiry - datetime.datetime.utcnow()) |
475 storage = self._get_storage() | 475 storage = self._get_storage() |
476 credentials.set_store(storage) | 476 credentials.set_store(storage) |
477 storage.put(credentials) | 477 storage.put(credentials) |
478 return AccessToken(str(credentials.access_token), credentials.token_expiry) | 478 return AccessToken(str(credentials.access_token), credentials.token_expiry) |
479 | 479 |
480 | 480 |
481 ## Private functions. | 481 ## Private functions. |
482 | 482 |
483 | 483 |
484 def _should_use_oauth2(): | |
485 """Default value for use_oauth2 config option. | |
486 | |
487 Used to selectively enable OAuth2 by default. | |
488 """ | |
489 return os.path.exists(os.path.join(DEPOT_TOOLS_DIR, 'USE_OAUTH2')) | |
490 | |
491 | |
492 def _is_headless(): | 484 def _is_headless(): |
493 """True if machine doesn't seem to have a display.""" | 485 """True if machine doesn't seem to have a display.""" |
494 return sys.platform == 'linux2' and not os.environ.get('DISPLAY') | 486 return sys.platform == 'linux2' and not os.environ.get('DISPLAY') |
495 | 487 |
496 | 488 |
497 def _read_refresh_token_json(path): | 489 def _read_refresh_token_json(path): |
498 """Returns RefreshToken by reading it from the JSON file.""" | 490 """Returns RefreshToken by reading it from the JSON file.""" |
499 try: | 491 try: |
500 with open(path, 'r') as f: | 492 with open(path, 'r') as f: |
501 data = json.load(f) | 493 data = json.load(f) |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
627 self.end_headers() | 619 self.end_headers() |
628 query = self.path.split('?', 1)[-1] | 620 query = self.path.split('?', 1)[-1] |
629 query = dict(urlparse.parse_qsl(query)) | 621 query = dict(urlparse.parse_qsl(query)) |
630 self.server.query_params = query | 622 self.server.query_params = query |
631 self.wfile.write('<html><head><title>Authentication Status</title></head>') | 623 self.wfile.write('<html><head><title>Authentication Status</title></head>') |
632 self.wfile.write('<body><p>The authentication flow has completed.</p>') | 624 self.wfile.write('<body><p>The authentication flow has completed.</p>') |
633 self.wfile.write('</body></html>') | 625 self.wfile.write('</body></html>') |
634 | 626 |
635 def log_message(self, _format, *args): | 627 def log_message(self, _format, *args): |
636 """Do not log messages to stdout while running as command line program.""" | 628 """Do not log messages to stdout while running as command line program.""" |
OLD | NEW |