|
3 | 3 | import json |
4 | 4 | import logging |
5 | 5 | import random |
6 | | -import re |
7 | 6 | import string |
8 | 7 | import time |
9 | 8 | from typing import Optional, Tuple |
|
12 | 11 | import jwt |
13 | 12 | import pkce |
14 | 13 | from aiohttp import ClientSession |
| 14 | +from bs4 import BeautifulSoup |
15 | 15 |
|
16 | 16 | from iec_api import commons |
17 | 17 | from iec_api.models.exceptions import IECLoginError |
@@ -47,10 +47,20 @@ async def authorize_session(session: ClientSession, session_token) -> str: |
47 | 47 | authorize_response = await commons.send_non_json_get_request( |
48 | 48 | session=session, url=cmd_url, encoding="unicode-escape" |
49 | 49 | ) |
50 | | - code = re.findall( |
51 | | - r"<input type=\"hidden\" name=\"code\" value=\"(.+)\"/>", |
52 | | - authorize_response.encode("latin1").decode("utf-8"), |
53 | | - )[0] |
| 50 | + |
| 51 | + # A) Validate that the response is indeed an HTML |
| 52 | + if not authorize_response.strip().startswith("<!DOCTYPE html>") and not authorize_response.strip().startswith( |
| 53 | + "<html" |
| 54 | + ): |
| 55 | + raise IECLoginError(-1, "Autorize Response is not an HTML document") |
| 56 | + |
| 57 | + # B) Use BeautifulSoup to extract the code value |
| 58 | + soup = BeautifulSoup(authorize_response, "html.parser") |
| 59 | + code_input = soup.find("input", {"name": "code"}) |
| 60 | + if not code_input: |
| 61 | + raise IECLoginError(-1, "Code input not found in Autorize HTML response") |
| 62 | + |
| 63 | + code = code_input.get("value") |
54 | 64 | return code |
55 | 65 |
|
56 | 66 |
|
@@ -183,8 +193,7 @@ async def manual_authorization(session: ClientSession, id_number) -> Optional[JW |
183 | 193 | raise IECLoginError(-1, "Failed to send OTP, no state_token") |
184 | 194 |
|
185 | 195 | otp_code = await commons.read_user_input("Enter your OTP code: ") |
186 | | - code = await authorize_session(session, otp_code) |
187 | | - jwt_token = await verify_otp_code(session, factor_id, state_token, code) |
| 196 | + jwt_token = await verify_otp_code(session, factor_id, state_token, otp_code) |
188 | 197 | logger.debug( |
189 | 198 | f"Access token: {jwt_token.access_token}\n" |
190 | 199 | f"Refresh token: {jwt_token.refresh_token}\n" |
|
0 commit comments