2020-12-02 08:09:08 +00:00
|
|
|
#
|
|
|
|
# Copyright (C) 2020 GNS3 Technologies Inc.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2021-06-03 06:10:12 +00:00
|
|
|
import re
|
2020-12-02 08:09:08 +00:00
|
|
|
|
2021-05-27 07:58:44 +00:00
|
|
|
from fastapi import Request, Depends, HTTPException, status
|
2020-12-02 08:09:08 +00:00
|
|
|
from fastapi.security import OAuth2PasswordBearer
|
|
|
|
|
|
|
|
from gns3server import schemas
|
|
|
|
from gns3server.db.repositories.users import UsersRepository
|
2021-05-27 07:58:44 +00:00
|
|
|
from gns3server.db.repositories.rbac import RbacRepository
|
2020-12-02 08:09:08 +00:00
|
|
|
from gns3server.services import auth_service
|
2020-12-07 06:22:36 +00:00
|
|
|
from .database import get_repository
|
2020-12-02 08:09:08 +00:00
|
|
|
|
2021-04-20 02:29:02 +00:00
|
|
|
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/v3/users/login")
|
2020-12-02 08:09:08 +00:00
|
|
|
|
|
|
|
|
2020-12-07 06:22:36 +00:00
|
|
|
async def get_user_from_token(
|
2021-04-13 09:16:50 +00:00
|
|
|
token: str = Depends(oauth2_scheme), user_repo: UsersRepository = Depends(get_repository(UsersRepository))
|
2020-12-07 06:22:36 +00:00
|
|
|
) -> schemas.User:
|
2020-12-02 08:09:08 +00:00
|
|
|
|
|
|
|
username = auth_service.get_username_from_token(token)
|
|
|
|
user = await user_repo.get_user_by_username(username)
|
|
|
|
if user is None:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
|
detail="Could not validate credentials",
|
2021-04-13 09:16:50 +00:00
|
|
|
headers={"WWW-Authenticate": "Bearer"},
|
2020-12-02 08:09:08 +00:00
|
|
|
)
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
2021-05-27 07:58:44 +00:00
|
|
|
async def get_current_active_user(
|
|
|
|
request: Request,
|
|
|
|
current_user: schemas.User = Depends(get_user_from_token),
|
|
|
|
rbac_repo: RbacRepository = Depends(get_repository(RbacRepository))
|
|
|
|
) -> schemas.User:
|
2020-12-02 08:09:08 +00:00
|
|
|
|
2021-04-19 00:10:04 +00:00
|
|
|
# Super admin is always authorized
|
|
|
|
if current_user.is_superadmin:
|
|
|
|
return current_user
|
|
|
|
|
2020-12-02 08:09:08 +00:00
|
|
|
if not current_user.is_active:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
|
detail="Not an active user",
|
2021-04-13 09:16:50 +00:00
|
|
|
headers={"WWW-Authenticate": "Bearer"},
|
2020-12-02 08:09:08 +00:00
|
|
|
)
|
2021-05-27 07:58:44 +00:00
|
|
|
|
|
|
|
# remove the prefix (e.g. "/v3") from URL path
|
2021-08-17 06:44:15 +00:00
|
|
|
path = re.sub(r"^/v[0-9]", "", request.url.path)
|
2021-05-27 07:58:44 +00:00
|
|
|
|
2021-06-03 06:10:12 +00:00
|
|
|
# special case: always authorize access to the "/users/me" endpoint
|
|
|
|
if path == "/users/me":
|
|
|
|
return current_user
|
|
|
|
|
2021-05-27 07:58:44 +00:00
|
|
|
authorized = await rbac_repo.check_user_is_authorized(current_user.user_id, request.method, path)
|
|
|
|
if not authorized:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
|
detail=f"User is not authorized '{current_user.user_id}' on {request.method} '{path}'",
|
|
|
|
headers={"WWW-Authenticate": "Bearer"},
|
|
|
|
)
|
|
|
|
|
2020-12-02 08:09:08 +00:00
|
|
|
return current_user
|