36 lines
1.1 KiB
Python
36 lines
1.1 KiB
Python
from pathlib import Path
|
|
|
|
from music_server.services.token_service import TokenService
|
|
|
|
_TOKEN_CACHE: dict[tuple[str, str], str] = {}
|
|
|
|
|
|
def issue_access_token(player_db_path: str | Path, *, label: str = "test-token") -> str:
|
|
cache_key = (str(player_db_path), label)
|
|
cached = _TOKEN_CACHE.get(cache_key)
|
|
if cached is not None:
|
|
return cached
|
|
|
|
service = TokenService(str(player_db_path))
|
|
issued = service.issue_token(label=label).plaintext_token
|
|
_TOKEN_CACHE[cache_key] = issued
|
|
return issued
|
|
|
|
|
|
def auth_headers(
|
|
player_db_path: str | Path,
|
|
*,
|
|
client_id: str = "test-client",
|
|
client_label: str = "Test Client",
|
|
token: str | None = None,
|
|
include_client_id: bool = True,
|
|
include_client_label: bool = True,
|
|
) -> dict[str, str]:
|
|
issued_token = token or issue_access_token(player_db_path)
|
|
headers = {"Authorization": f"Bearer {issued_token}"}
|
|
if include_client_id:
|
|
headers["X-Music-Client-Id"] = client_id
|
|
if include_client_label:
|
|
headers["X-Music-Client-Label"] = client_label
|
|
return headers
|