Files
weave-scope/app/multitenant/user.go
Marc Carré 2ba50b8b3d Update golang.org/x/net/context to latest
```
$ gvt delete golang.org/x/net/context
$ gvt fetch golang.org/x/net/context
2018/07/23 18:03:49 Fetching: golang.org/x/net/context
$ git grep -l "golang.org/x/net/context" | grep -v vendor | xargs sed -i '' 's:golang.org/x/net/context:context:g'
$ git grep -l "context/ctxhttp" | grep -v vendor | xargs sed -i '' 's:context/ctxhttp:golang.org/x/net/context/ctxhttp:g'
$ gofmt -s -w app
$ gofmt -s -w common
$ gofmt -s -w probe
$ gofmt -s -w prog
$ gofmt -s -w tools
```
fixed a bunch of:
```
cannot use func literal (type func("github.com/weaveworks/scope/vendor/golang.org/x/net/context".Context) error) as type func("context".Context) error
```
2018-07-23 20:10:18 +02:00

38 lines
910 B
Go

package multitenant
import (
"fmt"
"net/http"
"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
}