mirror of
https://github.com/labstack/echo.git
synced 2026-05-16 09:48:24 +02:00
Default handler now returns error.
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
+68
-67
@@ -1,69 +1,70 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"github.com/labstack/echo"
|
||||
)
|
||||
|
||||
type (
|
||||
BasicAuthFunc func(string, string) bool
|
||||
AuthorizedHandler echo.HandlerFunc
|
||||
UnauthorizedHandler func(*echo.Context, error)
|
||||
JwtKeyFunc func(string) ([]byte, error)
|
||||
Claims map[string]interface{}
|
||||
)
|
||||
|
||||
var (
|
||||
ErrBasicAuth = errors.New("echo: basic auth error")
|
||||
ErrJwtAuth = errors.New("echo: jwt auth error")
|
||||
)
|
||||
|
||||
func BasicAuth(ah AuthorizedHandler, uah UnauthorizedHandler, fn BasicAuthFunc) echo.HandlerFunc {
|
||||
return func(c *echo.Context) {
|
||||
auth := strings.Fields(c.Request.Header.Get("Authorization"))
|
||||
if len(auth) == 2 {
|
||||
scheme := auth[0]
|
||||
s, err := base64.StdEncoding.DecodeString(auth[1])
|
||||
if err != nil {
|
||||
uah(c, err)
|
||||
return
|
||||
}
|
||||
cred := strings.Split(string(s), ":")
|
||||
if scheme == "Basic" && len(cred) == 2 {
|
||||
if ok := fn(cred[0], cred[1]); ok {
|
||||
ah(c)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
uah(c, ErrBasicAuth)
|
||||
}
|
||||
}
|
||||
|
||||
func JwtAuth(ah AuthorizedHandler, uah UnauthorizedHandler, fn JwtKeyFunc) echo.HandlerFunc {
|
||||
return func(c *echo.Context) {
|
||||
auth := strings.Fields(c.Request.Header.Get("Authorization"))
|
||||
if len(auth) == 2 {
|
||||
t, err := jwt.Parse(auth[1], func(token *jwt.Token) (interface{}, error) {
|
||||
if kid := token.Header["kid"]; kid != nil {
|
||||
return fn(kid.(string))
|
||||
}
|
||||
return fn("")
|
||||
})
|
||||
if t.Valid {
|
||||
c.Set("claims", Claims(t.Claims))
|
||||
ah(c)
|
||||
// c.Next()
|
||||
} else {
|
||||
// TODO: capture errors
|
||||
uah(c, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
uah(c, ErrJwtAuth)
|
||||
}
|
||||
}
|
||||
//
|
||||
// import (
|
||||
// "encoding/base64"
|
||||
// "errors"
|
||||
// "strings"
|
||||
//
|
||||
// "github.com/dgrijalva/jwt-go"
|
||||
// "github.com/labstack/echo"
|
||||
// )
|
||||
//
|
||||
// type (
|
||||
// BasicAuthFunc func(string, string) bool
|
||||
// AuthorizedHandler echo.HandlerFunc
|
||||
// UnauthorizedHandler func(*echo.Context, error)
|
||||
// JwtKeyFunc func(string) ([]byte, error)
|
||||
// Claims map[string]interface{}
|
||||
// )
|
||||
//
|
||||
// var (
|
||||
// ErrBasicAuth = errors.New("echo: basic auth error")
|
||||
// ErrJwtAuth = errors.New("echo: jwt auth error")
|
||||
// )
|
||||
//
|
||||
// func BasicAuth(ah AuthorizedHandler, uah UnauthorizedHandler, fn BasicAuthFunc) echo.HandlerFunc {
|
||||
// return func(c *echo.Context) {
|
||||
// auth := strings.Fields(c.Request.Header.Get("Authorization"))
|
||||
// if len(auth) == 2 {
|
||||
// scheme := auth[0]
|
||||
// s, err := base64.StdEncoding.DecodeString(auth[1])
|
||||
// if err != nil {
|
||||
// uah(c, err)
|
||||
// return
|
||||
// }
|
||||
// cred := strings.Split(string(s), ":")
|
||||
// if scheme == "Basic" && len(cred) == 2 {
|
||||
// if ok := fn(cred[0], cred[1]); ok {
|
||||
// ah(c)
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// uah(c, ErrBasicAuth)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// func JwtAuth(ah AuthorizedHandler, uah UnauthorizedHandler, fn JwtKeyFunc) echo.HandlerFunc {
|
||||
// return func(c *echo.Context) {
|
||||
// auth := strings.Fields(c.Request.Header.Get("Authorization"))
|
||||
// if len(auth) == 2 {
|
||||
// t, err := jwt.Parse(auth[1], func(token *jwt.Token) (interface{}, error) {
|
||||
// if kid := token.Header["kid"]; kid != nil {
|
||||
// return fn(kid.(string))
|
||||
// }
|
||||
// return fn("")
|
||||
// })
|
||||
// if t.Valid {
|
||||
// c.Set("claims", Claims(t.Claims))
|
||||
// ah(c)
|
||||
// // c.Next()
|
||||
// } else {
|
||||
// // TODO: capture errors
|
||||
// uah(c, err)
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
// uah(c, ErrJwtAuth)
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -9,9 +9,9 @@ import (
|
||||
"github.com/mattn/go-colorable"
|
||||
)
|
||||
|
||||
func Logger(h echo.HandlerFunc) echo.HandlerFunc {
|
||||
func Logger(h echo.HandlerFunc) (echo.HandlerFunc, error) {
|
||||
log.SetOutput(colorable.NewColorableStdout())
|
||||
return echo.HandlerFunc(func(c *echo.Context) {
|
||||
return func(c *echo.Context) error {
|
||||
start := time.Now()
|
||||
h(c)
|
||||
end := time.Now()
|
||||
@@ -30,5 +30,6 @@ func Logger(h echo.HandlerFunc) echo.HandlerFunc {
|
||||
}
|
||||
|
||||
log.Printf("%s %s %s %s", m, p, col(s), end.Sub(start))
|
||||
})
|
||||
return nil
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user