From 7ecf29cef0a1671ae49e66a9fa2bd12715a0cc20 Mon Sep 17 00:00:00 2001 From: Stefan Stammberger Date: Tue, 17 Aug 2021 18:34:05 +0200 Subject: [PATCH] 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 --- .env_sample | 3 +++ app/models/system.py | 10 ++++++++++ app/routers/system.py | 6 ++++-- 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 app/models/system.py diff --git a/.env_sample b/.env_sample index 942e5cd..3445446 100644 --- a/.env_sample +++ b/.env_sample @@ -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 diff --git a/app/models/system.py b/app/models/system.py new file mode 100644 index 0000000..1fe65cf --- /dev/null +++ b/app/models/system.py @@ -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 diff --git a/app/routers/system.py b/app/routers/system.py index f8b2572..63904ac 100644 --- a/app/routers/system.py +++ b/app/routers/system.py @@ -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,