2017-02-18 23:00:46 +01:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
2017-06-06 00:01:05 +02:00
|
|
|
"strconv"
|
2017-12-07 23:00:56 +01:00
|
|
|
"strings"
|
2017-02-18 23:00:46 +01: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-06 00:01:05 +02:00
|
|
|
|
|
|
|
// Realm is a string to define realm attribute of BasicAuth.
|
|
|
|
// Default value "Restricted".
|
|
|
|
Realm string
|
2017-02-18 23:00:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// BasicAuthValidator defines a function to validate BasicAuth credentials.
|
2017-06-06 00:01:05 +02:00
|
|
|
BasicAuthValidator func(string, string, echo.Context) (bool, error)
|
2017-02-18 23:00:46 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-12-07 23:00:56 +01:00
|
|
|
basic = "basic"
|
2017-06-06 00:01:05 +02:00
|
|
|
defaultRealm = "Restricted"
|
2017-02-18 23:00:46 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// DefaultBasicAuthConfig is the default BasicAuth middleware config.
|
|
|
|
DefaultBasicAuthConfig = BasicAuthConfig{
|
|
|
|
Skipper: DefaultSkipper,
|
2017-06-06 00:01:05 +02:00
|
|
|
Realm: defaultRealm,
|
2017-02-18 23:00:46 +01: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 23:00:56 +01:00
|
|
|
panic("echo: basic-auth middleware requires a validator function")
|
2017-02-18 23:00:46 +01:00
|
|
|
}
|
|
|
|
if config.Skipper == nil {
|
|
|
|
config.Skipper = DefaultBasicAuthConfig.Skipper
|
|
|
|
}
|
2017-06-06 00:01:05 +02:00
|
|
|
if config.Realm == "" {
|
|
|
|
config.Realm = defaultRealm
|
|
|
|
}
|
2017-02-18 23:00:46 +01: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 23:00:56 +01:00
|
|
|
if len(auth) > l+1 && strings.ToLower(auth[:l]) == basic {
|
2017-02-18 23:00:46 +01: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-06 00:01:05 +02:00
|
|
|
valid, err := config.Validator(cred[:i], cred[i+1:], c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if valid {
|
2017-02-18 23:00:46 +01:00
|
|
|
return next(c)
|
|
|
|
}
|
2018-02-21 00:48:10 +01:00
|
|
|
break
|
2017-02-18 23:00:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-06 00:01:05 +02:00
|
|
|
realm := ""
|
|
|
|
if config.Realm == defaultRealm {
|
|
|
|
realm = defaultRealm
|
|
|
|
} else {
|
|
|
|
realm = strconv.Quote(config.Realm)
|
|
|
|
}
|
|
|
|
|
2017-02-18 23:00:46 +01:00
|
|
|
// Need to return `401` for browsers to pop-up login box.
|
2017-06-06 00:01:05 +02:00
|
|
|
c.Response().Header().Set(echo.HeaderWWWAuthenticate, basic+" realm="+realm)
|
2017-02-18 23:00:46 +01:00
|
|
|
return echo.ErrUnauthorized
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|