Merge pull request #1403 from dmcgowan/auth-const-keys

Update auth context keys to use constant
This commit is contained in:
Aaron Lehmann 2016-02-01 16:29:07 -08:00
commit e10efb2ecc
6 changed files with 18 additions and 7 deletions

View file

@ -39,6 +39,16 @@ import (
"github.com/docker/distribution/context" "github.com/docker/distribution/context"
) )
const (
// UserKey is used to get the user object from
// a user context
UserKey = "auth.user"
// UserNameKey is used to get the user name from
// a user context
UserNameKey = "auth.user.name"
)
// UserInfo carries information about // UserInfo carries information about
// an autenticated/authorized client. // an autenticated/authorized client.
type UserInfo struct { type UserInfo struct {
@ -102,9 +112,9 @@ type userInfoContext struct {
func (uic userInfoContext) Value(key interface{}) interface{} { func (uic userInfoContext) Value(key interface{}) interface{} {
switch key { switch key {
case "auth.user": case UserKey:
return uic.user return uic.user
case "auth.user.name": case UserNameKey:
return uic.user.Name return uic.user.Name
} }

View file

@ -56,7 +56,7 @@ func TestBasicAccessController(t *testing.T) {
} }
} }
userInfo, ok := authCtx.Value("auth.user").(auth.UserInfo) userInfo, ok := authCtx.Value(auth.UserKey).(auth.UserInfo)
if !ok { if !ok {
t.Fatal("basic accessController did not set auth.user context") t.Fatal("basic accessController did not set auth.user context")
} }

View file

@ -29,7 +29,7 @@ func TestSillyAccessController(t *testing.T) {
} }
} }
userInfo, ok := authCtx.Value("auth.user").(auth.UserInfo) userInfo, ok := authCtx.Value(auth.UserKey).(auth.UserInfo)
if !ok { if !ok {
t.Fatal("silly accessController did not set auth.user context") t.Fatal("silly accessController did not set auth.user context")
} }

View file

@ -376,7 +376,7 @@ func TestAccessController(t *testing.T) {
t.Fatalf("accessController returned unexpected error: %s", err) t.Fatalf("accessController returned unexpected error: %s", err)
} }
userInfo, ok := authCtx.Value("auth.user").(auth.UserInfo) userInfo, ok := authCtx.Value(auth.UserKey).(auth.UserInfo)
if !ok { if !ok {
t.Fatal("token accessController did not set auth.user context") t.Fatal("token accessController did not set auth.user context")
} }

View file

@ -597,7 +597,7 @@ func (app *App) dispatcher(dispatch dispatchFunc) http.Handler {
} }
// Add username to request logging // Add username to request logging
context.Context = ctxu.WithLogger(context.Context, ctxu.GetLogger(context.Context, "auth.user.name")) context.Context = ctxu.WithLogger(context.Context, ctxu.GetLogger(context.Context, auth.UserNameKey))
if app.nameRequired(r) { if app.nameRequired(r) {
nameRef, err := reference.ParseNamed(getName(context)) nameRef, err := reference.ParseNamed(getName(context))

View file

@ -10,6 +10,7 @@ import (
"github.com/docker/distribution/digest" "github.com/docker/distribution/digest"
"github.com/docker/distribution/registry/api/errcode" "github.com/docker/distribution/registry/api/errcode"
"github.com/docker/distribution/registry/api/v2" "github.com/docker/distribution/registry/api/v2"
"github.com/docker/distribution/registry/auth"
"golang.org/x/net/context" "golang.org/x/net/context"
) )
@ -77,7 +78,7 @@ func getUploadUUID(ctx context.Context) (uuid string) {
// getUserName attempts to resolve a username from the context and request. If // getUserName attempts to resolve a username from the context and request. If
// a username cannot be resolved, the empty string is returned. // a username cannot be resolved, the empty string is returned.
func getUserName(ctx context.Context, r *http.Request) string { func getUserName(ctx context.Context, r *http.Request) string {
username := ctxu.GetStringValue(ctx, "auth.user.name") username := ctxu.GetStringValue(ctx, auth.UserNameKey)
// Fallback to request user with basic auth // Fallback to request user with basic auth
if username == "" { if username == "" {