2016-04-28 06:08:06 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
2023-05-12 19:14:59 +02:00
|
|
|
"net/http"
|
2017-02-21 06:27:40 +02:00
|
|
|
"strconv"
|
2017-11-21 01:57:41 +02:00
|
|
|
"strings"
|
2016-04-28 06:08:06 +02:00
|
|
|
|
2019-01-30 12:56:56 +02:00
|
|
|
"github.com/labstack/echo/v4"
|
2016-04-28 06:08:06 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2016-07-27 18:34:44 +02:00
|
|
|
// BasicAuthConfig defines the config for BasicAuth middleware.
|
2016-04-28 06:08:06 +02:00
|
|
|
BasicAuthConfig struct {
|
2016-07-27 18:34:44 +02:00
|
|
|
// Skipper defines a function to skip middleware.
|
|
|
|
Skipper Skipper
|
|
|
|
|
|
|
|
// Validator is a function to validate BasicAuth credentials.
|
2016-09-05 19:20:10 +02:00
|
|
|
// Required.
|
2016-04-28 06:08:06 +02:00
|
|
|
Validator BasicAuthValidator
|
2017-02-21 06:27:40 +02:00
|
|
|
|
2017-02-22 01:18:09 +02:00
|
|
|
// Realm is a string to define realm attribute of BasicAuth.
|
|
|
|
// Default value "Restricted".
|
2017-02-21 06:27:40 +02:00
|
|
|
Realm string
|
2016-04-28 06:08:06 +02:00
|
|
|
}
|
|
|
|
|
2016-07-27 18:34:44 +02:00
|
|
|
// BasicAuthValidator defines a function to validate BasicAuth credentials.
|
2024-02-18 15:47:13 +02:00
|
|
|
// The function should return a boolean indicating whether the credentials are valid,
|
|
|
|
// and an error if any error occurs during the validation process.
|
2017-05-27 12:10:51 +02:00
|
|
|
BasicAuthValidator func(string, string, echo.Context) (bool, error)
|
2016-04-28 06:08:06 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-11-21 01:57:41 +02:00
|
|
|
basic = "basic"
|
2017-02-21 06:27:40 +02:00
|
|
|
defaultRealm = "Restricted"
|
2016-04-28 06:08:06 +02:00
|
|
|
)
|
|
|
|
|
2016-07-27 18:34:44 +02:00
|
|
|
var (
|
|
|
|
// DefaultBasicAuthConfig is the default BasicAuth middleware config.
|
|
|
|
DefaultBasicAuthConfig = BasicAuthConfig{
|
2017-01-28 21:43:56 +02:00
|
|
|
Skipper: DefaultSkipper,
|
2017-02-21 06:27:40 +02:00
|
|
|
Realm: defaultRealm,
|
2016-07-27 18:34:44 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// BasicAuth returns an BasicAuth middleware.
|
2016-04-28 06:08:06 +02:00
|
|
|
//
|
|
|
|
// For valid credentials it calls the next handler.
|
2017-01-05 22:12:31 +02:00
|
|
|
// For missing or invalid credentials, it sends "401 - Unauthorized" response.
|
2016-04-28 06:08:06 +02:00
|
|
|
func BasicAuth(fn BasicAuthValidator) echo.MiddlewareFunc {
|
2016-07-27 18:34:44 +02:00
|
|
|
c := DefaultBasicAuthConfig
|
|
|
|
c.Validator = fn
|
|
|
|
return BasicAuthWithConfig(c)
|
2016-04-28 06:08:06 +02:00
|
|
|
}
|
|
|
|
|
2016-09-01 05:10:14 +02:00
|
|
|
// BasicAuthWithConfig returns an BasicAuth middleware with config.
|
2016-04-28 06:08:06 +02:00
|
|
|
// See `BasicAuth()`.
|
|
|
|
func BasicAuthWithConfig(config BasicAuthConfig) echo.MiddlewareFunc {
|
2016-07-27 18:34:44 +02:00
|
|
|
// Defaults
|
2016-09-05 19:20:10 +02:00
|
|
|
if config.Validator == nil {
|
2017-06-23 07:48:23 +02:00
|
|
|
panic("echo: basic-auth middleware requires a validator function")
|
2016-09-05 19:20:10 +02:00
|
|
|
}
|
2016-07-27 18:34:44 +02:00
|
|
|
if config.Skipper == nil {
|
|
|
|
config.Skipper = DefaultBasicAuthConfig.Skipper
|
|
|
|
}
|
2017-02-21 06:27:40 +02:00
|
|
|
if config.Realm == "" {
|
|
|
|
config.Realm = defaultRealm
|
|
|
|
}
|
2016-07-27 18:34:44 +02:00
|
|
|
|
2016-04-28 06:08:06 +02:00
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
2016-07-27 18:34:44 +02:00
|
|
|
if config.Skipper(c) {
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
auth := c.Request().Header.Get(echo.HeaderAuthorization)
|
2016-04-28 06:08:06 +02:00
|
|
|
l := len(basic)
|
|
|
|
|
2021-02-26 23:52:32 +02:00
|
|
|
if len(auth) > l+1 && strings.EqualFold(auth[:l], basic) {
|
2022-05-27 18:44:51 +02:00
|
|
|
// Invalid base64 shouldn't be treated as error
|
|
|
|
// instead should be treated as invalid client input
|
2016-04-28 06:08:06 +02:00
|
|
|
b, err := base64.StdEncoding.DecodeString(auth[l+1:])
|
|
|
|
if err != nil {
|
2022-05-27 18:44:51 +02:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest).SetInternal(err)
|
2016-04-28 06:08:06 +02:00
|
|
|
}
|
2022-05-27 18:44:51 +02:00
|
|
|
|
2016-04-28 06:08:06 +02:00
|
|
|
cred := string(b)
|
|
|
|
for i := 0; i < len(cred); i++ {
|
|
|
|
if cred[i] == ':' {
|
|
|
|
// Verify credentials
|
2017-05-27 12:10:51 +02:00
|
|
|
valid, err := config.Validator(cred[:i], cred[i+1:], c)
|
2017-04-19 00:40:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if valid {
|
2016-04-28 06:08:06 +02:00
|
|
|
return next(c)
|
|
|
|
}
|
2018-01-30 06:02:11 +02:00
|
|
|
break
|
2016-04-28 06:08:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-03 06:12:06 +02:00
|
|
|
|
2018-02-21 20:44:17 +02:00
|
|
|
realm := defaultRealm
|
|
|
|
if config.Realm != defaultRealm {
|
2017-02-21 06:27:40 +02:00
|
|
|
realm = strconv.Quote(config.Realm)
|
|
|
|
}
|
|
|
|
|
2016-04-28 16:09:33 +02:00
|
|
|
// Need to return `401` for browsers to pop-up login box.
|
2017-02-21 06:27:40 +02:00
|
|
|
c.Response().Header().Set(echo.HeaderWWWAuthenticate, basic+" realm="+realm)
|
2016-04-28 16:09:33 +02:00
|
|
|
return echo.ErrUnauthorized
|
2016-04-28 06:08:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|