tested Mastodon OAuth Login - works now.

This commit is contained in:
b3yond 2018-03-23 13:14:06 +01:00
parent 81e2357e2f
commit bd2599c91a
2 changed files with 13 additions and 6 deletions

11
db.py
View file

@ -11,13 +11,14 @@ from mastodon import Mastodon
class DB(object): class DB(object):
def __init__(self): def __init__(self):
self.config = ticketfrei.get_config()
self.logger = ticketfrei.get_logger(self.config)
dbfile = path.join(path.dirname(path.abspath(__file__)), dbfile = path.join(path.dirname(path.abspath(__file__)),
'ticketfrei.sqlite') 'ticketfrei.sqlite')
self.conn = sqlite3.connect(dbfile) self.conn = sqlite3.connect(dbfile)
self.cur = self.conn.cursor() self.cur = self.conn.cursor()
self.create() self.create()
self.secret = urandom(32) self.secret = urandom(32)
self.config = ticketfrei.get_config()
def create(self): def create(self):
# init db # init db
@ -163,7 +164,9 @@ class User(object):
def get_mastodon_app_keys(self, instance): def get_mastodon_app_keys(self, instance):
self.db.cur.execute("SELECT client_id, client_secret FROM mastodon_instances WHERE instance = ?;", (instance, )) self.db.cur.execute("SELECT client_id, client_secret FROM mastodon_instances WHERE instance = ?;", (instance, ))
try: 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 return client_id, client_secret
except TypeError: except TypeError:
app_name = "ticketfrei" + str(self.db.secret)[0:4] app_name = "ticketfrei" + str(self.db.secret)[0:4]
@ -176,9 +179,9 @@ class User(object):
def save_masto_token(self, access_token, instance): def save_masto_token(self, access_token, instance):
self.db.cur.execute("SELECT id FROM mastodon_instances WHERE instance = ?;", (instance, )) self.db.cur.execute("SELECT id FROM mastodon_instances WHERE instance = ?;", (instance, ))
instance_id = self.db.cur.fetchone()[0] 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)) "VALUES(?, ?, ?, ?);", (self.uid, access_token, instance_id, 1))
self.db.commit() self.db.conn.commit()
class DBPlugin(object): class DBPlugin(object):

View file

@ -94,7 +94,8 @@ def login_twitter(user):
try: try:
redirect_url = auth.get_authorization_url() redirect_url = auth.get_authorization_url()
except tweepy.TweepError: 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) user.save_request_token(auth.request_token)
return bottle.redirect(redirect_url) return bottle.redirect(redirect_url)
@ -127,7 +128,9 @@ def login_mastodon(user):
# get app tokens # get app tokens
instance_url = bottle.request.forms.get('instance_url') instance_url = bottle.request.forms.get('instance_url')
masto_email = bottle.request.forms.get('email') 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) 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) m = Mastodon(client_id=client_id, client_secret=client_secret, api_base_url=instance_url)
try: try:
@ -135,6 +138,7 @@ def login_mastodon(user):
user.save_masto_token(access_token, instance_url) user.save_masto_token(access_token, instance_url)
return dict(info='Thanks for supporting decentralized social networks!') return dict(info='Thanks for supporting decentralized social networks!')
except: except:
user.db.logger.error('Login to Mastodon failed.', exc_info=True)
return dict(error='Login to Mastodon failed.') return dict(error='Login to Mastodon failed.')