forked from streamingfast/dauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
headers.go
52 lines (39 loc) · 1.06 KB
/
headers.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package dauth
import (
"context"
"strings"
"google.golang.org/grpc/metadata"
)
const (
SFHeaderUserID string = "x-sf-user-id"
SFHeaderApiKeyID string = "x-sf-api-key-id"
SFHeaderIP string = "x-real-ip"
)
type TrustedHeaders map[string]string
type trustedHeadersKeyType int
const trustedHeadersKey trustedHeadersKeyType = iota
func WithTrustedHeaders(ctx context.Context, h TrustedHeaders) context.Context {
return context.WithValue(ctx, trustedHeadersKey, h)
}
func FromContext(ctx context.Context) TrustedHeaders {
val := ctx.Value(trustedHeadersKey)
if val == nil {
return nil
}
return val.(TrustedHeaders)
}
func (h TrustedHeaders) UserID() string {
return h[SFHeaderUserID]
}
func (h TrustedHeaders) APIKeyID() string {
return h[SFHeaderApiKeyID]
}
func (h TrustedHeaders) RealIP() string {
return h[SFHeaderIP]
}
func (h TrustedHeaders) Get(key string) string {
return h[strings.ToLower(key)]
}
func (h TrustedHeaders) ToOutgoingGRPCContext(ctx context.Context) context.Context {
return metadata.NewOutgoingContext(ctx, metadata.New(h))
}