2018-11-14 09:18:03 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
goauth "github.com/abbot/go-http-auth"
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/config/dynamic"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/middlewares"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/middlewares/accesslog"
|
2024-03-12 08:48:04 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/middlewares/observability"
|
2024-01-08 08:10:06 +00:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2024-10-10 08:36:04 +00:00
|
|
|
"golang.org/x/sync/singleflight"
|
2018-11-14 09:18:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-01-08 08:10:06 +00:00
|
|
|
typeNameBasic = "BasicAuth"
|
2018-11-14 09:18:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type basicAuth struct {
|
|
|
|
next http.Handler
|
|
|
|
auth *goauth.BasicAuth
|
|
|
|
users map[string]string
|
|
|
|
headerField string
|
|
|
|
removeHeader bool
|
|
|
|
name string
|
2024-10-10 08:36:04 +00:00
|
|
|
|
|
|
|
checkSecret func(password, secret string) bool
|
|
|
|
singleflightGroup *singleflight.Group
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBasic creates a basicAuth middleware.
|
2019-07-10 07:26:04 +00:00
|
|
|
func NewBasic(ctx context.Context, next http.Handler, authConfig dynamic.BasicAuth, name string) (http.Handler, error) {
|
2024-01-08 08:10:06 +00:00
|
|
|
middlewares.GetLogger(ctx, name, typeNameBasic).Debug().Msg("Creating middleware")
|
2022-11-21 17:36:05 +00:00
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
users, err := getUsers(authConfig.UsersFile, authConfig.Users, basicUserParser)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ba := &basicAuth{
|
2024-10-10 08:36:04 +00:00
|
|
|
next: next,
|
|
|
|
users: users,
|
|
|
|
headerField: authConfig.HeaderField,
|
|
|
|
removeHeader: authConfig.RemoveHeader,
|
|
|
|
name: name,
|
|
|
|
checkSecret: goauth.CheckSecret,
|
|
|
|
singleflightGroup: new(singleflight.Group),
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
realm := defaultRealm
|
|
|
|
if len(authConfig.Realm) > 0 {
|
|
|
|
realm = authConfig.Realm
|
|
|
|
}
|
2020-03-25 13:28:04 +00:00
|
|
|
|
|
|
|
ba.auth = &goauth.BasicAuth{Realm: realm, Secrets: ba.secretBasic}
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
return ba, nil
|
|
|
|
}
|
|
|
|
|
2024-01-08 08:10:06 +00:00
|
|
|
func (b *basicAuth) GetTracingInformation() (string, string, trace.SpanKind) {
|
|
|
|
return b.name, typeNameBasic, trace.SpanKindInternal
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *basicAuth) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
2024-01-08 08:10:06 +00:00
|
|
|
logger := middlewares.GetLogger(req.Context(), b.name, typeNameBasic)
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2020-06-18 14:02:04 +00:00
|
|
|
user, password, ok := req.BasicAuth()
|
|
|
|
if ok {
|
2024-10-10 08:36:04 +00:00
|
|
|
ok = b.checkPassword(user, password)
|
2020-06-18 14:02:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logData := accesslog.GetLogData(req)
|
|
|
|
if logData != nil {
|
|
|
|
logData.Core[accesslog.ClientUsername] = user
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Debug().Msg("Authentication failed")
|
2024-03-12 08:48:04 +00:00
|
|
|
observability.SetStatusErrorf(req.Context(), "Authentication failed")
|
2020-06-18 14:02:04 +00:00
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
b.auth.RequireAuth(rw, req)
|
2020-06-18 14:02:04 +00:00
|
|
|
return
|
|
|
|
}
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Debug().Msg("Authentication succeeded")
|
2020-06-18 14:02:04 +00:00
|
|
|
req.URL.User = url.User(user)
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2020-06-18 14:02:04 +00:00
|
|
|
if b.headerField != "" {
|
|
|
|
req.Header[b.headerField] = []string{user}
|
|
|
|
}
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2020-06-18 14:02:04 +00:00
|
|
|
if b.removeHeader {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Debug().Msg("Removing authorization header")
|
2020-06-18 14:02:04 +00:00
|
|
|
req.Header.Del(authorizationHeader)
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
2020-06-18 14:02:04 +00:00
|
|
|
b.next.ServeHTTP(rw, req)
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
2024-10-10 08:36:04 +00:00
|
|
|
func (b *basicAuth) checkPassword(user, password string) bool {
|
|
|
|
secret := b.auth.Secrets(user, b.auth.Realm)
|
|
|
|
if secret == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
key := password + secret
|
|
|
|
match, _, _ := b.singleflightGroup.Do(key, func() (any, error) {
|
|
|
|
return b.checkSecret(password, secret), nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return match.(bool)
|
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
func (b *basicAuth) secretBasic(user, realm string) string {
|
|
|
|
if secret, ok := b.users[user]; ok {
|
|
|
|
return secret
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func basicUserParser(user string) (string, string, error) {
|
|
|
|
split := strings.Split(user, ":")
|
|
|
|
if len(split) != 2 {
|
|
|
|
return "", "", fmt.Errorf("error parsing BasicUser: %v", user)
|
|
|
|
}
|
|
|
|
return split[0], split[1], nil
|
|
|
|
}
|