-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
37 lines (30 loc) · 910 Bytes
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
}