Authentication and credential management
Spotifast turns a browser approval into the account and access needed by its client. It keeps Web API access separate from local playback access, stores reusable grants in protected system storage, and restores account data only after it confirms which Spotify account is active. This page complements Spotify service integration and Spotify API client and gateway.
Authentication model
Web API access
Web API access lets the client read account data and perform user actions. The client supports two grant sources: a legacy shared grant that can only be restored, and a personal grant made with the listener's Spotify app. The personal app is identified by a Spotify Client ID saved in settings.
The two grants serve different roles. The gateway can use the personal grant for the listener's account and use shared access for work that must remain available to other accounts. A personal grant cannot be widened by a refresh; new permissions require another browser approval.
References: src/auth.rs• L43-65 src/auth.rs• L77-108 src/…/gateway.rs• L121-143
Playback access
Playback access is a separate approval for the local Spotify Connect device. It uses Spotify's streaming identity and produces a reusable playback credential for the audio session. This grant is not used for Web API requests because Spotify limits that identity's Web API traffic.
Local playback is optional. The rest of the client can use Web API access when playback is unavailable, while a valid playback grant can restore the local player without opening the browser again.
References: src/auth.rs• L1-17 src/auth.rs• L33-45 src/backend.rs• L1163-1179 src/backend.rs• L3834-3870
Session identity
The client treats a session as ready only after Spotify verifies a grant and returns the account identity. That identity becomes the key for account data, API routing, and playback-account checks. The client does not treat a token alone as proof that a usable session exists.
When a Web API grant is ready, the gateway records SessionState::Ready with an AccountId. When playback connects, the engine checks that its account matches the verified account before it becomes the local player.
References: src/…/gateway.rs• L40-52 src/backend.rs• L3252-3306 src/backend.rs• L4088-4108
Login and authorization flows
Browser sign-in
The browser handles the Spotify password and consent screens. Spotifast creates a short-lived PKCE flow, reserves a loopback callback, opens the browser, and accepts only the callback that has the expected path and state.
The flow follows this sequence:
sequenceDiagram
participant Client as Spotifast
participant Browser
participant Spotify
Client->>Client: Create PKCE verifier and state
Client->>Spotify: Open authorization URL
Browser->>Spotify: User signs in and approves
Spotify-->>Client: Loopback callback with code and state
Client->>Spotify: Exchange code and verifier
Spotify-->>Client: Access and refresh grant
Client->>Spotify: Verify the account
Spotify-->>Client: Account profile
The Web API flow uses http://127.0.0.1:8989/login. Local playback uses a separate callback port. A port conflict, timeout, cancellation, bad state, or refused consent ends the flow without accepting a grant.
References: src/auth.rs• L158-192 src/auth.rs• L205-297 src/backend.rs• L3328-3465
Grant verification
After a code exchange, the client verifies the returned access token by reading the account profile. Only then does it mark the source ready and begin durable persistence. This prevents an incomplete or wrong-account grant from becoming the active client session.
If one Web API source fails, the other can keep the account signed in. If no source remains ready, the client moves to a signed-out or refused state and explains whether the Client ID, account permission, or saved grant needs attention.
References: src/backend.rs• L3135-3192 src/backend.rs• L3205-3250 src/backend.rs• L3252-3306
Playback authorization
Playback authorization starts only when the account is eligible for local playback and no matching saved playback grant can connect. The client first tries the saved grant for the active account. If Spotify rejects it, the client opens the separate browser flow and replaces the saved playback credential after a successful engine connection.
The resulting state moves from unavailable or authorizing to connecting and then ready. A network failure can trigger a later retry, but a rejected or wrong-account credential requires a fresh authorization.
References: src/backend.rs• L1163-1179 src/backend.rs• L3834-3947 src/backend.rs• L4051-4179
Credential persistence
Protected storage
Durable grants are stored as typed records in the platform credential store. Ordinary settings and state files keep configuration and non-secret markers, not usable Spotify tokens. Each grant has a separate slot so Web API, playback, proxy, and other protected credentials can be replaced or revoked independently.
The store performs reads and writes away from the UI and command loop. It verifies a saved record before restoring it and reports storage problems without exposing provider error text that could contain secret data.
References: src/credentials.rs• L1-5 src/credentials.rs• L25-64 src/credentials.rs• L617-667 src/credentials.rs• L965-1053
Token renewal
Web API access tokens are short-lived. WebTokens refreshes them before expiry and keeps the refresh token when Spotify omits a replacement. A refreshed token is written back only when the grant still belongs to the current authorization generation.
The client can keep using the current token when the refresh endpoint is temporarily unreachable and the token is still usable. A rejected refresh grant is different: it means the saved authorization is no longer valid and the browser flow must run again.
References: src/auth.rs• L390-435 src/…/client.rs• L109-215
Revocation and stale work
Sign-out invalidates in-flight work before it removes grants. A generation check then rejects late reads or writes from the old session, including a write that finishes after sign-out. A local revocation marker also prevents a failed credential deletion from restoring the old session on the next launch.
Changing the personal Client ID follows the same boundary. It revokes the old personal grant, clears its active API source, and starts authorization for the new app when one is configured.
References: src/credentials.rs• L598-614 src/credentials.rs• L721-759 src/credentials.rs• L907-1072 src/backend.rs• L3468-3507
Account cache handling
Account-scoped snapshots
Account caches provide fast startup data while Spotify is queried again. A snapshot contains metadata such as the account ID, playlist list, or Home shelves. It is accepted only when its format and account ID match the verified session.
The cache is not a credential and cannot establish a session. Missing, corrupt, old, or other-account snapshots are ignored, so the client falls back to a live read.
References: src/account_cache.rs• L1-43 src/paths.rs• L224-256
Cache restore and refresh
After a successful sign-in, the client resets account data, loads the matching library and Home snapshots, and starts live reads. Cached values fill only fields that Spotify has not answered yet. A live answer then replaces the cached value.
The client writes library data only after the full list is settled. It saves Home shelves as answers arrive, which preserves useful results when another shelf fails or the shared source is unavailable.
References: src/app.rs• L3397-3415 src/app.rs• L6132-6193 src/app.rs• L6231-6370 src/backend.rs• L2837-2879
Sign-out cleanup
Sign-out clears the active account state and removes account snapshots and playlist caches. The cleanup is ordered with pending cache writes so data from the old account cannot be written after a later sign-in.
This keeps the next session independent from the previous account. It also ensures that a cached library cannot make the interface appear signed in after the credential has been revoked.
References: src/backend.rs• L2416-2434 src/backend.rs• L3510-3550 src/paths.rs• L252-256
Client session state
Authentication states
The UI receives a small set of states that describe the user-visible session: starting, signed out, waiting for the browser, connecting, connected, failed, or refused. These states describe progress and recovery action; they do not expose tokens.
The connected state starts account loading and playback coordination. The signed-out state clears the visible account, local playback, device history, rootlist, and account-scoped data. Refused states explain a known cause such as an invalid Client ID, an account that cannot use the app, or an expired grant.
References: src/backend.rs• L98-130 src/backend.rs• L1024-1025 src/app.rs• L3397-3462 src/…/login.rs• L165-187
API source selection
The API gateway hides the choice between shared and personal Web API access. It waits for a ready source, routes account and catalog work according to the available grants, and falls back to the personal source when shared access is no longer available.
This choice affects quota and feature coverage, not the account identity shown to the user. See Spotify API client and gateway for the request-level view.
References: src/…/gateway.rs• L103-143 src/…/gateway.rs• L321-407 src/backend.rs• L4951-4977
Account-bound client data
The verified account controls which cached data and asynchronous results the client may apply. Requests carry a session or load generation, and responses for a changed account are discarded. This keeps late responses from the prior account out of the new session.
The same rule protects personalized Home results when a grant is renewed. The client retries work under the current grant instead of accepting a result tied to an earlier authorization.
References: src/backend.rs• L2497-2527 src/backend.rs• L5005-5027 src/app.rs• L6162-6173
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the Web API and playback grants separate. A playback token must not become a Web API bearer token.
- Keep grant verification before durable persistence and before exposing the account as connected.
- Preserve credential generations across sign-out, Client ID changes, cancellation, and replacement writes.
- Keep account IDs on cache records and reject cache or async results for a different account.
- Keep sign-out cleanup ordered behind pending cache writes and credential deletion markers.
- Keep API source routing aligned with the grant that owns each quota and permission set.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Redirect port is already in use | The browser result could reach another process or the flow cannot start | Reserve the callback listener before opening the browser; report the port conflict |
| Callback state or path is wrong | The authorization code is not accepted | Match the loopback path and expected PKCE state |
| Refresh grant is rejected | The Web API session expires and cached credentials must not restore it | Revoke that source and require browser authorization |
| Credential-store write is locked, unavailable, or not retained | The current run may work but the next run cannot restore the sign-in | Report a storage warning and keep the grant out of ordinary state files |
| A late write or response belongs to a signed-out account | Old credentials or account data can return to the active session | Invalidate the slot and compare authorization or load generations |
| Playback grant belongs to another Spotify account | Local playback could use the wrong account | Compare the playback account with the verified Web API account before accepting the engine |
| Cache file is corrupt, old, or for another account | Startup could show incorrect account data | Check cache version and account ID, then fall back to live reads |
Key files
| File | Why to read it |
|---|---|
| src/auth.rs | Defines the PKCE browser flows, callback checks, token exchange, expiry, and scope rules. |
| src/credentials.rs | Defines protected grant storage, revocation markers, profile separation, and stale-work protection. |
| src/backend.rs | Coordinates restore, sign-in, verification, playback authorization, sign-out, and cache cleanup. |
| src/…/client.rs | Shows how a verified Web API grant refreshes and supplies bearer tokens. |
| src/…/gateway.rs | Shows how shared and personal sources become one account-facing API session. |
| src/account_cache.rs | Defines account-bound snapshot validation and atomic cache writes. |
| src/app.rs | Applies authentication states and restores or stores account-scoped library and Home data. |
| src/paths.rs | Defines the boundary between protected credentials, durable state, and account caches. |
| src/…/login.rs | Explains the user-facing browser sign-in and Client ID setup states. |
| src/…/settings.rs | Shows how users manage the personal app and separate local playback approval. |