Example #1
0
from fastapi.responses import RedirectResponse
from fastapi.security import HTTPBasicCredentials, HTTPBasic
from fastapi.templating import Jinja2Templates

from myservices.auxiliary_methods import get_token_hex, get_response_by_format

homework3 = APIRouter()

templates = Jinja2Templates(directory='templates')

basic = HTTPBasic()

homework3.secret_key = "secret should be long because it is more secjurne"
login, password = '******', 'NotSoSecurePa$$'
homework3.saved_tokens = list()
homework3.saved_cookies = list()


# http://127.0.0.1:8000/hello
@homework3.get("/hello")
def get_hello_html(request: Request):
    return templates.TemplateResponse(
        "hello.html.j2", dict(request=request, tdate=str(date.today())))


@homework3.post("/login_session", status_code=201)
def post_login_session(response: Response,
                       credentials: HTTPBasicCredentials = Depends(basic)):
    if credentials.username == login and credentials.password == password:
        token_hex = get_token_hex(credentials.username, credentials.password,
                                  homework3.secret_key)