Skip to content

Commit dab2d12

Browse files
committed
feat(portaswitch): support API token-based login with version compatibility check
1 parent bdac267 commit dab2d12

3 files changed

Lines changed: 29 additions & 5 deletions

File tree

app/bss/adapters/portaswitch/adapter.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import re
23
from datetime import datetime, timedelta, UTC
34
from typing import Final, Iterator, Optional, Dict, List
45

@@ -69,6 +70,8 @@
6970
)
7071
from .utils import generate_otp_id, extract_fault_code, generate_hash_dictionary
7172

73+
PORTASWITCH_VERSION_WITH_TOKEN: Final[str] = "128"
74+
7275

7376
class PortaSwitchAdapter(BSSAdapter):
7477
"""Bridges WebTrit Core with PortaSwitch APIs.
@@ -158,7 +161,14 @@ def authenticate(self, user: UserInfo, password: str = None) -> SessionInfo:
158161
if self._portaswitch_settings.ALLOWED_ADDONS:
159162
self._check_allowed_addons(account_info)
160163

161-
session_data = self._account_api.login(account_info["login"], account_info["password"])
164+
version = self._admin_api.get_version()
165+
actual_portaswitch_mr = [int(d) for d in re.findall(r'\d+', version)]
166+
expected_portaswitch_mr_with_token_support = [int(d) for d in
167+
re.findall(r'\d+', PORTASWITCH_VERSION_WITH_TOKEN)]
168+
169+
token = account_info["password"]
170+
session_data = self._account_api.login(account_info["login"], account_info["password"],
171+
token if actual_portaswitch_mr <= expected_portaswitch_mr_with_token_support else None)
162172

163173
return SessionInfo(
164174
user_id=UserId(str(account_info["i_account"])),

app/bss/adapters/portaswitch/api/account.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,24 @@ def decode_response(self, response: requests.models.Response) -> Union[dict, tup
100100

101101
raise ValueError("Not expected response")
102102

103-
def login(self, login: str, password: str) -> dict:
103+
def login(self, login: str, password: str, token: str = None) -> dict:
104104
"""Performs an account login by its login and password.
105105
106106
Parameters:
107107
:login (str): The login of the account.
108108
:password (str): The password of the account.
109+
:token (str): The token of the account.
109110
110111
Returns:
111112
:(dict): The API method execution result.
112113
113114
"""
114-
return self.__send_request(
115-
module="Session", method="login", params={"login": login, "password": password}
116-
)
115+
params = {"login": login, "password": password}
116+
117+
if token:
118+
params["token"] = token
119+
120+
return self.__send_request(**params)
117121

118122
def logout(self, access_token: str) -> dict:
119123
"""Performs an account logout.

app/bss/adapters/portaswitch/api/admin.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
from typing import Optional
23

34
from bss.adapters.portaswitch.config import PortaSwitchSettings
45
from bss.adapters.portaswitch.types import PortaSwitchAdminUser
@@ -168,6 +169,15 @@ def get_env_info(self) -> dict:
168169

169170
return response.get("env_info", dict())
170171

172+
def get_version(self) -> Optional[str]:
173+
"""Returns PortaSwitch version.
174+
Returns:
175+
str: The API method execution result that contains a PortaSwitch version.
176+
"""
177+
response = self._send_request(module="Generic", method="get_version", params={})
178+
179+
return response.get("version")
180+
171181
def _send_request(self, module: str, method: str, params: dict, turn_off_login: bool = False):
172182
"""Sends the Porta-Billing API method by means of HTTP POST request.
173183

0 commit comments

Comments
 (0)