Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(144)

Side by Side Diff: third_party/oauth2client/old_run.py

Issue 1085893002: Upgrade 3rd packages (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools
Patch Set: rebase Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 # Copyright (C) 2013 Google Inc. 1 # Copyright 2014 Google Inc. All rights reserved.
2 # 2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); 3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License. 4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at 5 # You may obtain a copy of the License at
6 # 6 #
7 # http://www.apache.org/licenses/LICENSE-2.0 7 # http://www.apache.org/licenses/LICENSE-2.0
8 # 8 #
9 # Unless required by applicable law or agreed to in writing, software 9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, 10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and 12 # See the License for the specific language governing permissions and
13 # limitations under the License. 13 # limitations under the License.
14 14
15 """This module holds the old run() function which is deprecated, the 15 """This module holds the old run() function which is deprecated, the
16 tools.run_flow() function should be used in its place.""" 16 tools.run_flow() function should be used in its place."""
17 17
18 from __future__ import print_function
18 19
19 import logging 20 import logging
20 import socket 21 import socket
21 import sys 22 import sys
22 import webbrowser 23 import webbrowser
23 24
24 import gflags 25 import gflags
25 26 from third_party.six.moves import input
26 from oauth2client import client 27 from third_party.oauth2client import client
27 from oauth2client import util 28 from third_party.oauth2client import util
28 from tools import ClientRedirectHandler 29 from tools import ClientRedirectHandler
29 from tools import ClientRedirectServer 30 from tools import ClientRedirectServer
30 31
31 32
32 FLAGS = gflags.FLAGS 33 FLAGS = gflags.FLAGS
33 34
34 gflags.DEFINE_boolean('auth_local_webserver', True, 35 gflags.DEFINE_boolean('auth_local_webserver', True,
35 ('Run a local web server to handle redirects during ' 36 ('Run a local web server to handle redirects during '
36 'OAuth authorization.')) 37 'OAuth authorization.'))
37 38
38 gflags.DEFINE_string('auth_host_name', 'localhost', 39 gflags.DEFINE_string('auth_host_name', 'localhost',
39 ('Host name to use when running a local web server to ' 40 ('Host name to use when running a local web server to '
40 'handle redirects during OAuth authorization.')) 41 'handle redirects during OAuth authorization.'))
41 42
42 gflags.DEFINE_multi_int('auth_host_port', [8080, 8090], 43 gflags.DEFINE_multi_int('auth_host_port', [8080, 8090],
43 ('Port to use when running a local web server to ' 44 ('Port to use when running a local web server to '
44 'handle redirects during OAuth authorization.')) 45 'handle redirects during OAuth authorization.'))
45 46
46 47
47 @util.positional(2) 48 @util.positional(2)
48 def run(flow, storage, http=None): 49 def run(flow, storage, http=None):
49 """Core code for a command-line application. 50 """Core code for a command-line application.
50 51
51 The run() function is called from your application and runs through all 52 The ``run()`` function is called from your application and runs
52 the steps to obtain credentials. It takes a Flow argument and attempts to 53 through all the steps to obtain credentials. It takes a ``Flow``
53 open an authorization server page in the user's default web browser. The 54 argument and attempts to open an authorization server page in the
54 server asks the user to grant your application access to the user's data. 55 user's default web browser. The server asks the user to grant your
55 If the user grants access, the run() function returns new credentials. The 56 application access to the user's data. If the user grants access,
56 new credentials are also stored in the Storage argument, which updates the 57 the ``run()`` function returns new credentials. The new credentials
57 file associated with the Storage object. 58 are also stored in the ``storage`` argument, which updates the file
59 associated with the ``Storage`` object.
58 60
59 It presumes it is run from a command-line application and supports the 61 It presumes it is run from a command-line application and supports the
60 following flags: 62 following flags:
61 63
62 --auth_host_name: Host name to use when running a local web server 64 ``--auth_host_name`` (string, default: ``localhost``)
63 to handle redirects during OAuth authorization. 65 Host name to use when running a local web server to handle
64 (default: 'localhost') 66 redirects during OAuth authorization.
65 67
66 --auth_host_port: Port to use when running a local web server to handle 68 ``--auth_host_port`` (integer, default: ``[8080, 8090]``)
67 redirects during OAuth authorization.; 69 Port to use when running a local web server to handle redirects
68 repeat this option to specify a list of values 70 during OAuth authorization. Repeat this option to specify a list
69 (default: '[8080, 8090]') 71 of values.
70 (an integer)
71 72
72 --[no]auth_local_webserver: Run a local web server to handle redirects 73 ``--[no]auth_local_webserver`` (boolean, default: ``True``)
73 during OAuth authorization. 74 Run a local web server to handle redirects during OAuth authorization.
74 (default: 'true')
75 75
76 Since it uses flags make sure to initialize the gflags module before 76 Since it uses flags make sure to initialize the ``gflags`` module before
77 calling run(). 77 calling ``run()``.
78 78
79 Args: 79 Args:
80 flow: Flow, an OAuth 2.0 Flow to step through. 80 flow: Flow, an OAuth 2.0 Flow to step through.
81 storage: Storage, a Storage to store the credential in. 81 storage: Storage, a ``Storage`` to store the credential in.
82 http: An instance of httplib2.Http.request 82 http: An instance of ``httplib2.Http.request`` or something that acts
83 or something that acts like it. 83 like it.
84 84
85 Returns: 85 Returns:
86 Credentials, the obtained credential. 86 Credentials, the obtained credential.
87 """ 87 """
88 logging.warning('This function, oauth2client.tools.run(), and the use of ' 88 logging.warning('This function, oauth2client.tools.run(), and the use of '
89 'the gflags library are deprecated and will be removed in a future ' 89 'the gflags library are deprecated and will be removed in a future '
90 'version of the library.') 90 'version of the library.')
91 if FLAGS.auth_local_webserver: 91 if FLAGS.auth_local_webserver:
92 success = False 92 success = False
93 port_number = 0 93 port_number = 0
94 for port in FLAGS.auth_host_port: 94 for port in FLAGS.auth_host_port:
95 port_number = port 95 port_number = port
96 try: 96 try:
97 httpd = ClientRedirectServer((FLAGS.auth_host_name, port), 97 httpd = ClientRedirectServer((FLAGS.auth_host_name, port),
98 ClientRedirectHandler) 98 ClientRedirectHandler)
99 except socket.error, e: 99 except socket.error as e:
100 pass 100 pass
101 else: 101 else:
102 success = True 102 success = True
103 break 103 break
104 FLAGS.auth_local_webserver = success 104 FLAGS.auth_local_webserver = success
105 if not success: 105 if not success:
106 print 'Failed to start a local webserver listening on either port 8080' 106 print('Failed to start a local webserver listening on either port 8080')
107 print 'or port 9090. Please check your firewall settings and locally' 107 print('or port 9090. Please check your firewall settings and locally')
108 print 'running programs that may be blocking or using those ports.' 108 print('running programs that may be blocking or using those ports.')
109 print 109 print()
110 print 'Falling back to --noauth_local_webserver and continuing with', 110 print('Falling back to --noauth_local_webserver and continuing with')
111 print 'authorization.' 111 print('authorization.')
112 print 112 print()
113 113
114 if FLAGS.auth_local_webserver: 114 if FLAGS.auth_local_webserver:
115 oauth_callback = 'http://%s:%s/' % (FLAGS.auth_host_name, port_number) 115 oauth_callback = 'http://%s:%s/' % (FLAGS.auth_host_name, port_number)
116 else: 116 else:
117 oauth_callback = client.OOB_CALLBACK_URN 117 oauth_callback = client.OOB_CALLBACK_URN
118 flow.redirect_uri = oauth_callback 118 flow.redirect_uri = oauth_callback
119 authorize_url = flow.step1_get_authorize_url() 119 authorize_url = flow.step1_get_authorize_url()
120 120
121 if FLAGS.auth_local_webserver: 121 if FLAGS.auth_local_webserver:
122 webbrowser.open(authorize_url, new=1, autoraise=True) 122 webbrowser.open(authorize_url, new=1, autoraise=True)
123 print 'Your browser has been opened to visit:' 123 print('Your browser has been opened to visit:')
124 print 124 print()
125 print ' ' + authorize_url 125 print(' ' + authorize_url)
126 print 126 print()
127 print 'If your browser is on a different machine then exit and re-run' 127 print('If your browser is on a different machine then exit and re-run')
128 print 'this application with the command-line parameter ' 128 print('this application with the command-line parameter ')
129 print 129 print()
130 print ' --noauth_local_webserver' 130 print(' --noauth_local_webserver')
131 print 131 print()
132 else: 132 else:
133 print 'Go to the following link in your browser:' 133 print('Go to the following link in your browser:')
134 print 134 print()
135 print ' ' + authorize_url 135 print(' ' + authorize_url)
136 print 136 print()
137 137
138 code = None 138 code = None
139 if FLAGS.auth_local_webserver: 139 if FLAGS.auth_local_webserver:
140 httpd.handle_request() 140 httpd.handle_request()
141 if 'error' in httpd.query_params: 141 if 'error' in httpd.query_params:
142 sys.exit('Authentication request was rejected.') 142 sys.exit('Authentication request was rejected.')
143 if 'code' in httpd.query_params: 143 if 'code' in httpd.query_params:
144 code = httpd.query_params['code'] 144 code = httpd.query_params['code']
145 else: 145 else:
146 print 'Failed to find "code" in the query parameters of the redirect.' 146 print('Failed to find "code" in the query parameters of the redirect.')
147 sys.exit('Try running with --noauth_local_webserver.') 147 sys.exit('Try running with --noauth_local_webserver.')
148 else: 148 else:
149 code = raw_input('Enter verification code: ').strip() 149 code = input('Enter verification code: ').strip()
150 150
151 try: 151 try:
152 credential = flow.step2_exchange(code, http=http) 152 credential = flow.step2_exchange(code, http=http)
153 except client.FlowExchangeError, e: 153 except client.FlowExchangeError as e:
154 sys.exit('Authentication has failed: %s' % e) 154 sys.exit('Authentication has failed: %s' % e)
155 155
156 storage.put(credential) 156 storage.put(credential)
157 credential.set_store(storage) 157 credential.set_store(storage)
158 print 'Authentication successful.' 158 print('Authentication successful.')
159 159
160 return credential 160 return credential
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698