Feat v0.7.7 API tokens + extension permissions (#61)

Personal access tokens (PATs) for programmatic API access with
SHA-256 hashing, permission scoping (git model), and Settings/Admin UI.
Extension-declared user permissions with dynamic registry, gate_permission
manifest field, permissions Starlark module, and grouped admin UI.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-02 18:20:16 +00:00
parent e02b13dc12
commit f32eefab14
34 changed files with 1769 additions and 58 deletions

View File

@@ -2,13 +2,12 @@ package handlers
import (
"context"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
@@ -384,8 +383,7 @@ func (h *AuthHandler) generateTokens(user *models.User, keepLogin bool) (gin.H,
}
func hashToken(token string) string {
h := sha256.Sum256([]byte(token))
return hex.EncodeToString(h[:])
return auth.HashToken(token)
}
// ── Vault Lifecycle ─────────────────────────
@@ -582,6 +580,60 @@ func BootstrapAdmin(cfg *config.Config, s store.Stores, uekCache ...*crypto.UEKC
log.Printf(" ✅ Admin user '%s' created", cfg.AdminUsername)
}
// BootstrapPAT creates a personal access token for the admin user when
// ARMATURE_BOOTSTRAP_PAT=true. Writes the token to /tmp/armature-admin-pat.txt
// for use by CI scripts. The token has all permissions and no expiry.
func BootstrapPAT(cfg *config.Config, s store.Stores) {
if os.Getenv("ARMATURE_BOOTSTRAP_PAT") != "true" {
return
}
if cfg.AdminUsername == "" || s.APITokens == nil {
return
}
ctx := context.Background()
admin, err := s.Users.GetByUsername(ctx, cfg.AdminUsername)
if err != nil || admin == nil {
log.Printf("⚠ BootstrapPAT: admin user not found")
return
}
// Check if a bootstrap token already exists
existing, _ := s.APITokens.ListForUser(ctx, admin.ID)
for _, t := range existing {
if t.Name == "bootstrap-ci" {
log.Printf(" Bootstrap PAT already exists (prefix: %s)", t.Prefix)
return
}
}
// Generate and store
raw, tokenHash, prefix := generateToken()
allPerms := auth.AllPermissions
permsCopy := make([]string, len(allPerms))
copy(permsCopy, allPerms)
token := &models.APIToken{
UserID: admin.ID,
Name: "bootstrap-ci",
TokenHash: tokenHash,
Prefix: prefix,
Permissions: permsCopy,
CreatedBy: &admin.ID,
}
if err := s.APITokens.Create(ctx, token); err != nil {
log.Printf("⚠ BootstrapPAT: failed to create token: %v", err)
return
}
// Write token to file for CI consumption
if err := os.WriteFile("/tmp/armature-admin-pat.txt", []byte(raw), 0600); err != nil {
log.Printf("⚠ BootstrapPAT: failed to write token file: %v", err)
// Still log it for docker-compose stdout capture
}
log.Printf(" ✅ Bootstrap PAT created (prefix: %s), written to /tmp/armature-admin-pat.txt", prefix)
}
// SeedUsers creates or updates users from the SEED_USERS env var.
// Format: "user:pass[:admin],user2:pass2" — third field "admin" adds to Admins group.
// Upsert: existing users get their password refreshed on every restart.