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
|
|
|
|
from ..db import Connection, schemas
|
|
|
|
|
|
|
|
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)
|
|
|
|
user = schemas.User.from_db(db, username)
|
|
|
|
|
|
|
|
return user
|