Skip to content
This repository was archived by the owner on Aug 31, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions tmpauth/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"path"
Expand Down Expand Up @@ -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/") {
Expand All @@ -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")
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 != "" {
Expand All @@ -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
}
2 changes: 2 additions & 0 deletions tmpauth/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

type CachedToken struct {
StateID string
RawToken string
UserDescriptor string
CachedHeaders map[string]string
Expiry time.Time
Expand Down Expand Up @@ -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,
Expand Down