From 8e5ca410cf2842613f3e54ce8d1618e7f34a413a Mon Sep 17 00:00:00 2001 From: Simone Chemelli Date: Tue, 18 Jul 2023 08:34:18 +0000 Subject: [PATCH 1/2] chore: improve login handling --- library_test.py | 4 ++++ src/aiovodafone/api.py | 19 +++++++++++++------ src/aiovodafone/const.py | 13 +++++++++++++ src/aiovodafone/exceptions.py | 4 ++++ 4 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 src/aiovodafone/const.py diff --git a/library_test.py b/library_test.py index 72d8ef0..deca916 100644 --- a/library_test.py +++ b/library_test.py @@ -1,5 +1,6 @@ import argparse import asyncio +import logging from aiovodafone.api import VodafoneStationApi @@ -42,8 +43,11 @@ async def main() -> None: data = await api.get_user_data() print("Data:", data) print("-" * 20) + print("Logout & close session") await api.logout() + await api.close() if __name__ == "__main__": + logging.basicConfig(level=logging.DEBUG) asyncio.run(main()) diff --git a/src/aiovodafone/api.py b/src/aiovodafone/api.py index 920fb38..9a08fd4 100644 --- a/src/aiovodafone/api.py +++ b/src/aiovodafone/api.py @@ -3,7 +3,6 @@ import hashlib import hmac import html -import logging import re import urllib.parse from dataclasses import dataclass @@ -13,9 +12,8 @@ import aiohttp -from .exceptions import CannotAuthenticate, CannotConnect - -_LOGGER = logging.getLogger(__package__) +from .const import _LOGGER, LOGIN +from .exceptions import AlreadyLogged, CannotAuthenticate, CannotConnect @dataclass @@ -255,10 +253,19 @@ async def login(self) -> bool: verify_ssl=False, allow_redirects=True, ) - if reply.status != 200: + reply_json = await reply.json(content_type="text/html") + _LOGGER.debug("Login result: %s[%s]", LOGIN[int(reply_json)], reply_json) + + if reply_json == "1": + return True + + if reply_json == "2": + raise AlreadyLogged + + if reply_json in ["3", "4"]: raise CannotAuthenticate - return True + return False async def logout(self) -> None: """Router logout.""" diff --git a/src/aiovodafone/const.py b/src/aiovodafone/const.py new file mode 100644 index 0000000..5b1874b --- /dev/null +++ b/src/aiovodafone/const.py @@ -0,0 +1,13 @@ +"""Constants for Vodafone Station.""" +import logging + +_LOGGER = logging.getLogger(__package__) + +LOGIN = [ + "-", + "logged", + "already logged", + "credential error", + "credential error", + "password mismatch", +] diff --git a/src/aiovodafone/exceptions.py b/src/aiovodafone/exceptions.py index 446941a..9d806bd 100644 --- a/src/aiovodafone/exceptions.py +++ b/src/aiovodafone/exceptions.py @@ -12,3 +12,7 @@ class CannotConnect(VodafoneError): class CannotAuthenticate(VodafoneError): """Exception raised when credentials are incorrect.""" + + +class AlreadyLogged(VodafoneError): + """Exception raised if a user is already logged.""" From 3a31c025660443c3455f5d3e2acab4721c34a0be Mon Sep 17 00:00:00 2001 From: Simone Chemelli Date: Fri, 28 Jul 2023 20:49:39 +0000 Subject: [PATCH 2/2] fix: stop script if login fails --- library_test.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/library_test.py b/library_test.py index deca916..d3043e9 100644 --- a/library_test.py +++ b/library_test.py @@ -36,6 +36,9 @@ async def main() -> None: api = VodafoneStationApi(args.router, args.ssl, args.username, args.password) logged = await api.login() print("Logged:", logged) + if not logged: + exit(1) + print("-" * 20) devices = await api.get_all_devices() print("Devices:", devices)