11import logging
22import re
3+ import uuid
34from datetime import datetime , timedelta , UTC
45from typing import Final , Iterator , Optional , Dict , List
56
@@ -162,8 +163,11 @@ def authenticate(self, user: UserInfo, password: str = None) -> SessionInfo:
162163 if self ._portaswitch_settings .ALLOWED_ADDONS :
163164 self ._check_allowed_addons (account_info )
164165
165- token = account_info ["password" ] if self ._is_portaswitch_version_with_token () else None
166- session_data = self ._account_api .login (account_info ["login" ], account_info ["password" ], token )
166+ if self ._is_portaswitch_version_with_token ():
167+ token = self ._get_or_create_api_token (account_info )
168+ session_data = self ._account_api .login (account_info ["login" ], token = token )
169+ else :
170+ session_data = self ._account_api .login (account_info ["login" ], account_info ["password" ], token = account_info ["password" ])
167171
168172 return SessionInfo (
169173 user_id = UserId (str (account_info ["i_account" ])),
@@ -1420,12 +1424,24 @@ def _get_all_accounts_by_customer(self, i_customer: int) -> list[dict]:
14201424
14211425 return all_accounts
14221426
1427+ def _get_or_create_api_token (self , account_info : dict ) -> str :
1428+ """Return the account's api_token, creating and persisting one if absent."""
1429+ api_token = account_info .get ("api_token" )
1430+ if not api_token :
1431+ api_token = str (uuid .uuid4 ())
1432+ self ._admin_api .update_account (account_info ["i_account" ], api_token = api_token )
1433+ logging .info (f"Created new api_token for i_account={ account_info ['i_account' ]} " )
1434+ return api_token
1435+
14231436 def _emulate_account_login (self , i_account : str ) -> dict :
14241437 """Emulate a login for a PortaSwitch account."""
14251438 account_info = self ._admin_api .get_account_info (i_account = i_account ).get ("account_info" )
14261439
1427- token = account_info ["password" ] if self ._is_portaswitch_version_with_token () else None
1428- return self ._account_api .login (account_info ["login" ], account_info ["password" ], token )
1440+ if self ._is_portaswitch_version_with_token ():
1441+ token = self ._get_or_create_api_token (account_info )
1442+ return self ._account_api .login (account_info ["login" ], token = token )
1443+ else :
1444+ return self ._account_api .login (account_info ["login" ], account_info ["password" ], token = account_info ["password" ])
14291445
14301446 def _is_portaswitch_version_with_token (self ) -> bool :
14311447 """Check if the actual version of PortaSwitch is a version with token support."""
@@ -1434,4 +1450,4 @@ def _is_portaswitch_version_with_token(self) -> bool:
14341450 expected_portaswitch_mr_with_token_support = [int (d ) for d in
14351451 re .findall (r'\d+' , PORTASWITCH_VERSION_WITH_TOKEN )]
14361452
1437- return actual_portaswitch_mr < expected_portaswitch_mr_with_token_support
1453+ return actual_portaswitch_mr >= expected_portaswitch_mr_with_token_support
0 commit comments