Skip to content

Comments

API token management & API layer refactoring#116

Merged
asdek merged 1 commit intomasterfrom
feature/api_tokens
Feb 22, 2026
Merged

API token management & API layer refactoring#116
asdek merged 1 commit intomasterfrom
feature/api_tokens

Conversation

@asdek
Copy link
Contributor

@asdek asdek commented Feb 21, 2026

API Token Authentication

Description

Problem

Users needed programmatic access to PentAGI's REST and GraphQL APIs for automation, CI/CD integration, and custom applications. Previously, only session-based authentication was available.

Solution

Implemented JWT-based API token system with:

  • Token management UI (create, edit, revoke, delete)
  • Bearer token authentication for REST and GraphQL APIs
  • Two-tier caching (tokens + users) with 5-minute TTL
  • Role-based permissions inheritance
  • Soft delete for audit trails
  • Real-time updates via GraphQL subscriptions

Type of Change

  • New feature
  • Security update
  • Documentation update

Areas Affected

  • Core Services (Frontend UI/Backend API)
  • Documentation

Testing

Configuration

PentAGI Version: Latest development
Docker: 24.0.x
Host OS: macOS/Linux
LLM Provider: OpenAI

Test Coverage

Backend:

  • Unit tests: token service, JWT validation, cache (hit/miss/invalidation)
  • Integration tests: full auth flow, middleware, permissions
  • Security tests: expired/revoked/invalid tokens, cache invalidation

Frontend:

  • UI interactions: create, edit, delete, copy tokens
  • Toast notifications on copy operations
  • Real-time updates via subscriptions

API Testing:

  • GraphQL Playground: Bearer token authentication
  • Swagger UI: Bearer token authentication
  • Client generation: Python and TypeScript examples

Results:

  • ✅ All tests pass
  • ✅ Cache hit ratio >95%
  • ✅ First request ~1-2ms, cached <0.1ms
  • ✅ No breaking changes

Security

Implementation:

  • HMAC-SHA256 signed JWT tokens
  • One-time token display (never stored in DB)
  • Token expiration and revocation support
  • User-scoped tokens with role inheritance
  • Cache invalidation on token/user changes

Protection:

  • Negative caching prevents DoS from invalid tokens
  • Thread-safe cache operations (sync.Map)
  • No token secrets in cache or logs

Privileges Added:

  • settings.tokens.admin (Admin only)
  • settings.tokens.create/view/edit/delete/subscribe (Admin + User)

Performance

Caching:

  • TokenCache: Token status + privileges (5 min TTL)
  • UserCache: User hash + status (5 min TTL)
  • First request: ~1-2ms (JWT + DB)
  • Cached requests: <0.1ms (JWT + cache)
  • Cache hit ratio: >95%
  • Auto-invalidation on updates

Database:

  • Proper indexes on token_id, user_id, status
  • Soft delete with deleted_at index
  • Partial unique index on token names

Documentation

  • README: New "API Access" section with examples
  • GraphQL schema: New types, mutations, queries, subscriptions
  • Swagger: Auto-generated docs for REST endpoints
  • Code examples: Python and TypeScript clients
  • Instructions: GraphQL Playground and Swagger UI usage
  • Client generation: OpenAPI Generator, swagger-typescript-api

Deployment

Migration:

  • File: 20260218_150000_api_tokens.sql
  • Creates: api_tokens table, TOKEN_STATUS enum
  • Adds privileges for Admin and User roles
  • Runs automatically on deployment

Compatibility:

  • ✅ Fully backward compatible
  • ✅ No new environment variables
  • ✅ Uses existing COOKIE_SIGNING_SALT
  • ✅ No service interruption

Verification:

SELECT * FROM api_tokens LIMIT 1;
SELECT * FROM privileges WHERE name LIKE 'settings.tokens.%';

Checklist

Code Quality

  • Follows coding standards
  • Tests added and passing
  • go fmt and go vet passed
  • npm run lint passed

Security

  • Security implications considered
  • Security model maintained
  • Sensitive data properly handled

Compatibility

  • Backward compatible
  • No breaking changes
  • Dependencies updated

Documentation

  • Documentation complete
  • Code commented
  • API changes documented

Files Changed

Backend:

  • pkg/server/models/api_tokens.go (new)
  • pkg/server/services/api_tokens.go + tests (new)
  • pkg/server/auth/api_token_*.go (new: jwt, id, cache) + tests
  • pkg/server/auth/users_cache.go + tests (new)
  • pkg/server/auth/auth_middleware.go (modified)
  • pkg/server/auth/permissions.go (modified)
  • pkg/server/auth/session.go (modified)
  • pkg/graph/schema.graphqls + resolvers (modified)
  • migrations/sql/20260218_150000_api_tokens.sql (new)
  • Swagger docs auto-generated

Frontend:

  • src/pages/settings/settings-api-tokens.tsx (new)
  • src/components/layouts/settings-layout.tsx (modified)
  • GraphQL types generated

Documentation:

  • README.md (new API Access section)

Notes

Key Features:

  • JWT tokens with configurable TTL (1 min - 3 years)
  • In-memory caching with automatic invalidation
  • One-time token display for security
  • Interactive API testing (GraphQL Playground, Swagger UI)
  • Client generation support (multiple languages)

Future Enhancements:

  • Token usage analytics
  • IP restrictions
  • Custom scopes/permissions
  • Per-token rate limiting
  • Distributed cache (Redis)

Note

High Risk
Introduces a new authentication mechanism and permission surface (Bearer tokens, caching, revocation), so bugs could impact authorization and API access control.

Overview
Adds first-class API token authentication for programmatic access, including a new api_tokens table (soft delete, status enum, indexes) and new role privileges under settings.tokens.*.

Exposes token lifecycle over GraphQL (new APIToken/APITokenWithSecret types, create/update/delete mutations, list/get queries, and subscriptions) and wires in a token-status/privilege cache used by the auth middleware to validate Authorization: Bearer JWTs and enforce revocation. README is expanded with an API Access section covering token generation, Swagger/Playground usage, and client-generation examples.

Written by Cursor Bugbot for commit 6ac70c9. This will update automatically on new commits. Configure here.

@asdek asdek force-pushed the feature/api_tokens branch from c08a119 to c27471a Compare February 21, 2026 23:36
Implement JWT-based API token authentication to enable programmatic access
to REST and GraphQL APIs for automation and integration purposes.
@asdek asdek force-pushed the feature/api_tokens branch from c27471a to 6ac70c9 Compare February 22, 2026 00:06
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

@asdek asdek merged commit f111863 into master Feb 22, 2026
7 checks passed
@asdek asdek deleted the feature/api_tokens branch February 22, 2026 00:19
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.

1 participant