Files
stpweb/backend/graph/context.go
adamfandClaude Opus 5.5 d383774e97
Test and deploy / test (push) Failing after 28s
Test and deploy / deploy (push) Has been skipped
Harden auth: same-origin check, live admin flag, 90-day refresh
- Refuse non-GET requests whose Origin (or Sec-Fetch-Site) is not the
  apex or www. SameSite=Lax lets sibling subdomains such as radar. and
  git. send the auth cookies. The WebSocket check shares the helper and
  no longer accepts localhost in production.
- Take the admin flag from the user row the token check already loads,
  so a demotion applies on the next request, not on token expiry.
- Cut the refresh token lifetime from 365 to 90 days; refreshes renew it.
- Docs: fix the stale revocation notes in backend/README.md and the
  Cloudflare Access comment in routes.caddy.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-26 16:12:01 +00:00

61 lines
1.3 KiB
Go

package graph
import (
"context"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v5"
)
// Resolvers get only a context.Context; AuthContextMiddleware puts the
// verified claims and the *gin.Context into it.
// Unrelated to the "userClaims" key handlers.AuthMiddleware sets on Gin's context.
type contextKey string
const (
userClaimsKey contextKey = "userClaims"
ginContextKey contextKey = "ginContext"
)
// UserClaimsFromCtx returns nil for anonymous requests; refusing is the
// directives' job.
func UserClaimsFromCtx(ctx context.Context) *jwt.MapClaims {
claims, ok := ctx.Value(userClaimsKey).(*jwt.MapClaims)
if !ok {
return nil
}
return claims
}
func UserIDFromCtx(ctx context.Context) (uint, bool) {
claims := UserClaimsFromCtx(ctx)
if claims == nil {
return 0, false
}
idF, ok := (*claims)["id"].(float64)
if !ok {
return 0, false
}
return uint(idF), true
}
// IsAdminFromCtx reads the claims, whose admin flag comes from the user row.
func IsAdminFromCtx(ctx context.Context) bool {
claims := UserClaimsFromCtx(ctx)
if claims == nil {
return false
}
admin, ok := (*claims)["admin"].(bool)
return ok && admin
}
// GinContextFromCtx is nil if the middleware wasn't installed.
func GinContextFromCtx(ctx context.Context) *gin.Context {
gc, ok := ctx.Value(ginContextKey).(*gin.Context)
if !ok {
return nil
}
return gc
}