Login finally works (but sessions don't)

This commit is contained in:
Juhani Krekelä 2018-06-09 19:15:47 +03:00
parent 6d02c01751
commit 128f937a11
2 changed files with 25 additions and 8 deletions

View File

@ -90,14 +90,15 @@ def index():
soup = new_soup() soup = new_soup()
return page_skeleton(page_title = config.site_name, contents = [], soup = soup) return page_skeleton(page_title = config.site_name, contents = [], soup = soup)
def login_forward(raw_path): def login(raw_path, retrying = False):
"""Returns html """Returns html
Creates a page telling the user to log in""" Creates a page telling the user to log in
if retrying is true, show a text about username/pass being wrong"""
# TODO: Take the user back to where they were # TODO: Take the user back to where they were
soup = new_soup() soup = new_soup()
# TODO: Don't hardcode # TODO: Don't hardcode
contents = bs4.BeautifulSoup(''' contents = list(bs4.BeautifulSoup('''
<p>Login to access</p> <p>Login to access</p>
<form action="''' + config.url_prefix + '''/login" method="post"> <form action="''' + config.url_prefix + '''/login" method="post">
<div> <div>
@ -110,10 +111,15 @@ def login_forward(raw_path):
<input type="submit" value="Login"/> <input type="submit" value="Login"/>
</div> </div>
</form> </form>
''', 'html.parser') ''', 'html.parser'))
if retrying:
p_tag = soup.new_tag('p')
p_tag.string = 'Wrong username or password'
contents = [contents[0], p_tag] + contents[1:]
# TODO: Internationalization # TODO: Internationalization
return page_skeleton(page_title = 'Login to ' + config.site_name, contents = list(contents), soup = soup) return page_skeleton(page_title = 'Login to ' + config.site_name, contents = contents, soup = soup)
def error_404(path): def error_404(path):
"""Returns html""" """Returns html"""

View File

@ -3,6 +3,7 @@ import http.server
import urllib.parse import urllib.parse
import config import config
import database
import generate_html import generate_html
class HTTPRequestHandler(http.server.BaseHTTPRequestHandler): class HTTPRequestHandler(http.server.BaseHTTPRequestHandler):
@ -72,8 +73,18 @@ class HTTPRequestHandler(http.server.BaseHTTPRequestHandler):
post_keys = urllib.parse.parse_qs(post_data.decode('utf-8'), keep_blank_values = True) post_keys = urllib.parse.parse_qs(post_data.decode('utf-8'), keep_blank_values = True)
if len(path_components) == 1 and path_components[0] == 'login': if len(path_components) == 1 and path_components[0] == 'login':
print(post_keys['username'], post_keys['password']) with database.connect() as db:
self.__redirect(buranun_session = 'dihutenosa') username = post_keys['username'][0]
password = post_keys['password'][0]
userid = database.get_userid(db, username)
password_correct = database.check_password(db, userid, password)
if password_correct:
self.__redirect(buranun_session = 'dihutenosa')
else:
# TODO: Have it forward the user back to the page where they were at
html = generate_html.login(self.path, retrying = True)
self.__send_html(html)
else: else:
self.__send_404(path) self.__send_404(path)
@ -104,7 +115,7 @@ class HTTPRequestHandler(http.server.BaseHTTPRequestHandler):
if not logged_in: if not logged_in:
# Display page that tells user to login # Display page that tells user to login
# TODO: Have it forward the user back to the page where they were at # TODO: Have it forward the user back to the page where they were at
html = generate_html.login_forward(self.path) html = generate_html.login(self.path)
self.__send_html(html) self.__send_html(html)
# Don't run rest of the function # Don't run rest of the function