Address merge request comments
1. change string.format to string interpolation when creating the auth token 2. implemented migrate_configuration logic for salt_auth config value 3. added more explicit check of the token in the unit tests
This commit is contained in:
@@ -182,7 +182,8 @@ class SubsonicAdapter(Adapter):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def migrate_configuration(config_store: ConfigurationStore):
|
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):
|
def __init__(self, config: ConfigurationStore, data_directory: Path):
|
||||||
self.data_directory = data_directory
|
self.data_directory = data_directory
|
||||||
@@ -221,7 +222,7 @@ class SubsonicAdapter(Adapter):
|
|||||||
self.username = config["username"]
|
self.username = config["username"]
|
||||||
self.password = cast(str, config.get_secret("password"))
|
self.password = cast(str, config.get_secret("password"))
|
||||||
self.verify_cert = config["verify_cert"]
|
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
|
self.is_shutting_down = False
|
||||||
|
|
||||||
@@ -367,9 +368,8 @@ class SubsonicAdapter(Adapter):
|
|||||||
Authentication section of www.subsonic.org/pages/api.jsp for more information
|
Authentication section of www.subsonic.org/pages/api.jsp for more information
|
||||||
"""
|
"""
|
||||||
salt = "".join(random.choices(string.ascii_letters + string.digits, k=8))
|
salt = "".join(random.choices(string.ascii_letters + string.digits, k=8))
|
||||||
unhashed_token = "{}{}".format(self.password, salt)
|
token = hashlib.md5(f"{self.password}{salt}".encode()).hexdigest()
|
||||||
hashed_token = hashlib.md5(str.encode(unhashed_token)).hexdigest()
|
return (salt, token)
|
||||||
return (salt, hashed_token)
|
|
||||||
|
|
||||||
def _make_url(self, endpoint: str) -> str:
|
def _make_url(self, endpoint: str) -> str:
|
||||||
return f"{self.hostname}/rest/{endpoint}.view"
|
return f"{self.hostname}/rest/{endpoint}.view"
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
import hashlib
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
@@ -124,10 +125,35 @@ def test_salt_auth_logic(salt_auth_adapter: SubsonicAdapter):
|
|||||||
params = salt_auth_adapter._get_params()
|
params = salt_auth_adapter._get_params()
|
||||||
assert "p" not in params
|
assert "p" not in params
|
||||||
assert "s" in params
|
assert "s" in params
|
||||||
|
salt = params["s"]
|
||||||
assert "t" in params
|
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)
|
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):
|
def test_make_url(adapter: SubsonicAdapter):
|
||||||
assert adapter._make_url("foo") == "https://subsonic.example.com/rest/foo.view"
|
assert adapter._make_url("foo") == "https://subsonic.example.com/rest/foo.view"
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user