mirror of
https://github.com/labstack/echo.git
synced 2025-07-15 01:34:53 +02:00
Middleware and handler as closure
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
@ -8,10 +8,7 @@ import (
|
||||
)
|
||||
|
||||
type (
|
||||
// BasicAuth defines an HTTP basic authentication middleware.
|
||||
BasicAuth struct {
|
||||
function BasicAuthFunc
|
||||
priority int
|
||||
BasicAuthOption struct {
|
||||
}
|
||||
|
||||
BasicAuthFunc func(string, string) bool
|
||||
@ -21,47 +18,37 @@ const (
|
||||
basic = "Basic"
|
||||
)
|
||||
|
||||
func NewBasicAuth(fn BasicAuthFunc) *BasicAuth {
|
||||
return &BasicAuth{function: fn}
|
||||
}
|
||||
|
||||
func (ba *BasicAuth) SetPriority(p int) {
|
||||
ba.priority = p
|
||||
}
|
||||
|
||||
func (ba *BasicAuth) Priority() int {
|
||||
return ba.priority
|
||||
}
|
||||
|
||||
// Handle validates credentials using `AuthFunc`
|
||||
// BasicAuth returns an HTTP basic authentication middleware.
|
||||
//
|
||||
// For valid credentials it calls the next handler.
|
||||
// For invalid credentials, it sends "401 - Unauthorized" response.
|
||||
func (ba *BasicAuth) Handle(h echo.Handler) echo.Handler {
|
||||
return echo.HandlerFunc(func(c echo.Context) error {
|
||||
// Skip WebSocket
|
||||
if (c.Request().Header().Get(echo.Upgrade)) == echo.WebSocket {
|
||||
return nil
|
||||
}
|
||||
func BasicAuth(fn BasicAuthFunc, option ...*BasicAuthOption) echo.MiddlewareFunc {
|
||||
return func(h echo.Handler) echo.Handler {
|
||||
return echo.HandlerFunc(func(c echo.Context) error {
|
||||
// Skip WebSocket
|
||||
if (c.Request().Header().Get(echo.Upgrade)) == echo.WebSocket {
|
||||
return nil
|
||||
}
|
||||
|
||||
auth := c.Request().Header().Get(echo.Authorization)
|
||||
l := len(basic)
|
||||
auth := c.Request().Header().Get(echo.Authorization)
|
||||
l := len(basic)
|
||||
|
||||
if len(auth) > l+1 && auth[:l] == basic {
|
||||
b, err := base64.StdEncoding.DecodeString(auth[l+1:])
|
||||
if err == nil {
|
||||
cred := string(b)
|
||||
for i := 0; i < len(cred); i++ {
|
||||
if cred[i] == ':' {
|
||||
// Verify credentials
|
||||
if ba.function(cred[:i], cred[i+1:]) {
|
||||
return nil
|
||||
if len(auth) > l+1 && auth[:l] == basic {
|
||||
b, err := base64.StdEncoding.DecodeString(auth[l+1:])
|
||||
if err == nil {
|
||||
cred := string(b)
|
||||
for i := 0; i < len(cred); i++ {
|
||||
if cred[i] == ':' {
|
||||
// Verify credentials
|
||||
if fn(cred[:i], cred[i+1:]) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
c.Response().Header().Set(echo.WWWAuthenticate, basic+" realm=Restricted")
|
||||
return echo.NewHTTPError(http.StatusUnauthorized)
|
||||
})
|
||||
c.Response().Header().Set(echo.WWWAuthenticate, basic+" realm=Restricted")
|
||||
return echo.NewHTTPError(http.StatusUnauthorized)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user