@@ -61,7 +61,7 @@ type StatusResponse struct {
61
61
UserDescriptor json.RawMessage `json:"loggedInUser,omitempty"`
62
62
}
63
63
64
- func (t * Tmpauth ) serveStatus (w http.ResponseWriter , r * http. Request , token * CachedToken ) (int , error ) {
64
+ func (t * Tmpauth ) serveStatus (w http.ResponseWriter , token * CachedToken ) (int , error ) {
65
65
w .Header ().Set ("Content-Type" , "application/json" )
66
66
w .WriteHeader (http .StatusOK )
67
67
@@ -126,6 +126,7 @@ func (t *Tmpauth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)
126
126
}
127
127
128
128
statusRequested := false
129
+ whomstRequested := false
129
130
130
131
if t .Matches (r .URL .Path , "/.well-known/tmpauth/" ) {
131
132
if t .miniServerHost != "" {
@@ -170,6 +171,9 @@ func (t *Tmpauth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)
170
171
case "status" :
171
172
statusRequested = true
172
173
break
174
+ case "whomst" :
175
+ whomstRequested = true
176
+ break
173
177
default :
174
178
return http .StatusBadRequest , fmt .Errorf ("tmpauth: no such path" )
175
179
}
@@ -225,10 +229,14 @@ func (t *Tmpauth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)
225
229
})
226
230
}
227
231
232
+ // Not authed, return an empty status or whomst response if requested
228
233
if statusRequested {
229
- return t .serveStatus (w , r , nil )
234
+ return t .serveStatus (w , nil )
235
+ } else if whomstRequested {
236
+ return t .serveWhomst (w , nil )
230
237
}
231
238
239
+ // Begin auth flow
232
240
if authRequired {
233
241
return t .StartAuth (w , r )
234
242
}
@@ -240,8 +248,9 @@ func (t *Tmpauth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)
240
248
}
241
249
}
242
250
251
+ // Token is available (authenticated, but not necessarily allowed), serve the status response if requested
243
252
if statusRequested {
244
- return t .serveStatus (w , r , cachedToken )
253
+ return t .serveStatus (w , cachedToken )
245
254
}
246
255
247
256
userAuthorized := false
@@ -267,6 +276,11 @@ func (t *Tmpauth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)
267
276
return http .StatusForbidden , fmt .Errorf ("tmpauth: user not in allowed list" )
268
277
}
269
278
279
+ // Now serve the whomst response if requested (authenticated and authorized)
280
+ if whomstRequested {
281
+ return t .serveWhomst (w , cachedToken )
282
+ }
283
+
270
284
return t .Next (w , r )
271
285
}
272
286
@@ -374,6 +388,8 @@ func (t *Tmpauth) StartAuth(w http.ResponseWriter, r *http.Request) (int, error)
374
388
return 0 , nil
375
389
}
376
390
391
+ // authFromCookie attempts to get the auth token from the cookie or the X-Tmpauth-Token header, and returns the
392
+ // cachedToken (if it was successfully parsed), and any error.
377
393
func (t * Tmpauth ) authFromCookie (r * http.Request ) (* CachedToken , error ) {
378
394
token := r .Header .Get ("X-Tmpauth-Token" )
379
395
if token != "" {
@@ -388,6 +404,25 @@ func (t *Tmpauth) authFromCookie(r *http.Request) (*CachedToken, error) {
388
404
return t .ParseWrappedAuthJWT (cookie .Value )
389
405
}
390
406
407
+ // serveWhomst returns the entire whomst database if the user is logged in.
408
+ func (t * Tmpauth ) serveWhomst (w http.ResponseWriter , token * CachedToken ) (int , error ) {
409
+ // If the user is not logged in, return an error
410
+ if token == nil {
411
+ return http .StatusUnauthorized , fmt .Errorf ("tmpauth: must be logged in to retrieve whomst database" )
412
+ }
413
+
414
+ whomstData , err := t .Whomst ()
415
+ if err != nil {
416
+ return http .StatusInternalServerError , fmt .Errorf ("tmpauth: failed to retrieve whomst data: %w" , err )
417
+ }
418
+
419
+ w .Header ().Set ("Content-Type" , "application/json" )
420
+ w .WriteHeader (http .StatusOK )
421
+ json .NewEncoder (w ).Encode (whomstData )
422
+
423
+ return 0 , nil
424
+ }
425
+
391
426
func (t * Tmpauth ) Whomst () (map [string ]json.RawMessage , error ) {
392
427
var resp * http.Response
393
428
var respErr error
0 commit comments