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

112 lines
2.8 KiB
Python
Raw Normal View History

2022-03-20 03:45:40 +00:00
"""
Common dependencies for routers.
"""
2022-03-23 00:39:19 +00:00
from fastapi import Depends, HTTPException, status
2022-03-19 04:07:19 +00:00
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
2022-03-23 01:14:02 +00:00
from ..db.schemas import User
2022-03-19 04:07:19 +00:00
2022-03-20 03:45:40 +00:00
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="user/authenticate")
2022-03-19 04:07:19 +00:00
2022-03-19 17:11:52 +00:00
class Responses:
2022-03-20 03:45:40 +00:00
"""
Just a namespace.
Describes API response status codes.
"""
OK = {
2022-03-19 17:11:52 +00:00
"content": None,
}
2022-03-20 03:45:40 +00:00
INSTALLED = {
2022-03-19 17:11:52 +00:00
"description": "kiwi-vpn already installed",
"content": None,
}
2022-03-20 03:45:40 +00:00
NOT_INSTALLED = {
2022-03-19 17:11:52 +00:00
"description": "kiwi-vpn not installed",
"content": None,
}
2022-03-20 03:45:40 +00:00
NEEDS_USER = {
2022-03-19 17:11:52 +00:00
"description": "Must be logged in",
"content": None,
}
2022-03-20 03:45:40 +00:00
NEEDS_ADMIN = {
2022-03-19 17:11:52 +00:00
"description": "Must be admin",
"content": None,
}
2022-03-20 03:45:40 +00:00
ENTRY_EXISTS = {
2022-03-19 18:06:28 +00:00
"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),
2022-03-23 00:39:19 +00:00
) -> User | None:
2022-03-20 03:45:40 +00:00
"""
Get the currently logged-in user from the database.
"""
# can't connect to an unconfigured database
2022-03-19 04:07:19 +00:00
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
2022-03-23 00:39:19 +00:00
2022-03-23 01:14:02 +00:00
async def get_current_user_if_admin(
2022-03-23 00:39:19 +00:00
current_config: Config | None = Depends(Config.load),
current_user: User | None = Depends(get_current_user),
) -> User:
"""
2022-03-23 01:14:02 +00:00
Get the currently logged-in user if it is an admin.
2022-03-23 00:39:19 +00:00
"""
# fail if not installed
if current_config is None:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
2022-03-23 01:14:02 +00:00
# fail if not requested by a user
if current_user is None:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
2022-03-23 00:39:19 +00:00
# fail if not requested by an admin
2022-03-23 01:14:02 +00:00
if not current_user.is_admin():
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
async def get_current_user_if_admin_or_self(
user_name: str,
current_config: Config | None = Depends(Config.load),
current_user: User | None = Depends(get_current_user),
) -> User:
"""
Get the currently logged-in user.
Fails a) if the currently logged-in user is not the requested user,
and b) if it is not an admin.
"""
# fail if not installed
if current_config is None:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
# fail if not requested by a user
if current_user is None:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
# fail if not requested by an admin or self
if not (current_user.is_admin() or current_user.name == user_name):
2022-03-23 00:39:19 +00:00
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)