/
identity.go
132 lines (109 loc) · 3.96 KB
/
identity.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package request
import (
"context"
"net/http"
)
// Header constants
const (
AuthHeaderKey = "Authorization"
DownloadServiceHeaderKey = "X-Download-Service-Token"
FlorenceHeaderKey = "X-Florence-Token"
UserHeaderKey = "User-Identity"
)
// Cookie constants
const (
FlorenceCookieKey = "access_token"
)
// Other identity-related constants
const (
DeprecatedAuthHeader = "Internal-Token"
LegacyUser = "legacyUser"
BearerPrefix = "Bearer "
)
// CheckRequester is an interface to allow mocking of auth.CheckRequest
type CheckRequester interface {
CheckRequest(*http.Request) (context.Context, int, error)
}
// IdentityResponse represents the response from the identity service
type IdentityResponse struct {
Identifier string `json:"identifier"`
}
// IsUserPresent determines if a user identity is present on the given context
func IsUserPresent(ctx context.Context) bool {
userIdentity := ctx.Value(UserIdentityKey)
return userIdentity != nil && userIdentity != ""
}
// IsFlorenceIdentityPresent determines if a florence identity is present on the given context
func IsFlorenceIdentityPresent(ctx context.Context) bool {
florenceID := ctx.Value(FlorenceIdentityKey)
return florenceID != nil && florenceID != ""
}
// AddUserHeader sets the given user ID on the given request
func AddUserHeader(r *http.Request, user string) {
r.Header.Add(UserHeaderKey, user)
}
// AddServiceTokenHeader sets the given service token on the given request
func AddServiceTokenHeader(r *http.Request, serviceToken string) {
if len(serviceToken) > 0 {
r.Header.Add(AuthHeaderKey, BearerPrefix+serviceToken)
}
}
// AddDownloadServiceTokenHeader sets the given download service token on the given request
func AddDownloadServiceTokenHeader(r *http.Request, serviceToken string) {
if len(serviceToken) > 0 {
r.Header.Add(DownloadServiceHeaderKey, serviceToken)
}
}
// User gets the user identity from the context
func User(ctx context.Context) string {
userIdentity, _ := ctx.Value(UserIdentityKey).(string)
return userIdentity
}
// SetUser sets the user identity on the context
func SetUser(ctx context.Context, user string) context.Context {
return context.WithValue(ctx, UserIdentityKey, user)
}
// SetFlorenceIdentity sets the florence identity for authentication
func SetFlorenceIdentity(ctx context.Context, user string) context.Context {
return context.WithValue(ctx, FlorenceIdentityKey, user)
}
// SetFlorenceHeader sets a florence Header if the corresponding Identity key is in context
func SetFlorenceHeader(ctx context.Context, r *http.Request) {
if IsFlorenceIdentityPresent(ctx) {
r.Header.Set(FlorenceHeaderKey, ctx.Value(FlorenceIdentityKey).(string))
}
}
// AddFlorenceHeader sets the given user access token (florence token) token on the given request
func AddFlorenceHeader(r *http.Request, userAccessToken string) {
if len(userAccessToken) > 0 {
r.Header.Add(FlorenceHeaderKey, userAccessToken)
}
}
// AddAuthHeaders sets authentication headers for request
func AddAuthHeaders(ctx context.Context, r *http.Request, serviceToken string) {
if IsUserPresent(ctx) {
AddUserHeader(r, User(ctx))
}
AddServiceTokenHeader(r, serviceToken)
}
// AddDeprecatedHeader sets the deprecated header on the given request
func AddDeprecatedHeader(r *http.Request, token string) {
if len(token) > 0 {
r.Header.Add(DeprecatedAuthHeader, token)
}
}
// IsCallerPresent determines if an identity is present on the given context.
func IsCallerPresent(ctx context.Context) bool {
callerIdentity := ctx.Value(CallerIdentityKey)
isPresent := callerIdentity != nil && callerIdentity != ""
return isPresent
}
// Caller gets the caller identity from the context
func Caller(ctx context.Context) string {
callerIdentity, _ := ctx.Value(CallerIdentityKey).(string)
return callerIdentity
}
// SetCaller sets the caller identity on the context
func SetCaller(ctx context.Context, caller string) context.Context {
return context.WithValue(ctx, CallerIdentityKey, caller)
}