2017-09-18 15:48:07 +00:00
|
|
|
package auth
|
2016-07-20 22:29:00 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-02-24 02:46:50 +00:00
|
|
|
"io/ioutil"
|
2016-12-30 08:21:13 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2017-08-25 16:22:03 +00:00
|
|
|
goauth "github.com/abbot/go-http-auth"
|
2016-09-23 16:27:01 +00:00
|
|
|
"github.com/containous/traefik/log"
|
2016-07-20 22:29:00 +00:00
|
|
|
"github.com/containous/traefik/types"
|
2017-07-19 10:02:51 +00:00
|
|
|
"github.com/urfave/negroni"
|
2016-07-20 22:29:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Authenticator is a middleware that provides HTTP basic and digest authentication
|
|
|
|
type Authenticator struct {
|
|
|
|
handler negroni.Handler
|
|
|
|
users map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAuthenticator builds a new Autenticator given a config
|
|
|
|
func NewAuthenticator(authConfig *types.Auth) (*Authenticator, error) {
|
|
|
|
if authConfig == nil {
|
|
|
|
return nil, fmt.Errorf("Error creating Authenticator: auth is nil")
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
authenticator := Authenticator{}
|
|
|
|
if authConfig.Basic != nil {
|
2017-02-24 02:46:50 +00:00
|
|
|
authenticator.users, err = parserBasicUsers(authConfig.Basic)
|
2016-07-20 22:29:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-25 16:22:03 +00:00
|
|
|
basicAuth := goauth.NewBasicAuthenticator("traefik", authenticator.secretBasic)
|
2016-07-20 22:29:00 +00:00
|
|
|
authenticator.handler = negroni.HandlerFunc(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
|
|
|
if username := basicAuth.CheckAuth(r); username == "" {
|
2017-05-26 15:03:14 +00:00
|
|
|
log.Debug("Basic auth failed...")
|
2016-07-20 22:29:00 +00:00
|
|
|
basicAuth.RequireAuth(w, r)
|
|
|
|
} else {
|
2017-05-26 15:03:14 +00:00
|
|
|
log.Debug("Basic auth success...")
|
2016-12-16 15:42:51 +00:00
|
|
|
if authConfig.HeaderField != "" {
|
|
|
|
r.Header[authConfig.HeaderField] = []string{username}
|
|
|
|
}
|
2016-07-20 22:29:00 +00:00
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else if authConfig.Digest != nil {
|
2017-02-24 02:46:50 +00:00
|
|
|
authenticator.users, err = parserDigestUsers(authConfig.Digest)
|
2016-07-20 22:29:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-25 16:22:03 +00:00
|
|
|
digestAuth := goauth.NewDigestAuthenticator("traefik", authenticator.secretDigest)
|
2016-07-20 22:29:00 +00:00
|
|
|
authenticator.handler = negroni.HandlerFunc(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
|
|
|
if username, _ := digestAuth.CheckAuth(r); username == "" {
|
2017-05-26 15:03:14 +00:00
|
|
|
log.Debug("Digest auth failed...")
|
2016-07-20 22:29:00 +00:00
|
|
|
digestAuth.RequireAuth(w, r)
|
|
|
|
} else {
|
2017-05-26 15:03:14 +00:00
|
|
|
log.Debug("Digest auth success...")
|
2016-12-16 15:42:51 +00:00
|
|
|
if authConfig.HeaderField != "" {
|
|
|
|
r.Header[authConfig.HeaderField] = []string{username}
|
|
|
|
}
|
2016-07-20 22:29:00 +00:00
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
})
|
2017-08-25 16:22:03 +00:00
|
|
|
} else if authConfig.Forward != nil {
|
|
|
|
authenticator.handler = negroni.HandlerFunc(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
2017-09-18 15:48:07 +00:00
|
|
|
Forward(authConfig.Forward, w, r, next)
|
2017-08-25 16:22:03 +00:00
|
|
|
})
|
2016-07-20 22:29:00 +00:00
|
|
|
}
|
|
|
|
return &authenticator, nil
|
|
|
|
}
|
|
|
|
|
2017-02-24 02:46:50 +00:00
|
|
|
func getLinesFromFile(filename string) ([]string, error) {
|
|
|
|
dat, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Trim lines and filter out blanks
|
|
|
|
rawLines := strings.Split(string(dat), "\n")
|
|
|
|
var filteredLines []string
|
|
|
|
for _, rawLine := range rawLines {
|
|
|
|
line := strings.TrimSpace(rawLine)
|
|
|
|
if line != "" {
|
|
|
|
filteredLines = append(filteredLines, line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filteredLines, nil
|
|
|
|
}
|
|
|
|
|
2016-07-20 22:29:00 +00:00
|
|
|
func (a *Authenticator) secretBasic(user, realm string) string {
|
|
|
|
if secret, ok := a.users[user]; ok {
|
|
|
|
return secret
|
|
|
|
}
|
|
|
|
log.Debugf("User not found: %s", user)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Authenticator) secretDigest(user, realm string) string {
|
|
|
|
if secret, ok := a.users[user+":"+realm]; ok {
|
|
|
|
return secret
|
|
|
|
}
|
|
|
|
log.Debugf("User not found: %s:%s", user, realm)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Authenticator) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
|
|
|
a.handler.ServeHTTP(rw, r, next)
|
|
|
|
}
|