From 55e507366c707feadfac19d416472dae2ec2449d Mon Sep 17 00:00:00 2001 From: montezdesousa <79287829+montezdesousa@users.noreply.github.com> Date: Sat, 2 Sep 2023 12:00:19 +0100 Subject: [PATCH] add flag to disable api auth (#5399) --- openbb_sdk/sdk/core/README.md | 7 ++++--- .../core/openbb_core/api/router/commands.py | 16 +++++++++------- openbb_sdk/sdk/core/openbb_core/env.py | 18 +++++++++++++----- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/openbb_sdk/sdk/core/README.md b/openbb_sdk/sdk/core/README.md index 5d7de7a8da4..7b8f5e6bcab 100644 --- a/openbb_sdk/sdk/core/README.md +++ b/openbb_sdk/sdk/core/README.md @@ -353,9 +353,10 @@ To apply an environment variable use one of the following: The variables we use are: -- `OPENBB_DEBUG_MODE`: enable/disable debug mode -- `OPENBB_DEVELOP_MODE`: enable/disable develop mode -- `OPENBB_AUTO_BUILD`: enable/disable automatic build for python package +- `OPENBB_DEBUG_MODE`: enables verbosity while running the program +- `OPENBB_DEVELOP_MODE`: points hub service to .co or .dev +- `OPENBB_AUTO_BUILD`: enables automatic SDK package build on import +- `OPENBB_API_AUTH`: enables commands authentication in FastAPI ## 4.2 Dynamic version diff --git a/openbb_sdk/sdk/core/openbb_core/api/router/commands.py b/openbb_sdk/sdk/core/openbb_core/api/router/commands.py index 4e036f64112..3b4cd969198 100644 --- a/openbb_sdk/sdk/core/openbb_core/api/router/commands.py +++ b/openbb_sdk/sdk/core/openbb_core/api/router/commands.py @@ -13,6 +13,7 @@ from openbb_core.app.model.user_settings import UserSettings from openbb_core.app.router import RouterLoader from openbb_core.app.service.system_service import SystemService from openbb_core.app.service.user_service import UserService +from openbb_core.env import Env from pydantic import BaseModel from typing_extensions import Annotated, ParamSpec @@ -64,14 +65,15 @@ def build_new_signature(func): ) ) - new_parameter_list.append( - Parameter( - "__authenticated_user_settings", - kind=Parameter.POSITIONAL_OR_KEYWORD, - default=UserSettings(), - annotation=Annotated[UserSettings, Depends(get_user)], + if Env().API_AUTH: + new_parameter_list.append( + Parameter( + "__authenticated_user_settings", + kind=Parameter.POSITIONAL_OR_KEYWORD, + default=UserSettings(), + annotation=Annotated[UserSettings, Depends(get_user)], + ) ) - ) return Signature( parameters=new_parameter_list, diff --git a/openbb_sdk/sdk/core/openbb_core/env.py b/openbb_sdk/sdk/core/openbb_core/env.py index 9a034002fcf..1a49bd5ac16 100644 --- a/openbb_sdk/sdk/core/openbb_core/env.py +++ b/openbb_sdk/sdk/core/openbb_core/env.py @@ -17,20 +17,28 @@ class Env(metaclass=SingletonMeta): @property def DEBUG_MODE(self) -> bool: - return self.str_to_bool(self._environ.get("OPENBB_DEBUG_MODE", False)) + """Debug mode: enables verbosity while running the program""" + return self.str2bool(self._environ.get("OPENBB_DEBUG_MODE", False)) @property def DEV_MODE(self) -> bool: + """Develop mode: points hub service to .co or .dev""" # TODO: Change default to false when ready to deploy - return self.str_to_bool(self._environ.get("OPENBB_DEV_MODE", True)) + return self.str2bool(self._environ.get("OPENBB_DEV_MODE", True)) @property def AUTO_BUILD(self) -> bool: - return self.str_to_bool(self._environ.get("OPENBB_AUTO_BUILD", True)) + """Automatic build: enables automatic SDK package build on import""" + return self.str2bool(self._environ.get("OPENBB_AUTO_BUILD", True)) + + @property + def API_AUTH(self) -> bool: + """API authentication: enables commands authentication in FastAPI""" + return self.str2bool(self._environ.get("OPENBB_API_AUTH", True)) @staticmethod - def str_to_bool(value) -> bool: - """Match a string to a boolean value.""" + def str2bool(value) -> bool: + """Match a value to its boolean correspondent.""" if isinstance(value, bool): return value if value.lower() in {"false", "f", "0", "no", "n"}: