Last.fm API core: signatures, XML/JSON responses and dispatcher
- /2.0/ dispatcher backed by a method registry that declares each method's params and signature/session requirements - api_sig verification per the Last.fm auth spec - XML (default) and Last.fm-style JSON rendering, including error shapes and HTTP statuses per error code - Last.fm request, error and auth-failure metrics with bounded labels Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ae8d3b65db
commit
9af8a4be8b
11 files changed
+585
No files matched your search
@@ -0,0 +1,19 @@
|
||||
"""Last.fm request signatures: https://www.last.fm/api/authspec#_8-signing-calls"""
|
||||
|
||||
import hashlib
|
||||
import hmac
|
||||
from collections.abc import Mapping
|
||||
|
||||
UNSIGNED_PARAMS = frozenset({"format", "callback", "api_sig"})
|
||||
|
||||
|
||||
def sign(params: Mapping[str, str], secret: str) -> str:
|
||||
payload = "".join(
|
||||
f"{name}{params[name]}" for name in sorted(params) if name not in UNSIGNED_PARAMS
|
||||
)
|
||||
return hashlib.md5((payload + secret).encode("utf-8")).hexdigest()
|
||||
|
||||
|
||||
def is_valid(params: Mapping[str, str], secret: str) -> bool:
|
||||
supplied = params.get("api_sig", "")
|
||||
return bool(supplied) and hmac.compare_digest(sign(params, secret), supplied.lower())
|
||||
Reference in new issue
Block a user