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

+2 -1
View File
@@ -43,8 +43,9 @@ def init_app(app: Flask) -> None:
},
)
from scrobbler.api import apps, auth, sessions
from scrobbler.api import apps, auth, sessions, tokens
api.register_blueprint(auth.blp, url_prefix="/api/v1/auth")
api.register_blueprint(apps.blp, url_prefix="/api/v1/apps")
api.register_blueprint(sessions.blp, url_prefix="/api/v1/sessions")
api.register_blueprint(tokens.blp, url_prefix="/api/v1/tokens")
+50
View File
@@ -0,0 +1,50 @@
from flask import g
from flask_smorest import Blueprint, abort
from scrobbler.api.decorators import authenticated
from scrobbler.schemas import ApproveTokenSchema, AuthTokenInfoSchema, ErrorSchema
from scrobbler.services import accounts
blp = Blueprint(
"ui_tokens",
__name__,
description=(
"Approve Last.fm desktop-auth tokens. Clients send the user to `/api/auth/`, "
"which redirects to the UI's approve page, which calls these endpoints."
),
)
def _token_or_404(token: str):
try:
return accounts.get_auth_token(token)
except accounts.AuthTokenError as err:
abort(404, code=f"token_{err.reason}", message="Unknown or expired token")
@blp.route("/<string:token>", methods=["GET"])
@authenticated(blp)
@blp.response(200, AuthTokenInfoSchema)
@blp.alt_response(404, schema=ErrorSchema, description="Unknown or expired token")
def token_info(token):
"""Describe a token awaiting approval
Returns the name of the application asking for access.
"""
auth_token = _token_or_404(token)
return {"api_app": auth_token.api_app, "approved": auth_token.user_id is not None}
@blp.route("/approve", methods=["POST"])
@authenticated(blp)
@blp.arguments(ApproveTokenSchema)
@blp.response(204)
@blp.alt_response(404, schema=ErrorSchema, description="Unknown or expired token")
def approve(body):
"""Approve a token
Grants the requesting application a session for your account once it calls
auth.getSession.
"""
_token_or_404(body["token"])
accounts.approve_auth_token(g.user, body["token"])