From bd2599c91a27f7db3a22d5abdd2e88b5631f1d0c Mon Sep 17 00:00:00 2001 From: b3yond Date: Fri, 23 Mar 2018 13:14:06 +0100 Subject: [PATCH] tested Mastodon OAuth Login - works now. --- db.py | 11 +++++++---- ticketfrei-web.py | 8 ++++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/db.py b/db.py index c6f903f..ae83a17 100644 --- a/db.py +++ b/db.py @@ -11,13 +11,14 @@ from mastodon import Mastodon class DB(object): def __init__(self): + self.config = ticketfrei.get_config() + self.logger = ticketfrei.get_logger(self.config) dbfile = path.join(path.dirname(path.abspath(__file__)), 'ticketfrei.sqlite') self.conn = sqlite3.connect(dbfile) self.cur = self.conn.cursor() self.create() self.secret = urandom(32) - self.config = ticketfrei.get_config() def create(self): # init db @@ -163,7 +164,9 @@ class User(object): def get_mastodon_app_keys(self, instance): self.db.cur.execute("SELECT client_id, client_secret FROM mastodon_instances WHERE instance = ?;", (instance, )) try: - client_id, client_secret = self.db.cur.fetchone()[0] + row = self.db.cur.fetchone() + client_id = row[0] + client_secret = row[1] return client_id, client_secret except TypeError: app_name = "ticketfrei" + str(self.db.secret)[0:4] @@ -176,9 +179,9 @@ class User(object): def save_masto_token(self, access_token, instance): self.db.cur.execute("SELECT id FROM mastodon_instances WHERE instance = ?;", (instance, )) instance_id = self.db.cur.fetchone()[0] - self.db.cur.execute("INSERT INTO mastodon_accounts(user_id, access_token, mastodon_instances_id, active) " + self.db.cur.execute("INSERT INTO mastodon_accounts(user_id, access_token, instance_id, active) " "VALUES(?, ?, ?, ?);", (self.uid, access_token, instance_id, 1)) - self.db.commit() + self.db.conn.commit() class DBPlugin(object): diff --git a/ticketfrei-web.py b/ticketfrei-web.py index d176ef3..eb57ec8 100644 --- a/ticketfrei-web.py +++ b/ticketfrei-web.py @@ -94,7 +94,8 @@ def login_twitter(user): try: redirect_url = auth.get_authorization_url() except tweepy.TweepError: - return 'Error! Failed to get request token.' + user.db.logger.error('Twitter OAuth Error: Failed to get request token.', exc_info=True) + return dict(error="Failed to get request token.") user.save_request_token(auth.request_token) return bottle.redirect(redirect_url) @@ -127,7 +128,9 @@ def login_mastodon(user): # get app tokens instance_url = bottle.request.forms.get('instance_url') masto_email = bottle.request.forms.get('email') - masto_pass = bottle.request.forms.get('password') + print(masto_email) + masto_pass = bottle.request.forms.get('pass') + print(masto_pass) client_id, client_secret = user.get_mastodon_app_keys(instance_url) m = Mastodon(client_id=client_id, client_secret=client_secret, api_base_url=instance_url) try: @@ -135,6 +138,7 @@ def login_mastodon(user): user.save_masto_token(access_token, instance_url) return dict(info='Thanks for supporting decentralized social networks!') except: + user.db.logger.error('Login to Mastodon failed.', exc_info=True) return dict(error='Login to Mastodon failed.')