-
Notifications
You must be signed in to change notification settings - Fork 323
/
middleware.go
274 lines (241 loc) · 8.14 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package graph
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
firebase "firebase.google.com/go"
"firebase.google.com/go/auth"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
"github.com/golang-jwt/jwt/v4"
"github.com/samber/lo"
log "github.com/sirupsen/logrus"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/go-oauth2/oauth2/v4"
e "github.com/pkg/errors"
"github.com/highlight-run/highlight/backend/model"
"github.com/highlight-run/highlight/backend/oauth"
"github.com/highlight-run/highlight/backend/util"
)
type APITokenHandler func(ctx context.Context, apiKey string) (*int, error)
var (
AuthClient Client
OAuthServer *oauth.Server
workspaceTokenHandler APITokenHandler
)
var HighlightAdminEmailDomains = []string{"@highlight.run", "@highlight.io"}
type AuthMode = string
const (
Simple AuthMode = "Simple"
Firebase AuthMode = "Firebase"
Password AuthMode = "Password"
)
func GetEnvAuthMode() AuthMode {
if strings.EqualFold(os.Getenv("REACT_APP_AUTH_MODE"), Simple) {
return Simple
}
if strings.EqualFold(os.Getenv("REACT_APP_AUTH_MODE"), Password) {
return Password
}
return Firebase
}
type Client interface {
updateContextWithAuthenticatedUser(ctx context.Context, token string) (context.Context, error)
GetUser(ctx context.Context, uid string) (*auth.UserRecord, error)
}
type SimpleAuthClient struct{}
type PasswordAuthClient struct{}
type FirebaseAuthClient struct {
AuthClient *auth.Client
}
func (c *FirebaseAuthClient) GetUser(ctx context.Context, uid string) (*auth.UserRecord, error) {
return c.AuthClient.GetUser(ctx, uid)
}
func SetupAuthClient(ctx context.Context, authMode AuthMode, oauthServer *oauth.Server, wsTokenHandler APITokenHandler) {
OAuthServer = oauthServer
workspaceTokenHandler = wsTokenHandler
if authMode == Firebase {
secret := os.Getenv("FIREBASE_SECRET")
creds, err := google.CredentialsFromJSON(context.Background(), []byte(secret),
"https://www.googleapis.com/auth/firebase",
"https://www.googleapis.com/auth/identitytoolkit",
"https://www.googleapis.com/auth/userinfo.email")
if err != nil {
log.WithContext(ctx).Errorf("error converting credentials from json: %v", err)
return
}
app, err := firebase.NewApp(context.Background(), nil, option.WithCredentials(creds))
if err != nil {
log.WithContext(ctx).Errorf("error initializing firebase app: %v", err)
return
}
// create a client to communicate with firebase project
var client *auth.Client
if client, err = app.Auth(context.Background()); err != nil {
log.WithContext(ctx).Errorf("error creating firebase client: %v", err)
return
}
AuthClient = &FirebaseAuthClient{AuthClient: client}
} else if authMode == Simple {
AuthClient = &SimpleAuthClient{}
} else if authMode == Password {
AuthClient = &PasswordAuthClient{}
} else {
log.WithContext(ctx).Fatalf("private graph auth client configured with unknown auth mode")
}
}
func (c *SimpleAuthClient) GetUser(_ context.Context, uid string) (*auth.UserRecord, error) {
return &auth.UserRecord{
UserInfo: GetPasswordAuthUser(uid),
EmailVerified: true,
}, nil
}
func authenticateToken(tokenString string) (jwt.MapClaims, error) {
claims := jwt.MapClaims{}
_, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
return []byte(JwtAccessSecret), nil
})
if err != nil {
return claims, e.Wrap(err, "invalid id token")
}
exp, ok := claims["exp"]
if !ok {
return claims, e.Wrap(err, "invalid exp claim")
}
expClaim := int64(exp.(float64))
if time.Now().After(time.Unix(expClaim, 0)) {
return claims, e.Wrap(err, "token expired")
}
return claims, nil
}
func (c *PasswordAuthClient) GetUser(_ context.Context, uid string) (*auth.UserRecord, error) {
return &auth.UserRecord{
UserInfo: GetPasswordAuthUser(uid),
EmailVerified: true,
}, nil
}
func (c *PasswordAuthClient) updateContextWithAuthenticatedUser(ctx context.Context, token string) (context.Context, error) {
var uid string
email := ""
if token != "" {
claims, err := authenticateToken(token)
if err != nil {
return ctx, err
}
email = claims["email"].(string)
uid = claims["uid"].(string)
}
ctx = context.WithValue(ctx, model.ContextKeys.UID, uid)
ctx = context.WithValue(ctx, model.ContextKeys.Email, email)
return ctx, nil
}
func (c *SimpleAuthClient) updateContextWithAuthenticatedUser(ctx context.Context, token string) (context.Context, error) {
ctx = context.WithValue(ctx, model.ContextKeys.UID, "demo@example.com")
ctx = context.WithValue(ctx, model.ContextKeys.Email, "demo@example.com")
return ctx, nil
}
func (c *FirebaseAuthClient) updateContextWithAuthenticatedUser(ctx context.Context, token string) (context.Context, error) {
var uid string
email := ""
if token != "" {
t, err := c.AuthClient.VerifyIDToken(context.Background(), token)
if err != nil {
return ctx, e.Wrap(err, "invalid id token")
}
uid = t.UID
if userRecord, err := c.AuthClient.GetUser(context.Background(), uid); err == nil {
email = userRecord.Email
// This is to prevent attackers from impersonating Highlight staff.
_, isAdmin := lo.Find(HighlightAdminEmailDomains, func(domain string) bool { return strings.Contains(email, domain) })
if isAdmin && !userRecord.EmailVerified {
email = ""
}
}
}
ctx = context.WithValue(ctx, model.ContextKeys.UID, uid)
ctx = context.WithValue(ctx, model.ContextKeys.Email, email)
return ctx, nil
}
func getSourcemapRequestToken(r *http.Request) string {
body, err := io.ReadAll(r.Body)
// put the body back so that graphql can also read it
r.Body = io.NopCloser(bytes.NewBuffer(body))
if err != nil {
return ""
}
var graphqlQuery struct {
Query string
Variables struct {
APIKey string `json:"api_key"`
}
}
err = json.Unmarshal(body, &graphqlQuery)
if err != nil {
return ""
}
return graphqlQuery.Variables.APIKey
}
func PrivateMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
span, ctx := util.StartSpanFromContext(ctx, "middleware.private")
defer span.Finish()
var err error
if token := r.Header.Get("token"); token != "" {
span.SetAttribute("type", "tokenHeader")
ctx, err = AuthClient.updateContextWithAuthenticatedUser(ctx, token)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
} else if apiKey := r.Header.Get("ApiKey"); apiKey != "" {
span.SetAttribute("type", "apiKeyHeader")
workspaceID, err := workspaceTokenHandler(ctx, apiKey)
if err != nil || workspaceID == nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
} else if sourcemapRequestToken := getSourcemapRequestToken(r); sourcemapRequestToken != "" {
span.SetAttribute("type", "sourcemapBody")
workspaceID, err := workspaceTokenHandler(ctx, sourcemapRequestToken)
if err != nil || workspaceID == nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
} else if OAuthServer.HasCookie(r) || OAuthServer.HasBearer(r) {
span.SetAttribute("type", "oauth")
var cookie *http.Cookie
var tokenInfo oauth2.TokenInfo
ctx, tokenInfo, cookie, err = OAuthServer.Validate(ctx, r)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
http.SetCookie(w, cookie)
span.SetAttribute("client_id", tokenInfo.GetClientID())
span.SetAttribute("user_id", tokenInfo.GetUserID())
}
ctx = context.WithValue(ctx, model.ContextKeys.AcceptEncoding, r.Header.Get("Accept-Encoding"))
r = r.WithContext(ctx)
next.ServeHTTP(w, r)
})
}
func WebsocketInitializationFunction() transport.WebsocketInitFunc {
return transport.WebsocketInitFunc(func(socketContext context.Context, initPayload transport.InitPayload) (context.Context, *transport.InitPayload, error) {
token := ""
if initPayload["token"] != nil {
token = fmt.Sprintf("%v", initPayload["token"])
}
ctx, err := AuthClient.updateContextWithAuthenticatedUser(socketContext, token)
if err != nil {
log.WithContext(ctx).Errorf("Unable to authenticate/initialize websocket: %s", err.Error())
}
return ctx, &initPayload, err
})
}