feat: enhancements to /system/login

* password_a must now be posted in the request body as a json object
* password is now read from the .env file instead of hardcoded
* password will be checked if it is min 8 chars
* one_time_password is not yet in use and optional
This commit is contained in:
Stefan Stammberger 2021-08-17 18:34:05 +02:00
parent 343d1703dd
commit 7ecf29cef0
No known key found for this signature in database
GPG key ID: 645FA807E935D9D5
3 changed files with 17 additions and 2 deletions

View file

@ -3,6 +3,9 @@ algorithm=HS256
# expiry time in seconds
jwt_expiry_time=300
# login password
password_a=12345678
# Amount of seconds the app will wait until it'll
# send another hardware update
gather_hw_info_interval = 2

10
app/models/system.py Normal file
View file

@ -0,0 +1,10 @@
from typing import Optional
from pydantic import BaseModel
from pydantic.types import conint, constr
class LoginInput(BaseModel):
password_a: constr(min_length=8)
one_time_password: Optional[constr(
min_length=6, max_length=6, regex="^[0-9]+$")] = None

View file

@ -1,10 +1,12 @@
from app.auth.auth_bearer import JWTBearer
from app.auth.auth_handler import signJWT
from app.models.system import LoginInput
from app.repositories.hardware_info import (HW_INFO_YIELD_TIME,
get_hardware_info,
subscribe_hardware_info)
from app.routers.system_docs import get_hw_info_json
from app.sse_starlette import EventSourceResponse
from decouple import config
from fastapi import APIRouter, HTTPException, Request, status
from fastapi.params import Depends
@ -17,8 +19,8 @@ router = APIRouter(
@router.post("/login", summary="Logs the user in with password A",
response_description="JWT token for the current session.",
status_code=status.HTTP_200_OK)
def login(password_a: str):
if password_a == "123":
def login(i: LoginInput):
if i.password_a == config("password_a", cast=str):
return signJWT()
raise HTTPException(status.HTTP_401_UNAUTHORIZED,