I want my listening history and my playlists from Spotify in Sonda, so that Mixes, Discover and the smart playlists know what I listened to before. - POST /me/import/spotify takes the zip of a Spotify data export, or the JSON files in it (extended streaming history, the older StreamingHistory files, the playlists), as a multipart form. A request may be up to 512 MB; the zip is limited in how much it may inflate and how many files it may hold, and only the history and playlist files are opened. - Everything is read on the server and nothing is sent anywhere. The files are not kept, and of each play only the song, artist, album, time and how it ended are read: never the IP addresses, the country or the platform. - A play is matched to a song of the library by artist and title (no case, accents or punctuation; a "Remastered" note on either side; any credited artist; a compilation by its album). A match is recorded as a listen at the time of the play: complete, skip or play. Nothing is posted to Mastodon. - Importing the same files again records nothing twice, and the same play in the two shapes of history file is counted once. One import per person at a time. - A playlist becomes a playlist of mine with the songs that were found; one with a name I already have is left alone, and one with none of its songs found is not made. - The report counts what was added, what was already there and what is not in the library, and lists the missing artists, most played first. - Settings gets an Import from Spotify section; the API client can send files. - English and Italian.
166 lines
5.6 KiB
Go
166 lines
5.6 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"go.uber.org/zap"
|
|
|
|
"codeberg.org/squidd/sonda/internal/auth"
|
|
)
|
|
|
|
const (
|
|
ctxUserID = "user_id"
|
|
ctxEmail = "email"
|
|
ctxAccount = "account" // the auth.User that requireAuth already looked up
|
|
)
|
|
|
|
// limitBody caps the request body size. The import of a Spotify export is the one request that may be far larger.
|
|
func limitBody(max int64) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
limit := max
|
|
if c.FullPath() == spotifyImportPath {
|
|
limit = importMaxBytes
|
|
}
|
|
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, limit)
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
// requestLog logs one line per request. It never logs headers, query strings
|
|
// or bodies, so tokens and passwords cannot leak into the log.
|
|
func requestLog(logger *zap.Logger) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
start := time.Now()
|
|
c.Next()
|
|
fields := []zap.Field{
|
|
zap.String("method", c.Request.Method),
|
|
zap.String("path", c.Request.URL.Path),
|
|
zap.Int("status", c.Writer.Status()),
|
|
zap.Duration("latency", time.Since(start)),
|
|
}
|
|
if last := c.Errors.Last(); last != nil {
|
|
// Internal errors are hidden from the client; keep them in the log.
|
|
fields = append(fields, zap.String("error", last.Error()))
|
|
}
|
|
if c.Request.URL.Path == healthPath && c.Writer.Status() == http.StatusOK {
|
|
logger.Debug("http request", fields...) // a health check asks all day: it is not news
|
|
return
|
|
}
|
|
logger.Info("http request", fields...)
|
|
}
|
|
}
|
|
|
|
// healthPath is what the health check of a container asks; see the Dockerfile.
|
|
const healthPath = "/api/v1/health"
|
|
|
|
// currentUserID returns the authenticated user's id (set by requireAuth).
|
|
func currentUserID(c *gin.Context) int64 {
|
|
v, _ := c.Get(ctxUserID)
|
|
id, _ := v.(int64)
|
|
return id
|
|
}
|
|
|
|
// userLookup finds an account by id.
|
|
type userLookup interface {
|
|
User(ctx context.Context, id int64) (auth.User, error)
|
|
}
|
|
|
|
// requireAdmin runs after requireAuth. It reads the role from the database on
|
|
// every call, so a demoted or deleted admin loses access immediately instead
|
|
// of when the access token expires.
|
|
func requireAdmin(users userLookup) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
// requireAuth just read the account in this same request: use it, do not
|
|
// read it twice.
|
|
account, known := accountOf(c)
|
|
var err error
|
|
if !known {
|
|
account, err = users.User(c.Request.Context(), currentUserID(c))
|
|
}
|
|
switch {
|
|
case errors.Is(err, auth.ErrUserNotFound):
|
|
writeError(c, http.StatusUnauthorized, "unauthorized", "account no longer exists")
|
|
case err != nil:
|
|
_ = c.Error(err)
|
|
writeError(c, http.StatusInternalServerError, "internal", "internal error")
|
|
case !account.IsAdmin:
|
|
writeError(c, http.StatusForbidden, "admin_required", "this action needs an admin account")
|
|
default:
|
|
c.Next()
|
|
}
|
|
}
|
|
}
|
|
|
|
// accountOf returns the account that requireAuth looked up for this request.
|
|
func accountOf(c *gin.Context) (auth.User, bool) {
|
|
v, ok := c.Get(ctxAccount)
|
|
u, isUser := v.(auth.User)
|
|
return u, ok && isUser
|
|
}
|
|
|
|
// requireAuth checks the access token, then that it was issued for the account
|
|
// that has this id now. User ids can be given again after an account is deleted
|
|
// (SQLite reuses the highest one), and a token lives for minutes: without this check
|
|
// the token of a deleted user would work as whoever gets the id next. The account
|
|
// must exist, have the same name, and not have been created after the token was signed
|
|
// (both times are whole seconds from the server's clock; a clock stepped back can make
|
|
// a new account look newer than its token for the length of the step).
|
|
//
|
|
// It reads "Authorization: Bearer <jwt>";
|
|
// when allowQuery is true and the header is absent it also accepts the
|
|
// access_token query parameter. That is for routes loaded by an <audio> element,
|
|
// which cannot send headers. A header that is present always wins, so a bad
|
|
// header is never rescued by a query token.
|
|
func requireAuth(tokens tokenParser, users userLookup, allowQuery bool) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
token := ""
|
|
if header := c.GetHeader("Authorization"); header != "" {
|
|
// The scheme name is case-insensitive (RFC 7235). Any other scheme,
|
|
// or a bare token, is not accepted.
|
|
const scheme = "Bearer "
|
|
if len(header) < len(scheme) || !strings.EqualFold(header[:len(scheme)], scheme) {
|
|
writeError(c, http.StatusUnauthorized, "unauthorized", "missing bearer token")
|
|
return
|
|
}
|
|
token = header[len(scheme):]
|
|
} else if allowQuery {
|
|
token = c.Query("access_token")
|
|
}
|
|
if token == "" {
|
|
writeError(c, http.StatusUnauthorized, "unauthorized", "missing bearer token")
|
|
return
|
|
}
|
|
claims, err := tokens.Parse(token)
|
|
if err != nil {
|
|
writeError(c, http.StatusUnauthorized, "unauthorized", "invalid or expired token")
|
|
return
|
|
}
|
|
user, err := users.User(c.Request.Context(), claims.UserID)
|
|
switch {
|
|
case errors.Is(err, auth.ErrUserNotFound):
|
|
writeError(c, http.StatusUnauthorized, "unauthorized", "account no longer exists")
|
|
return
|
|
case err != nil && c.Request.Context().Err() != nil:
|
|
// The client left (a player seeking aborts its Range request): nobody
|
|
// is waiting and nothing went wrong here.
|
|
c.AbortWithStatus(statusClientClosed)
|
|
return
|
|
case err != nil:
|
|
_ = c.Error(err)
|
|
writeError(c, http.StatusInternalServerError, "internal", "internal error")
|
|
return
|
|
case user.Email != claims.Email || user.CreatedAt.Unix() > claims.IssuedAt.Unix():
|
|
writeError(c, http.StatusUnauthorized, "unauthorized", "invalid or expired token")
|
|
return
|
|
}
|
|
c.Set(ctxUserID, claims.UserID)
|
|
c.Set(ctxAccount, user)
|
|
c.Set(ctxEmail, claims.Email)
|
|
c.Next()
|
|
}
|
|
}
|