2015-04-21 19:57:12 +00:00
|
|
|
// Package basic provides a simple authentication scheme that checks for the
|
|
|
|
// user credential hash in an htpasswd formatted file in a configuration-determined
|
|
|
|
// location.
|
|
|
|
//
|
|
|
|
// This authentication method MUST be used under TLS, as simple token-replay attack is possible.
|
|
|
|
package basic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2015-06-11 02:29:27 +00:00
|
|
|
"os"
|
2015-04-21 19:57:12 +00:00
|
|
|
|
|
|
|
ctxu "github.com/docker/distribution/context"
|
|
|
|
"github.com/docker/distribution/registry/auth"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2015-06-09 01:56:48 +00:00
|
|
|
var (
|
|
|
|
// ErrInvalidCredential is returned when the auth token does not authenticate correctly.
|
|
|
|
ErrInvalidCredential = errors.New("invalid authorization credential")
|
|
|
|
|
|
|
|
// ErrAuthenticationFailure returned when authentication failure to be presented to agent.
|
|
|
|
ErrAuthenticationFailure = errors.New("authentication failured")
|
|
|
|
)
|
|
|
|
|
2015-04-21 19:57:12 +00:00
|
|
|
type accessController struct {
|
|
|
|
realm string
|
2015-06-06 05:37:32 +00:00
|
|
|
htpasswd *htpasswd
|
2015-04-21 19:57:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ auth.AccessController = &accessController{}
|
|
|
|
|
|
|
|
func newAccessController(options map[string]interface{}) (auth.AccessController, error) {
|
|
|
|
realm, present := options["realm"]
|
|
|
|
if _, ok := realm.(string); !present || !ok {
|
|
|
|
return nil, fmt.Errorf(`"realm" must be set for basic access controller`)
|
|
|
|
}
|
|
|
|
|
|
|
|
path, present := options["path"]
|
|
|
|
if _, ok := path.(string); !present || !ok {
|
|
|
|
return nil, fmt.Errorf(`"path" must be set for basic access controller`)
|
|
|
|
}
|
|
|
|
|
2015-06-11 02:29:27 +00:00
|
|
|
f, err := os.Open(path.(string))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
h, err := newHTPasswd(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &accessController{realm: realm.(string), htpasswd: h}, nil
|
2015-04-21 19:57:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ac *accessController) Authorized(ctx context.Context, accessRecords ...auth.Access) (context.Context, error) {
|
|
|
|
req, err := ctxu.GetRequest(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-06-09 01:56:48 +00:00
|
|
|
username, password, ok := req.BasicAuth()
|
|
|
|
if !ok {
|
|
|
|
return nil, &challenge{
|
2015-04-21 19:57:12 +00:00
|
|
|
realm: ac.realm,
|
2015-06-09 01:56:48 +00:00
|
|
|
err: ErrInvalidCredential,
|
2015-04-21 19:57:12 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-04 16:02:13 +00:00
|
|
|
|
2015-06-11 02:29:27 +00:00
|
|
|
if err := ac.htpasswd.authenticateUser(username, password); err != nil {
|
2015-06-09 01:56:48 +00:00
|
|
|
ctxu.GetLogger(ctx).Errorf("error authenticating user %q: %v", username, err)
|
|
|
|
return nil, &challenge{
|
2015-06-04 15:46:34 +00:00
|
|
|
realm: ac.realm,
|
2015-06-09 01:56:48 +00:00
|
|
|
err: ErrAuthenticationFailure,
|
2015-06-04 15:46:34 +00:00
|
|
|
}
|
2015-04-21 19:57:12 +00:00
|
|
|
}
|
|
|
|
|
2015-06-09 01:56:48 +00:00
|
|
|
return auth.WithUser(ctx, auth.UserInfo{Name: username}), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// challenge implements the auth.Challenge interface.
|
|
|
|
type challenge struct {
|
|
|
|
realm string
|
|
|
|
err error
|
2015-04-21 19:57:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ch *challenge) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2015-04-22 14:35:59 +00:00
|
|
|
header := fmt.Sprintf("Basic realm=%q", ch.realm)
|
2015-04-21 19:57:12 +00:00
|
|
|
w.Header().Set("WWW-Authenticate", header)
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ch *challenge) Error() string {
|
|
|
|
return fmt.Sprintf("basic authentication challenge: %#v", ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
auth.Register("basic", auth.InitFunc(newAccessController))
|
|
|
|
}
|