32 lines
1.1 KiB
Python
32 lines
1.1 KiB
Python
|
|
from typing import Any, Dict, Tuple, Union
|
|
|
|
import urllib.parse
|
|
|
|
from matemat.exceptions import AuthenticationError, HttpException
|
|
from matemat.webserver import pagelet, RequestArguments
|
|
from matemat.primitives import User
|
|
from matemat.db import MatematDatabase
|
|
|
|
|
|
@pagelet('/touchkey')
|
|
def touchkey_page(method: str,
|
|
path: str,
|
|
args: RequestArguments,
|
|
session_vars: Dict[str, Any],
|
|
headers: Dict[str, str])\
|
|
-> Union[bytes, str, Tuple[int, str], Tuple[str, Dict[str, Any]]]:
|
|
if 'user' in session_vars:
|
|
return 301, '/'
|
|
if method == 'GET':
|
|
return 'touchkey.html', {'username': str(args.username)} if 'username' in args else {}
|
|
elif method == 'POST':
|
|
with MatematDatabase('test.db') as db:
|
|
try:
|
|
user: User = db.login(str(args.username), touchkey=str(args.touchkey))
|
|
except AuthenticationError:
|
|
quoted = urllib.parse.quote_plus(bytes(args.username))
|
|
return 301, f'/touchkey?username={quoted}'
|
|
session_vars['user'] = user
|
|
return 301, '/'
|
|
raise HttpException(405)
|