1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-28 08:38:39 +02:00

Signature changed for key and basic auth validator

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2017-04-18 15:40:28 -07:00
parent c3069d42c1
commit 5f392f3bb1
6 changed files with 20 additions and 14 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -14,11 +14,11 @@ Basic auth middleware provides an HTTP basic authentication.
*Usage*
```go
e.Use(middleware.BasicAuth(func(username, password string, c echo.Context) bool {
e.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (error, bool) {
if username == "joe" && password == "secret" {
return true
return nil, true
}
return false
return nil, false
}))
```

View File

@ -15,8 +15,8 @@ Key auth middleware provides a key based authentication.
*Usage*
```go
e.Use(middleware.KeyAuth(func(key string) bool {
return key == "valid-key"
e.Use(middleware.KeyAuth(func(key string) (error, bool) {
return nil, key == "valid-key"
}))
```