confirmation emails work now, accounts can be created.

This commit is contained in:
b3yond 2018-02-16 11:33:27 +01:00
parent 3bc1010edf
commit 821f201454
3 changed files with 25 additions and 19 deletions

View file

@ -12,7 +12,7 @@ import pylibscrypt
class Datagetter(object): class Datagetter(object):
def __init__(self): def __init__(self):
self.db = "../ticketfrei.sqlite" self.db = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "ticketfrei.sqlite")
self.conn = self.create_connection(self.db) self.conn = self.create_connection(self.db)
self.cur = self.conn.cursor() self.cur = self.conn.cursor()
@ -44,13 +44,15 @@ def login():
psw = bottle.request.forms.get('psw') psw = bottle.request.forms.get('psw')
psw = psw.encode("utf-8") psw = psw.encode("utf-8")
db.cur.execute("SELECT pass_hashed FROM user WHERE email=?;", (uname, )), psw db.cur.execute("SELECT pass_hashed FROM user WHERE email=?;", (uname, )), psw
pass_hashed = db.cur.fetchone() try:
print(pass_hashed) pass_hashed = db.cur.fetchone()[0]
except TypeError:
return "Wrong Credentials." # no user with this email
if pylibscrypt.scrypt_mcf_check(pass_hashed, psw): if pylibscrypt.scrypt_mcf_check(pass_hashed, psw):
# :todo Generate Session Cookie and give to user # :todo Generate Session Cookie and give to user
return bottle.static_file("../static/bot.html", root="../static") return bottle.static_file("../static/bot.html", root="../static")
else: else:
return "Wrong Credentials." return "Wrong Credentials." # passphrase is wrong
@app.route('/register', method="POST") @app.route('/register', method="POST")
@ -68,33 +70,37 @@ def register():
# check if email is already in use # check if email is already in use
# needs to be encoded somehow # hash and format for being encoded in the confirmation mail
psw = psw.encode("utf-8") psw = psw.encode("utf-8")
psw = pylibscrypt.scrypt_mcf(psw) pass_hashed = pylibscrypt.scrypt_mcf(psw) # hash password
psw = base64.encodebytes(psw) pass_hashed = base64.encodebytes(pass_hashed)
psw = psw.decode("ascii") pass_hashed = pass_hashed.decode("ascii")
payload = {"email": email, "psw_hashed": psw} # hash password payload = {"email": email, "pass_hashed": pass_hashed}
encoded_jwt = jwt.encode(payload, secret)
confirmlink = "ticketfrei.links-tech.org/confirm?" + str(encoded_jwt) # create confirmlink
print(type(confirmlink)) encoded_jwt = jwt.encode(payload, secret).decode('utf-8')
host = bottle.request.get_header('host')
confirmlink = "http://" + host + "/confirm/" + str(encoded_jwt) # to be changed to https
# send the mail
m = sendmail.Mailer(config) m = sendmail.Mailer(config)
m.send("Complete your registration here: " + confirmlink, email, "[Ticketfrei] Confirm your account") m.send("Complete your registration here: " + confirmlink, email, "[Ticketfrei] Confirm your account")
return "We sent you an E-Mail. Please click on the confirmation link." return "We sent you an E-Mail. Please click on the confirmation link."
# How can I parse the arguments from the URI? @app.route('/confirm/<encoded_jwt>', method="GET")
# https://ticketfrei.links-tech.org/confirm?user=asdf&pass=sup3rs3cur3 def confirmaccount(encoded_jwt):
@app.route('/confirm', method="GET")
def confirmaccount():
""" """
Confirm the account creation and create a database entry. Confirm the account creation and create a database entry.
:return: Redirection to bot.html :return: Redirection to bot.html
""" """
encoded_jwt = bottle.request.forms.get('encoded_jwt') # get values from URL
dict = jwt.decode(encoded_jwt, secret) dict = jwt.decode(encoded_jwt, secret)
uname = dict["email"] uname = dict["email"]
pass_hashed = dict["psw_hashed"] pass_hashed = base64.b64decode(dict["pass_hashed"])
print(uname, pass_hashed) print(uname, pass_hashed)
# create db entry
db.cur.execute("INSERT INTO user(email, pass_hashed, enabled) VALUES(?, ?, ?);", (uname, pass_hashed, True)) db.cur.execute("INSERT INTO user(email, pass_hashed, enabled) VALUES(?, ?, ?);", (uname, pass_hashed, True))
db.conn.commit() db.conn.commit()
return bottle.static_file("../static/bot.html", root='../static') return bottle.static_file("../static/bot.html", root='../static')

View file

@ -1,6 +1,6 @@
<head> <head>
<title>Ticketfrei</title> <title>Ticketfrei</title>
<link rel='stylesheet' href='static/css/style.css'> <link rel='stylesheet' href='/static/css/style.css'>
</head> </head>
<body style="background-image: url(static/img/wallpaper.png)"> <body style="background-image: url(static/img/wallpaper.png)">
<div class="area"> <div class="area">

BIN
ticketfrei.sqlite Normal file

Binary file not shown.