Repository: jugmac00/hibpcli

Issue:
catch exception when keepass password is wrong
currently users get to see:
> pykeepass.exceptions.CredentialsIntegrityError: Credentials are wrong or integrity check failed

Comments:

Patch:
diff --git a/CHANGES.rst b/CHANGES.rst
index b1f39f3..2025e39 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -10,6 +10,7 @@ added
 - add support for Python 3.9
 - add type annotations
 - improve message when there are no known leaks for a password(@eumiro)
+- provide user friendly error message when given password is wrong
 
 0.4.1 (30.09.2020)
 ------------------
diff --git a/setup.py b/setup.py
index 1f01e4d..517a56f 100644
--- a/setup.py
+++ b/setup.py
@@ -53,7 +53,7 @@ setup(
     zip_safe=True,
     install_requires=[
         "click>=7.1.2",
-        "pykeepass>=3.2.0",
+        "pykeepass>=3.2.1",
         "httpx>=0.13.3",
     ],
     entry_points={"console_scripts": ["hibpcli = hibpcli.cli:main"]},
diff --git a/src/hibpcli/cli.py b/src/hibpcli/cli.py
index ac754e7..4c52ff7 100644
--- a/src/hibpcli/cli.py
+++ b/src/hibpcli/cli.py
@@ -1,7 +1,7 @@
 from typing import Optional
 
 import click
-from hibpcli.exceptions import ApiError
+from hibpcli.exceptions import ApiError, KeepassError
 from hibpcli.keepass import check_passwords_from_db
 from hibpcli.password import Password
 
@@ -23,6 +23,8 @@ def check_keepass(path: str, password: Optional[str]) -> None:
         )
     try:
         rv = check_passwords_from_db(path=path, master_password=password)
+    except KeepassError:
+        click.echo("The entered password is not correct. Please try again.")
     except ApiError as e:
         click.echo(str(e))
     else:
diff --git a/src/hibpcli/exceptions.py b/src/hibpcli/exceptions.py
index dc11309..1f17636 100644
--- a/src/hibpcli/exceptions.py
+++ b/src/hibpcli/exceptions.py
@@ -4,3 +4,7 @@ class HibpError(Exception):
 
 class ApiError(HibpError):
     pass
+
+
+class KeepassError(HibpError):
+    pass
diff --git a/src/hibpcli/keepass.py b/src/hibpcli/keepass.py
index 723237d..6835589 100644
--- a/src/hibpcli/keepass.py
+++ b/src/hibpcli/keepass.py
@@ -1,12 +1,20 @@
 from typing import List
 
+from hibpcli.exceptions import KeepassError
 from hibpcli.password import Password
 from pykeepass import PyKeePass  # type: ignore
+from pykeepass.exceptions import CredentialsError
 
 
 def check_passwords_from_db(path: str, master_password: str) -> List[str]:
     """ - """
-    kp = PyKeePass(path, password=master_password)
-    return [
-        entry for entry in kp.entries if Password(password=entry.password).is_leaked()
-    ]
+    try:
+        kp = PyKeePass(path, password=master_password)
+    except CredentialsError:
+        raise KeepassError
+    else:
+        return [
+            entry
+            for entry in kp.entries
+            if Password(password=entry.password).is_leaked()
+        ]


Test patch:
diff --git a/tests/test_cli_for_check_keepass.py b/tests/test_cli_for_check_keepass.py
index c698180..791a47a 100644
--- a/tests/test_cli_for_check_keepass.py
+++ b/tests/test_cli_for_check_keepass.py
@@ -80,3 +80,11 @@ def test_keepass_subcommand_error_handling(mock_password):
     )
     expected_output = "Error\n"
     assert result.output == expected_output
+
+
+def test_keepass_wrong_password():
+    runner = CliRunner()
+    result = runner.invoke(
+        main, ["check-keepass", "tests/test.kdbx", "--password", "wrong-password"]
+    )
+    assert result.output == "The entered password is not correct. Please try again.\n"