Files
weave-scope/app/multitenant/user.go
2016-03-23 15:41:37 +00:00

38 lines
927 B
Go

package multitenant
import (
"fmt"
"net/http"
"golang.org/x/net/context"
"github.com/weaveworks/scope/app"
)
// ErrUserIDNotFound should be returned by a UserIDer when it fails to ID the
// user for a request.
var ErrUserIDNotFound = fmt.Errorf("User ID not found")
// UserIDer identifies users given a request context.
type UserIDer func(context.Context) (string, error)
// UserIDHeader returns a UserIDer which a header by the supplied key.
func UserIDHeader(headerName string) UserIDer {
return func(ctx context.Context) (string, error) {
request, ok := ctx.Value(app.RequestCtxKey).(*http.Request)
if !ok || request == nil {
return "", ErrUserIDNotFound
}
userID := request.Header.Get(headerName)
if userID == "" {
return "", ErrUserIDNotFound
}
return userID, nil
}
}
// NoopUserIDer always returns the empty user ID.
func NoopUserIDer(context.Context) (string, error) {
return "", nil
}