44import sys
55from enum import Enum
66from pathlib import Path
7- from typing import List , Sequence , Union
7+ from typing import List , Sequence , Tuple , Union
88
99from pydantic import AnyHttpUrl , AnyUrl , BaseModel , BaseSettings , Extra , root_validator
1010
@@ -53,19 +53,19 @@ class Config(BaseSettingsConfig):
5353
5454
5555class CommaSeparatedTuple (str ):
56- """Pydantic field type validating comma separated strings or lists/tuples."""
56+ """Pydantic field type validating comma- separated strings or lists/tuples."""
5757
5858 @classmethod
5959 def __get_validators__ (cls ): # noqa: D105
6060 def validate (value : Union [str , Sequence [str ]]) -> Sequence [str ]:
61- """Check whether the value is a comma separated string or a list/tuple."""
61+ """Check whether the value is a comma- separated string or a list/tuple."""
6262 if isinstance (value , (tuple , list )):
6363 return tuple (value )
6464
6565 if isinstance (value , str ):
6666 return tuple (value .split ("," ))
6767
68- raise TypeError ("Invalid comma separated list" )
68+ raise TypeError ("Invalid comma- separated list" )
6969
7070 yield validate
7171
@@ -133,6 +133,44 @@ class Config: # noqa: D106
133133 timeout : float
134134
135135
136+ class AuthBackend (str , Enum ):
137+ """Model for valid authentication methods."""
138+
139+ BASIC = "basic"
140+ OIDC = "oidc"
141+
142+
143+ class AuthBackends (Tuple [AuthBackend ]):
144+ """Model representing a tuple of authentication backends."""
145+
146+ @classmethod
147+ def __get_validators__ (cls ):
148+ """Check whether the value is a comma-separated string or a tuple representing
149+ an AuthBackend.
150+ """ # noqa: D205
151+
152+ def validate (
153+ auth_backends : Union [
154+ str , AuthBackend , Tuple [AuthBackend ], List [AuthBackend ]
155+ ]
156+ ) -> Tuple [AuthBackend ]:
157+ """Check whether the value is a comma-separated string or a list/tuple."""
158+ if isinstance (auth_backends , str ):
159+ return tuple (
160+ AuthBackend (value .lower ()) for value in auth_backends .split ("," )
161+ )
162+
163+ if isinstance (auth_backends , AuthBackend ):
164+ return (auth_backends ,)
165+
166+ if isinstance (auth_backends , (tuple , list )):
167+ return tuple (auth_backends )
168+
169+ raise TypeError ("Invalid comma-separated list" )
170+
171+ yield validate
172+
173+
136174class Settings (BaseSettings ):
137175 """Pydantic model for Ralph's global environment & configuration settings."""
138176
@@ -142,12 +180,6 @@ class Config(BaseSettingsConfig):
142180 env_file = ".env"
143181 env_file_encoding = core_settings .LOCALE_ENCODING
144182
145- class AuthBackends (Enum ):
146- """Enum of the authentication backends."""
147-
148- BASIC = "basic"
149- OIDC = "oidc"
150-
151183 _CORE : CoreSettings = core_settings
152184 AUTH_FILE : Path = _CORE .APP_DIR / "auth.json"
153185 AUTH_CACHE_MAX_SIZE = 100
@@ -188,7 +220,7 @@ class AuthBackends(Enum):
188220 },
189221 }
190222 PARSERS : ParserSettings = ParserSettings ()
191- RUNSERVER_AUTH_BACKEND : AuthBackends = AuthBackends . BASIC
223+ RUNSERVER_AUTH_BACKENDS : AuthBackends = AuthBackends ( "basic" )
192224 RUNSERVER_AUTH_OIDC_AUDIENCE : str = None
193225 RUNSERVER_AUTH_OIDC_ISSUER_URI : AnyHttpUrl = None
194226 RUNSERVER_BACKEND : Literal [
0 commit comments