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 for git cl commands.""" | |
7 | |
8 import copy | |
9 import logging | |
10 import optparse | |
11 import os | |
12 | |
13 from third_party.google_api_python_client import apiclient | |
nodir
2015/03/10 22:39:40
this is still not sorted
sheyang
2015/03/13 20:58:27
Don;t remember the rule to sort 'from...import...'
| |
14 import third_party.oauth2client.client as oa2client | |
15 from third_party.oauth2client.file import Storage | |
16 from third_party.oauth2client import tools | |
17 | |
18 | |
19 REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob' | |
20 CLIENT_ID = ('174799409470-8k3b89iov4racu9jrf7if3k4591voig3' | |
21 '.apps.googleusercontent.com') | |
22 CLIENT_SECRET = 'DddcCK1d6_ADwxqGDEGlsisy' | |
23 SCOPE = 'email' | |
24 | |
25 | |
26 def _fetch_storage(code_review_server): | |
27 storage_dir = os.path.expanduser(os.path.join('~', '.git_cl_credentials')) | |
28 if not os.path.isdir(storage_dir): | |
29 os.makedirs(storage_dir) | |
30 storage_path = os.path.join(storage_dir, code_review_server) | |
31 storage = Storage(storage_path) | |
32 return storage | |
33 | |
34 | |
35 def _fetch_creds_from_storage(storage): | |
36 logging.debug('Fetching OAuth2 credentials from local storage ...') | |
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 | |
45 def add_oauth2_options(parser): | |
46 """Add OAuth2-related options.""" | |
47 group = optparse.OptionGroup(parser, "OAuth2 options") | |
48 group.add_option( | |
49 '--auth-host-name', | |
50 default='localhost', | |
51 help='Host name to use when running a local web server ' | |
52 'to handle redirects during OAuth authorization.' | |
53 'Default: localhost.' | |
54 ) | |
55 group.add_option( | |
56 '--auth-host-port', | |
57 type=int, | |
58 action="append", | |
nodir
2015/03/10 22:39:40
this still uses double quotes
sheyang
2015/03/13 20:58:27
Done.
| |
59 default=[8080, 8090], | |
60 help='Port to use when running a local web server to handle ' | |
61 'redirects during OAuth authorization. ' | |
62 'Repeat this option to specify a list of values.' | |
63 'Default: [8080, 8090].' | |
64 ) | |
65 group.add_option( | |
66 '--noauth-local-webserver', | |
67 action='store_true', | |
68 default=False, | |
69 help='Run a local web server to handle redirects ' | |
70 'during OAuth authorization.' | |
71 'Default: False.' | |
72 ) | |
73 group.add_option( | |
74 '--no-cache', | |
75 action='store_true', | |
76 default=False, | |
77 help='Get fresh credentials from web server instead of using ' | |
78 'the crendentials stored on a local storage file.' | |
79 'Default: False.' | |
80 ) | |
81 parser.add_option_group(group) | |
82 | |
83 | |
84 def get_oauth2_creds(options, code_review_server): | |
85 """Get OAuth2 credentials. | |
86 | |
87 Args: | |
88 options: Command line options. | |
89 code_review_server: Code review server name, e.g., codereview.chromium.org. | |
90 """ | |
91 storage = _fetch_storage(code_review_server) | |
92 creds = None | |
93 if not options.no_cache: | |
94 creds = _fetch_creds_from_storage(storage) | |
95 if creds is None: | |
96 logging.debug('Fetching OAuth2 credentials from web server...') | |
97 flow = oa2client.OAuth2WebServerFlow( | |
98 client_id=CLIENT_ID, | |
99 client_secret=CLIENT_SECRET, | |
100 scope=SCOPE, | |
101 redirect_uri=REDIRECT_URI) | |
102 flags = copy.deepcopy(options) | |
103 flags.logging_level = 'WARNING' | |
104 creds = tools.run_flow(flow, storage, flags) | |
105 return creds | |
OLD | NEW |