Skip to content

Commit

Permalink
fix: settings comma separated list of string parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
azmeuk committed Apr 5, 2024
1 parent 7d8f24c commit 1636350
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
36 changes: 17 additions & 19 deletions web/b3desk/settings.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import datetime
import json
from typing import Annotated
from typing import Any
from typing import Dict
from typing import List
from typing import Optional

from flask_babel import lazy_gettext as _
from pydantic import BeforeValidator
from pydantic import ValidationInfo
from pydantic import computed_field
from pydantic import field_validator
Expand All @@ -14,6 +16,17 @@

from b3desk.constants import DEFAULT_EMAIL_WHITELIST


def split_comma_separated_strings(value):
if not isinstance(value, str):
return value

return map(str.strip, value.split(","))


ListOfStrings = Annotated[List[str], BeforeValidator(split_comma_separated_strings)]


AVAILABLE_WORDINGS = {
"MEETING": {"cours": "cours", "reunion": "réunion", "seminaire": "séminaire"},
"MEETINGS": {"cours": "cours", "reunion": "réunions", "seminaire": "séminaires"},
Expand Down Expand Up @@ -289,18 +302,14 @@ def get_allowed_mime_types_server_side(
OIDC_OPENID_REALM: str = "apps"
"""Probablement un relicat de flask-oidc, semble inutilisé."""

OIDC_SCOPES: List[str] = ["openid", "email", "profile"]
OIDC_SCOPES: ListOfStrings = ["openid", "email", "profile"]
"""Liste des scopes OpenID Connect pour lesquels une autorisation sera
demandée au serveur d’identité, séparés par des virgules.
Passé en paramètre ``auth_request_params`` de flask-pyoidc.
Plus d’infos sur https://flask-pyoidc.readthedocs.io/en/latest/api.html#module-flask_pyoidc.provider_configuration
"""

@field_validator("OIDC_SCOPES", mode="before")
def get_oidc_scopes(cls, oidc_scopes: List[str], info: ValidationInfo) -> str:
return oidc_scopes.split(",") if isinstance(oidc_scopes, str) else oidc_scopes

OIDC_USERINFO_HTTP_METHOD: str = "POST"
"""Méthode ``GET`` ou ``POST`` à utiliser pour les requêtes sur le point
d’entrée *UserInfo* du serveur d’identité.
Expand Down Expand Up @@ -415,7 +424,7 @@ def get_oidc_scopes(cls, oidc_scopes: List[str], info: ValidationInfo) -> str:
Si non renseigné, prend la valeur de ``OIDC_SERVICE_NAME``.
"""

OIDC_ATTENDEE_SCOPES: Optional[List[str]] = None
OIDC_ATTENDEE_SCOPES: Optional[ListOfStrings] = None
"""Liste des scopes OpenID Connect pour lesquels une autorisation sera
demandée au serveur d’identité des participants authentifiés, séparés par
des virgules.
Expand Down Expand Up @@ -474,8 +483,7 @@ def get_attendee_attendee_service_name(
def get_attendee_attendee_scopes(
cls, attendee_scopes: str, info: ValidationInfo
) -> str:
scopes = attendee_scopes or info.data.get("OIDC_SCOPES")
return scopes.split(",") if isinstance(scopes, str) else scopes
return attendee_scopes or info.data.get("OIDC_SCOPES")

DOCUMENTATION_LINK_URL: Optional[str] = None
"""Surcharge l’adresse de la page de documentation si renseigné."""
Expand Down Expand Up @@ -967,23 +975,13 @@ def get_email_whitelist(
https://docs.bigbluebutton.org/development/api/#create
"""

RIE_NETWORK_IPS: Optional[List[str]] = None
RIE_NETWORK_IPS: Optional[ListOfStrings] = None
"""Plages d’adresses IP du réseau interministériel de l'État.
Affiche un encart particulier pour les utilisateurs se connectant
depuis ce réseau.
"""

@field_validator("RIE_NETWORK_IPS", mode="before")
def get_rie_network_ips(
cls, rie_network_ips: List[str], info: ValidationInfo
) -> str:
return (
rie_network_ips.split(",")
if isinstance(rie_network_ips, str)
else rie_network_ips
)

MAX_PARTICIPANTS: int = 200
"""Nombre moyen de participants indicatif sur la plateforme.
Expand Down
11 changes: 11 additions & 0 deletions web/tests/test_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from b3desk.settings import MainSettings


def test_list_of_strings_type(configuration):
configuration["OIDC_SCOPES"] = "openid, profile, ect.scope.cv"
configuration["OIDC_ATTENDEE_SCOPES"] = "openid, profile, ect.scope.cv"

config_obj = MainSettings.model_validate(configuration)

assert config_obj.OIDC_SCOPES == ["openid", "profile", "ect.scope.cv"]
assert config_obj.OIDC_ATTENDEE_SCOPES == ["openid", "profile", "ect.scope.cv"]

0 comments on commit 1636350

Please sign in to comment.