diff --git a/tmpauth/handler.go b/tmpauth/handler.go index 09baed9..1cd42c7 100644 --- a/tmpauth/handler.go +++ b/tmpauth/handler.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "io" "net/http" "net/url" "path" @@ -80,6 +81,7 @@ func (t *Tmpauth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) } statusRequested := false + whomstRequested := false if httpserver.Path(r.URL.Path).Matches("/.well-known/tmpauth/") { switch strings.TrimPrefix(r.URL.Path, "/.well-known/tmpauth/") { @@ -88,6 +90,9 @@ func (t *Tmpauth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) case "status": statusRequested = true break + case "whomst": + whomstRequested = true + break default: return http.StatusBadRequest, fmt.Errorf("tmpauth: no such path") } @@ -143,10 +148,14 @@ func (t *Tmpauth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) }) } + // Not authed, return an empty status or whomst response if requested if statusRequested { return t.serveStatus(w, r, nil) + } else if whomstRequested { + return t.serveWhomst(w, nil) } + // Begin auth flow if authRequired { return t.startAuth(w, r) } @@ -158,6 +167,7 @@ func (t *Tmpauth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) } } + // Token is available (authenticated, but not necessarily allowed), serve the status response if requested if statusRequested { return t.serveStatus(w, r, cachedToken) } @@ -185,6 +195,11 @@ func (t *Tmpauth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) 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.ServeHTTP(w, r) } @@ -248,6 +263,8 @@ func (t *Tmpauth) startAuth(w http.ResponseWriter, r *http.Request) (int, error) 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 != "" { @@ -261,3 +278,36 @@ func (t *Tmpauth) authFromCookie(r *http.Request) (*CachedToken, error) { return t.parseWrappedAuthJWT(cookie.Value) } + +type WhomstResponse struct { + Whomst json.RawMessage `json:"whomst"` +} + +// 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") + } + + // Fetch the whomst database from tmpauth using the user's token + resp, err := t.HttpClient.Get("https://" + TmpAuthHost + "/whomst/tmpauth/db?token=" + url.QueryEscape(token.RawToken)) + if err != nil { + return http.StatusInternalServerError, fmt.Errorf("tmpauth: failed to create request: %w", err) + } + + if resp.StatusCode != http.StatusOK { + resp.Body.Close() + return http.StatusInternalServerError, fmt.Errorf("tmpauth: got non OK response when retrieving whomst database: %v", resp.Status) + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + + _, err = io.Copy(w, resp.Body) + if err != nil { + return http.StatusInternalServerError, fmt.Errorf("tmpauth: failed to write response: %w", err) + } + + return 0, nil +} diff --git a/tmpauth/token.go b/tmpauth/token.go index c63c1e2..85f51ee 100644 --- a/tmpauth/token.go +++ b/tmpauth/token.go @@ -16,6 +16,7 @@ import ( type CachedToken struct { StateID string + RawToken string UserDescriptor string CachedHeaders map[string]string Expiry time.Time @@ -174,6 +175,7 @@ func (t *Tmpauth) parseAuthJWT(tokenStr string, minValidationTime time.Time) (*C cachedToken := &CachedToken{ UserDescriptor: string(descriptor), + RawToken: tokenStr, CachedHeaders: make(map[string]string), Expiry: expiry, RevalidateAt: revalidateAt,