mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-06 05:57:35 +03:00
Introduces context.Context in API controllers
This commit is contained in:
parent
dc98b187d9
commit
2861a97a89
5 changed files with 47 additions and 14 deletions
|
@ -1,6 +1,7 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
|
@ -18,6 +19,7 @@ type ControllerInterface interface {
|
|||
}
|
||||
|
||||
func Validate(controller BaseAPIController) {
|
||||
addNewContext(controller)
|
||||
if !conf.Sonic.DisableValidation {
|
||||
checkParameters(controller)
|
||||
authenticate(controller)
|
||||
|
@ -25,6 +27,20 @@ func Validate(controller BaseAPIController) {
|
|||
}
|
||||
}
|
||||
|
||||
func getData(c BaseAPIController, name string) string {
|
||||
if v, ok := c.Ctx.Input.GetData(name).(string); ok {
|
||||
return v
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func addNewContext(c BaseAPIController) {
|
||||
ctx := context.Background()
|
||||
|
||||
id := getData(c, "requestId")
|
||||
c.context = context.WithValue(ctx, "requestId", id)
|
||||
}
|
||||
|
||||
func checkParameters(c BaseAPIController) {
|
||||
requiredParameters := []string{"u", "v", "c"}
|
||||
|
||||
|
@ -56,10 +72,10 @@ func authenticate(c BaseAPIController) {
|
|||
pass = string(dec)
|
||||
}
|
||||
}
|
||||
valid = (pass == password)
|
||||
valid = pass == password
|
||||
case token != "":
|
||||
t := fmt.Sprintf("%x", md5.Sum([]byte(password+salt)))
|
||||
valid = (t == token)
|
||||
valid = t == token
|
||||
}
|
||||
|
||||
if user != conf.Sonic.User || !valid {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue