package subsonic import ( "errors" "net/http" "github.com/mithral/vellum/internal/apitoken" "github.com/mithral/vellum/internal/auth" "github.com/mithral/vellum/internal/httpx" ) // handleCredentials returns the username and Subsonic key a user configures a // client with. The key is minted on first request so the user never has to // manage tokens by hand; a later request returns the same key. This endpoint is // cookie-authenticated, not Subsonic-authenticated: the user is in the web UI, // not in the client. func (h *Handlers) handleCredentials(w http.ResponseWriter, r *http.Request, u auth.User) { key, err := apitoken.Plain(r.Context(), h.db, u.ID) if errors.Is(err, apitoken.ErrNotFound) { key, err = apitoken.Mint(r.Context(), h.db, u.ID, apitoken.KindSubsonic, "Subsonic client") } if err != nil { h.log.Error("subsonic: credentials", "err", err) httpx.WriteError(w, http.StatusInternalServerError, "internal server error") return } httpx.WriteJSON(w, http.StatusOK, map[string]any{ "username": u.Username, "key": key, }) }