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

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

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

Powered by Google App Engine
This is Rietveld 408576698