2015-05-11 15:43:54 -07:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
2015-07-05 23:21:05 +10:00
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
2015-05-11 15:43:54 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2016-03-31 16:30:19 -07:00
|
|
|
// BasicAuthConfig defines the config for HTTP basic auth middleware.
|
2016-03-14 13:55:38 -07:00
|
|
|
BasicAuthConfig struct {
|
2016-03-23 22:56:28 -05:00
|
|
|
// AuthFunc is the function to validate basic auth credentials.
|
2016-03-14 13:55:38 -07:00
|
|
|
AuthFunc BasicAuthFunc
|
2016-02-17 21:01:47 -08:00
|
|
|
}
|
|
|
|
|
2016-03-19 15:47:20 -07:00
|
|
|
// BasicAuthFunc defines a function to validate basic auth credentials.
|
2016-02-17 21:01:47 -08:00
|
|
|
BasicAuthFunc func(string, string) bool
|
2015-05-11 15:43:54 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-02-07 23:02:37 -08:00
|
|
|
basic = "Basic"
|
2015-05-11 15:43:54 -07:00
|
|
|
)
|
|
|
|
|
2016-03-14 13:55:38 -07:00
|
|
|
var (
|
2016-03-19 15:47:20 -07:00
|
|
|
// DefaultBasicAuthConfig is the default basic auth middleware config.
|
2016-03-14 13:55:38 -07:00
|
|
|
DefaultBasicAuthConfig = BasicAuthConfig{}
|
|
|
|
)
|
|
|
|
|
2016-03-19 15:47:20 -07:00
|
|
|
// BasicAuth returns an HTTP basic auth middleware.
|
2015-06-09 20:06:51 -07:00
|
|
|
//
|
|
|
|
// For valid credentials it calls the next handler.
|
2015-05-21 14:02:29 -07:00
|
|
|
// For invalid credentials, it sends "401 - Unauthorized" response.
|
2016-03-18 20:02:02 -07:00
|
|
|
func BasicAuth(f BasicAuthFunc) echo.MiddlewareFunc {
|
2016-03-14 13:55:38 -07:00
|
|
|
c := DefaultBasicAuthConfig
|
2016-03-18 20:02:02 -07:00
|
|
|
c.AuthFunc = f
|
2016-04-07 21:20:50 -07:00
|
|
|
return BasicAuthWithConfig(c)
|
2016-03-14 13:55:38 -07:00
|
|
|
}
|
|
|
|
|
2016-04-07 21:20:50 -07:00
|
|
|
// BasicAuthWithConfig returns an HTTP basic auth middleware from config.
|
2016-03-19 15:47:20 -07:00
|
|
|
// See `BasicAuth()`.
|
2016-04-07 21:20:50 -07:00
|
|
|
func BasicAuthWithConfig(config BasicAuthConfig) echo.MiddlewareFunc {
|
2016-04-02 14:19:39 -07:00
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
2016-04-06 07:28:53 -07:00
|
|
|
auth := c.Request().Header().Get(echo.HeaderAuthorization)
|
2016-02-17 21:49:31 -08:00
|
|
|
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
|
2016-03-14 13:55:38 -07:00
|
|
|
if config.AuthFunc(cred[:i], cred[i+1:]) {
|
2016-04-02 14:19:39 -07:00
|
|
|
return next(c)
|
2016-02-17 21:49:31 -08:00
|
|
|
}
|
2015-05-11 15:43:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-06 07:28:53 -07:00
|
|
|
c.Response().Header().Set(echo.HeaderWWWAuthenticate, basic+" realm=Restricted")
|
2016-03-12 10:18:40 -08:00
|
|
|
return echo.ErrUnauthorized
|
2016-04-02 14:19:39 -07:00
|
|
|
}
|
2016-02-17 21:49:31 -08:00
|
|
|
}
|
2015-06-09 20:06:51 -07:00
|
|
|
}
|