Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 # Copyright (c) 2015 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 """OAuth2 related utilities and implementation got git cl commands.""" | |
|
nodir
2015/03/05 03:58:56
what "got" means here? Did you mean "for"?
sheyang
2015/03/06 00:29:10
Yep... Fixed.
| |
| 7 | |
| 8 import copy | |
| 9 import logging | |
| 10 import optparse | |
| 11 import os | |
| 12 | |
| 13 import third_party.oauth2client.client as oa2client | |
| 14 from third_party.oauth2client.file import Storage | |
| 15 from third_party.oauth2client import tools | |
| 16 from third_party.google_api_python_client import apiclient | |
|
nodir
2015/03/05 03:58:55
sort imports
sheyang
2015/03/06 00:29:10
Done.
| |
| 17 | |
| 18 | |
| 19 REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob' | |
| 20 CLIENT_SECRETS = { | |
| 21 'client_id': ('174799409470-8k3b89iov4racu9jrf7if3k4591voig3' | |
| 22 '.apps.googleusercontent.com'), | |
| 23 'client_secret': 'DddcCK1d6_ADwxqGDEGlsisy' | |
|
nodir
2015/03/05 03:58:56
Make two constants instead of one dict
sheyang
2015/03/06 00:29:10
Done.
| |
| 24 } | |
| 25 | |
| 26 | |
| 27 def _fetch_storage(code_review_server): | |
| 28 storage_dir = os.path.expanduser('~/git_cl_credentials') | |
|
nodir
2015/03/05 03:58:56
Does this work on windows? I'd use os.path.expandu
sheyang
2015/03/06 00:29:10
Done.
| |
| 29 if not os.path.isdir(storage_dir): | |
| 30 os.makedirs(storage_dir) | |
| 31 storage_path = os.path.join(storage_dir, '.%s' % code_review_server) | |
|
nodir
2015/03/05 03:58:55
I'd make the parent dir invisible, and the files v
sheyang
2015/03/06 00:29:10
Done.
| |
| 32 storage = Storage(storage_path) | |
| 33 return storage | |
| 34 | |
|
nodir
2015/03/05 03:58:55
nit: double blank lines between top-level definiti
sheyang
2015/03/06 00:29:10
Done.
| |
| 35 def _fetch_cred_from_storage(storage): | |
| 36 print 'Fetching OAuth2 credential from local storage ...' | |
|
nodir
2015/03/05 03:58:55
Use logging.info or logging.debug instead of print
nodir
2015/03/05 03:58:56
Please use plural form "credentials", "creds", eve
sheyang
2015/03/06 00:29:10
Done.
sheyang
2015/03/06 00:29:10
Done.
| |
| 37 credentials = storage.get() | |
| 38 if not credentials or credentials.invalid: | |
| 39 return None | |
| 40 if not credentials.access_token or credentials.access_token_expired: | |
| 41 return None | |
| 42 return credentials | |
| 43 | |
| 44 def add_oauth2_options(parser): | |
| 45 """Add OAuth2-related options.""" | |
| 46 group = optparse.OptionGroup(parser, "OAuth2 options") | |
| 47 group.add_option( | |
| 48 '--auth_host_name', | |
| 49 default='localhost', | |
| 50 help='Host name to use when running a local web server ' | |
| 51 'to handle redirects during OAuth authorization.' | |
| 52 'Default: localhost.' | |
| 53 ) | |
| 54 group.add_option( | |
| 55 '--auth-host-port', | |
|
nodir
2015/03/05 03:58:56
use either - or _ between words in options, be con
sheyang
2015/03/06 00:29:10
use - now.
| |
| 56 type=int, | |
| 57 action="append", | |
|
nodir
2015/03/05 03:58:56
Use single quotes everywhere
sheyang
2015/03/06 00:29:10
Copy-Paste error... Fixed.
| |
| 58 default=[8080, 8090], | |
| 59 help='Port to use when running a local web server to handle ' | |
| 60 'redirects during OAuth authorization. ' | |
| 61 'Repeat this option to specify a list of values.' | |
| 62 'Default: [8080, 8090].' | |
| 63 ) | |
| 64 group.add_option( | |
| 65 '--noauth_local_webserver', | |
| 66 action="store_true", | |
| 67 default=False, | |
| 68 help='Run a local web server to handle redirects ' | |
| 69 'during OAuth authorization.' | |
| 70 'Default: False.' | |
| 71 ) | |
| 72 group.add_option( | |
| 73 '--no_cache', | |
| 74 action="store_true", | |
| 75 default=False, | |
| 76 help='Get a new credential from web server instead of using ' | |
| 77 'the crendential stored on a local storage file.' | |
| 78 'Default: False.' | |
| 79 ) | |
| 80 parser.add_option_group(group) | |
| 81 | |
| 82 def get_oauth2_cred(options, code_review_server): | |
| 83 """Get OAuth2 credential.""" | |
|
nodir
2015/03/05 03:58:56
Please document args. Is code_review_server a host
sheyang
2015/03/06 00:29:10
CODE_REVIEW_SERVER is the same key name used in co
| |
| 84 storage = _fetch_storage(code_review_server) | |
| 85 cred = None | |
| 86 if not options.no_cache: | |
| 87 cred = _fetch_cred_from_storage(storage) | |
| 88 if cred is None: | |
| 89 print 'Fetching OAuth2 credential from web server...' | |
| 90 flow = oa2client.OAuth2WebServerFlow( | |
| 91 client_id=CLIENT_SECRETS['client_id'], | |
| 92 client_secret=CLIENT_SECRETS['client_secret'], | |
| 93 scope='email', | |
|
nodir
2015/03/05 03:58:56
please make it a constant
sheyang
2015/03/06 00:29:10
Done.
| |
| 94 redirect_uri=REDIRECT_URI) | |
| 95 flags = copy.deepcopy(options) | |
| 96 flags.logging_level = 'WARNING' | |
| 97 cred = tools.run_flow(flow, storage, flags) | |
| 98 return cred | |
| OLD | NEW |