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/>.
|
|
|
|
|
|
|
|
|
|
|
|
from jose import JWTError, jwt
|
|
|
|
from datetime import datetime, timedelta
|
2024-03-07 17:17:17 +00:00
|
|
|
import bcrypt
|
2020-12-02 08:09:08 +00:00
|
|
|
|
|
|
|
from typing import Optional
|
|
|
|
from fastapi import HTTPException, status
|
2021-04-15 08:42:08 +00:00
|
|
|
from gns3server.schemas.controller.tokens import TokenData
|
2020-12-16 07:54:21 +00:00
|
|
|
from gns3server.config import Config
|
2020-12-02 08:09:08 +00:00
|
|
|
from pydantic import ValidationError
|
|
|
|
|
2020-12-16 07:54:21 +00:00
|
|
|
import logging
|
2021-04-13 09:16:50 +00:00
|
|
|
|
2020-12-16 07:54:21 +00:00
|
|
|
log = logging.getLogger(__name__)
|
2020-12-02 08:09:08 +00:00
|
|
|
|
2020-12-18 06:21:54 +00:00
|
|
|
DEFAULT_JWT_SECRET_KEY = "efd08eccec3bd0a1be2e086670e5efa90969c68d07e072d7354a76cea5e33d4e"
|
|
|
|
|
2020-12-02 08:09:08 +00:00
|
|
|
|
|
|
|
class AuthService:
|
2021-11-18 08:07:10 +00:00
|
|
|
|
2020-12-02 08:09:08 +00:00
|
|
|
def hash_password(self, password: str) -> str:
|
|
|
|
|
2024-03-07 17:17:17 +00:00
|
|
|
salt = bcrypt.gensalt()
|
|
|
|
hashed_password = bcrypt.hashpw(password=password.encode('utf-8'), salt=salt)
|
|
|
|
return hashed_password.decode('utf-8')
|
2020-12-02 08:09:08 +00:00
|
|
|
|
|
|
|
def verify_password(self, password, hashed_password) -> bool:
|
|
|
|
|
2024-03-07 17:17:17 +00:00
|
|
|
return bcrypt.checkpw(password=password.encode('utf-8'), hashed_password=hashed_password.encode('utf-8'))
|
2020-12-02 08:09:08 +00:00
|
|
|
|
2021-04-13 09:16:50 +00:00
|
|
|
def create_access_token(self, username, secret_key: str = None, expires_in: int = 0) -> str:
|
2020-12-02 08:09:08 +00:00
|
|
|
|
2020-12-16 07:54:21 +00:00
|
|
|
if not expires_in:
|
2021-04-12 10:07:59 +00:00
|
|
|
expires_in = Config.instance().settings.Controller.jwt_access_token_expire_minutes
|
2020-12-07 06:22:36 +00:00
|
|
|
expire = datetime.utcnow() + timedelta(minutes=expires_in)
|
2020-12-02 08:09:08 +00:00
|
|
|
to_encode = {"sub": username, "exp": expire}
|
2020-12-16 07:54:21 +00:00
|
|
|
if secret_key is None:
|
2021-04-12 10:07:59 +00:00
|
|
|
secret_key = Config.instance().settings.Controller.jwt_secret_key
|
2020-12-16 07:54:21 +00:00
|
|
|
if secret_key is None:
|
2020-12-18 06:21:54 +00:00
|
|
|
secret_key = DEFAULT_JWT_SECRET_KEY
|
2021-04-05 08:13:35 +00:00
|
|
|
log.error("A JWT secret key must be configured to secure the server, using an unsecured default key!")
|
2021-04-12 10:07:59 +00:00
|
|
|
algorithm = Config.instance().settings.Controller.jwt_algorithm
|
2020-12-16 07:54:21 +00:00
|
|
|
encoded_jwt = jwt.encode(to_encode, secret_key, algorithm=algorithm)
|
2020-12-02 08:09:08 +00:00
|
|
|
return encoded_jwt
|
|
|
|
|
2020-12-16 07:54:21 +00:00
|
|
|
def get_username_from_token(self, token: str, secret_key: str = None) -> Optional[str]:
|
2020-12-02 08:09:08 +00:00
|
|
|
|
|
|
|
credentials_exception = HTTPException(
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
|
detail="Could not validate credentials",
|
|
|
|
headers={"WWW-Authenticate": "Bearer"},
|
|
|
|
)
|
|
|
|
try:
|
2020-12-16 07:54:21 +00:00
|
|
|
if secret_key is None:
|
2021-04-12 10:07:59 +00:00
|
|
|
secret_key = Config.instance().settings.Controller.jwt_secret_key
|
2020-12-16 07:54:21 +00:00
|
|
|
if secret_key is None:
|
2020-12-18 06:21:54 +00:00
|
|
|
secret_key = DEFAULT_JWT_SECRET_KEY
|
2021-04-05 08:13:35 +00:00
|
|
|
log.error("A JWT secret key must be configured to secure the server, using an unsecured default key!")
|
2021-04-12 10:07:59 +00:00
|
|
|
algorithm = Config.instance().settings.Controller.jwt_algorithm
|
2020-12-16 07:54:21 +00:00
|
|
|
payload = jwt.decode(token, secret_key, algorithms=[algorithm])
|
2020-12-02 08:09:08 +00:00
|
|
|
username: str = payload.get("sub")
|
|
|
|
if username is None:
|
|
|
|
raise credentials_exception
|
|
|
|
token_data = TokenData(username=username)
|
|
|
|
except (JWTError, ValidationError):
|
|
|
|
raise credentials_exception
|
|
|
|
return token_data.username
|