[PR #318] [MERGED] feat: add Spotify OAuth redirect URI handling and manual paste-back s… #4558

Closed
opened 2026-10-22 04:08:42 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/318
Author: @LightD31
Created: 4/29/2026
Status: ✅ Merged
Merged: 5/3/2026
Merged by: @SpicyMarinara

Base: main ← Head: main


📝 Commits (7)

  • 61082de feat: add Spotify OAuth redirect URI handling and manual paste-back support for LAN installs
  • a0bfb60 fix(spotify): address review feedback on PR #318
  • 9a5cc13 fix(spotify): re-read agent settings before persisting tokens
  • e0051bb fix(spotify): enforce PKCE TTL inside completeExchange
  • 465d3c3 chore(spotify): trim comments to match surrounding style
  • 9026504 fix(spotify): tidy review nits on PR #318
  • 60b5de1 Merge branch 'main' into main

📊 Changes

7 files changed (+392 additions, -150 deletions)

View changed files

📝 .env.example (+10 -0)
📝 docs/CONFIGURATION.md (+1 -0)
📝 docs/FAQ.md (+4 -0)
📝 docs/TROUBLESHOOTING.md (+13 -0)
📝 packages/client/src/components/agents/AgentEditor.tsx (+162 -41)
📝 packages/server/src/config/runtime-config.ts (+48 -1)
📝 packages/server/src/routes/spotify-auth.routes.ts (+154 -108)

📄 Description

feat: add Spotify OAuth redirect URI handling and manual paste-back

Linked issue

Closes #317

Why this change

The Spotify DJ agent's OAuth callback was hardcoded to http://127.0.0.1:<PORT>/api/spotify/callback. When Marinara runs on a different machine than the browser (LAN install, remote server, Tailscale, phone-on-Wi-Fi), Spotify redirects the popup to 127.0.0.1 on the browser's machine — where nothing is listening — and the connect flow silently times out.

A naive "use the LAN IP" fix doesn't work either: Spotify's Feb 2025 policy only accepts https://<any-host> or the loopback literals http://127.0.0.1 / http://[::1] as registered redirect URIs. localhost and LAN IPs are rejected at registration. So the fix has to cover both auto-deriving the right URI when it can legally be derived (HTTPS deployments) and providing a usable manual fallback when it can't (plain-HTTP LAN).

What changed

  • Server: request-aware redirect URI resolution. Replaced the no-arg getSpotifyRedirectUri() in packages/server/src/config/runtime-config.ts with buildSpotifyRedirectUri(req). Honours a SPOTIFY_REDIRECT_URI env override; otherwise picks https://<host>/... for HTTPS requests (including X-Forwarded-Proto: https from a reverse proxy), http://<host>/... only when the host is a loopback literal, and falls back to http://127.0.0.1:<PORT>/... for everything else (the only HTTP form Spotify accepts).
  • Server: manual paste-back endpoint. New POST /api/spotify/exchange in packages/server/src/routes/spotify-auth.routes.ts accepts either a full callback URL or pre-extracted code+state, validates against the existing PKCE pendingAuth map, and runs the same token exchange the redirect callback uses (factored into a shared completeExchange helper to avoid drift). 10-minute pending-auth window unchanged.
  • Client: paste-back UI. Under the Connect button in packages/client/src/components/agents/AgentEditor.tsx, added a collapsed "Browser couldn't reach the callback?" disclosure (only shown while a connection is in flight) with a textarea, error display, and Submit button that POSTs to /api/spotify/exchange. Replaced the hardcoded http://127.0.0.1:7860/... fallback display with ${window.location.origin}/api/spotify/callback. Added a short note explaining Spotify's loopback-or-HTTPS constraint so users understand why the registered URI may not match where they're browsing.
  • Docs. Added SPOTIFY_REDIRECT_URI to .env.example and docs/CONFIGURATION.md, a new "Spotify DJ Login Fails on a Remote or LAN Install" section in docs/TROUBLESHOOTING.md covering all four deployment shapes, and a one-paragraph cross-link from the LAN-access entry in docs/FAQ.md.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

Treat the items below as a to-do list — tick each only after running it yourself.

  • Manually verify localhost regression: pnpm dev, browse http://127.0.0.1:7860, agent editor shows redirect URI http://127.0.0.1:7860/api/spotify/callback, full Connect → popup → token exchange completes via the existing redirect handler.
  • Manually verify HTTPS auto-derivation: run with SSL_CERT/SSL_KEY set, browse https://localhost:7860, agent editor shows https://localhost:7860/api/spotify/callback, register that URI in the Spotify Developer Dashboard, full Connect flow completes.
  • Manually verify reverse-proxy headers: curl -H 'X-Forwarded-Proto: https' -H 'X-Forwarded-Host: marinara.example.com' http://127.0.0.1:7860/api/spotify/status?agentId=spotify returns redirectUri: "https://marinara.example.com/api/spotify/callback".
  • Manually verify explicit override: set SPOTIFY_REDIRECT_URI=https://music.example.com/api/spotify/callback in .env, restart, confirm both /api/spotify/status and the agent editor display that exact string.
  • Manually verify HTTP LAN paste-back (the reported case): HOST=0.0.0.0 pnpm start on machine A; browser on machine B at http://<machine-A-IP>:7860; agent editor displays the loopback URI plus the explanatory note; register the loopback URI in Spotify; click Connect; popup lands on a connection-refused page on machine B; copy the full URL from the popup's address bar; expand "Browser couldn't reach the callback?" and paste; click Complete connection → status flips to connected.
  • Manually verify stale state error: paste a callback URL older than 10 minutes → server returns "Authorization session expired or was already used." surfaced inline in the disclosure.
  • Manually verify dark mode + mobile viewport: the new paste-back disclosure renders correctly and the textarea is usable on a phone-sized viewport.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Version/release files updated (only if this PR includes a version bump)

Updated .env.example, docs/CONFIGURATION.md, docs/TROUBLESHOOTING.md, and docs/FAQ.md. No version bump (bug fix).

UI evidence (if applicable)

Summary by CodeRabbit

  • New Features

    • Manual paste-back fallback for Spotify DJ authentication (paste callback URL or code/state)
    • Extended Spotify connection timeout to 10 minutes
    • Improved error messages and connection-status UI during Spotify authorization
    • Server-side configurable Spotify redirect URI override (SPOTIFY_REDIRECT_URI)
  • Documentation

    • Troubleshooting guide for Spotify OAuth on LAN/remote installs
    • FAQ entry about Spotify login behavior on local networks
    • Guidance for configuring SPOTIFY_REDIRECT_URI and redirect-URI setup instructions

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/318 **Author:** [@LightD31](https://github.com/LightD31) **Created:** 4/29/2026 **Status:** ✅ Merged **Merged:** 5/3/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `main` ← **Head:** `main` --- ### 📝 Commits (7) - [`61082de`](https://github.com/Pasta-Devs/Marinara-Engine/commit/61082deafbac57ef893085e89079c6c588a6d5d7) feat: add Spotify OAuth redirect URI handling and manual paste-back support for LAN installs - [`a0bfb60`](https://github.com/Pasta-Devs/Marinara-Engine/commit/a0bfb60fdeae88ac308b8b21229a2c0d6990a87f) fix(spotify): address review feedback on PR #318 - [`9a5cc13`](https://github.com/Pasta-Devs/Marinara-Engine/commit/9a5cc1330edc0e07f7c0ca98cd1bbb3159d6d986) fix(spotify): re-read agent settings before persisting tokens - [`e0051bb`](https://github.com/Pasta-Devs/Marinara-Engine/commit/e0051bb7afb0128ae61f8b87f219479b1fd9a1b6) fix(spotify): enforce PKCE TTL inside completeExchange - [`465d3c3`](https://github.com/Pasta-Devs/Marinara-Engine/commit/465d3c3c4b2b9065b37556b4dc55e0a87e9e02f4) chore(spotify): trim comments to match surrounding style - [`9026504`](https://github.com/Pasta-Devs/Marinara-Engine/commit/90265045a942331374ef2bea67b177b16ee74bc8) fix(spotify): tidy review nits on PR #318 - [`60b5de1`](https://github.com/Pasta-Devs/Marinara-Engine/commit/60b5de1c181d1b2370d261175bdaf2da55887be5) Merge branch 'main' into main ### 📊 Changes **7 files changed** (+392 additions, -150 deletions) <details> <summary>View changed files</summary> 📝 `.env.example` (+10 -0) 📝 `docs/CONFIGURATION.md` (+1 -0) 📝 `docs/FAQ.md` (+4 -0) 📝 `docs/TROUBLESHOOTING.md` (+13 -0) 📝 `packages/client/src/components/agents/AgentEditor.tsx` (+162 -41) 📝 `packages/server/src/config/runtime-config.ts` (+48 -1) 📝 `packages/server/src/routes/spotify-auth.routes.ts` (+154 -108) </details> ### 📄 Description feat: add Spotify OAuth redirect URI handling and manual paste-back ## Linked issue Closes #317 ## Why this change The Spotify DJ agent's OAuth callback was hardcoded to `http://127.0.0.1:<PORT>/api/spotify/callback`. When Marinara runs on a different machine than the browser (LAN install, remote server, Tailscale, phone-on-Wi-Fi), Spotify redirects the popup to `127.0.0.1` on the *browser's* machine — where nothing is listening — and the connect flow silently times out. A naive "use the LAN IP" fix doesn't work either: [Spotify's Feb 2025 policy](https://developer.spotify.com/blog/2025-02-12-increasing-the-security-requirements-for-integrating-with-spotify) only accepts `https://<any-host>` or the loopback literals `http://127.0.0.1` / `http://[::1]` as registered redirect URIs. `localhost` and LAN IPs are rejected at registration. So the fix has to cover both auto-deriving the right URI when it *can* legally be derived (HTTPS deployments) and providing a usable manual fallback when it can't (plain-HTTP LAN). ## What changed - **Server: request-aware redirect URI resolution.** Replaced the no-arg `getSpotifyRedirectUri()` in [`packages/server/src/config/runtime-config.ts`](packages/server/src/config/runtime-config.ts) with `buildSpotifyRedirectUri(req)`. Honours a `SPOTIFY_REDIRECT_URI` env override; otherwise picks `https://<host>/...` for HTTPS requests (including `X-Forwarded-Proto: https` from a reverse proxy), `http://<host>/...` only when the host is a loopback literal, and falls back to `http://127.0.0.1:<PORT>/...` for everything else (the only HTTP form Spotify accepts). - **Server: manual paste-back endpoint.** New `POST /api/spotify/exchange` in [`packages/server/src/routes/spotify-auth.routes.ts`](packages/server/src/routes/spotify-auth.routes.ts) accepts either a full callback URL or pre-extracted `code`+`state`, validates against the existing PKCE `pendingAuth` map, and runs the same token exchange the redirect callback uses (factored into a shared `completeExchange` helper to avoid drift). 10-minute pending-auth window unchanged. - **Client: paste-back UI.** Under the Connect button in [`packages/client/src/components/agents/AgentEditor.tsx`](packages/client/src/components/agents/AgentEditor.tsx), added a collapsed "Browser couldn't reach the callback?" disclosure (only shown while a connection is in flight) with a textarea, error display, and Submit button that POSTs to `/api/spotify/exchange`. Replaced the hardcoded `http://127.0.0.1:7860/...` fallback display with `${window.location.origin}/api/spotify/callback`. Added a short note explaining Spotify's loopback-or-HTTPS constraint so users understand why the registered URI may not match where they're browsing. - **Docs.** Added `SPOTIFY_REDIRECT_URI` to [`.env.example`](.env.example) and [`docs/CONFIGURATION.md`](docs/CONFIGURATION.md), a new "Spotify DJ Login Fails on a Remote or LAN Install" section in [`docs/TROUBLESHOOTING.md`](docs/TROUBLESHOOTING.md) covering all four deployment shapes, and a one-paragraph cross-link from the LAN-access entry in [`docs/FAQ.md`](docs/FAQ.md). ## Validation - [x] `pnpm check` passes locally - [x] Container (Docker / Podman) built and ran without issue - [x] Ran the app, clicked through the changes manually - [ ] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [ ] Above manual verification completed (describe below) - [x] Read and followed `CONTRIBUTING.md` ### Manual verification notes Treat the items below as a to-do list — tick each only after running it yourself. - Manually verify **localhost regression**: `pnpm dev`, browse `http://127.0.0.1:7860`, agent editor shows redirect URI `http://127.0.0.1:7860/api/spotify/callback`, full Connect → popup → token exchange completes via the existing redirect handler. - Manually verify **HTTPS auto-derivation**: run with `SSL_CERT`/`SSL_KEY` set, browse `https://localhost:7860`, agent editor shows `https://localhost:7860/api/spotify/callback`, register that URI in the Spotify Developer Dashboard, full Connect flow completes. - Manually verify **reverse-proxy headers**: `curl -H 'X-Forwarded-Proto: https' -H 'X-Forwarded-Host: marinara.example.com' http://127.0.0.1:7860/api/spotify/status?agentId=spotify` returns `redirectUri: "https://marinara.example.com/api/spotify/callback"`. - Manually verify **explicit override**: set `SPOTIFY_REDIRECT_URI=https://music.example.com/api/spotify/callback` in `.env`, restart, confirm both `/api/spotify/status` and the agent editor display that exact string. - Manually verify **HTTP LAN paste-back (the reported case)**: `HOST=0.0.0.0 pnpm start` on machine A; browser on machine B at `http://<machine-A-IP>:7860`; agent editor displays the loopback URI plus the explanatory note; register the loopback URI in Spotify; click Connect; popup lands on a connection-refused page on machine B; copy the full URL from the popup's address bar; expand "Browser couldn't reach the callback?" and paste; click Complete connection → status flips to connected. - Manually verify **stale state error**: paste a callback URL older than 10 minutes → server returns "Authorization session expired or was already used." surfaced inline in the disclosure. - Manually verify **dark mode + mobile viewport**: the new paste-back disclosure renders correctly and the textarea is usable on a phone-sized viewport. ## Docs and release impact - [ ] No docs changes needed - [x] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Version/release files updated (only if this PR includes a version bump) Updated `.env.example`, `docs/CONFIGURATION.md`, `docs/TROUBLESHOOTING.md`, and `docs/FAQ.md`. No version bump (bug fix). ## UI evidence (if applicable) <!-- Add before/after screenshots of the agent editor: 1. The "Connect Spotify Account" button + new explanatory note. 2. The "Browser couldn't reach the callback?" disclosure expanded with the textarea. --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Manual paste-back fallback for Spotify DJ authentication (paste callback URL or code/state) * Extended Spotify connection timeout to 10 minutes * Improved error messages and connection-status UI during Spotify authorization * Server-side configurable Spotify redirect URI override (SPOTIFY_REDIRECT_URI) * **Documentation** * Troubleshooting guide for Spotify OAuth on LAN/remote installs * FAQ entry about Spotify login behavior on local networks * Guidance for configuring SPOTIFY_REDIRECT_URI and redirect-URI setup instructions <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:08:42 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#4558
No description provided.