traefik/pkg/middlewares/auth/basic_auth.go

115 lines
2.6 KiB
Go
Raw Normal View History

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"
"github.com/opentracing/opentracing-go/ext"
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"
"github.com/traefik/traefik/v3/pkg/tracing"
2018-11-14 09:18:03 +00:00
)
const (
basicTypeName = "BasicAuth"
)
type basicAuth struct {
next http.Handler
auth *goauth.BasicAuth
users map[string]string
headerField string
removeHeader bool
name string
}
// NewBasic creates a basicAuth middleware.
func NewBasic(ctx context.Context, next http.Handler, authConfig dynamic.BasicAuth, name string) (http.Handler, error) {
2022-11-21 17:36:05 +00:00
middlewares.GetLogger(ctx, name, basicTypeName).Debug().Msg("Creating middleware")
2018-11-14 09:18:03 +00:00
users, err := getUsers(authConfig.UsersFile, authConfig.Users, basicUserParser)
if err != nil {
return nil, err
}
ba := &basicAuth{
next: next,
users: users,
headerField: authConfig.HeaderField,
removeHeader: authConfig.RemoveHeader,
name: name,
}
realm := defaultRealm
if len(authConfig.Realm) > 0 {
realm = authConfig.Realm
}
ba.auth = &goauth.BasicAuth{Realm: realm, Secrets: ba.secretBasic}
2018-11-14 09:18:03 +00:00
return ba, nil
}
func (b *basicAuth) GetTracingInformation() (string, ext.SpanKindEnum) {
return b.name, tracing.SpanKindNoneEnum
}
func (b *basicAuth) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
2022-11-21 17:36:05 +00:00
logger := middlewares.GetLogger(req.Context(), b.name, basicTypeName)
2018-11-14 09:18:03 +00:00
user, password, ok := req.BasicAuth()
if ok {
secret := b.auth.Secrets(user, b.auth.Realm)
if secret == "" || !goauth.CheckSecret(password, secret) {
ok = false
}
}
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")
2018-11-14 09:18:03 +00:00
tracing.SetErrorWithEvent(req, "Authentication failed")
2018-11-14 09:18:03 +00:00
b.auth.RequireAuth(rw, req)
return
}
2018-11-14 09:18:03 +00:00
2022-11-21 17:36:05 +00:00
logger.Debug().Msg("Authentication succeeded")
req.URL.User = url.User(user)
2018-11-14 09:18:03 +00:00
if b.headerField != "" {
req.Header[b.headerField] = []string{user}
}
2018-11-14 09:18:03 +00:00
if b.removeHeader {
2022-11-21 17:36:05 +00:00
logger.Debug().Msg("Removing authorization header")
req.Header.Del(authorizationHeader)
2018-11-14 09:18:03 +00:00
}
b.next.ServeHTTP(rw, req)
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
}