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 | 06d852b | 2011-03-25 15:03:10 -0400 | [diff] [blame] | 32 | from client import FlowExchangeError |
Joe Gregorio | 9e5fe4d | 2011-03-10 09:33:47 -0500 | [diff] [blame] | 33 | |
| 34 | try: |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 35 | from urlparse import parse_qsl |
Joe Gregorio | 9e5fe4d | 2011-03-10 09:33:47 -0500 | [diff] [blame] | 36 | except ImportError: |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 37 | from cgi import parse_qsl |
Joe Gregorio | 9e5fe4d | 2011-03-10 09:33:47 -0500 | [diff] [blame] | 38 | |
| 39 | |
| 40 | FLAGS = gflags.FLAGS |
| 41 | |
| 42 | gflags.DEFINE_boolean('auth_local_webserver', True, |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 43 | ('Run a local web server to handle redirects during ' |
Joe Gregorio | 9e5fe4d | 2011-03-10 09:33:47 -0500 | [diff] [blame] | 44 | 'OAuth authorization.')) |
| 45 | |
| 46 | gflags.DEFINE_string('auth_host_name', 'localhost', |
| 47 | ('Host name to use when running a local web server to ' |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 48 | 'handle redirects during OAuth authorization.')) |
Joe Gregorio | 9e5fe4d | 2011-03-10 09:33:47 -0500 | [diff] [blame] | 49 | |
| 50 | gflags.DEFINE_multi_int('auth_host_port', [8080, 8090], |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 51 | ('Port to use when running a local web server to ' |
| 52 | 'handle redirects during OAuth authorization.')) |
Joe Gregorio | 9e5fe4d | 2011-03-10 09:33:47 -0500 | [diff] [blame] | 53 | |
| 54 | |
| 55 | class ClientRedirectServer(BaseHTTPServer.HTTPServer): |
| 56 | """A server to handle OAuth 2.0 redirects back to localhost. |
| 57 | |
| 58 | Waits for a single request and parses the query parameters |
| 59 | into query_params and then stops serving. |
| 60 | """ |
| 61 | query_params = {} |
| 62 | |
| 63 | |
| 64 | class ClientRedirectHandler(BaseHTTPServer.BaseHTTPRequestHandler): |
| 65 | """A handler for OAuth 2.0 redirects back to localhost. |
| 66 | |
| 67 | Waits for a single request and parses the query parameters |
| 68 | into the servers query_params and then stops serving. |
| 69 | """ |
| 70 | |
| 71 | def do_GET(s): |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 72 | """Handle a GET request. |
Joe Gregorio | 9e5fe4d | 2011-03-10 09:33:47 -0500 | [diff] [blame] | 73 | |
| 74 | Parses the query parameters and prints a message |
| 75 | if the flow has completed. Note that we can't detect |
| 76 | if an error occurred. |
| 77 | """ |
| 78 | s.send_response(200) |
| 79 | s.send_header("Content-type", "text/html") |
| 80 | s.end_headers() |
| 81 | query = s.path.split('?', 1)[-1] |
| 82 | query = dict(parse_qsl(query)) |
| 83 | s.server.query_params = query |
| 84 | s.wfile.write("<html><head><title>Authentication Status</title></head>") |
| 85 | s.wfile.write("<body><p>The authentication flow has completed.</p>") |
| 86 | s.wfile.write("</body></html>") |
| 87 | |
| 88 | def log_message(self, format, *args): |
| 89 | """Do not log messages to stdout while running as command line program.""" |
| 90 | pass |
| 91 | |
| 92 | |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 93 | def run(flow, storage): |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 94 | """Core code for a command-line application. |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 95 | |
| 96 | Args: |
| 97 | flow: Flow, an OAuth 2.0 Flow to step through. |
| 98 | storage: Storage, a Storage to store the credential in. |
| 99 | |
| 100 | Returns: |
| 101 | Credentials, the obtained credential. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 102 | """ |
Joe Gregorio | 9e5fe4d | 2011-03-10 09:33:47 -0500 | [diff] [blame] | 103 | if FLAGS.auth_local_webserver: |
| 104 | success = False |
| 105 | port_number = 0 |
| 106 | for port in FLAGS.auth_host_port: |
| 107 | port_number = port |
| 108 | try: |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 109 | httpd = ClientRedirectServer((FLAGS.auth_host_name, port), |
| 110 | ClientRedirectHandler) |
Joe Gregorio | 9e5fe4d | 2011-03-10 09:33:47 -0500 | [diff] [blame] | 111 | except socket.error, e: |
| 112 | pass |
| 113 | else: |
| 114 | success = True |
| 115 | break |
| 116 | FLAGS.auth_local_webserver = success |
| 117 | |
| 118 | if FLAGS.auth_local_webserver: |
| 119 | oauth_callback = 'http://%s:%s/' % (FLAGS.auth_host_name, port_number) |
| 120 | else: |
| 121 | oauth_callback = 'oob' |
| 122 | authorize_url = flow.step1_get_authorize_url(oauth_callback) |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 123 | |
Joe Gregorio | 8097e2a | 2011-05-17 11:11:34 -0400 | [diff] [blame] | 124 | if FLAGS.auth_local_webserver: |
Joe Gregorio | 2fdd295 | 2012-01-17 09:03:28 -0500 | [diff] [blame] | 125 | webbrowser.open(authorize_url, new=1, autoraise=True) |
| 126 | print 'Your browser has been opened to visit:' |
| 127 | print |
| 128 | print ' ' + authorize_url |
| 129 | print |
Joe Gregorio | 8097e2a | 2011-05-17 11:11:34 -0400 | [diff] [blame] | 130 | 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] | 131 | print 'application with the command-line parameter ' |
Joe Gregorio | 2fdd295 | 2012-01-17 09:03:28 -0500 | [diff] [blame] | 132 | print |
| 133 | print ' --noauth_local_webserver' |
| 134 | print |
| 135 | else: |
| 136 | print 'Go to the following link in your browser:' |
| 137 | print |
| 138 | print ' ' + authorize_url |
Joe Gregorio | 8097e2a | 2011-05-17 11:11:34 -0400 | [diff] [blame] | 139 | print |
| 140 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 141 | code = None |
Joe Gregorio | 9e5fe4d | 2011-03-10 09:33:47 -0500 | [diff] [blame] | 142 | if FLAGS.auth_local_webserver: |
| 143 | httpd.handle_request() |
| 144 | if 'error' in httpd.query_params: |
| 145 | sys.exit('Authentication request was rejected.') |
| 146 | if 'code' in httpd.query_params: |
| 147 | code = httpd.query_params['code'] |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 148 | else: |
| 149 | print 'Failed to find "code" in the query parameters of the redirect.' |
| 150 | sys.exit('Try running with --noauth_local_webserver.') |
Joe Gregorio | 9e5fe4d | 2011-03-10 09:33:47 -0500 | [diff] [blame] | 151 | else: |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 152 | code = raw_input('Enter verification code: ').strip() |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 153 | |
Joe Gregorio | fffa7d7 | 2011-02-18 17:20:39 -0500 | [diff] [blame] | 154 | try: |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 155 | credential = flow.step2_exchange(code) |
| 156 | except FlowExchangeError, e: |
| 157 | sys.exit('Authentication has failed: %s' % e) |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 158 | |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 159 | storage.put(credential) |
| 160 | credential.set_store(storage) |
| 161 | print 'Authentication successful.' |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 162 | |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 163 | return credential |