2016-02-25 12:31:06 -09:00
|
|
|
package api
|
2016-02-24 14:06:49 -09:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/astaxie/beego"
|
2016-02-25 12:31:06 -09:00
|
|
|
"github.com/deluan/gosonic/api/responses"
|
2016-02-24 14:06:49 -09:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ControllerInterface interface {
|
|
|
|
|
GetString(key string, def ...string) string
|
|
|
|
|
CustomAbort(status int, body string)
|
2016-03-02 09:04:55 -09:00
|
|
|
SendError(errorCode int, message ...interface{})
|
2016-02-24 14:06:49 -09:00
|
|
|
}
|
|
|
|
|
|
2016-02-24 16:21:51 -09:00
|
|
|
func Validate(controller ControllerInterface) {
|
2016-02-24 14:06:49 -09:00
|
|
|
if beego.AppConfig.String("disableValidation") != "true" {
|
|
|
|
|
checkParameters(controller)
|
|
|
|
|
authenticate(controller)
|
2016-02-24 15:16:42 -09:00
|
|
|
// TODO Validate version
|
2016-02-24 14:06:49 -09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func checkParameters(c ControllerInterface) {
|
2016-02-25 14:52:07 -09:00
|
|
|
requiredParameters := []string{"u", "p", "v", "c"}
|
2016-02-24 14:06:49 -09:00
|
|
|
|
2016-02-25 14:52:07 -09:00
|
|
|
for _, p := range requiredParameters {
|
2016-02-24 14:06:49 -09:00
|
|
|
if c.GetString(p) == "" {
|
2016-03-02 09:04:55 -09:00
|
|
|
abortRequest(c, responses.ERROR_MISSING_PARAMETER)
|
2016-02-24 14:06:49 -09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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") {
|
2016-03-02 09:04:55 -09:00
|
|
|
abortRequest(c, responses.ERROR_AUTHENTICATION_FAIL)
|
2016-02-24 14:06:49 -09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-02 09:04:55 -09:00
|
|
|
func abortRequest(c ControllerInterface, code int) {
|
|
|
|
|
c.SendError(code)
|
2016-02-25 14:52:07 -09:00
|
|
|
}
|