Use gflags for AOuth 1.0. Fixes issue #33. Fixes issue #34. Reviewed in http://codereview.appspot.com/4551069/

This commit is contained in:
Joe Gregorio
2011-05-27 14:04:59 -04:00
parent b696467176
commit 3b020d65ac
2 changed files with 37 additions and 28 deletions

View File

@@ -24,6 +24,7 @@ __author__ = 'jcgregorio@google.com (Joe Gregorio)'
__all__ = ["run"] __all__ = ["run"]
import BaseHTTPServer import BaseHTTPServer
import gflags
import logging import logging
import socket import socket
import sys import sys
@@ -37,6 +38,21 @@ except ImportError:
from cgi import parse_qsl from cgi import parse_qsl
FLAGS = gflags.FLAGS
gflags.DEFINE_boolean('auth_local_webserver', True,
('Run a local web server to handle redirects during '
'OAuth authorization.'))
gflags.DEFINE_string('auth_host_name', 'localhost',
('Host name to use when running a local web server to '
'handle redirects during OAuth authorization.'))
gflags.DEFINE_multi_int('auth_host_port', [8080, 8090],
('Port to use when running a local web server to '
'handle redirects during OAuth authorization.'))
class ClientRedirectServer(BaseHTTPServer.HTTPServer): class ClientRedirectServer(BaseHTTPServer.HTTPServer):
"""A server to handle OAuth 1.0 redirects back to localhost. """A server to handle OAuth 1.0 redirects back to localhost.
@@ -47,7 +63,7 @@ class ClientRedirectServer(BaseHTTPServer.HTTPServer):
class ClientRedirectHandler(BaseHTTPServer.BaseHTTPRequestHandler): class ClientRedirectHandler(BaseHTTPServer.BaseHTTPRequestHandler):
"""A handler for OAuth 2.0 redirects back to localhost. """A handler for OAuth 1.0 redirects back to localhost.
Waits for a single request and parses the query parameters Waits for a single request and parses the query parameters
into the servers query_params and then stops serving. into the servers query_params and then stops serving.
@@ -89,34 +105,24 @@ def run(flow, storage):
RequestError: if step2 of the flow fails. RequestError: if step2 of the flow fails.
Args: Args:
""" """
parser = OptionParser()
parser.add_option("-p", "--no_local_web_server", dest="localhost",
action="store_false",
default=True,
help="Do not run a web server on localhost to handle redirect URIs")
parser.add_option("-w", "--local_web_server", dest="localhost",
action="store_true",
default=True,
help="Run a web server on localhost to handle redirect URIs")
(options, args) = parser.parse_args() if FLAGS.auth_local_webserver:
success = False
host_name = 'localhost' port_number = 0
port_numbers = [8080, 8090] for port in FLAGS.auth_host_port:
if options.localhost: port_number = port
server_class = BaseHTTPServer.HTTPServer
try:
port_number = port_numbers[0]
httpd = server_class((host_name, port_number), ClientRedirectHandler)
except socket.error:
port_number = port_numbers[1]
try: try:
httpd = server_class((host_name, port_number), ClientRedirectHandler) httpd = BaseHTTPServer.HTTPServer((FLAGS.auth_host_name, port),
except socket.error: ClientRedirectHandler)
options.localhost = False except socket.error, e:
pass
else:
success = True
break
FLAGS.auth_local_webserver = success
if options.localhost: if FLAGS.auth_local_webserver:
oauth_callback = 'http://%s:%s/' % (host_name, port_number) oauth_callback = 'http://%s:%s/' % (FLAGS.auth_host_name, port_number)
else: else:
oauth_callback = 'oob' oauth_callback = 'oob'
authorize_url = flow.step1_get_authorize_url(oauth_callback) authorize_url = flow.step1_get_authorize_url(oauth_callback)
@@ -124,8 +130,12 @@ def run(flow, storage):
print 'Go to the following link in your browser:' print 'Go to the following link in your browser:'
print authorize_url print authorize_url
print print
if FLAGS.auth_local_webserver:
print 'If your browser is on a different machine then exit and re-run this'
print 'application with the command-line parameter --noauth_local_webserver.'
print
if options.localhost: if FLAGS.auth_local_webserver:
httpd.handle_request() httpd.handle_request()
if 'error' in httpd.query_params: if 'error' in httpd.query_params:
sys.exit('Authentication request was rejected.') sys.exit('Authentication request was rejected.')

View File

@@ -29,7 +29,6 @@ import logging
import socket import socket
import sys import sys
from optparse import OptionParser
from client import FlowExchangeError from client import FlowExchangeError
try: try: