Last.fm auth: mobile sessions and the desktop token flow
CI / lint (push) Successful in 9s
CI / test (push) Successful in 25s
CI / notify (push) Successful in 5s

- auth.getMobileSession (username + password), auth.getToken and
  auth.getSession with Last.fm error codes 4/14/15
- /api/auth/ redirects clients' approval links to the web UI's
  approve page; /api/v1/tokens lets the UI show and approve a token
- Tokens expire after an hour, are single use and bound to their app
- The legacy md5 authToken form is deliberately unsupported (it needs
  unsalted password hashes)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mattandClaude Opus 5.5 committed 2026-09-25 14:01:12 +01:00
1 parent e026e56ee3
commit 7622f7433a
8 files changed
+292 -3

No files matched your search

+9 -1
View File
@@ -2,7 +2,7 @@
import time
from flask import Blueprint, Response, request
from flask import Blueprint, Response, current_app, redirect, request
from scrobbler import metrics
from scrobbler.extensions import db
@@ -92,4 +92,12 @@ def dispatch() -> Response:
return response
@bp.route("/api/auth/", methods=["GET"], strict_slashes=False)
def approve_redirect() -> Response:
"""Where Last.fm clients send users to approve a token. Approval happens in the
separately hosted web UI, so hand the query string over to its approve page."""
query = request.query_string.decode()
return redirect(f"{current_app.config['UI_BASE_URL']}/approve.html?{query}", code=302)
from scrobbler.lastfm import methods # noqa: E402,F401 (register handlers)
+2
View File
@@ -1 +1,3 @@
"""Importing this package registers every Last.fm method handler."""
from scrobbler.lastfm.methods import auth # noqa: F401
+76
View File
@@ -0,0 +1,76 @@
from scrobbler.lastfm import errors
from scrobbler.lastfm.errors import LastFMError
from scrobbler.lastfm.registry import Call, Param, lastfm_method
from scrobbler.lastfm.responses import El
from scrobbler.services import accounts
def _session_el(session) -> El:
return El.of(
"session",
El.text_el("name", session.user.username),
El.text_el("key", session.session_key),
El.text_el("subscriber", 0),
)
@lastfm_method(
"auth.getMobileSession",
summary="Create a session key from a username and password",
signed=True,
params=(
Param("username", "Username", required=True, example="alice"),
Param("password", "Password (send over HTTPS, as a POST body)", required=True),
),
notes=(
"The legacy `authToken` (md5 of username + md5(password)) form is not supported, "
"because it requires storing unsalted password hashes. Clients that only support "
"it (such as pylast's `password_hash`) should use the desktop flow instead."
),
example_xml=(
'<lfm status="ok"><session><name>alice</name><key>d580d57f32848f5dcf574d1ce18d78b2</key>'
"<subscriber>0</subscriber></session></lfm>"
),
)
def get_mobile_session(call: Call) -> El:
user = accounts.authenticate(call.params["username"], call.params["password"])
if user is None:
raise LastFMError(errors.AUTHENTICATION_FAILED, "Invalid username or password")
return _session_el(accounts.create_session(user, call.api_app, flow="mobile"))
@lastfm_method(
"auth.getToken",
summary="Fetch an unapproved request token (desktop auth flow, step 1)",
signed=True,
notes=(
"Send the user to `/api/auth/?api_key=…&token=…` to approve the token, then call "
"auth.getSession. Tokens expire after 60 minutes."
),
example_xml='<lfm status="ok"><token>cf45fe5a3e3cebe168480a086d7fe481</token></lfm>',
)
def get_token(call: Call) -> El:
return El.text_el("token", accounts.create_auth_token(call.api_app))
@lastfm_method(
"auth.getSession",
summary="Exchange an approved token for a session key (desktop auth flow, step 3)",
signed=True,
params=(Param("token", "Token from auth.getToken, approved by the user", required=True),),
example_xml=(
'<lfm status="ok"><session><name>alice</name><key>d580d57f32848f5dcf574d1ce18d78b2</key>'
"<subscriber>0</subscriber></session></lfm>"
),
)
def get_session(call: Call) -> El:
try:
session = accounts.exchange_auth_token(call.api_app, call.params["token"])
except accounts.AuthTokenError as err:
code = {
"expired": errors.TOKEN_EXPIRED,
"unauthorized": errors.UNAUTHORIZED_TOKEN,
}.get(err.reason, errors.AUTHENTICATION_FAILED)
message = "Invalid authentication token supplied" if err.reason == "invalid" else None
raise LastFMError(code, message) from err
return _session_el(session)