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 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
Prev Previous commit
refactor: cache the raw tokens
  • Loading branch information
Lemmmy committed Jul 28, 2024
commit f41246dd7b8f138a2598070c7730af7bc43eae82
34 changes: 11 additions & 23 deletions tmpauth/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,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, tokenStr, err := t.authFromCookie(r)
cachedToken, err := t.authFromCookie(r)
if err != nil {
t.DebugLog("failed to get JWT token: %v", err)

Expand All @@ -152,7 +152,7 @@ func (t *Tmpauth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)
if statusRequested {
return t.serveStatus(w, r, nil)
} else if whomstRequested {
return t.serveWhomst(w, "")
return t.serveWhomst(w, nil)
}

// Begin auth flow
Expand Down Expand Up @@ -197,7 +197,7 @@ func (t *Tmpauth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)

// Now serve the whomst response if requested (authenticated and authorized)
if whomstRequested {
return t.serveWhomst(w, tokenStr)
return t.serveWhomst(w, cachedToken)
}

return t.Next.ServeHTTP(w, r)
Expand Down Expand Up @@ -264,46 +264,34 @@ func (t *Tmpauth) startAuth(w http.ResponseWriter, r *http.Request) (int, 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) {
// 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 != "" {
cachedToken, err := t.parseWrappedAuthJWT(token)
return cachedToken, token, err
return t.parseWrappedAuthJWT(token)
}

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

cachedToken, err := t.parseWrappedAuthJWT(cookie.Value)
return cachedToken, cookie.Value, err
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, tokenStr string) (int, error) {
func (t *Tmpauth) serveWhomst(w http.ResponseWriter, token *CachedToken) (int, error) {
// If the user is not logged in, return an error
if tokenStr == "" {
if token == nil {
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))
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)
}
Expand Down
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