mirror of
https://github.com/labstack/echo.git
synced 2024-11-24 08:22:21 +02:00
c4caeb8ffb
Signed-off-by: Vishal Rana <vr@labstack.com>
65 lines
1.6 KiB
Go
65 lines
1.6 KiB
Go
package middleware
|
|
|
|
import (
|
|
"encoding/base64"
|
|
|
|
"github.com/labstack/echo"
|
|
)
|
|
|
|
type (
|
|
// BasicAuthConfig defines config for HTTP basic auth middleware.
|
|
BasicAuthConfig struct {
|
|
AuthFunc BasicAuthFunc
|
|
}
|
|
|
|
// BasicAuthFunc defines a function to validate basic auth credentials.
|
|
BasicAuthFunc func(string, string) bool
|
|
)
|
|
|
|
const (
|
|
basic = "Basic"
|
|
)
|
|
|
|
var (
|
|
// DefaultBasicAuthConfig is the default basic auth middleware config.
|
|
DefaultBasicAuthConfig = BasicAuthConfig{}
|
|
)
|
|
|
|
// BasicAuth returns an HTTP basic auth middleware.
|
|
//
|
|
// For valid credentials it calls the next handler.
|
|
// For invalid credentials, it sends "401 - Unauthorized" response.
|
|
func BasicAuth(f BasicAuthFunc) echo.MiddlewareFunc {
|
|
c := DefaultBasicAuthConfig
|
|
c.AuthFunc = f
|
|
return BasicAuthFromConfig(c)
|
|
}
|
|
|
|
// BasicAuthFromConfig returns an HTTP basic auth middleware from config.
|
|
// See `BasicAuth()`.
|
|
func BasicAuthFromConfig(config BasicAuthConfig) echo.MiddlewareFunc {
|
|
return func(next echo.Handler) echo.Handler {
|
|
return echo.HandlerFunc(func(c echo.Context) error {
|
|
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 config.AuthFunc(cred[:i], cred[i+1:]) {
|
|
return next.Handle(c)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
c.Response().Header().Set(echo.WWWAuthenticate, basic+" realm=Restricted")
|
|
return echo.ErrUnauthorized
|
|
})
|
|
}
|
|
}
|