Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional detail to login errors #229

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions src/synology_dsm/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,54 +80,52 @@ def __init__(self, code, details=None):
class SynologyDSMLoginInvalidException(SynologyDSMLoginFailedException):
"""Invalid password & not admin account exception."""

def __init__(self, username):
def __init__(self, username: str, error: str) -> None:
"""Constructor method."""
message = f"Invalid password or not admin account: {username}"
message = f"Invalid password or not admin account: {username}: {error}"
super().__init__(400, message)


class SynologyDSMLoginDisabledAccountException(SynologyDSMLoginFailedException):
"""Guest & disabled account exception."""

def __init__(self, username):
def __init__(self, username: str, error: str) -> None:
"""Constructor method."""
message = f"Guest or disabled account: {username}"
message = f"Guest or disabled account: {username}: {error}"
super().__init__(401, message)


class SynologyDSMLoginPermissionDeniedException(SynologyDSMLoginFailedException):
"""No access to login exception."""

def __init__(self, username):
def __init__(self, username: str, error: str) -> None:
"""Constructor method."""
message = f"Permission denied for account: {username}"
message = f"Permission denied for account: {username}: {error}"
super().__init__(402, message)


class SynologyDSMLogin2SARequiredException(SynologyDSMLoginFailedException):
"""2SA required to login exception."""

def __init__(self, username):
def __init__(self, username: str, error: str) -> None:
"""Constructor method."""
message = f"Two-step authentication required for account: {username}"
message = f"Two-step authentication required for account: {username}: {error}"
super().__init__(403, message)


class SynologyDSMLogin2SAFailedException(SynologyDSMLoginFailedException):
"""2SA code failed exception."""

def __init__(self):
def __init__(self, username: str, error: str) -> None:
"""Constructor method."""
message = "Two-step authentication failed, retry with a new pass code"
message = f"Two-step authentication failed for account: {username}, retry with a new pass code: {error}"
super().__init__(404, message)


class SynologyDSMLogin2SAForcedException(SynologyDSMLoginFailedException):
"""2SA force to setup exception."""

def __init__(self, username):
def __init__(self, username: str, error: str) -> None:
"""Constructor method."""
message = (
f"Two-step authentication forced to be setuped for account: {username}"
)
message = f"Two-step authentication forced to be setuped for account: {username}: {error}"
super().__init__(406, message)
18 changes: 11 additions & 7 deletions src/synology_dsm/synology_dsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,20 @@ async def login(self, otp_code: str = None) -> bool:

# Request login
result = await self.get(API_AUTH, "login", params)
error = result.get("error")

# Handle errors
if result.get("error"):
if error:
# Remove this
_LOGGER.error("Attempt to login (%s/%s) failed: %s", self.username, self._password, error)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would obviously be a very bad idea in production 😉


switcher = {
400: SynologyDSMLoginInvalidException(self.username),
401: SynologyDSMLoginDisabledAccountException(self.username),
402: SynologyDSMLoginPermissionDeniedException(self.username),
403: SynologyDSMLogin2SARequiredException(self.username),
404: SynologyDSMLogin2SAFailedException(),
406: SynologyDSMLogin2SAForcedException(self.username),
400: SynologyDSMLoginInvalidException(self.username, error),
401: SynologyDSMLoginDisabledAccountException(self.username, error),
402: SynologyDSMLoginPermissionDeniedException(self.username, error),
403: SynologyDSMLogin2SARequiredException(self.username, error),
404: SynologyDSMLogin2SAFailedException(self.username, error),
406: SynologyDSMLogin2SAForcedException(self.username, error),
}
raise switcher.get(
result["error"]["code"],
Expand Down