Chore: Imperative request data binding (#39837)

* rename Bind to BindMiddleware

* make things private

* removed unused part of data bindings

* provide json and form binding helpers

* add example of binding migration in login api

* implement validation

* fix tests

* remove debug output

* put new bind api into macaron pacakge

* revert bind api breaking change
This commit is contained in:
Serge Zaitsev
2021-10-06 12:52:27 +02:00
committed by GitHub
parent 7fd7c98540
commit 3131388084
6 changed files with 225 additions and 129 deletions
+1 -1
View File
@@ -40,7 +40,7 @@ func (hs *HTTPServer) registerRoutes() {
// not logged in views
r.Get("/logout", hs.Logout)
r.Post("/login", quota("session"), bind(dtos.LoginCommand{}), routing.Wrap(hs.LoginPost))
r.Post("/login", quota("session"), routing.Wrap(hs.LoginPost))
r.Get("/login/:name", quota("session"), hs.OAuthLogin)
r.Get("/login", hs.LoginView)
r.Get("/invite/:code", hs.Index)
+6 -1
View File
@@ -19,6 +19,7 @@ import (
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util/errutil"
macaron "gopkg.in/macaron.v1"
)
const (
@@ -170,7 +171,11 @@ func (hs *HTTPServer) LoginAPIPing(c *models.ReqContext) response.Response {
return response.Error(401, "Unauthorized", nil)
}
func (hs *HTTPServer) LoginPost(c *models.ReqContext, cmd dtos.LoginCommand) response.Response {
func (hs *HTTPServer) LoginPost(c *models.ReqContext) response.Response {
cmd := dtos.LoginCommand{}
if err := macaron.Bind(c.Req, &cmd); err != nil {
return response.Error(http.StatusBadRequest, "bad login data", err)
}
authModule := ""
var user *models.User
var resp *response.NormalResponse
+9 -10
View File
@@ -1,9 +1,11 @@
package api
import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
@@ -336,11 +338,9 @@ func TestLoginPostRedirect(t *testing.T) {
hs.Cfg.CookieSecure = true
sc.defaultHandler = routing.Wrap(func(w http.ResponseWriter, c *models.ReqContext) response.Response {
cmd := dtos.LoginCommand{
User: "admin",
Password: "admin",
}
return hs.LoginPost(c, cmd)
c.Req.Header.Set("Content-Type", "application/json")
c.Req.Body = io.NopCloser(bytes.NewBufferString(`{"user":"admin","password":"admin"}`))
return hs.LoginPost(c)
})
bus.AddHandler("grafana-auth", func(query *models.LoginUserQuery) error {
@@ -614,11 +614,10 @@ func TestLoginPostRunLokingHook(t *testing.T) {
}
sc.defaultHandler = routing.Wrap(func(w http.ResponseWriter, c *models.ReqContext) response.Response {
cmd := dtos.LoginCommand{
User: "admin",
Password: "admin",
}
return hs.LoginPost(c, cmd)
c.Req.Header.Set("Content-Type", "application/json")
c.Req.Body = io.NopCloser(bytes.NewBufferString(`{"user":"admin","password":"admin"}`))
x := hs.LoginPost(c)
return x
})
testHook := loginHookTest{}