craftbeerpi4-pione/cbpi/http_endpoints/http_login.py

34 lines
1 KiB
Python
Raw Normal View History

2018-11-01 19:50:04 +01:00
from aiohttp import web
from aiohttp_auth import auth
2019-01-05 20:43:48 +01:00
from cbpi.api import *
2018-11-01 21:25:42 +01:00
2018-11-01 19:50:04 +01:00
class Login():
2018-11-01 21:25:42 +01:00
def __init__(self,cbpi):
self.cbpi = cbpi
2019-01-02 21:20:44 +01:00
self.cbpi.register(self, url_prefix="/")
2018-11-01 21:25:42 +01:00
2019-01-01 15:35:35 +01:00
self.db = {cbpi.static_config.get("username", "cbpi"): cbpi.static_config.get("password", "cbpi")}
2018-11-01 19:50:04 +01:00
2018-11-01 21:25:42 +01:00
@request_mapping(path="/logout", name="Logout", method="GET", auth_required=True)
2018-11-01 19:50:04 +01:00
async def logout_view(self, request):
await auth.forget(request)
return web.Response(body='OK'.encode('utf-8'))
2018-11-01 21:25:42 +01:00
@request_mapping(path="/login",name="Login", method="POST", auth_required=False)
2018-11-01 19:50:04 +01:00
async def login_view(self, request):
2018-11-01 19:50:04 +01:00
params = await request.post()
2018-11-01 21:25:42 +01:00
2018-11-01 19:50:04 +01:00
user = params.get('username', None)
2019-01-02 21:20:44 +01:00
if (user in self.db and params.get('password', None) == str(self.db[user])):
2018-11-01 19:50:04 +01:00
# User is in our database, remember their login details
await auth.remember(request, user)
return web.Response(body='OK'.encode('utf-8'))
raise web.HTTPForbidden()