OAuth flows¶
MuseFlow links a user to their Spotify account through OAuth's authorisation code flow, from the CLI or from an API client (a web page or a mobile app). In every flow, Spotify never calls MuseFlow: it sends the browser back to the client, which hands the code and the state over. The flow ends only for the user who started it (#101): the state is stored with that user, and the code is accepted only from them.
CLI¶
museflow provider connect sends the browser back to a loopback address where nothing listens: the page doesn't load,
and the user pastes its URL into the terminal. Neither the API nor a local server sees the code. Usage:
provider connect.
- The code never leaves the machine. Spotify redirects to
127.0.0.1, the browser itself. Nothing listens on the port: no service, not even the API, receives the code. - The state belongs to the command. It must be the one this command created for this user: a URL pasted from someone else's flow holds their state, which is refused.
- Spotify checks the exact URI. The redirect URI is registered in the Spotify dashboard, port included, and sent twice (steps 3 and 12): Spotify refuses the exchange if they differ.
Web client¶
The client is a page served by https://app.example.com, logged in to the API with a Bearer token (a JWT). Its
redirect URI is listed in SPOTIFY_REDIRECT_URIS and registered in the Spotify dashboard.
The API stores no Bearer token: the client keeps its own JWT and sends it with each request, and the API reads the user
from its signed sub (see API authentication). The redirect to Spotify reloads the page, so a web client keeps the JWT in a storage that
survives it (sessionStorage, for instance) to send it again to connect/complete.
Mobile client¶
The same exchange with the API. The app opens the consent page in a system browser (a Custom Tab on Android,
ASWebAuthenticationSession on iOS), and Spotify redirects to the app's own scheme, museflow://, which the system
hands to the app.
Another app could register museflow:// too and catch the redirect. It would get a code and a state issued to the
user: without that user's Bearer token the API refuses them, and without the client secret, which only the API holds,
Spotify refuses the exchange.
The attack the flow prevents (#101)¶
Mallory wants to drive Alice's Spotify from his own MuseFlow account. He starts a flow with his Bearer token, then gets Alice to open the authorisation link. Alice agrees with her Spotify account.
Before: the API's GET /callback, unauthenticated, trusted the state alone.
Now: the flow ends at POST /connect/complete, with the Bearer token.
A state only proves that someone started a flow, not that the person ending it is the same. The Bearer token makes that link, and it works from a web page as from an app, unlike a cookie.