Files
FlowReader/internal/handler/admin.go
T
Antigravity AgentandClaude Opus 5.5 03e57e4308 fix(backend): harden security and speed up feeds and article API
Security:
- WebSocket events are routed to their owner only (no cross-user leak);
  hub close is idempotent (fixes double-close panic), adds ping/pong and
  write deadlines.
- Session tokens stored as SHA-256 (migration 008 keeps sessions valid);
  single-query auth middleware puts the user in the request context.
- Client IP only trusts X-Forwarded-For from TRUSTED_PROXIES; rate limiter
  map is bounded; per-user limit on AI summaries.
- Argon2id at OWASP minimum with a concurrency cap; constant-time login
  for unknown emails; atomic first-admin bootstrap; REGISTRATION_ENABLED.
- CSP/HSTS/COOP headers, same-origin guard on mutations, body size limits,
  wider SSRF denylist, bounded feed/page/AI response reads, generic errors.
- Upgrade chi, pgx, x/net, x/text, x/crypto (known CVEs); commit go.sum.

Performance:
- List endpoints return a plain-text excerpt and reading time instead of
  full HTML; content is sanitized once at ingest (legacy rows backfilled).
- Keyset pagination on (sort_at, id) with matching partial indexes;
  redundant indexes dropped (migration 007).
- Fetcher: bounded worker pool, conditional GET (ETag/Last-Modified),
  exponential backoff, dedupe before insert, column-safe truncation,
  retention-aware ingest, per-user refresh coalescing.
- Read/favorite/read-all are single ownership-scoped statements.
- gzip compression, immutable caching for hashed assets, path-safe SPA
  handler, server timeouts; expired sessions purged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 07:34:08 +02:00

78 lines
2.2 KiB
Go

package handler
import (
"net/http"
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
"github.com/michael/flowreader/internal/domain"
"github.com/michael/flowreader/internal/service"
)
// AdminHandler handles administrative tasks.
type AdminHandler struct {
userRepo domain.UserRepository
authService *service.AuthService
}
// NewAdminHandler creates a new admin handler.
func NewAdminHandler(userRepo domain.UserRepository, authService *service.AuthService) *AdminHandler {
return &AdminHandler{
userRepo: userRepo,
authService: authService,
}
}
// ListUsers handles GET /api/v1/admin/users
func (h *AdminHandler) ListUsers(w http.ResponseWriter, r *http.Request) {
users, err := h.userRepo.List()
if err != nil {
respondError(w, http.StatusInternalServerError, "Failed to list users")
return
}
respondJSON(w, http.StatusOK, users)
}
// DeleteUser handles DELETE /api/v1/admin/users/{id}
func (h *AdminHandler) DeleteUser(w http.ResponseWriter, r *http.Request) {
userID, err := uuid.Parse(chi.URLParam(r, "id"))
if err != nil {
respondError(w, http.StatusBadRequest, "Invalid user ID")
return
}
// Prevent an admin from deleting their own account (lockout / accidental
// self-removal). The AdminOnly middleware has already verified admin rights.
if current := currentUser(r); current != nil && current.ID == userID {
respondError(w, http.StatusForbidden, "You cannot delete your own account")
return
}
// Logic to delete user and all associated data
if err := h.userRepo.Delete(userID); err != nil {
respondError(w, http.StatusInternalServerError, "Failed to delete user")
return
}
respondJSON(w, http.StatusOK, map[string]string{"message": "User deleted successfully"})
}
// AdminOnly middleware restricts access to admins. Must run after RequireAuth.
func (h *AdminHandler) AdminOnly(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user := currentUser(r)
if user == nil {
respondError(w, http.StatusUnauthorized, "Not authenticated")
return
}
if user.Role != domain.RoleAdmin {
respondError(w, http.StatusForbidden, "Admin access required")
return
}
next.ServeHTTP(w, r)
})
}