Skip to content

feat: add API key management with fine-grained RBAC scopes#208

Open
mrgoonie wants to merge 7 commits intomainfrom
claude/add-api-key-management-pHMQx
Open

feat: add API key management with fine-grained RBAC scopes#208
mrgoonie wants to merge 7 commits intomainfrom
claude/add-api-key-management-pHMQx

Conversation

@mrgoonie
Copy link
Contributor

@mrgoonie mrgoonie commented Mar 15, 2026

Summary

Closes #207

  • Add multi-key API authentication with 5 RBAC scopes (operator.admin, operator.read, operator.write, operator.approvals, operator.pairing) mapped to 3 roles (Admin/Operator/Viewer)
  • API keys use SHA-256 hashed storage, 128-bit entropy, goclaw_ prefix format, optional expiration, and soft-revocation
  • Full CRUD via HTTP REST (/v1/api-keys) and WebSocket RPC (api_keys.list/create/revoke), both admin-only
  • Web UI page with create dialog (scope checkboxes, expiry selector), show-once key modal, revoke confirmation, search/pagination, i18n (en/vi/zh)
  • Interactive API documentation with Swagger UI at /docs + comprehensive HTTP/WebSocket/auth docs
  • Backward compatible: existing gateway token auth unchanged, API key auth is additive
  • Migration renumbered from 000019 to 000020 to avoid conflict with concurrent work

Security

  • Raw key never stored — only SHA-256 hash persisted
  • Expiry enforced in SQL WHERE clause (not application layer)
  • Constant-time token comparison (crypto/subtle)
  • Centralized permissions.ValidScope() for scope validation
  • Generic error messages to clients (no err.Error() leaks)
  • Warning logged on unauthenticated API key management access

Test plan

  • Create API key via Web UI — verify raw key shown once, copy works
  • Authenticate HTTP request with created key — verify role-based access
  • Authenticate WebSocket with API key in connect token — verify scopes enforced
  • Revoke key — verify subsequent requests fail
  • Create key with 7d expiry — verify expires_at set correctly
  • Try invalid scope in create — verify 400 error
  • Verify existing gateway token auth still works unchanged
  • Verify Swagger UI accessible at /docs
  • Verify i18n strings render in en/vi/zh

claude and others added 5 commits March 15, 2026 02:42
Implements a multi-key API key management system for the GoClaw gateway,
replacing the single-token auth model with support for multiple API keys
each with configurable permission scopes.

Backend:
- Migration 000019: api_keys table with SHA-256 hashed keys, scopes,
  expiry, and revocation support
- Store layer: APIKeyStore interface + PG implementation with indexed
  hash lookups and expiry checks
- Key generation: goclaw_ prefixed keys with SHA-256 hashing (show once)
- Auth integration: HTTP Bearer + WebSocket connect both resolve API keys
  and derive roles from scopes (admin/read/write/approvals/pairing)
- HTTP API: GET/POST/DELETE /v1/api-keys (admin-only)
- WebSocket RPC: api_keys.list, api_keys.create, api_keys.revoke
- Permissions: all API key methods require admin role

Web UI:
- API Keys management page with list, create dialog, revoke confirmation
- Show-once key display with copy-to-clipboard
- Scope checkboxes and configurable expiry (never/7d/30d/90d)
- Full i18n support (en/vi/zh)
- Sidebar navigation under System group

https://claude.ai/code/session_017sg1fT9pN25DsArUxyGxHK
Serve OpenAPI 3.0 spec at /v1/openapi.json and Swagger UI at /docs
with embedded spec covering all HTTP endpoints. Add API Docs link
to sidebar with i18n support (en/vi/zh).

https://claude.ai/code/session_017sg1fT9pN25DsArUxyGxHK
- 18-http-api.md: Complete HTTP REST API reference covering 130+ endpoints
  across 23 sections (chat, agents, skills, providers, MCP, memory, KG, etc.)
- 19-websocket-rpc.md: Full WebSocket RPC method catalog with 64+ methods,
  permission matrix, event types, and Mermaid flow diagrams
- 20-api-keys-auth.md: API key authentication model, RBAC scopes, security
  design (SHA-256 hashing, show-once), database schema, usage examples
- Updated changelog with API keys and docs entries

https://claude.ai/code/session_017sg1fT9pN25DsArUxyGxHK
- Features: add API key management to Security section, update Web Dashboard
- API section: replace old links with table linking to new docs (HTTP API,
  WebSocket RPC, API Keys & Auth, Gateway Protocol) + mention Swagger UI
- Security section: add API key management entry with doc link
- Project Status: add API keys and Swagger UI entries

https://claude.ai/code/session_017sg1fT9pN25DsArUxyGxHK
- Move expiry check from Go to SQL WHERE clause (prevent race condition)
- Extract permissions.ValidScope() to deduplicate scope validation
- Change DELETE to POST /v1/api-keys/{id}/revoke (semantic correctness)
- Handle unmarshal error in WS revoke handler
- Remove redundant allKeys field from APIKeysHandler
- Sanitize error messages (no internal err.Error() to client)
- Fix display prefix to use hex part instead of goclaw_ prefix
- Replace raw div overlay with accessible Radix Dialog
@mrgoonie mrgoonie requested a review from viettranx March 15, 2026 04:04
OAuth token format (sk-ant-oat01-*) should be passed as anthropic_api_key,
not claude_code_oauth_token which expects a different auth flow (OIDC).
Another maintainer is working on migration 19, so bump api_keys migration
to 000020 and update RequiredSchemaVersion + doc references accordingly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: API Key Management — Fine-grained RBAC scopes for HTTP & WebSocket API access

2 participants