-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
480 lines (398 loc) · 12.3 KB
/
handler.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
package tmpauth
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"path"
"strings"
"time"
"github.com/dgrijalva/jwt-go"
)
// Path represents a URI path.
type Path string
// Matches checks to see if base matches p. The correct
// usage of this method sets p as the request path, and
// base as a Casketfile (user-defined) rule path.
//
// Path matching will probably not always be a direct
// comparison; this method assures that paths can be
// easily and consistently matched.
//
// Multiple slashes are collapsed/merged.
// Lifted from https://github.com/tmpim/casket/blob/v1.2.11/caskethttp/httpserver/path.go
// This code sample may be considered to be licensed under the Apache License 2.0
// which can be found at https://github.com/tmpim/casket/blob/master/LICENSE.txt
func (t *Tmpauth) Matches(urlPath, base string) bool {
if base == "/" || base == "" {
return true
}
// sanitize the paths for comparison, very important
// (slightly lossy if the base path requires multiple
// consecutive forward slashes, since those will be merged)
pHasTrailingSlash := strings.HasSuffix(string(urlPath), "/")
baseHasTrailingSlash := strings.HasSuffix(base, "/")
urlPath = path.Clean(string(urlPath))
base = path.Clean(base)
if pHasTrailingSlash {
urlPath += "/"
}
if baseHasTrailingSlash {
base += "/"
}
if t.Config.CaseSensitiveMatching {
return strings.HasPrefix(string(urlPath), base)
}
return strings.HasPrefix(strings.ToLower(string(urlPath)), strings.ToLower(base))
}
type StatusResponse struct {
Tmpauth bool `json:"tmpauth"`
ClientID string `json:"clientID"`
IsLoggedIn bool `json:"isLoggedIn"`
UserDescriptor json.RawMessage `json:"loggedInUser,omitempty"`
}
func (t *Tmpauth) serveStatus(w http.ResponseWriter, token *CachedToken) (int, error) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
resp := &StatusResponse{
Tmpauth: true,
ClientID: t.Config.ClientID,
IsLoggedIn: token != nil,
}
if token != nil {
resp.UserDescriptor = json.RawMessage(token.UserDescriptor)
}
json.NewEncoder(w).Encode(resp)
return 0, nil
}
func (t *Tmpauth) janitor() {
t.DebugLog("token cache janitor started")
ticker := time.NewTicker(2 * time.Minute)
for {
select {
case <-t.done:
t.DebugLog("stopping token cache janitor")
ticker.Stop()
return
case <-ticker.C:
t.DebugLog("running token cache janitor")
t.tokenCacheMutex.Lock()
now := time.Now()
for k, v := range t.TokenCache {
if now.After(v.RevalidateAt) {
delete(t.TokenCache, k)
}
}
t.tokenCacheMutex.Unlock()
t.stateIDMutex.Lock()
now = time.Now()
for k, v := range t.stateIDCache {
if now.After(v.ExpiresAt) {
delete(t.stateIDCache, k)
}
}
t.stateIDMutex.Unlock()
}
}
}
func (t *Tmpauth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
if len(t.Config.Headers) > 0 {
for header := range t.Config.Headers {
r.Header.Del(header)
}
}
statusRequested := false
whomstRequested := false
if t.Matches(r.URL.Path, "/.well-known/tmpauth/") {
if t.miniServerHost != "" {
u, err := url.Parse(t.miniServerHost)
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("parse mini server host: %w", err)
}
u.Path = r.URL.Path
u.RawQuery = r.URL.RawQuery
req, err := http.NewRequest(r.Method, u.String(), r.Body)
if err != nil {
return http.StatusInternalServerError, err
}
req.Header = r.Header
req.Header.Set(ConfigIDHeader, t.miniConfigID)
t.DebugLog(fmt.Sprintf("proxying request to mini server: %s", u.String()))
resp, err := t.miniClient(req, 0)
if err != nil {
return http.StatusInternalServerError, err
}
defer resp.Body.Close()
for k, v := range resp.Header {
w.Header()[k] = v
}
w.WriteHeader(resp.StatusCode)
_, err = io.Copy(w, resp.Body)
return 0, err
}
switch strings.TrimPrefix(r.URL.Path, "/.well-known/tmpauth/") {
case "callback":
return t.authCallback(w, r)
case "status":
statusRequested = true
break
case "whomst":
whomstRequested = true
break
default:
return http.StatusBadRequest, fmt.Errorf("tmpauth: no such path")
}
}
// determine if auth is required
authRequired := true
// If the URL path is weird, it signals a possible attack attempt.
// always require authentication in such a condition.
if path.Clean(r.URL.Path) == r.URL.Path {
if len(t.Config.Except) > 0 {
for _, exempt := range t.Config.Except {
if t.Matches(r.URL.Path, exempt) {
authRequired = false
break
}
}
} else if len(t.Config.Include) > 0 {
found := false
for _, included := range t.Config.Include {
if t.Matches(r.URL.Path, included) {
found = true
break
}
}
if !found {
authRequired = false
}
}
} else {
t.DebugLog(fmt.Sprintf("url path is suspicious, authentication being mandated: %v != %v",
path.Clean(r.URL.Path), r.URL.Path))
}
t.DebugLog(fmt.Sprintf("auth requirement for %q: %v", r.URL.Path, authRequired))
cachedToken, err := t.authFromCookie(r)
if err != nil {
t.DebugLog(fmt.Sprintf("failed to get JWT token: %v", err))
if _, err := r.Cookie(t.CookieName()); err != http.ErrNoCookie {
t.DebugLog("cookie exists and deemed to be invalid, requesting client to delete cookie")
http.SetCookie(w, &http.Cookie{
Name: t.CookieName(),
Value: "",
MaxAge: -1,
Path: "/",
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteLaxMode,
})
}
// Not authed, return an empty status or whomst response if requested
if statusRequested {
return t.serveStatus(w, nil)
} else if whomstRequested {
return t.serveWhomst(w, nil)
}
// Begin auth flow
if authRequired {
return t.StartAuth(w, r)
}
} else if len(t.Config.Headers) > 0 {
err := t.SetHeaders(cachedToken, r.Header)
if err != nil {
t.DebugLog(fmt.Sprintf("failed to set headers: %v", err))
return http.StatusPreconditionRequired, fmt.Errorf("tmpauth: missing required header value")
}
}
// Token is available (authenticated, but not necessarily allowed), serve the status response if requested
if statusRequested {
return t.serveStatus(w, cachedToken)
}
userAuthorized := false
if len(t.Config.AllowedUsers) > 0 {
t.DebugLog(fmt.Sprintf("checking if user is allowed on allowed users list: %v", cachedToken.UserIDs))
userIDs := make(map[string]bool)
for _, userID := range cachedToken.UserIDs {
userIDs[userID] = true
}
for _, allowedUser := range t.Config.AllowedUsers {
if userIDs[allowedUser] {
userAuthorized = true
break
}
}
} else {
userAuthorized = true
}
if !userAuthorized {
t.DebugLog("user not on allowed users list")
return http.StatusForbidden, fmt.Errorf("tmpauth: user not in allowed list")
}
// Now serve the whomst response if requested (authenticated and authorized)
if whomstRequested {
return t.serveWhomst(w, cachedToken)
}
return t.Next(w, r)
}
func (t *Tmpauth) StartAuth(w http.ResponseWriter, r *http.Request) (int, error) {
if t.miniServerHost != "" {
req, err := http.NewRequest(http.MethodGet, t.miniServerHost+"/start-auth", nil)
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("invalid mini server request: %w", err)
}
req.Header.Set(ConfigIDHeader, t.miniConfigID)
req.Header.Set(RequestURIHeader, r.RequestURI)
req.Header.Set(HostHeader, r.Host)
req.Header.Set("Content-Type", "application/jwt")
resp, err := t.miniClient(req, 0)
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("StartAuth on mini server: %w", err)
}
defer resp.Body.Close()
for k, v := range resp.Header {
w.Header()[k] = v
}
w.WriteHeader(resp.StatusCode)
_, err = io.Copy(w, resp.Body)
return 0, err
}
now := time.Now()
expiry := time.Now().Add(5 * time.Minute)
tokenID := generateTokenID()
host := t.Config.Host
if host.Host == "" {
var err error
host, err = url.Parse("https://" + r.Header.Get("Host"))
if err != nil {
t.DebugLog(fmt.Sprintf("could not determine host: %v", err))
return http.StatusInternalServerError, errors.New("tmpauth: could not determine host")
}
}
token, err := jwt.NewWithClaims(jwt.SigningMethodHS256, &stateClaims{
CallbackURL: "https://" + host.Host + host.Path + "/.well-known/tmpauth/callback",
StandardClaims: jwt.StandardClaims{
Id: tokenID,
Issuer: TmpAuthHost + ":server:" + t.Config.ClientID,
Audience: TmpAuthHost + ":central:state",
IssuedAt: now.Unix(),
NotBefore: now.Unix(),
ExpiresAt: expiry.Unix(),
},
}).SignedString(t.Config.Secret)
if err != nil {
t.DebugLog(fmt.Sprintf("failed to sign state token: %v", err))
return http.StatusInternalServerError, errors.New("tmpauth: failed to start authentication")
}
requestURI := r.URL.RequestURI()
t.stateIDMutex.Lock()
t.stateIDCache[tokenID] = &StateIDSession{
RedirectURI: requestURI,
ExpiresAt: time.Now().Add(time.Minute * 5),
}
t.stateIDMutex.Unlock()
// store request URIs in cookies sometimes in case this is a distributed
// casket instance or something and it'll still work in most cases
if len(requestURI) <= 128 {
http.SetCookie(w, &http.Cookie{
Name: t.StateIDCookieName(tokenID),
Value: url.QueryEscape(requestURI),
Expires: expiry,
Path: "/",
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteLaxMode,
})
} else {
http.SetCookie(w, &http.Cookie{
Name: t.StateIDCookieName(tokenID),
Value: "ok",
Expires: expiry,
Path: "/",
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteLaxMode,
})
}
queryParams := url.Values{
"state": []string{token},
"client_id": []string{t.Config.ClientID},
"method": []string{"tmpauth"},
}
http.Redirect(w, r, "https://"+TmpAuthHost+"/auth?"+queryParams.Encode(), http.StatusSeeOther)
return 0, nil
}
// authFromCookie attempts to get the auth token from the cookie or the X-Tmpauth-Token header, and returns the
// cachedToken (if it was successfully parsed), and any error.
func (t *Tmpauth) authFromCookie(r *http.Request) (*CachedToken, error) {
token := r.Header.Get("X-Tmpauth-Token")
if token != "" {
return t.ParseWrappedAuthJWT(token)
}
cookie, err := r.Cookie(t.CookieName())
if err != nil {
return nil, err
}
return t.ParseWrappedAuthJWT(cookie.Value)
}
// serveWhomst returns the entire whomst database if the user is logged in.
func (t *Tmpauth) serveWhomst(w http.ResponseWriter, token *CachedToken) (int, error) {
// If the user is not logged in, return an error
if token == nil {
return http.StatusUnauthorized, fmt.Errorf("tmpauth: must be logged in to retrieve whomst database")
}
whomstData, err := t.Whomst(token)
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("tmpauth: failed to retrieve whomst data: %w", err)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(whomstData)
return 0, nil
}
func (t *Tmpauth) Whomst(token *CachedToken) (map[string]json.RawMessage, error) {
var resp *http.Response
var respErr error
if t.miniServerHost != "" {
req, err := http.NewRequest(http.MethodGet, t.miniServerHost+"/tmpauth/whomst", nil)
if err != nil {
return nil, fmt.Errorf("invalid mini server request: %w", err)
}
req.Header.Set(TokenHeader, token.InnerToken)
resp, respErr = t.miniClient(req, 0)
} else {
resp, respErr = t.HttpClient.Get("https://" + TmpAuthHost + "/whomst/tmpauth/db?token=" +
url.QueryEscape(token.InnerToken))
}
if respErr != nil {
return nil, respErr
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("tmpauth: status code %d", resp.StatusCode)
}
var whomst map[string]json.RawMessage
err := json.NewDecoder(resp.Body).Decode(&whomst)
if err != nil {
return nil, err
}
return whomst, nil
}
// Stdlib returns a http.Handler compatible version of the Tmpauth middleware.
func (t *Tmpauth) Stdlib() *TmpauthStdlib {
return &TmpauthStdlib{tmpauth: t}
}
type TmpauthStdlib struct {
tmpauth *Tmpauth
}
func (t *TmpauthStdlib) ServeHTTP(w http.ResponseWriter, r *http.Request) {
code, err := t.tmpauth.ServeHTTP(w, r)
if err != nil {
if code == 0 {
code = http.StatusInternalServerError
}
w.WriteHeader(code)
t.tmpauth.DebugLog(fmt.Sprintf("tmpauth error: %+v", err))
}
}