Introduces context.Context in API controllers
This commit is contained in:
+18
-2
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user