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

51 lines
1.2 KiB
Python
Raw Normal View History

2022-03-19 04:07:19 +00:00
from fastapi import Depends
from fastapi.security import OAuth2PasswordBearer
from sqlalchemy.orm import Session
from ..config import Config
2022-03-20 02:32:40 +00:00
from ..db import Connection
from ..db.schemas import User
2022-03-19 04:07:19 +00:00
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="user/auth")
2022-03-19 17:11:52 +00:00
# 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,
}
2022-03-19 18:06:28 +00:00
entry_exists = {
"description": "Entry exists in database",
"content": None,
}
2022-03-19 17:11:52 +00:00
2022-03-19 04:07:19 +00:00
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)
2022-03-20 02:32:40 +00:00
user = User.from_db(db, username)
2022-03-19 04:07:19 +00:00
return user