Skip to content
This repository was archived by the owner on Aug 31, 2026. It is now read-only.
Merged
Changes from 1 commit
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
Next Next commit
feat(tmpauth): endpoint for whomst db
  • Loading branch information
Lemmmy committed Jul 28, 2024
commit 0006aecb02d32291311f12de5868bbe93162f9ea
72 changes: 67 additions & 5 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 @@ -125,7 +130,7 @@ func (t *Tmpauth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)

t.DebugLog("auth requirement for %q: %v", r.URL.Path, authRequired)

cachedToken, err := t.authFromCookie(r)
cachedToken, tokenStr, err := t.authFromCookie(r)
if err != nil {
t.DebugLog("failed to get JWT token: %v", err)

Expand All @@ -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, "")
}

// 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, tokenStr)
}

return t.Next.ServeHTTP(w, r)
}

Expand Down Expand Up @@ -248,16 +263,63 @@ func (t *Tmpauth) startAuth(w http.ResponseWriter, r *http.Request) (int, error)
return 0, nil
}

func (t *Tmpauth) authFromCookie(r *http.Request) (*CachedToken, error) {
// 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), the token string (if it existed), and any error.
func (t *Tmpauth) authFromCookie(r *http.Request) (*CachedToken, string, error) {
token := r.Header.Get("X-Tmpauth-Token")
if token != "" {
return t.parseWrappedAuthJWT(token)
cachedToken, err := t.parseWrappedAuthJWT(token)
return cachedToken, token, err
}

cookie, err := r.Cookie(t.CookieName())
if err != nil {
return nil, err
return nil, "", err
}

return t.parseWrappedAuthJWT(cookie.Value)
cachedToken, err := t.parseWrappedAuthJWT(cookie.Value)
return cachedToken, cookie.Value, err
}

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, tokenStr string) (int, error) {
// If the user is not logged in, return an error
if tokenStr == "" {
return http.StatusUnauthorized, fmt.Errorf("tmpauth: must be logged in to retrieve whomst database")
}

// Fetch the inner token from the wrapped token (slow)
wTokenRaw, err := jwt.ParseWithClaims(tokenStr, &wrappedToken{
clientID: t.Config.ClientID,
}, t.VerifyWithSecret)
if err != nil {
return http.StatusUnauthorized, fmt.Errorf("tmpauth: failed to parse wrapped token: %w", err)
}

wToken := wTokenRaw.Claims.(*wrappedToken)

// Fetch the whomst database from tmpauth using the user's token
resp, err := t.HttpClient.Get("https://" + TmpAuthHost + "/whomst/tmpauth/db?token=" + url.QueryEscape(wToken.Token))
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
}