fixed db init, fixed confirmation mails, added logout button

This commit is contained in:
b3yond 2018-03-22 11:22:28 +01:00
parent c48704ea73
commit 9e09dcea84
4 changed files with 32 additions and 11 deletions

21
db.py
View file

@ -5,6 +5,9 @@ import jwt
from os import path, urandom
from pylibscrypt import scrypt_mcf, scrypt_mcf_check
import sqlite3
import pytoml as toml
import sendmail
import smtplib
class DB(object):
@ -15,9 +18,12 @@ class DB(object):
self.conn = sqlite3.connect(dbfile)
self.cur = self.conn.cursor()
self.cur.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='user';")
if self.cur.fetchone()[0] != 'user':
if self.cur.fetchall() == []:
self.create()
print("Initialized new sqlite database.")
self.secret = urandom(32)
with open('config.toml') as configfile:
self.config = toml.load(configfile)
def create(self):
# init db
@ -112,10 +118,10 @@ class DB(object):
(email, ))
row = self.cur.fetchone()
if not row:
return None
if not scrypt_mcf_check(row[1].decode('ascii').encode("utf-8"),
return None # No user with this email
if not scrypt_mcf_check(row[1].encode("utf-8"),
password.encode('utf-8')):
return None
return None # Wrong passphrase
return User(self, row[0])
def by_email(self, email):
@ -125,6 +131,13 @@ class DB(object):
return None
return User(self, row[0])
def send_confirmation_mail(self, confirm_link, email):
m = sendmail.Mailer(self.config)
try:
m.send("Complete your registration here: " + confirm_link, email, "[Ticketfrei] Confirm your account")
except smtplib.SMTPRecipientsRefused:
return "Please enter a valid E-Mail address."
def close(self):
self.conn.close()

View file

@ -1,4 +1,6 @@
% rebase('template/wrapper.tpl')
<a href="/logout/"><button>Logout</button></a>
<div id="enablebutton" style="float: right; padding: 2em;">asdf</div>
<a class='button' style="padding: 1.5em;" href="/login/twitter">

View file

@ -12,7 +12,7 @@
</head>
<body>
<div id="content">
<img src="/static/img/ticketfrei_logo.png" alt="Ticketfrei" id="logo">
<a href="/"><img src="/static/img/ticketfrei_logo.png" alt="<h1>Ticketfrei</h1>" id="logo"></a>
% if defined('error'):
<div class="ui-widget">
<div class="ui-state-error ui-corner-all" style="padding: 0.7em;">

View file

@ -6,9 +6,7 @@ from db import DBPlugin
@get('/')
@view('template/propaganda.tpl')
def propaganda():
# clear auth cookie
response.set_cookie('uid', '', expires=0)
pass
@post('/register', db='db')
@view('template/register.tpl')
@ -21,9 +19,9 @@ def register_post(db):
if db.by_email(email):
return dict(error='Email address already in use.')
# send confirmation mail
# XXX
return dict(info='<a href="%s/../confirm/%s">Confirmation mail sent.</a>' %
(request.url, db.token(email, password)))
confirm_link = request.url + "/../confirm/" + db.token(email, password)
db.send_confirmation_mail(confirm_link, email)
return dict(info='Confirmation mail sent.')
@get('/confirm/<token>', db='db')
@ -31,6 +29,7 @@ def register_post(db):
def confirm(db, token):
# create db-entry
if db.register(token):
# :todo show info "Account creation successful."
return redirect('/settings')
return dict(error='Account creation failed.')
@ -60,6 +59,13 @@ def api_enable(user):
def static(filename):
return bottle.static_file(filename, root='static')
@get('/logout/')
def logout():
# clear auth cookie
response.set_cookie('uid', '', expires=0, path="/")
# :todo show info "Logout successful."
return redirect('/')
bottle.install(DBPlugin('/'))
bottle.run(host='localhost', port=8080)