This commit is contained in:
greeneclipse committed 2026-10-14 16:13:51 +02:00
1 parent 7ab7acd533
commit 176b1fe47f
18 files changed
+423 -31

No files matched your search

+57 -3
View File
@@ -6,11 +6,65 @@ import (
"strings"
"github.com/descrybe/descrybe-v2/apps/api/internal/auth"
"github.com/descrybe/descrybe-v2/apps/api/internal/billing"
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
)
// allowAPIKeyManager authorizes API-key management using the SAME per-member
// settings.api_keys permission as the dashboard and RequireMemberFeature.
// Members with the area disabled cannot create, rename or revoke keys. Company
// admins, owners and platform admins keep their existing privileges.
// This check is deliberately repeated at the handler: direct calls must not
// bypass the router's member-permission middleware.
func (s *Server) allowAPIKeyManager(w http.ResponseWriter, r *http.Request) bool {
if CompanyAdminAllowed(r.Context()) {
return true
}
uid, okUser := UserIDFromContext(r.Context())
cid, okCompany := CompanyIDFromContext(r.Context())
if !okUser || !okCompany {
Error(w, http.StatusUnauthorized, "unauthorized")
return false
}
isPlatformAdmin, err := s.checkPlatformAdmin(r.Context(), uid)
if err != nil {
Error(w, http.StatusInternalServerError, "authorization check failed")
return false
}
if isPlatformAdmin {
return true
}
isOwner, err := s.checkCompanyOwner(r.Context(), uid)
if err != nil {
Error(w, http.StatusInternalServerError, "authorization check failed")
return false
}
if isOwner {
return true
}
// Ordinary members need both the company's API entitlement and their
// effective per-member grant. A permissions lookup error fails closed.
if s.Billing == nil {
Error(w, http.StatusForbidden, "API keys permission required")
return false
}
if !s.requireFeatures(w, r, "settings.api_keys", "capability.api_access") {
return false
}
perms, err := s.Billing.MemberPermissions(r.Context(), cid, uid)
if err != nil {
Error(w, http.StatusServiceUnavailable, "permissions temporarily unavailable")
return false
}
if billing.MemberDeniesFeature(perms, "settings.api_keys") {
Error(w, http.StatusForbidden, "API keys permission required")
return false
}
return true
}
func (s *Server) handleListAPIKeys(w http.ResponseWriter, r *http.Request) {
cid, _ := CompanyIDFromContext(r.Context())
limit, offset := ParseLimitOffset(r)
@@ -49,7 +103,7 @@ func (s *Server) handleListAPIKeys(w http.ResponseWriter, r *http.Request) {
func (s *Server) handleCreateAPIKey(w http.ResponseWriter, r *http.Request) {
cid, _ := CompanyIDFromContext(r.Context())
uid, _ := UserIDFromContext(r.Context())
if !s.allowCompanyAdminOrPlatform(w, r) {
if !s.allowAPIKeyManager(w, r) {
return
}
if !s.requireFeatures(w, r, "settings.api_keys", "capability.api_access") {
@@ -85,7 +139,7 @@ func (s *Server) handleCreateAPIKey(w http.ResponseWriter, r *http.Request) {
func (s *Server) handleUpdateAPIKey(w http.ResponseWriter, r *http.Request) {
cid, _ := CompanyIDFromContext(r.Context())
if !s.allowCompanyAdminOrPlatform(w, r) {
if !s.allowAPIKeyManager(w, r) {
return
}
if !s.requireFeatures(w, r, "settings.api_keys", "capability.api_access") {
@@ -140,7 +194,7 @@ func (s *Server) handleUpdateAPIKey(w http.ResponseWriter, r *http.Request) {
func (s *Server) handleRevokeAPIKey(w http.ResponseWriter, r *http.Request) {
cid, _ := CompanyIDFromContext(r.Context())
if !s.allowCompanyAdminOrPlatform(w, r) {
if !s.allowAPIKeyManager(w, r) {
return
}
id, err := uuid.Parse(chi.URLParam(r, "id"))