1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2017-05-27 03:10:51 -07:00
parent 3359eae306
commit 466d509e34
4 changed files with 9 additions and 9 deletions

View File

@ -23,7 +23,7 @@ type (
} }
// BasicAuthValidator defines a function to validate BasicAuth credentials. // BasicAuthValidator defines a function to validate BasicAuth credentials.
BasicAuthValidator func(string, string, echo.Context) (error, bool) BasicAuthValidator func(string, string, echo.Context) (bool, error)
) )
const ( const (
@ -81,7 +81,7 @@ func BasicAuthWithConfig(config BasicAuthConfig) echo.MiddlewareFunc {
for i := 0; i < len(cred); i++ { for i := 0; i < len(cred); i++ {
if cred[i] == ':' { if cred[i] == ':' {
// Verify credentials // Verify credentials
err, valid := config.Validator(cred[:i], cred[i+1:], c) valid, err := config.Validator(cred[:i], cred[i+1:], c)
if err != nil { if err != nil {
return err return err
} else if valid { } else if valid {

View File

@ -15,11 +15,11 @@ func TestBasicAuth(t *testing.T) {
req := httptest.NewRequest(echo.GET, "/", nil) req := httptest.NewRequest(echo.GET, "/", nil)
res := httptest.NewRecorder() res := httptest.NewRecorder()
c := e.NewContext(req, res) c := e.NewContext(req, res)
f := func(u, p string, c echo.Context) (error, bool) { f := func(u, p string, c echo.Context) (bool, error) {
if u == "joe" && p == "secret" { if u == "joe" && p == "secret" {
return nil, true return true, nil
} }
return nil, false return false, nil
} }
h := BasicAuth(f)(func(c echo.Context) error { h := BasicAuth(f)(func(c echo.Context) error {
return c.String(http.StatusOK, "test") return c.String(http.StatusOK, "test")

View File

@ -32,7 +32,7 @@ type (
} }
// KeyAuthValidator defines a function to validate KeyAuth credentials. // KeyAuthValidator defines a function to validate KeyAuth credentials.
KeyAuthValidator func(string, echo.Context) (error, bool) KeyAuthValidator func(string, echo.Context) (bool, error)
keyExtractor func(echo.Context) (string, error) keyExtractor func(echo.Context) (string, error)
) )
@ -94,7 +94,7 @@ func KeyAuthWithConfig(config KeyAuthConfig) echo.MiddlewareFunc {
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error()) return echo.NewHTTPError(http.StatusBadRequest, err.Error())
} }
err, valid := config.Validator(key, c) valid, err := config.Validator(key, c)
if err != nil { if err != nil {
return err return err
} else if valid { } else if valid {

View File

@ -15,8 +15,8 @@ func TestKeyAuth(t *testing.T) {
res := httptest.NewRecorder() res := httptest.NewRecorder()
c := e.NewContext(req, res) c := e.NewContext(req, res)
config := KeyAuthConfig{ config := KeyAuthConfig{
Validator: func(key string, c echo.Context) (error, bool) { Validator: func(key string, c echo.Context) (bool, error) {
return nil, key == "valid-key" return key == "valid-key", nil
}, },
} }
h := KeyAuthWithConfig(config)(func(c echo.Context) error { h := KeyAuthWithConfig(config)(func(c echo.Context) error {