From 98dfc019344facbf073a3933fef529ea079d19f0 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Wed, 30 Aug 2023 18:58:12 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=92=20refactor(auth.py):=20move=20SECR?= =?UTF-8?q?ET=5FKEY=20logic=20to=20AuthSettings=20class=20to=20improve=20c?= =?UTF-8?q?ode=20organization=20and=20reusability=20=F0=9F=94=92=20refacto?= =?UTF-8?q?r(base.py):=20remove=20SECRET=5FKEY=20field=20from=20Settings?= =?UTF-8?q?=20class=20since=20it=20is=20now=20handled=20by=20AuthSettings?= =?UTF-8?q?=20class=20=F0=9F=94=92=20refactor(manager.py):=20pass=20CONFIG?= =?UTF-8?q?=5FDIR=20to=20AuthSettings=20constructor=20when=20creating=20an?= =?UTF-8?q?=20instance?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../langflow/services/settings/auth.py | 42 +++++++++++++++++- .../langflow/services/settings/base.py | 44 +------------------ .../langflow/services/settings/manager.py | 2 +- 3 files changed, 42 insertions(+), 46 deletions(-) diff --git a/src/backend/langflow/services/settings/auth.py b/src/backend/langflow/services/settings/auth.py index ce1bfe108..7550d3ddd 100644 --- a/src/backend/langflow/services/settings/auth.py +++ b/src/backend/langflow/services/settings/auth.py @@ -1,13 +1,21 @@ +from pathlib import Path from typing import Optional import secrets +from langflow.services.settings.utils import read_secret_from_file, write_secret_to_file -from pydantic import BaseSettings +from pydantic import BaseSettings, Field, validator from passlib.context import CryptContext +from langflow.utils.logger import logger class AuthSettings(BaseSettings): # Login settings - SECRET_KEY: str = secrets.token_hex(32) + CONFIG_DIR: str + SECRET_KEY: Optional[str] = Field( + None, + description="Secret key for JWT. If not provided, a random one will be generated.", + env="LANGFLOW_SECRET_KEY", + ) ALGORITHM: str = "HS256" ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 REFRESH_TOKEN_EXPIRE_MINUTES: int = 70 @@ -31,3 +39,33 @@ class AuthSettings(BaseSettings): validate_assignment = True extra = "ignore" env_prefix = "LANGFLOW_" + + @validator("SECRET_KEY", pre=True) + def get_secret_key(cls, value, values): + config_dir = values.get("CONFIG_DIR") + + if not config_dir: + logger.debug("No CONFIG_DIR provided, not saving secret key") + return value or secrets.token_urlsafe(32) + + secret_key_path = Path(config_dir) / "secret_key" + + if value: + logger.debug("Secret key provided") + write_secret_to_file(secret_key_path, value) + else: + logger.debug("No secret key provided, generating a random one") + + if secret_key_path.exists(): + value = read_secret_from_file(secret_key_path) + logger.debug("Loaded secret key") + if not value: + value = secrets.token_urlsafe(32) + write_secret_to_file(secret_key_path, value) + logger.debug("Saved secret key") + else: + value = secrets.token_urlsafe(32) + write_secret_to_file(secret_key_path, value) + logger.debug("Saved secret key") + + return value diff --git a/src/backend/langflow/services/settings/base.py b/src/backend/langflow/services/settings/base.py index 4df77f8b2..00cd2085f 100644 --- a/src/backend/langflow/services/settings/base.py +++ b/src/backend/langflow/services/settings/base.py @@ -1,7 +1,5 @@ import contextlib import json -import secrets -from langflow.services.settings.utils import read_secret_from_file, write_secret_to_file import orjson import os from shutil import copy2 @@ -9,7 +7,7 @@ from typing import Optional, List from pathlib import Path import yaml -from pydantic import BaseSettings, Field, root_validator, validator +from pydantic import BaseSettings, root_validator, validator from langflow.utils.logger import logger # BASE_COMPONENTS_PATH = str(Path(__file__).parent / "components") @@ -43,46 +41,6 @@ class Settings(BaseSettings): REMOVE_API_KEYS: bool = False COMPONENTS_PATH: List[str] = [] - # Login settings - SECRET_KEY: Optional[str] = Field(None, env="LANGFLOW_SECRET_KEY") - - ALGORITHM: str = "HS256" - ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 - REFRESH_TOKEN_EXPIRE_MINUTES: int = 70 - # If AUTO_LOGIN = True - # > The application does not request login and logs in automatically as a super user. - AUTO_LOGIN: bool = True - - @validator("SECRET_KEY", pre=True) - def get_secret_key(cls, value, values): - config_dir = values.get("CONFIG_DIR") - - if not config_dir: - logger.debug("No CONFIG_DIR provided, not saving secret key") - return value or secrets.token_urlsafe(32) - - secret_key_path = Path(config_dir) / "secret_key" - - if value: - logger.debug("Secret key provided") - write_secret_to_file(secret_key_path, value) - else: - logger.debug("No secret key provided, generating a random one") - - if secret_key_path.exists(): - value = read_secret_from_file(secret_key_path) - logger.debug("Loaded secret key") - if not value: - value = secrets.token_urlsafe(32) - write_secret_to_file(secret_key_path, value) - logger.debug("Saved secret key") - else: - value = secrets.token_urlsafe(32) - write_secret_to_file(secret_key_path, value) - logger.debug("Saved secret key") - - return value - @validator("CONFIG_DIR", pre=True, allow_reuse=True) def set_langflow_dir(cls, value): if not value: diff --git a/src/backend/langflow/services/settings/manager.py b/src/backend/langflow/services/settings/manager.py index 1a6c0feeb..67e06108e 100644 --- a/src/backend/langflow/services/settings/manager.py +++ b/src/backend/langflow/services/settings/manager.py @@ -35,5 +35,5 @@ class SettingsManager(Service): ) settings = Settings(**settings_dict) - auth_settings = AuthSettings() + auth_settings = AuthSettings(CONFIG_DIR=settings.CONFIG_DIR) return cls(settings, auth_settings)