35 lines
1.4 KiB
Python
35 lines
1.4 KiB
Python
|
|
from typing import Any, Dict, Tuple, Union
|
|
|
|
import urllib.parse
|
|
|
|
from matemat.exceptions import AuthenticationError, HttpException
|
|
from matemat.webserver import pagelet, RequestArguments, PageletResponse, RedirectResponse, TemplateResponse
|
|
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],
|
|
config: Dict[str, str])\
|
|
-> Union[bytes, str, PageletResponse]:
|
|
if 'user' in session_vars:
|
|
return RedirectResponse('/')
|
|
if method == 'GET':
|
|
return TemplateResponse('touchkey.html',
|
|
username=str(args.username) if 'username' in args else None,
|
|
setupname=config['InstanceName'])
|
|
elif method == 'POST':
|
|
with MatematDatabase(config['DatabaseFile']) 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 RedirectResponse(f'/touchkey?username={quoted}')
|
|
session_vars['user'] = user
|
|
return RedirectResponse('/')
|
|
raise HttpException(405)
|