@@ -12,18 +12,84 @@ def __init__(self):
1212 self .api_key = None
1313 self .jwt_secret = None
1414 self .session_secret = None
15-
15+
1616 def configure_api_key (self , api_key ):
1717 self .auth_method = AuthMethod .API_KEY
1818 self .api_key = api_key
19-
19+
2020 def configure_jwt (self , secret_key ):
2121 self .auth_method = AuthMethod .JWT
2222 self .jwt_secret = secret_key
23-
23+
2424 def configure_session (self , secret_key ):
2525 self .auth_method = AuthMethod .SESSION
2626 self .session_secret = secret_key
27-
27+
2828 def disable_auth (self ):
29- self .auth_method = AuthMethod .NONE
29+ self .auth_method = AuthMethod .NONE
30+
31+ def update_from_dict (self , config_dict ):
32+ """Update authentication configuration from a dictionary.
33+
34+ Args:
35+ config_dict (dict): Configuration dictionary with 'auth' key containing:
36+ - method: one of 'none', 'api_key', 'jwt', 'session'
37+ - api_key: API key (required if method is 'api_key')
38+ - secret: Secret key (required if method is 'jwt' or 'session')
39+
40+ Raises:
41+ ValueError: If configuration is invalid
42+ """
43+ auth_config = config_dict .get ('auth' , {})
44+ method = auth_config .get ('method' , 'none' )
45+
46+ # Validate method
47+ valid_methods = [e .value for e in AuthMethod ]
48+ if method not in valid_methods :
49+ raise ValueError (f"Invalid authentication method: { method } . Must be one of: { valid_methods } " )
50+
51+ # Reset current configuration
52+ self .auth_method = AuthMethod .NONE
53+ self .api_key = None
54+ self .jwt_secret = None
55+ self .session_secret = None
56+
57+ # Apply new configuration
58+ if method == 'none' :
59+ self .disable_auth ()
60+ elif method == 'api_key' :
61+ api_key = auth_config .get ('api_key' )
62+ if not api_key :
63+ raise ValueError ("API key must be provided when using api_key authentication" )
64+ self .configure_api_key (api_key )
65+ elif method == 'jwt' :
66+ secret = auth_config .get ('secret' )
67+ if not secret :
68+ raise ValueError ("Secret key must be provided when using JWT authentication" )
69+ self .configure_jwt (secret )
70+ elif method == 'session' :
71+ secret = auth_config .get ('secret' )
72+ if not secret :
73+ raise ValueError ("Secret key must be provided when using session authentication" )
74+ self .configure_session (secret )
75+
76+ def to_dict (self ):
77+ """Convert current configuration to dictionary format.
78+
79+ Returns:
80+ dict: Configuration dictionary in the same format as the YAML file
81+ """
82+ config = {
83+ 'auth' : {
84+ 'method' : self .auth_method .value
85+ }
86+ }
87+
88+ if self .auth_method == AuthMethod .API_KEY and self .api_key :
89+ config ['auth' ]['api_key' ] = self .api_key
90+ elif self .auth_method == AuthMethod .JWT and self .jwt_secret :
91+ config ['auth' ]['secret' ] = self .jwt_secret
92+ elif self .auth_method == AuthMethod .SESSION and self .session_secret :
93+ config ['auth' ]['secret' ] = self .session_secret
94+
95+ return config
0 commit comments