matterbridge/vendor/github.com/labstack/echo/middleware/basic_auth.go

107 lines
2.5 KiB
Go
Raw Normal View History

2017-02-18 22:00:46 +00:00
package middleware
import (
"encoding/base64"
2017-06-05 22:01:05 +00:00
"strconv"
2017-12-07 22:00:56 +00:00
"strings"
2017-02-18 22:00:46 +00:00
"github.com/labstack/echo"
)
type (
// BasicAuthConfig defines the config for BasicAuth middleware.
BasicAuthConfig struct {
// Skipper defines a function to skip middleware.
Skipper Skipper
// Validator is a function to validate BasicAuth credentials.
// Required.
Validator BasicAuthValidator
2017-06-05 22:01:05 +00:00
// Realm is a string to define realm attribute of BasicAuth.
// Default value "Restricted".
Realm string
2017-02-18 22:00:46 +00:00
}
// BasicAuthValidator defines a function to validate BasicAuth credentials.
2017-06-05 22:01:05 +00:00
BasicAuthValidator func(string, string, echo.Context) (bool, error)
2017-02-18 22:00:46 +00:00
)
const (
2017-12-07 22:00:56 +00:00
basic = "basic"
2017-06-05 22:01:05 +00:00
defaultRealm = "Restricted"
2017-02-18 22:00:46 +00:00
)
var (
// DefaultBasicAuthConfig is the default BasicAuth middleware config.
DefaultBasicAuthConfig = BasicAuthConfig{
Skipper: DefaultSkipper,
2017-06-05 22:01:05 +00:00
Realm: defaultRealm,
2017-02-18 22:00:46 +00:00
}
)
// BasicAuth returns an BasicAuth middleware.
//
// For valid credentials it calls the next handler.
// For missing or invalid credentials, it sends "401 - Unauthorized" response.
func BasicAuth(fn BasicAuthValidator) echo.MiddlewareFunc {
c := DefaultBasicAuthConfig
c.Validator = fn
return BasicAuthWithConfig(c)
}
// BasicAuthWithConfig returns an BasicAuth middleware with config.
// See `BasicAuth()`.
func BasicAuthWithConfig(config BasicAuthConfig) echo.MiddlewareFunc {
// Defaults
if config.Validator == nil {
2017-12-07 22:00:56 +00:00
panic("echo: basic-auth middleware requires a validator function")
2017-02-18 22:00:46 +00:00
}
if config.Skipper == nil {
config.Skipper = DefaultBasicAuthConfig.Skipper
}
2017-06-05 22:01:05 +00:00
if config.Realm == "" {
config.Realm = defaultRealm
}
2017-02-18 22:00:46 +00:00
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if config.Skipper(c) {
return next(c)
}
auth := c.Request().Header.Get(echo.HeaderAuthorization)
l := len(basic)
2017-12-07 22:00:56 +00:00
if len(auth) > l+1 && strings.ToLower(auth[:l]) == basic {
2017-02-18 22:00:46 +00:00
b, err := base64.StdEncoding.DecodeString(auth[l+1:])
if err != nil {
return err
}
cred := string(b)
for i := 0; i < len(cred); i++ {
if cred[i] == ':' {
// Verify credentials
2017-06-05 22:01:05 +00:00
valid, err := config.Validator(cred[:i], cred[i+1:], c)
if err != nil {
return err
} else if valid {
2017-02-18 22:00:46 +00:00
return next(c)
}
2018-02-20 23:48:10 +00:00
break
2017-02-18 22:00:46 +00:00
}
}
}
realm := defaultRealm
if config.Realm != defaultRealm {
2017-06-05 22:01:05 +00:00
realm = strconv.Quote(config.Realm)
}
2017-02-18 22:00:46 +00:00
// Need to return `401` for browsers to pop-up login box.
2017-06-05 22:01:05 +00:00
c.Response().Header().Set(echo.HeaderWWWAuthenticate, basic+" realm="+realm)
2017-02-18 22:00:46 +00:00
return echo.ErrUnauthorized
}
}
}