5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-15 07:30:26 +00:00
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-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-06-05 22:01:05 +00:00
basic = "Basic"
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 {
panic("basic-auth middleware requires a validator function")
}
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)
if len(auth) > l+1 && auth[:l] == basic {
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)
}
}
}
}
2017-06-05 22:01:05 +00:00
realm := ""
if config.Realm == defaultRealm {
realm = defaultRealm
} else {
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
}
}
}