Skip to content

Commit 9c2cd39

Browse files
committed
fix(portaswitch): add fallback logic to user authorization flow
1 parent d1e7030 commit 9c2cd39

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

app/bss/adapters/portaswitch/adapter.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from datetime import datetime, timedelta, UTC
55
from typing import Final, Iterator, Optional, Dict, List
66

7+
from docusign_esign.client.api_exception import ArgumentException
78
from jose.exceptions import ExpiredSignatureError, JWTError
89

910
from app_config import AppConfig
@@ -165,7 +166,10 @@ def authenticate(self, user: UserInfo, password: str = None) -> SessionInfo:
165166

166167
if self._is_portaswitch_version_with_token():
167168
token = self._get_or_create_api_token(account_info)
168-
session_data = self._account_api.login(account_info["login"], token=token)
169+
if token:
170+
session_data = self._account_api.login(account_info["login"], token=token)
171+
else:
172+
session_data = self._account_api.login(account_info["login"], account_info["password"])
169173
else:
170174
session_data = self._account_api.login(account_info["login"], account_info["password"], token=account_info["password"])
171175

@@ -1470,13 +1474,17 @@ def _get_all_accounts_by_customer(self, i_customer: int) -> list[dict]:
14701474

14711475
return all_accounts
14721476

1473-
def _get_or_create_api_token(self, account_info: dict) -> str:
1477+
def _get_or_create_api_token(self, account_info: dict) -> Optional[str]:
14741478
"""Return the account's api_token, creating and persisting one if absent."""
14751479
api_token = account_info.get("api_token")
14761480
if not api_token:
14771481
api_token = str(uuid.uuid4())
1478-
self._admin_api.update_account(account_info["i_account"], api_token=api_token)
1479-
logging.info(f"Created new api_token for i_account={account_info['i_account']}")
1482+
try:
1483+
self._admin_api.update_account(account_info["i_account"], api_token=api_token)
1484+
logging.info(f"Created new api_token for i_account={account_info['i_account']}")
1485+
except Exception as e:
1486+
logging.warning(f"Failed to create new api_token for i_account={account_info['i_account']}: {e}")
1487+
return None
14801488
return api_token
14811489

14821490
def _emulate_account_login(self, i_account: str) -> dict:
@@ -1485,7 +1493,10 @@ def _emulate_account_login(self, i_account: str) -> dict:
14851493

14861494
if self._is_portaswitch_version_with_token():
14871495
token = self._get_or_create_api_token(account_info)
1488-
return self._account_api.login(account_info["login"], token=token)
1496+
if token:
1497+
return self._account_api.login(account_info["login"], token=token)
1498+
else:
1499+
return self._account_api.login(account_info["login"], account_info["password"])
14891500
else:
14901501
return self._account_api.login(account_info["login"], account_info["password"], token=account_info["password"])
14911502

0 commit comments

Comments
 (0)