/
middleware.go
194 lines (166 loc) · 5.72 KB
/
middleware.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package handlers
import (
"context"
"fmt"
"net/http"
rts "github.com/ory/keto/proto/ory/keto/relation_tuples/v1alpha2"
kratosClient "github.com/ory/kratos-client-go"
"github.com/pluralsh/trace-shield/consts"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/propagation"
)
// A private key for context that only this package can access. This is important
// to prevent collisions between different context uses
var userCtxKey = &contextKey{"user"}
type contextKey struct {
name string
}
type AccountType string
const (
TypeServiceAccount AccountType = "ServiceAccount"
TypeUser AccountType = "User"
)
// A stand-in for our backed user object
type User struct {
// Type AccountType
// Username string
Id string
Name string
Email string
// Groups []string
// ProfileImage *string
// JWT *string
KratosSession *kratosClient.Session
IsAdmin bool
}
// Middleware decodes the share session cookie and packs the session into context
func (h *Handler) Middleware() func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log := h.Log.WithName("Middleware")
// ctx := h.Propagators.Extract(r.Context(), propagation.HeaderCarrier(r.Header))
ctx := r.Context()
ctx, span := h.C.Tracer.Start(ctx, "AuthenticationMiddleware")
defer span.End()
cookie, err := r.Cookie("ory_kratos_session")
// Allow unauthenticated users in
if err != nil && err != http.ErrNoCookie {
log.Error(err, "Error when getting cookie")
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
next.ServeHTTP(w, r)
return
}
sessionHeader := r.Header.Get("X-Session-Token")
// log.Info(fmt.Sprintf("Cookie: %s", cookie.String()))
var resp *kratosClient.Session
if cookie != nil {
clientResp, req, err := h.C.KratosPublicClient.FrontendApi.ToSession(ctx).Cookie(cookie.String()).Execute()
if err != nil {
// TODO: should we return here?
log.Error(err, fmt.Sprintf("Error when calling `V0alpha2Api.ToSession``: %v\n", err))
log.Error(err, fmt.Sprintf("Full HTTP response: %v\n", req))
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
next.ServeHTTP(w, r) // TODO: find proper way to handle unauthenticated response?
return
}
resp = clientResp
} else if sessionHeader != "" {
clientResp, req, err := h.C.KratosPublicClient.FrontendApi.ToSession(ctx).XSessionToken(sessionHeader).Execute()
if err != nil {
// TODO: should we return here?
log.Error(err, fmt.Sprintf("Error when calling `V0alpha2Api.ToSession``: %v\n", err))
log.Error(err, fmt.Sprintf("Full HTTP response: %v\n", req))
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
next.ServeHTTP(w, r) // TODO: find proper way to handle unauthenticated response?
return
}
resp = clientResp
} else {
// TODO: re-enable below once we have a proper health and/or metrics endpoint
// err := fmt.Errorf("No session cookie or header found")
// log.Error(err, "Error while authenticating user", "path", r.URL.Path, "method", r.Method, "request", r)
// span.RecordError(err)
// span.SetStatus(codes.Error, err.Error())
next.ServeHTTP(w, r)
return
}
// response from `ToSession`: Session
log.Info(fmt.Sprintf("%s", resp.Identity.Traits))
var email string
var name string
if val, ok := resp.Identity.Traits.(map[string]interface{})["email"]; ok {
if foundEmail, ok := val.(string); ok {
email = foundEmail
} else {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
log.Error(err, "Error when parsing email")
}
}
if val, ok := resp.Identity.Traits.(map[string]interface{})["name"]; ok {
first, firstFound := val.(map[string]interface{})["first"]
last, lastFound := val.(map[string]interface{})["last"]
if firstName, ok := first.(string); ok {
if lastName, ok := last.(string); ok {
if firstFound && lastFound {
name = firstName + " " + lastName
}
}
}
}
user := &User{
KratosSession: resp,
Email: email,
Name: name,
Id: resp.Identity.Id,
}
// user.Type = TypeUser
// user.Username = strings.Replace(strings.Split(user.Email, "@")[0], ".", "-", -1)
user.IsAdmin = false
adminQuery := rts.RelationTuple{
Namespace: consts.OrganizationNamespace.String(),
Object: consts.MainOrganizationName,
Relation: consts.OrganizationPermissionAdmin.String(),
Subject: rts.NewSubjectSet(
consts.UserNamespace.String(),
resp.Identity.Id,
"",
),
}
isAdmin, err := h.C.KetoClient.Check(ctx, &adminQuery)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
log.Error(err, "Error when checking if user is admin")
}
log.Info("Admin check", "user", user.Email, "admin", isAdmin)
user.IsAdmin = isAdmin
if span.IsRecording() {
span.SetAttributes(
attribute.String("user_id", user.Id),
attribute.String("email", user.Email),
)
}
// for _, adminEmail := range kubricksConfig.Spec.Admins {
// if adminEmail == email {
// user.IsAdmin = true
// }
// }
ctx = context.WithValue(ctx, userCtxKey, user)
// and call the next with our new context
log.Info("Success auth", "user", user.Email)
r = r.WithContext(ctx)
h.Propagators.Inject(ctx, propagation.HeaderCarrier(r.Header))
next.ServeHTTP(w, r)
})
}
}
// ForContext finds the user from the context. REQUIRES Middleware to have run.
func ForContext(ctx context.Context) *User {
raw, _ := ctx.Value(userCtxKey).(*User)
return raw
}