77 lines
2.2 KiB
Python
77 lines
2.2 KiB
Python
"""Front page view factory."""
|
|
|
|
# pyright: reportUnusedFunction=false
|
|
import logging
|
|
from typing import Annotated
|
|
from fastapi import APIRouter, Depends, Request
|
|
from fastapi.responses import RedirectResponse
|
|
from pydantic import BaseModel
|
|
from sshecret_admin.auth import User
|
|
from sshecret_admin.services import AdminBackend
|
|
|
|
from ..dependencies import FrontendDependencies
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
START_PAGE = "/dashboard"
|
|
LOGIN_PAGE = "/login"
|
|
|
|
|
|
class StatsView(BaseModel):
|
|
"""Stats for the frontend."""
|
|
|
|
clients: int = 0
|
|
secrets: int = 0
|
|
audit_events: int = 0
|
|
|
|
|
|
async def get_stats(admin: AdminBackend) -> StatsView:
|
|
"""Get stats for the frontpage."""
|
|
clients = await admin.get_clients()
|
|
secrets = await admin.get_secrets()
|
|
audit = await admin.get_audit_log_count()
|
|
return StatsView(clients=len(clients), secrets=len(secrets), audit_events=audit)
|
|
|
|
|
|
def create_router(dependencies: FrontendDependencies) -> APIRouter:
|
|
"""Create auth router."""
|
|
|
|
app = APIRouter()
|
|
templates = dependencies.templates
|
|
|
|
@app.get("/")
|
|
def get_index(logged_in: Annotated[bool, Depends(dependencies.get_login_status)]):
|
|
"""Get the index."""
|
|
next = LOGIN_PAGE
|
|
if logged_in:
|
|
next = START_PAGE
|
|
|
|
return RedirectResponse(url=next)
|
|
|
|
@app.get("/dashboard")
|
|
async def get_dashboard(
|
|
request: Request,
|
|
admin: Annotated[AdminBackend, Depends(dependencies.get_admin_backend)],
|
|
current_user: Annotated[User, Depends(dependencies.get_user_from_access_token)],
|
|
):
|
|
"""Dashboard for mocking up the dashboard."""
|
|
stats = await get_stats(admin)
|
|
last_login_events = await admin.get_audit_log_detailed(limit=5, operation="login")
|
|
last_audit_events = await admin.get_audit_log_detailed(limit=10)
|
|
|
|
|
|
return templates.TemplateResponse(
|
|
request,
|
|
"dashboard.html",
|
|
{
|
|
"page_title": "sshecret",
|
|
"user": current_user.username,
|
|
"stats": stats,
|
|
"last_login_events": last_login_events,
|
|
"last_audit_events": last_audit_events,
|
|
|
|
},
|
|
)
|
|
|
|
return app
|