kiwi-vpn/api/kiwi_vpn_api/routers/_deps.py

49 lines
1.1 KiB
Python

from fastapi import Depends
from fastapi.security import OAuth2PasswordBearer
from sqlalchemy.orm import Session
from ..config import Config
from ..db import Connection, schemas
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="user/auth")
# just a namespace
class Responses:
ok = {
"content": None,
}
installed = {
"description": "kiwi-vpn already installed",
"content": None,
}
not_installed = {
"description": "kiwi-vpn not installed",
"content": None,
}
needs_user = {
"description": "Must be logged in",
"content": None,
}
needs_admin = {
"description": "Must be admin",
"content": None,
}
entry_exists = {
"description": "Entry exists in database",
"content": None,
}
async def get_current_user(
token: str = Depends(oauth2_scheme),
db: Session | None = Depends(Connection.get),
current_config: Config | None = Depends(Config.load),
):
if current_config is None:
return None
username = await current_config.jwt.decode_token(token)
user = schemas.User.from_db(db, username)
return user