1616from asgi_webdav .constants import (
1717 DEFAULT_FILENAME_CONTENT_TYPE_MAPPING ,
1818 DEFAULT_HTTP_BASIC_AUTH_CACHE_TIMEOUT ,
19+ DEFAULT_PASSWORD ,
20+ DEFAULT_PASSWORD_ANONYMOUS ,
21+ DEFAULT_PERMISSIONS ,
1922 DEFAULT_SUFFIX_CONTENT_TYPE_MAPPING ,
23+ DEFAULT_USERNAME ,
24+ DEFAULT_USERNAME_ANONYMOUS ,
2025 AppEntryParameters ,
2126 DAVCompressLevel ,
2227)
@@ -44,6 +49,17 @@ class User:
4449 admin : bool = False
4550
4651
52+ @dataclass
53+ class Anonymous :
54+ enable : bool = False
55+ user : User = field (
56+ default_factory = lambda : User (
57+ DEFAULT_USERNAME_ANONYMOUS , DEFAULT_PASSWORD_ANONYMOUS , DEFAULT_PERMISSIONS
58+ )
59+ )
60+ allow_missing_auth_header : bool = True
61+
62+
4763@dataclass
4864class HTTPBasicAuth :
4965 # enable: bool = True
@@ -149,7 +165,8 @@ class Logging:
149165class Config (JSONPyWizard ):
150166 # auth
151167 account_mapping : list [User ] = field (default_factory = list )
152- anonymous_username : str = ""
168+
169+ anonymous : Anonymous = field (default_factory = Anonymous )
153170
154171 http_basic_auth : HTTPBasicAuth = field (default_factory = HTTPBasicAuth )
155172 http_digest_auth : HTTPDigestAuth = field (default_factory = HTTPDigestAuth )
@@ -183,7 +200,7 @@ def _update_from_env_config(self):
183200 User (
184201 username = env_config .username ,
185202 password = env_config .password ,
186- permissions = [ "+" ] ,
203+ permissions = DEFAULT_PERMISSIONS ,
187204 ),
188205 )
189206 logger .info (f"Add user from ENV: { self .account_mapping [0 ].username } " )
@@ -208,7 +225,7 @@ def _update_from_app_args(self, aep: AppEntryParameters):
208225 User (
209226 username = aep .admin_user [0 ],
210227 password = aep .admin_user [1 ],
211- permissions = [ "+" ] ,
228+ permissions = DEFAULT_PERMISSIONS ,
212229 admin = True ,
213230 ),
214231 )
@@ -228,17 +245,27 @@ def _update_from_app_args(self, aep: AppEntryParameters):
228245 else :
229246 self .provider_mapping [root_path_index ].uri = root_path_uri
230247
231- def _fix_config (self ):
232- # account_mapping
248+ def _complete_config (self ):
249+ # auth - anonymous
250+ if self .anonymous .enable :
251+ self .account_mapping .append (self .anonymous .user )
252+ logger .info (
253+ f"Enable anonymous user: { self .anonymous .user .username } , permissions: { self .anonymous .user .permissions } , admin: { self .anonymous .user .admin } "
254+ )
255+
256+ # auth - default(admin) user
233257 if len (self .account_mapping ) == 0 :
234258 self .account_mapping .append (
235- User (username = "username" , password = "password" , permissions = ["+" ])
259+ User (
260+ username = DEFAULT_USERNAME ,
261+ password = DEFAULT_PASSWORD ,
262+ permissions = DEFAULT_PERMISSIONS ,
263+ admin = True ,
264+ )
265+ )
266+ logger .warning (
267+ f"Add defalut(admin) user: { DEFAULT_USERNAME } , password:{ DEFAULT_PASSWORD } , permissions: { DEFAULT_PERMISSIONS } "
236268 )
237- logger .warning ("Add defalut user: username/password" )
238-
239- if len (self .account_mapping ) == 1 :
240- self .account_mapping [0 ].admin = True
241- logger .warning ("Set the only account as admin" )
242269
243270 # provider - default
244271 if len (self .provider_mapping ) == 0 :
@@ -263,7 +290,7 @@ def update_from_app_args_and_env_and_default_value(self, aep: AppEntryParameters
263290 """
264291 self ._update_from_env_config ()
265292 self ._update_from_app_args (aep )
266- self ._fix_config ()
293+ self ._complete_config ()
267294
268295
269296_config : Config = Config ()
@@ -273,20 +300,24 @@ def get_config() -> Config:
273300 return _config
274301
275302
276- def get_config_copy_from_dict (data : dict ) -> Config :
277- return Config .from_dict (data )
303+ def get_config_copy_from_dict (data : dict , complete_config : bool = False ) -> Config :
304+ config = Config .from_dict (data )
305+ if complete_config :
306+ config ._complete_config ()
307+
308+ return config
278309
279310
280- def reinit_config_from_dict (data : dict ) -> Config :
311+ def reinit_config_from_dict (data : dict , complete_config : bool = False ) -> Config :
281312 global _config
282313
283314 logger .debug ("Load config value from python object(dict)" )
284- _config = get_config_copy_from_dict (data )
315+ _config = get_config_copy_from_dict (data , complete_config )
285316
286317 return _config
287318
288319
289- def reinit_config_from_file (file_name : str ) -> bool :
320+ def reinit_config_from_file (file_name : str , complete_config : bool = False ) -> bool :
290321 file = Path (file_name )
291322 match file .suffix :
292323 case ".json" :
@@ -314,6 +345,6 @@ def reinit_config_from_file(file_name: str) -> bool:
314345 logger .error (e )
315346 return False
316347
317- reinit_config_from_dict (data )
348+ reinit_config_from_dict (data , complete_config )
318349 logger .info (f"Load config from file: [{ file } ] success!" )
319350 return True
0 commit comments