Merge branch 'oauth2'

This commit is contained in:
Théo Piboubès
2015-09-06 02:45:55 +02:00
2 changed files with 38 additions and 15 deletions

View File

@@ -11,7 +11,8 @@ from six.moves import configparser
from . import config
from .curses_helpers import show_notification, prompt_input
from tornado import ioloop, web
from tornado import gen, ioloop, web, httpserver
from concurrent.futures import ThreadPoolExecutor
__all__ = ['token_validity', 'OAuthTool']
_logger = logging.getLogger(__name__)
@@ -27,18 +28,22 @@ class HomeHandler(web.RequestHandler):
class AuthHandler(web.RequestHandler):
def initialize(self):
self.compact = os.environ.get('BROWSER') in ['w3m', 'links', 'elinks', 'lynx']
def get(self):
try:
global oauth_state
global oauth_code
global oauth_error
global oauth_state
global oauth_code
global oauth_error
oauth_state = self.get_argument('state', default='state_placeholder')
oauth_code = self.get_argument('code', default='code_placeholder')
oauth_error = self.get_argument('error', default='error_placeholder')
oauth_state = self.get_argument('state', default='state_placeholder')
oauth_code = self.get_argument('code', default='code_placeholder')
oauth_error = self.get_argument('error', default='error_placeholder')
self.render('auth.html', state=oauth_state, code=oauth_code, error=oauth_error)
finally:
self.render('auth.html', state=oauth_state, code=oauth_code, error=oauth_error)
# Stop IOLoop if using BackgroundBrowser (or GUI browser)
if not self.compact:
ioloop.IOLoop.current().stop()
class OAuthTool(object):
@@ -69,6 +74,8 @@ class OAuthTool(object):
(r'/auth', AuthHandler),
], template_path='rtv/templates')
self.http_server = None
def get_config_fp(self):
HOME = os.path.expanduser('~')
XDG_CONFIG_HOME = os.getenv('XDG_CONFIG_HOME',
@@ -99,6 +106,13 @@ class OAuthTool(object):
self.config.remove_option('oauth', 'refresh_token')
self.save_config()
@gen.coroutine
def open_terminal_browser(self, url):
with ThreadPoolExecutor(max_workers=1) as executor:
yield executor.submit(webbrowser.open_new_tab, url)
ioloop.IOLoop.current().stop()
def authorize(self):
if self.compact and not '.compact' in self.reddit.config.API_PATHS['authorize']:
self.reddit.config.API_PATHS['authorize'] += '.compact'
@@ -110,9 +124,10 @@ class OAuthTool(object):
self.open_config(update=True)
# If no previous OAuth data found, starting from scratch
if not self.config.has_section('oauth') or not self.config.has_option('oauth', 'refresh_token'):
# Start HTTP server and listen on port 65000
self.callback_app.listen(65000)
if self.http_server is None:
self.http_server = httpserver.HTTPServer(self.callback_app)
self.http_server.listen(65000)
# Generate a random UUID
hex_uuid = uuid.uuid4().hex
@@ -122,7 +137,7 @@ class OAuthTool(object):
if self.compact:
show_notification(self.stdscr, ['Opening ' + os.environ.get('BROWSER')])
curses.endwin()
webbrowser.open_new_tab(permission_ask_page_link)
ioloop.IOLoop.current().add_callback(self.open_terminal_browser, permission_ask_page_link)
ioloop.IOLoop.current().start()
curses.doupdate()
else:

View File

@@ -1,6 +1,14 @@
from setuptools import setup
from version import __version__ as version
import sys
requirements = ['tornado', 'praw>=3.1.0', 'six', 'requests', 'kitchen']
# Python 2: add required concurrent.futures backport from Python 3.2
if sys.version_info.major <= 2:
requirements.append('futures')
setup(
name='rtv',
version=version,
@@ -13,7 +21,7 @@ setup(
keywords='reddit terminal praw curses',
packages=['rtv'],
include_package_data=True,
install_requires=['tornado', 'praw>=3.1.0', 'six', 'requests', 'kitchen'],
install_requires=requirements,
entry_points={'console_scripts': ['rtv=rtv.__main__:main']},
classifiers=[
'Intended Audience :: End Users/Desktop',