Let PRAW manage authentication

This commit is contained in:
Théo Piboubès
2015-09-01 22:32:56 +02:00
parent 314d2dbf26
commit f6546aaf75
6 changed files with 4 additions and 73 deletions

View File

@@ -16,8 +16,6 @@ from tornado import ioloop, web
__all__ = ['token_validity', 'OAuthTool']
_logger = logging.getLogger(__name__)
token_validity = 3540
oauth_state = None
oauth_code = None
oauth_error = None
@@ -62,8 +60,6 @@ class OAuthTool(object):
self.access_info = {}
self.token_expiration = 0
# Initialize Tornado webapp and listen on port 65000
self.callback_app = web.Application([
(r'/', HomeHandler),
@@ -95,31 +91,6 @@ class OAuthTool(object):
with open(self.config_fp, 'w') as cfg:
self.config.write(cfg)
def set_token_expiration(self):
self.token_expiration = time.time() + token_validity
def token_expired(self):
return time.time() > self.token_expiration
def refresh(self, force=False):
if self.token_expired() or force:
try:
with self.loader(message='Refreshing token'):
new_access_info = self.reddit.refresh_access_information(
self.config.get('oauth', 'refresh_token'))
self.access_info = new_access_info
self.reddit.set_access_credentials(scope=set(self.access_info['scope']),
access_token=self.access_info['access_token'],
refresh_token=self.access_info['refresh_token'])
self.set_token_expiration()
except (praw.errors.OAuthAppRequired, praw.errors.OAuthInvalidToken,
praw.errors.HTTPException) as e:
show_notification(self.stdscr, ['Invalid OAuth data'])
else:
self.config.set('oauth', 'access_token', self.access_info['access_token'])
self.config.set('oauth', 'refresh_token', self.access_info['refresh_token'])
self.save_config()
def authorize(self):
self.reddit.set_oauth_app_info(self.client_id,
self.client_secret,
@@ -127,7 +98,7 @@ 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', 'access_token'):
if not self.config.has_section('oauth') or not self.config.has_option('oauth', 'refresh_token'):
# Generate a random UUID
hex_uuid = uuid.uuid4().hex
@@ -164,21 +135,15 @@ class OAuthTool(object):
with self.loader(message='Logging in'):
# Get access information (tokens and scopes)
self.access_info = self.reddit.get_access_information(self.final_code)
self.reddit.set_access_credentials(
scope=set(self.access_info['scope']),
access_token=self.access_info['access_token'],
refresh_token=self.access_info['refresh_token'])
self.set_token_expiration()
except (praw.errors.OAuthAppRequired, praw.errors.OAuthInvalidToken) as e:
show_notification(self.stdscr, ['Invalid OAuth data'])
else:
if not self.config.has_section('oauth'):
self.config.add_section('oauth')
self.config.set('oauth', 'access_token', self.access_info['access_token'])
self.config.set('oauth', 'refresh_token', self.access_info['refresh_token'])
self.save_config()
# Otherwise, fetch new access token
else:
self.refresh(force=True)
with self.loader(message='Logging in'):
self.reddit.refresh_access_information(self.config.get('oauth', 'refresh_token'))