The admin panel lists every published status page, and every suspended page, organization or account, with the organization and owner behind it, searchable by title, address, organization or email. Each can be suspended with a reason, reinstated, or deleted. A suspended page is not served; status page servers drop it when they next ask. A suspended organization's monitors leave the schedule, its pages go offline, heartbeat pings and Alertmanager webhooks are no longer recorded, and its members and API tokens get 403 org_suspended; the console explains why. Reinstating restarts heartbeat waits so they do not fire at once. A suspended account is signed out everywhere and can neither sign in nor use its tokens; only someone who knows the password learns of the suspension. Platform admins cannot be suspended or deleted. Account deletion shares its logic with the GDPR erasure, including the refusal while the account owns shared organizations. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
223 lines
6.2 KiB
Go
223 lines
6.2 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/auth"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/authz"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/tenant"
|
|
)
|
|
|
|
// tokenPrefix makes tokens recognizable to secret scanners and to humans.
|
|
const tokenPrefix = "ust_"
|
|
|
|
// HashToken returns the stored form of an API or agent token.
|
|
func HashToken(tok string) []byte {
|
|
sum := sha256.Sum256([]byte(tok))
|
|
return sum[:]
|
|
}
|
|
|
|
func bearerToken(c *gin.Context) string {
|
|
h := c.GetHeader("Authorization")
|
|
if len(h) < 7 || !strings.EqualFold(h[:7], "bearer ") {
|
|
return ""
|
|
}
|
|
return strings.TrimSpace(h[7:])
|
|
}
|
|
|
|
func (s *Server) tokenPrincipal(ctx context.Context, tok string) (*principal, int, string, error) {
|
|
if !strings.HasPrefix(tok, tokenPrefix) {
|
|
return nil, http.StatusUnauthorized, "unauthenticated", nil
|
|
}
|
|
hash := HashToken(tok)
|
|
var org uuid.UUID
|
|
err := s.db.Tx(ctx, tenant.Global(), func(q *store.Queries) error {
|
|
var err error
|
|
org, err = q.APITokenOrg(ctx, hash)
|
|
return err
|
|
})
|
|
if err != nil {
|
|
return nil, 0, "", err
|
|
}
|
|
if org == uuid.Nil {
|
|
return nil, http.StatusUnauthorized, "unauthenticated", nil
|
|
}
|
|
p := &principal{}
|
|
var (
|
|
row store.ApiToken
|
|
member bool
|
|
)
|
|
err = s.db.Tx(ctx, tenant.Org(org, uuid.Nil), func(q *store.Queries) error {
|
|
var err error
|
|
if row, err = q.GetAPITokenByHash(ctx, hash); err != nil {
|
|
return err
|
|
}
|
|
if row.ExpiresAt != nil && !row.ExpiresAt.After(time.Now()) {
|
|
return nil
|
|
}
|
|
if p.Org, err = q.GetOrg(ctx, org); err != nil {
|
|
return err
|
|
}
|
|
if p.Grants, member, err = loadGrants(ctx, q, org, row.UserID); err != nil || !member {
|
|
return err
|
|
}
|
|
return q.TouchAPIToken(ctx, row.ID)
|
|
})
|
|
if errors.Is(err, pgx.ErrNoRows) || (err == nil && !member) {
|
|
return nil, http.StatusUnauthorized, "unauthenticated", nil
|
|
}
|
|
if err != nil {
|
|
return nil, 0, "", err
|
|
}
|
|
if p.Org.SuspendedAt != nil {
|
|
return nil, http.StatusForbidden, "org_suspended", nil
|
|
}
|
|
// Tokens act for their creator and stop with the creator's account.
|
|
if u, err := s.loadUser(ctx, row.UserID); err != nil || u.DisabledAt != nil {
|
|
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, 0, "", err
|
|
}
|
|
return nil, http.StatusUnauthorized, "unauthenticated", nil
|
|
}
|
|
p.UserID, p.TokenID = row.UserID, row.ID
|
|
p.Grants = p.Grants.Restrict(authz.NewSet(row.Permissions...))
|
|
return p, 0, "", nil
|
|
}
|
|
|
|
type tokenView struct {
|
|
ID uuid.UUID `json:"id"`
|
|
Name string `json:"name"`
|
|
Prefix string `json:"prefix"`
|
|
UserID uuid.UUID `json:"user_id"`
|
|
UserEmail string `json:"user_email"`
|
|
Permissions []string `json:"permissions"`
|
|
ExpiresAt *time.Time `json:"expires_at"`
|
|
LastUsedAt *time.Time `json:"last_used_at"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
}
|
|
|
|
func (s *Server) listTokens(c *gin.Context) {
|
|
p := current(c)
|
|
ctx := c.Request.Context()
|
|
// Members see their own tokens; tokens.manage reveals everyone's so the
|
|
// owner can audit and revoke them.
|
|
var filter *uuid.UUID
|
|
if !p.Grants.Can(authz.TokensManage) {
|
|
filter = &p.UserID
|
|
}
|
|
var rows []store.ListAPITokensRow
|
|
err := s.db.Tx(ctx, p.scope(), func(q *store.Queries) error {
|
|
var err error
|
|
rows, err = q.ListAPITokens(ctx, store.ListAPITokensParams{OrgID: p.Org.ID, UserID: filter})
|
|
return err
|
|
})
|
|
if err != nil {
|
|
internalError(c, err)
|
|
return
|
|
}
|
|
out := make([]tokenView, len(rows))
|
|
for i, r := range rows {
|
|
out[i] = tokenView{
|
|
ID: r.ID, Name: r.Name, Prefix: r.Prefix, UserID: r.UserID, UserEmail: r.UserEmail,
|
|
Permissions: r.Permissions, ExpiresAt: r.ExpiresAt, LastUsedAt: r.LastUsedAt, CreatedAt: r.CreatedAt,
|
|
}
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"tokens": out})
|
|
}
|
|
|
|
type createTokenRequest struct {
|
|
Name string `json:"name" binding:"required,max=100"`
|
|
Permissions []string `json:"permissions" binding:"required,min=1,max=50,dive,max=50"`
|
|
// ExpiresInDays of 0 creates a token that does not expire, unless the
|
|
// org limits token lifetime.
|
|
ExpiresInDays int `json:"expires_in_days" binding:"min=0,max=3650"`
|
|
}
|
|
|
|
func (s *Server) createToken(c *gin.Context) {
|
|
var req createTokenRequest
|
|
if !bind(c, &req) {
|
|
return
|
|
}
|
|
p := current(c)
|
|
perms := authz.NewSet(req.Permissions...)
|
|
if len(perms) == 0 {
|
|
fail(c, http.StatusUnprocessableEntity, "invalid_permissions")
|
|
return
|
|
}
|
|
if !p.Grants.CanGrant(perms, nil) {
|
|
fail(c, http.StatusForbidden, "permission_escalation")
|
|
return
|
|
}
|
|
if limit := p.Org.ApiTokenMaxDays; limit != nil && (req.ExpiresInDays == 0 || req.ExpiresInDays > int(*limit)) {
|
|
fail(c, http.StatusUnprocessableEntity, "token_lifetime_exceeds_policy", "Count", int(*limit))
|
|
return
|
|
}
|
|
var expires *time.Time
|
|
if req.ExpiresInDays > 0 {
|
|
t := time.Now().AddDate(0, 0, req.ExpiresInDays)
|
|
expires = &t
|
|
}
|
|
tok := tokenPrefix + auth.RandomToken()
|
|
ctx := c.Request.Context()
|
|
var row store.ApiToken
|
|
err := s.db.Tx(ctx, p.scope(), func(q *store.Queries) error {
|
|
var err error
|
|
row, err = q.CreateAPIToken(ctx, store.CreateAPITokenParams{
|
|
OrgID: p.Org.ID, UserID: p.UserID, Name: strings.TrimSpace(req.Name),
|
|
TokenHash: HashToken(tok), Prefix: tok[:len(tokenPrefix)+6],
|
|
Permissions: perms.Strings(), ExpiresAt: expires,
|
|
})
|
|
return err
|
|
})
|
|
if err != nil {
|
|
internalError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusCreated, gin.H{"id": row.ID, "token": tok, "prefix": row.Prefix, "permissions": row.Permissions})
|
|
}
|
|
|
|
func (s *Server) deleteToken(c *gin.Context) {
|
|
id, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
fail(c, http.StatusNotFound, "not_found")
|
|
return
|
|
}
|
|
p := current(c)
|
|
ctx := c.Request.Context()
|
|
var found bool
|
|
err = s.db.Tx(ctx, p.scope(), func(q *store.Queries) error {
|
|
row, err := q.GetAPIToken(ctx, id)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if row.UserID != p.UserID && !p.Grants.Can(authz.TokensManage) {
|
|
return nil
|
|
}
|
|
found = true
|
|
_, err = q.DeleteAPIToken(ctx, id)
|
|
return err
|
|
})
|
|
if err != nil {
|
|
internalError(c, err)
|
|
return
|
|
}
|
|
if !found {
|
|
fail(c, http.StatusNotFound, "not_found")
|
|
return
|
|
}
|
|
c.Status(http.StatusNoContent)
|
|
}
|