Skip to content

Token encryption

MuseFlow keeps each user's Spotify access and refresh token in museflow_auth_token, to call Spotify on their behalf. Since #250 the database holds them encrypted: a dump, a backup or a psql session shows bytes, not a token. The repository encrypts them right before the SQL and decrypts them right after: the use cases, the Spotify session and provider info --show-tokens still see the token in clear.

Call between programs Fails, or loses the race

Hash, sign, encrypt

Three different operations, each with its own key in MuseFlow:

What it does Reversible? Used for
Hash A fixed-size fingerprint No: it can only be computed again and compared Passwords (Argon2)
Sign Proves who issued a content and that nobody changed it; the content stays readable Nothing to reverse The API's JWT (MUSEFLOW_JWT_SIGNING_KEY, see API authentication)
Encrypt Makes a content unreadable without the key Yes, with the key The provider tokens (MUSEFLOW_PROVIDER_TOKEN_ENCRYPTION_KEY)

Being deterministic (the same input always gives the same output) is another axis: SHA-256 is, Argon2 is not (random salt); AES-SIV is, AES-GCM and Fernet are not (random nonce). Encryption here must be: see the refresh.

The cipher

  • AES-SIV (RFC 5297, cryptography), behind TokenCipherPort (AesSivTokenCipher). The wiring builds it from MUSEFLOW_PROVIDER_TOKEN_ENCRYPTION_KEY, any secret of 32+ characters that HKDF-SHA256 turns into the 64 bytes AES-256-SIV takes. It is distinct from the JWT signing key: each key has one job, and changing one leaves the other alone. During a rotation, the former keys (MUSEFLOW_PROVIDER_TOKEN_ENCRYPTION_KEY_FALLBACKS) still decrypt; only the current key encrypts.
  • Authenticated, and bound to its row. Each token is encrypted with a context, user_id:provider:column, that the ciphertext authenticates: bytes copied to another user, provider or column, altered, or encrypted with another key fail to decrypt instead of giving a wrong token.
  • Stored as BYTEA, with no length limit: a 320-character Spotify access token takes 336 bytes (16 of them the SIV tag). Spotify sets the length of its tokens and lengthened them before; String(512) would hold the base64 of SIV (448 characters) but not Fernet's (524).

Save: provider connect

Read: provider info --show-tokens, a Spotify session

Tokens are decrypted only when read. The SIV tag authenticates the bytes: a row that fails it is never returned as a wrong token.

Refresh: the compare-and-swap of #243

A refresh saves the new token only if the row still holds the access token the session read (#244): one UPDATE … WHERE token_access = :expected, which PostgreSQL checks again on the latest version of the row once it holds its lock (Unit of work). With a random nonce, the same token encrypts to different bytes each time, and that condition would never hold: every refresh would think another session won. SIV is deterministic: the repository encrypts the expected token with the same context, and PostgreSQL compares bytes. During a rotation the stored token may still be under a former key, so the expected token is encrypted with each key and the condition becomes token_access IN (…): still one statement. The port and the session are the same as before #250.

What determinism reveals: two equal tokens in one context would give equal bytes. Spotify tokens are random and the context differs on every row, so nothing can be learnt from it.

What it protects against, and what it doesn't

  • Protected: whoever reads the database without the key: a dump or a backup that leaks, a shared database, a read-only SQL access. make db-dump writes encrypted tokens.
  • Not protected: whoever reads both the database and the application's environment (the key sits next to the database credentials), or runs code in the application. Keeping the key elsewhere (a secrets manager, a KMS) would be the next step for a hosted service.

Rotating the key

A key is rotated without anyone reconnecting (#286), as Django rotates its SECRET_KEY with SECRET_KEY_FALLBACKS and Python's MultiFernet keeps several keys:

  1. Move the current key to the fallbacks and set a new one (openssl rand -hex 32), then restart the processes:
    MUSEFLOW_PROVIDER_TOKEN_ENCRYPTION_KEY=<new key>
    MUSEFLOW_PROVIDER_TOKEN_ENCRYPTION_KEY_FALLBACKS=["<former key>"]
    
    New writes use the new key; every token still reads, and refreshes under concurrency still compare, whichever key wrote them.
  2. Run museflow provider reencrypt-tokens: it encrypts every stored token again with the new key. A refresh is not enough on its own: Spotify doesn't always issue a new refresh token, and the stored one would stay under the former key.
  3. Drop the fallback and restart.

Known limits

  • The rotation is manual: three steps and two restarts, and nothing checks that the command ran before a fallback is dropped. A token still under a dropped key reads as no token, with a warning, and its user reconnects.
  • A lost key loses the tokens, not the rest of the data: reconnecting is enough.