2014-12-18 20:30:19 +00:00
|
|
|
// Package silly provides a simple authentication scheme that checks for the
|
|
|
|
// existence of an Authorization header and issues access if is present and
|
|
|
|
// non-empty.
|
|
|
|
//
|
|
|
|
// This package is present as an example implementation of a minimal
|
|
|
|
// auth.AccessController and for testing. This is not suitable for any kind of
|
|
|
|
// production security.
|
|
|
|
package silly
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2020-08-24 11:18:39 +00:00
|
|
|
"github.com/distribution/distribution/v3/registry/auth"
|
2023-11-18 06:50:40 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2014-12-18 20:30:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// accessController provides a simple implementation of auth.AccessController
|
|
|
|
// that simply checks for a non-empty Authorization header. It is useful for
|
|
|
|
// demonstration and testing.
|
|
|
|
type accessController struct {
|
|
|
|
realm string
|
|
|
|
service string
|
|
|
|
}
|
|
|
|
|
|
|
|
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 silly access controller`)
|
|
|
|
}
|
|
|
|
|
|
|
|
service, present := options["service"]
|
|
|
|
if _, ok := service.(string); !present || !ok {
|
|
|
|
return nil, fmt.Errorf(`"service" must be set for silly access controller`)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &accessController{realm: realm.(string), service: service.(string)}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Authorized simply checks for the existence of the authorization header,
|
|
|
|
// responding with a bearer challenge if it doesn't exist.
|
2023-10-24 20:41:54 +00:00
|
|
|
func (ac *accessController) Authorized(req *http.Request, accessRecords ...auth.Access) (*auth.Grant, error) {
|
2014-12-18 20:30:19 +00:00
|
|
|
if req.Header.Get("Authorization") == "" {
|
|
|
|
challenge := challenge{
|
|
|
|
realm: ac.realm,
|
|
|
|
service: ac.service,
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(accessRecords) > 0 {
|
|
|
|
var scopes []string
|
|
|
|
for _, access := range accessRecords {
|
2014-12-19 01:20:35 +00:00
|
|
|
scopes = append(scopes, fmt.Sprintf("%s:%s:%s", access.Type, access.Resource.Name, access.Action))
|
2014-12-18 20:30:19 +00:00
|
|
|
}
|
|
|
|
challenge.scope = strings.Join(scopes, " ")
|
|
|
|
}
|
|
|
|
|
2015-02-04 01:59:24 +00:00
|
|
|
return nil, &challenge
|
2014-12-18 20:30:19 +00:00
|
|
|
}
|
|
|
|
|
2023-10-24 20:41:54 +00:00
|
|
|
return &auth.Grant{User: auth.UserInfo{Name: "silly"}}, nil
|
2014-12-18 20:30:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type challenge struct {
|
|
|
|
realm string
|
|
|
|
service string
|
|
|
|
scope string
|
|
|
|
}
|
|
|
|
|
2015-07-24 02:39:56 +00:00
|
|
|
var _ auth.Challenge = challenge{}
|
|
|
|
|
|
|
|
// SetHeaders sets a simple bearer challenge on the response.
|
2017-03-13 23:35:15 +00:00
|
|
|
func (ch challenge) SetHeaders(r *http.Request, w http.ResponseWriter) {
|
2014-12-18 20:30:19 +00:00
|
|
|
header := fmt.Sprintf("Bearer realm=%q,service=%q", ch.realm, ch.service)
|
|
|
|
|
|
|
|
if ch.scope != "" {
|
|
|
|
header = fmt.Sprintf("%s,scope=%q", header, ch.scope)
|
|
|
|
}
|
|
|
|
|
2015-03-11 19:10:49 +00:00
|
|
|
w.Header().Set("WWW-Authenticate", header)
|
2014-12-18 20:30:19 +00:00
|
|
|
}
|
|
|
|
|
2015-07-24 02:39:56 +00:00
|
|
|
func (ch challenge) Error() string {
|
2014-12-18 20:30:19 +00:00
|
|
|
return fmt.Sprintf("silly authentication challenge: %#v", ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// init registers the silly auth backend.
|
|
|
|
func init() {
|
2023-11-18 06:50:40 +00:00
|
|
|
if err := auth.Register("silly", auth.InitFunc(newAccessController)); err != nil {
|
|
|
|
logrus.Errorf("failed to register silly auth: %v", err)
|
|
|
|
}
|
2014-12-18 20:30:19 +00:00
|
|
|
}
|