2020-01-20 00:21:44 +01:00
|
|
|
package subsonic
|
2020-01-07 20:56:26 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2020-01-27 21:10:46 +01:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2020-01-07 20:56:26 +01:00
|
|
|
|
2020-01-24 01:44:08 +01:00
|
|
|
"github.com/deluan/navidrome/engine"
|
|
|
|
"github.com/deluan/navidrome/log"
|
|
|
|
"github.com/deluan/navidrome/model"
|
|
|
|
"github.com/deluan/navidrome/server/subsonic/responses"
|
2020-02-07 00:55:38 +01:00
|
|
|
"github.com/deluan/navidrome/utils"
|
2020-01-07 20:56:26 +01:00
|
|
|
)
|
|
|
|
|
2020-01-27 21:10:46 +01:00
|
|
|
func postFormToQueryParams(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
SendError(w, r, NewError(responses.ErrorGeneric, err.Error()))
|
|
|
|
}
|
|
|
|
var parts []string
|
|
|
|
for key, values := range r.Form {
|
|
|
|
for _, v := range values {
|
|
|
|
parts = append(parts, url.QueryEscape(key)+"="+url.QueryEscape(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.URL.RawQuery = strings.Join(parts, "&")
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-07 20:56:26 +01:00
|
|
|
func checkRequiredParameters(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
requiredParameters := []string{"u", "v", "c"}
|
|
|
|
|
|
|
|
for _, p := range requiredParameters {
|
2020-02-07 00:55:38 +01:00
|
|
|
if utils.ParamString(r, p) == "" {
|
2020-01-07 20:56:26 +01:00
|
|
|
msg := fmt.Sprintf(`Missing required parameter "%s"`, p)
|
2020-01-09 02:45:07 +01:00
|
|
|
log.Warn(r, msg)
|
2020-01-07 20:56:26 +01:00
|
|
|
SendError(w, r, NewError(responses.ErrorMissingParameter, msg))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 00:55:38 +01:00
|
|
|
user := utils.ParamString(r, "u")
|
|
|
|
client := utils.ParamString(r, "c")
|
|
|
|
version := utils.ParamString(r, "v")
|
2020-01-07 20:56:26 +01:00
|
|
|
ctx := r.Context()
|
2020-01-21 00:12:17 +01:00
|
|
|
ctx = context.WithValue(ctx, "username", user)
|
2020-01-09 16:35:00 +01:00
|
|
|
ctx = context.WithValue(ctx, "client", client)
|
|
|
|
ctx = context.WithValue(ctx, "version", version)
|
2020-02-02 02:07:15 +01:00
|
|
|
log.Info(ctx, "API: New request "+r.URL.Path, "username", user, "client", client, "version", version)
|
2020-01-07 20:56:26 +01:00
|
|
|
|
2020-01-09 16:35:00 +01:00
|
|
|
r = r.WithContext(ctx)
|
2020-01-07 20:56:26 +01:00
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-20 19:35:59 +01:00
|
|
|
func authenticate(users engine.Users) func(next http.Handler) http.Handler {
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2020-02-07 00:55:38 +01:00
|
|
|
username := utils.ParamString(r, "u")
|
|
|
|
pass := utils.ParamString(r, "p")
|
|
|
|
token := utils.ParamString(r, "t")
|
|
|
|
salt := utils.ParamString(r, "s")
|
|
|
|
jwt := utils.ParamString(r, "jwt")
|
2020-01-07 20:56:26 +01:00
|
|
|
|
2020-02-06 22:48:35 +01:00
|
|
|
usr, err := users.Authenticate(r.Context(), username, pass, token, salt, jwt)
|
2020-01-20 19:35:59 +01:00
|
|
|
if err == model.ErrInvalidAuth {
|
2020-01-21 00:12:17 +01:00
|
|
|
log.Warn(r, "Invalid login", "username", username, err)
|
2020-01-20 19:35:59 +01:00
|
|
|
} else if err != nil {
|
2020-01-21 00:12:17 +01:00
|
|
|
log.Error(r, "Error authenticating username", "username", username, err)
|
2020-01-07 20:56:26 +01:00
|
|
|
}
|
|
|
|
|
2020-01-20 19:35:59 +01:00
|
|
|
if err != nil {
|
2020-01-21 00:12:17 +01:00
|
|
|
log.Warn(r, "Invalid login", "username", username)
|
2020-01-20 19:35:59 +01:00
|
|
|
SendError(w, r, NewError(responses.ErrorAuthenticationFail))
|
|
|
|
return
|
|
|
|
}
|
2020-01-07 20:56:26 +01:00
|
|
|
|
2020-01-21 00:12:17 +01:00
|
|
|
ctx := r.Context()
|
|
|
|
ctx = context.WithValue(ctx, "user", usr)
|
|
|
|
r = r.WithContext(ctx)
|
|
|
|
|
2020-01-20 19:35:59 +01:00
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
2020-01-07 20:56:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func requiredParams(params ...string) func(next http.Handler) http.Handler {
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
for _, p := range params {
|
|
|
|
_, err := RequiredParamString(r, p, fmt.Sprintf("%s parameter is required", p))
|
|
|
|
if err != nil {
|
|
|
|
SendError(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|