feat: finish forecast rendering and account-authenticated API explorer
This commit is contained in:
@@ -7,13 +7,15 @@ import secrets
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import Request, Response
|
||||
from fastapi import Request, Response, Security
|
||||
from fastapi.security import APIKeyCookie
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from pigway_frame_flow.errors import ErrorCode as E
|
||||
from pigway_frame_flow.errors import FlowError
|
||||
|
||||
COOKIE = "frameflow_session"
|
||||
cookie = APIKeyCookie(name=COOKIE, auto_error=False, scheme_name="AccountSession")
|
||||
|
||||
|
||||
def password_hash(password: str) -> str:
|
||||
@@ -153,18 +155,33 @@ def install_accounts(app, db):
|
||||
raise FlowError(E.UNAUTHORIZED, "Account login required", 401)
|
||||
return row
|
||||
|
||||
@app.get("/api/v1/auth/me", response_model=AccountView, tags=["Accounts"])
|
||||
@app.get(
|
||||
"/api/v1/auth/me",
|
||||
response_model=AccountView,
|
||||
tags=["Accounts"],
|
||||
dependencies=[Security(cookie)],
|
||||
)
|
||||
def me(request: Request):
|
||||
row = current(request)
|
||||
return AccountView(username=row["username"], csrf_token=row["csrf"])
|
||||
|
||||
@app.post("/api/v1/auth/logout", status_code=204, tags=["Accounts"])
|
||||
@app.post(
|
||||
"/api/v1/auth/logout",
|
||||
status_code=204,
|
||||
tags=["Accounts"],
|
||||
dependencies=[Security(cookie)],
|
||||
)
|
||||
def logout(request: Request, response: Response):
|
||||
row = current(request)
|
||||
db.execute("DELETE FROM sessions WHERE token_hash=?", (row["token_hash"],))
|
||||
response.delete_cookie(COOKIE, path="/")
|
||||
|
||||
@app.put("/api/v1/auth/password", status_code=204, tags=["Accounts"])
|
||||
@app.put(
|
||||
"/api/v1/auth/password",
|
||||
status_code=204,
|
||||
tags=["Accounts"],
|
||||
dependencies=[Security(cookie)],
|
||||
)
|
||||
def change_password(payload: PasswordChange, request: Request, response: Response):
|
||||
session = current(request)
|
||||
user = db.one("SELECT * FROM users WHERE username=?", (session["username"],))
|
||||
|
||||
Reference in New Issue
Block a user