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:
andre
2020-07-12 12:22:56 -07:00
parent 84e2a0e23c
commit d37bb160f6
2 changed files with 31 additions and 5 deletions

View File

@@ -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"

View File

@@ -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"