diff --git a/sublime/adapters/subsonic/adapter.py b/sublime/adapters/subsonic/adapter.py index cdb1126..cfbeacb 100644 --- a/sublime/adapters/subsonic/adapter.py +++ b/sublime/adapters/subsonic/adapter.py @@ -182,7 +182,8 @@ class SubsonicAdapter(Adapter): @staticmethod def migrate_configuration(config_store: ConfigurationStore): - pass + if "salt_auth" not in config_store: + config_store["salt_auth"] = False def __init__(self, config: ConfigurationStore, data_directory: Path): self.data_directory = data_directory @@ -221,7 +222,7 @@ class SubsonicAdapter(Adapter): self.username = config["username"] self.password = cast(str, config.get_secret("password")) self.verify_cert = config["verify_cert"] - self.use_salt_auth = config["salt_auth"] if "salt_auth" in config else False + self.use_salt_auth = config["salt_auth"] self.is_shutting_down = False @@ -367,9 +368,8 @@ class SubsonicAdapter(Adapter): Authentication section of www.subsonic.org/pages/api.jsp for more information """ salt = "".join(random.choices(string.ascii_letters + string.digits, k=8)) - unhashed_token = "{}{}".format(self.password, salt) - hashed_token = hashlib.md5(str.encode(unhashed_token)).hexdigest() - return (salt, hashed_token) + token = hashlib.md5(f"{self.password}{salt}".encode()).hexdigest() + return (salt, token) def _make_url(self, endpoint: str) -> str: return f"{self.hostname}/rest/{endpoint}.view" diff --git a/tests/adapter_tests/subsonic_adapter_tests.py b/tests/adapter_tests/subsonic_adapter_tests.py index df48009..036adc8 100644 --- a/tests/adapter_tests/subsonic_adapter_tests.py +++ b/tests/adapter_tests/subsonic_adapter_tests.py @@ -1,3 +1,4 @@ +import hashlib import json import logging import re @@ -124,10 +125,35 @@ def test_salt_auth_logic(salt_auth_adapter: SubsonicAdapter): params = salt_auth_adapter._get_params() assert "p" not in params assert "s" in params + salt = params["s"] assert "t" in params + assert params["t"] == hashlib.md5(f"testpass{salt}".encode()).hexdigest() assert all(key in params and params[key] == expected[key] for key in expected) +def test_migrate_configuration_populate_salt_auth(): + config = ConfigurationStore( + server_address="https://subsonic.example.com", + username="test", + verify_cert=True, + ) + SubsonicAdapter.migrate_configuration(config) + assert "salt_auth" in config + assert not config["salt_auth"] + + +def test_migrate_configuration_salt_auth_present(): + config = ConfigurationStore( + server_address="https://subsonic.example.com", + username="test", + verify_cert=True, + salt_auth=True, + ) + SubsonicAdapter.migrate_configuration(config) + assert "salt_auth" in config + assert config["salt_auth"] + + def test_make_url(adapter: SubsonicAdapter): assert adapter._make_url("foo") == "https://subsonic.example.com/rest/foo.view"