Skip to content

Commit

Permalink
fix: improve login handling
Browse files Browse the repository at this point in the history
Improve login handling
  • Loading branch information
chemelli74 authored Jul 28, 2023
2 parents 337d196 + 3a31c02 commit e50cf1a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
7 changes: 7 additions & 0 deletions library_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import argparse
import asyncio
import logging

from aiovodafone.api import VodafoneStationApi

Expand Down Expand Up @@ -35,15 +36,21 @@ 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)
print("-" * 20)
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())
19 changes: 13 additions & 6 deletions src/aiovodafone/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import hashlib
import hmac
import html
import logging
import re
import urllib.parse
from dataclasses import dataclass
Expand All @@ -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
Expand Down Expand Up @@ -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."""
Expand Down
13 changes: 13 additions & 0 deletions src/aiovodafone/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""Constants for Vodafone Station."""
import logging

_LOGGER = logging.getLogger(__package__)

LOGIN = [
"-",
"logged",
"already logged",
"credential error",
"credential error",
"password mismatch",
]
4 changes: 4 additions & 0 deletions src/aiovodafone/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

0 comments on commit e50cf1a

Please sign in to comment.