File tree Expand file tree Collapse file tree 2 files changed +20
-9
lines changed
Expand file tree Collapse file tree 2 files changed +20
-9
lines changed Original file line number Diff line number Diff line change @@ -33,18 +33,19 @@ def load_config(path: Optional[Path] = None) -> ProxyConfig:
3333 with open (path ) as f :
3434 data = safe_load (f )
3535
36- config = ProxyConfig .model_validate (data )
37- return config
36+ return ProxyConfig (** data )
3837
3938
4039def token_defined (config : ProxyConfig , token : str ) -> bool :
4140 sha512 = hashlib .sha512 ()
4241 sha512 .update (token .encode ())
4342 token_digest = sha512 .digest ().hex ()
44- for env in config .environments :
45- if token_digest == env .token_sha512 :
46- logger .info (f'Authenticated environment "{ env .name } "' )
47- return True
43+
44+ if token_digest in config .token_env_map :
45+ logger .info (
46+ f'Authenticated environment "{ config .token_env_map [token_digest ].name } "'
47+ )
48+ return True
4849 return False
4950
5051
@@ -92,9 +93,10 @@ def get_environment_for_token(
9293 sha512 = hashlib .sha512 ()
9394 sha512 .update (token .encode ())
9495 token_digest = sha512 .digest ().hex ()
95- for env in config .environments :
96- if token_digest == env .token_sha512 :
97- return env
96+
97+ if token_digest in config .token_env_map :
98+ return config .token_env_map [token_digest ]
99+
98100 raise ValueError ("Could not find a environment for the given token" )
99101
100102
Original file line number Diff line number Diff line change @@ -163,6 +163,8 @@ class ProxyConfig(BaseModel):
163163 </body>
164164 </html>
165165"""
166+ # Dictionary for fast token lookups
167+ token_env_map : dict [str , ProxyConfigEnvironment ] = {}
166168
167169 @field_validator ("pdns_api_url" )
168170 @classmethod
@@ -178,6 +180,13 @@ def api_token_defined(cls, v):
178180 raise ValueError ("pdns_api_token must a non-empty string" )
179181 return v
180182
183+ def __init__ (self , ** data ):
184+ super ().__init__ (** data )
185+
186+ # Automatically populate token_env_map during initialization
187+ for env in self .environments :
188+ self .token_env_map [env .token_sha512 ] = env
189+
181190
182191class ResponseAllowed (BaseModel ):
183192 zones : list [ProxyConfigZone ]
You can’t perform that action at this time.
0 commit comments