Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 1 | # Copyright (C) 2010 Google Inc. |
| 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 | """Command-line tools for authenticating via OAuth 2.0 |
| 16 | |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 17 | Do the OAuth 2.0 Web Server dance for a command line application. Stores the |
| 18 | generated credentials in a common file that is used by other example apps in |
| 19 | the same directory. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 20 | """ |
| 21 | |
| 22 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 23 | __all__ = ['run'] |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 24 | |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 25 | |
Joe Gregorio | 9e5fe4d | 2011-03-10 09:33:47 -0500 | [diff] [blame] | 26 | import BaseHTTPServer |
| 27 | import gflags |
Joe Gregorio | 9e5fe4d | 2011-03-10 09:33:47 -0500 | [diff] [blame] | 28 | import socket |
| 29 | import sys |
Joe Gregorio | 2fdd295 | 2012-01-17 09:03:28 -0500 | [diff] [blame] | 30 | import webbrowser |
Joe Gregorio | 9e5fe4d | 2011-03-10 09:33:47 -0500 | [diff] [blame] | 31 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame^] | 32 | from oauth2client.client import FlowExchangeError |
| 33 | from oauth2client.client import OOB_CALLBACK_URN |
| 34 | from oauth2client import util |
Joe Gregorio | 9e5fe4d | 2011-03-10 09:33:47 -0500 | [diff] [blame] | 35 | |
| 36 | try: |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 37 | from urlparse import parse_qsl |
Joe Gregorio | 9e5fe4d | 2011-03-10 09:33:47 -0500 | [diff] [blame] | 38 | except ImportError: |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 39 | from cgi import parse_qsl |
Joe Gregorio | 9e5fe4d | 2011-03-10 09:33:47 -0500 | [diff] [blame] | 40 | |
| 41 | |
| 42 | FLAGS = gflags.FLAGS |
| 43 | |
| 44 | gflags.DEFINE_boolean('auth_local_webserver', True, |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 45 | ('Run a local web server to handle redirects during ' |
Joe Gregorio | 9e5fe4d | 2011-03-10 09:33:47 -0500 | [diff] [blame] | 46 | 'OAuth authorization.')) |
| 47 | |
| 48 | gflags.DEFINE_string('auth_host_name', 'localhost', |
| 49 | ('Host name to use when running a local web server to ' |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 50 | 'handle redirects during OAuth authorization.')) |
Joe Gregorio | 9e5fe4d | 2011-03-10 09:33:47 -0500 | [diff] [blame] | 51 | |
| 52 | gflags.DEFINE_multi_int('auth_host_port', [8080, 8090], |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 53 | ('Port to use when running a local web server to ' |
| 54 | 'handle redirects during OAuth authorization.')) |
Joe Gregorio | 9e5fe4d | 2011-03-10 09:33:47 -0500 | [diff] [blame] | 55 | |
| 56 | |
| 57 | class ClientRedirectServer(BaseHTTPServer.HTTPServer): |
| 58 | """A server to handle OAuth 2.0 redirects back to localhost. |
| 59 | |
| 60 | Waits for a single request and parses the query parameters |
| 61 | into query_params and then stops serving. |
| 62 | """ |
| 63 | query_params = {} |
| 64 | |
| 65 | |
| 66 | class ClientRedirectHandler(BaseHTTPServer.BaseHTTPRequestHandler): |
| 67 | """A handler for OAuth 2.0 redirects back to localhost. |
| 68 | |
| 69 | Waits for a single request and parses the query parameters |
| 70 | into the servers query_params and then stops serving. |
| 71 | """ |
| 72 | |
| 73 | def do_GET(s): |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 74 | """Handle a GET request. |
Joe Gregorio | 9e5fe4d | 2011-03-10 09:33:47 -0500 | [diff] [blame] | 75 | |
| 76 | Parses the query parameters and prints a message |
| 77 | if the flow has completed. Note that we can't detect |
| 78 | if an error occurred. |
| 79 | """ |
| 80 | s.send_response(200) |
| 81 | s.send_header("Content-type", "text/html") |
| 82 | s.end_headers() |
| 83 | query = s.path.split('?', 1)[-1] |
| 84 | query = dict(parse_qsl(query)) |
| 85 | s.server.query_params = query |
| 86 | s.wfile.write("<html><head><title>Authentication Status</title></head>") |
| 87 | s.wfile.write("<body><p>The authentication flow has completed.</p>") |
| 88 | s.wfile.write("</body></html>") |
| 89 | |
| 90 | def log_message(self, format, *args): |
| 91 | """Do not log messages to stdout while running as command line program.""" |
| 92 | pass |
| 93 | |
| 94 | |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame^] | 95 | @util.positional(2) |
Joe Gregorio | 8e000ed | 2012-02-07 15:41:44 -0500 | [diff] [blame] | 96 | def run(flow, storage, http=None): |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 97 | """Core code for a command-line application. |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 98 | |
| 99 | Args: |
| 100 | flow: Flow, an OAuth 2.0 Flow to step through. |
| 101 | storage: Storage, a Storage to store the credential in. |
Joe Gregorio | 8e000ed | 2012-02-07 15:41:44 -0500 | [diff] [blame] | 102 | http: An instance of httplib2.Http.request |
| 103 | or something that acts like it. |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 104 | |
| 105 | Returns: |
| 106 | Credentials, the obtained credential. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 107 | """ |
Joe Gregorio | 9e5fe4d | 2011-03-10 09:33:47 -0500 | [diff] [blame] | 108 | if FLAGS.auth_local_webserver: |
| 109 | success = False |
| 110 | port_number = 0 |
| 111 | for port in FLAGS.auth_host_port: |
| 112 | port_number = port |
| 113 | try: |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 114 | httpd = ClientRedirectServer((FLAGS.auth_host_name, port), |
| 115 | ClientRedirectHandler) |
Joe Gregorio | 9e5fe4d | 2011-03-10 09:33:47 -0500 | [diff] [blame] | 116 | except socket.error, e: |
| 117 | pass |
| 118 | else: |
| 119 | success = True |
| 120 | break |
| 121 | FLAGS.auth_local_webserver = success |
Joe Gregorio | 01c86b1 | 2012-06-07 14:31:32 -0400 | [diff] [blame] | 122 | if not success: |
| 123 | print 'Failed to start a local webserver listening on either port 8080' |
| 124 | print 'or port 9090. Please check your firewall settings and locally' |
| 125 | print 'running programs that may be blocking or using those ports.' |
| 126 | print |
| 127 | print 'Falling back to --noauth_local_webserver and continuing with', |
| 128 | print 'authorization.' |
| 129 | print |
Joe Gregorio | 9e5fe4d | 2011-03-10 09:33:47 -0500 | [diff] [blame] | 130 | |
| 131 | if FLAGS.auth_local_webserver: |
| 132 | oauth_callback = 'http://%s:%s/' % (FLAGS.auth_host_name, port_number) |
| 133 | else: |
Joe Gregorio | f2326c0 | 2012-02-09 12:18:44 -0500 | [diff] [blame] | 134 | oauth_callback = OOB_CALLBACK_URN |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame^] | 135 | flow.redirect_uri = oauth_callback |
| 136 | authorize_url = flow.step1_get_authorize_url() |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 137 | |
Joe Gregorio | 8097e2a | 2011-05-17 11:11:34 -0400 | [diff] [blame] | 138 | if FLAGS.auth_local_webserver: |
Joe Gregorio | 2fdd295 | 2012-01-17 09:03:28 -0500 | [diff] [blame] | 139 | webbrowser.open(authorize_url, new=1, autoraise=True) |
| 140 | print 'Your browser has been opened to visit:' |
| 141 | print |
| 142 | print ' ' + authorize_url |
| 143 | print |
Joe Gregorio | 8097e2a | 2011-05-17 11:11:34 -0400 | [diff] [blame] | 144 | print 'If your browser is on a different machine then exit and re-run this' |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 145 | print 'application with the command-line parameter ' |
Joe Gregorio | 2fdd295 | 2012-01-17 09:03:28 -0500 | [diff] [blame] | 146 | print |
| 147 | print ' --noauth_local_webserver' |
| 148 | print |
| 149 | else: |
| 150 | print 'Go to the following link in your browser:' |
| 151 | print |
| 152 | print ' ' + authorize_url |
Joe Gregorio | 8097e2a | 2011-05-17 11:11:34 -0400 | [diff] [blame] | 153 | print |
| 154 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 155 | code = None |
Joe Gregorio | 9e5fe4d | 2011-03-10 09:33:47 -0500 | [diff] [blame] | 156 | if FLAGS.auth_local_webserver: |
| 157 | httpd.handle_request() |
| 158 | if 'error' in httpd.query_params: |
| 159 | sys.exit('Authentication request was rejected.') |
| 160 | if 'code' in httpd.query_params: |
| 161 | code = httpd.query_params['code'] |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 162 | else: |
| 163 | print 'Failed to find "code" in the query parameters of the redirect.' |
| 164 | sys.exit('Try running with --noauth_local_webserver.') |
Joe Gregorio | 9e5fe4d | 2011-03-10 09:33:47 -0500 | [diff] [blame] | 165 | else: |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 166 | code = raw_input('Enter verification code: ').strip() |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 167 | |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 168 | try: |
Joe Gregorio | 68a8cfe | 2012-08-03 16:17:40 -0400 | [diff] [blame^] | 169 | credential = flow.step2_exchange(code, http=http) |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 170 | except FlowExchangeError, e: |
| 171 | sys.exit('Authentication has failed: %s' % e) |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 172 | |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 173 | storage.put(credential) |
| 174 | credential.set_store(storage) |
| 175 | print 'Authentication successful.' |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 176 | |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 177 | return credential |