Skip to content

Commit

Permalink
refactor(auth.AuthContext-auth.AuthUser): add classes to handle authe…
Browse files Browse the repository at this point in the history
…ntication context, authenticated user data and related operations

in gui.py AuthUser is passed to graphic interface objects, AuthContext is stored inside AuthUser class
in core.py AuthUser is instantiated when required
in __main__.py only an instance of AuthContext is necessary
in __init__.py an instance of AuthUser is created and then passed to gui objects
  • Loading branch information
Michele-Alberti committed Jan 3, 2025
1 parent e33c385 commit d0ca762
Show file tree
Hide file tree
Showing 9 changed files with 670 additions and 736 deletions.
32 changes: 20 additions & 12 deletions dlunch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
from . import core
from .core import __version__
from . import gui
from . import auth
from .auth import pn_user
from .auth import AuthUser

# LOGGER ----------------------------------------------------------------------
log: logging.Logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -41,16 +40,21 @@ def create_app(config: DictConfig) -> pn.Template:
"""
log.info("starting initialization process")

# Create an instance of AuthUser (which has also an instance of AuthContext
# among its attributes)
auth_user = AuthUser(config=config)

log.info("initialize database")
# Create tables
models.create_database(
config, add_basic_auth_users=auth.is_basic_auth_active(config=config)
config,
add_basic_auth_users=auth_user.auth_context.is_basic_auth_active(),
)

log.info("initialize support variables")
# Generate a random password only if requested (check on flag)
log.debug("config guest user")
guest_password = auth.set_guest_user_password(config)
guest_password = auth_user.auth_context.set_guest_user_password()

log.info("instantiate app")

Expand All @@ -62,13 +66,11 @@ def create_app(config: DictConfig) -> pn.Template:
# Set guest override flag if it is None (not found in flags table)
# Guest override flag is per-user and is not set for guests
if (
models.get_flag(config=config, id=f"{pn_user(config)}_guest_override")
models.get_flag(config=config, id=f"{auth_user.name}_guest_override")
is None
) and not auth.is_guest(
user=pn_user(config), config=config, allow_override=False
):
) and not auth_user.is_guest():
models.set_flag(
config=config, id=f"{pn_user(config)}_guest_override", value=False
config=config, id=f"{auth_user.name}_guest_override", value=False
)

# DASHBOARD BASE TEMPLATE
Expand Down Expand Up @@ -100,6 +102,7 @@ def create_app(config: DictConfig) -> pn.Template:
app=app,
person=person,
guest_password=guest_password,
auth_user=auth_user,
)

# DASHBOARD
Expand Down Expand Up @@ -127,7 +130,7 @@ def create_app(config: DictConfig) -> pn.Template:
gi.reload_on_guest_override(
toggle=models.get_flag(
config=config,
id=f"{pn_user(config)}_guest_override",
id=f"{auth_user.name}_guest_override",
value_if_missing=False,
),
reload=False,
Expand Down Expand Up @@ -156,10 +159,15 @@ def create_backend(config: DictConfig) -> pn.Template:

log.info("starting initialization process")

# Create an instance of AuthUser (which has also an instance of AuthContext
# among its attributes)
auth_user = AuthUser(config=config)

log.info("initialize database")
# Create tables
models.create_database(
config, add_basic_auth_users=auth.is_basic_auth_active(config=config)
config,
add_basic_auth_users=auth_user.auth_context.is_basic_auth_active(),
)

log.info("instantiate backend")
Expand All @@ -180,7 +188,7 @@ def create_backend(config: DictConfig) -> pn.Template:
)

# CONFIGURABLE OBJECTS
backend_gi = gui.BackendInterface(config)
backend_gi = gui.BackendInterface(config, auth_user=auth_user)

# DASHBOARD
# Build dashboard
Expand Down
15 changes: 8 additions & 7 deletions dlunch/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ def run_app(config: DictConfig) -> None:
waiter.set_config(config)

# Set auth configurations
log.info("set auth config and encryption")
log.info("set auth context and encryption")
auth_context = auth.AuthContext(config=config)
# Auth encryption
auth.set_app_auth_and_encryption(config)
auth_context.set_app_auth_and_encryption()
log.debug(
f'authentication {"" if auth.is_auth_active(config) else "not "}active'
f'authentication {"" if auth_context.is_auth_active() else "not "}active'
)

log.info("set panel config")
Expand All @@ -68,12 +69,12 @@ def run_app(config: DictConfig) -> None:
pn.config.auth_template = config.auth.auth_error_template

# If basic auth is used the database and users credentials shall be created here
if auth.is_basic_auth_active:
if auth_context.is_basic_auth_active():
log.info("initialize database and users credentials for basic auth")
# Create tables
models.create_database(
config,
add_basic_auth_users=auth.is_basic_auth_active(config=config),
add_basic_auth_users=auth_context.is_basic_auth_active(),
)

# Starting scheduled tasks
Expand All @@ -94,14 +95,14 @@ def run_app(config: DictConfig) -> None:
# Health is an endpoint for app health assessments
# Pass a dictionary for a multipage app
pages = {"": lambda: create_app(config=config)}
if auth.is_auth_active(config=config):
if auth_context.is_auth_active():
pages["backend"] = lambda: create_backend(config=config)

# If basic authentication is active, instantiate ta special auth object
# otherwise leave an empty dict
# This step is done before panel.serve because auth_provider requires that
# the whole config is passed as an input
if auth.is_basic_auth_active(config=config):
if auth_context.is_basic_auth_active():
auth_object = {
"auth_provider": hydra.utils.instantiate(
config.basic_auth.auth_provider, config
Expand Down
Loading

0 comments on commit d0ca762

Please sign in to comment.