quintodrome/api/validation.go

43 lines
1 KiB
Go
Raw Normal View History

package api
import (
"github.com/astaxie/beego"
"github.com/deluan/gosonic/api/responses"
)
type ControllerInterface interface {
GetString(key string, def ...string) string
CustomAbort(status int, body string)
SendError(errorCode int, message ...interface{})
}
func Validate(controller ControllerInterface) {
if beego.AppConfig.String("disableValidation") != "true" {
checkParameters(controller)
authenticate(controller)
2016-02-24 15:16:42 -09:00
// TODO Validate version
}
}
func checkParameters(c ControllerInterface) {
2016-02-25 14:52:07 -09:00
requiredParameters := []string{"u", "p", "v", "c"}
2016-02-25 14:52:07 -09:00
for _, p := range requiredParameters {
if c.GetString(p) == "" {
abortRequest(c, responses.ERROR_MISSING_PARAMETER)
}
}
}
func authenticate(c ControllerInterface) {
user := c.GetString("u")
2016-02-24 15:16:42 -09:00
pass := c.GetString("p") // TODO Handle hex-encoded password
2016-02-25 14:52:07 -09:00
if user != beego.AppConfig.String("user") || pass != beego.AppConfig.String("password") {
abortRequest(c, responses.ERROR_AUTHENTICATION_FAIL)
}
}
func abortRequest(c ControllerInterface, code int) {
c.SendError(code)
2016-02-25 14:52:07 -09:00
}