Skip to content

feat(auth): add bearer token authentication#145

Merged
gricha merged 10 commits into
mainfrom
feat/auth-token-support
Jan 24, 2026
Merged

feat(auth): add bearer token authentication#145
gricha merged 10 commits into
mainfrom
feat/auth-token-support

Conversation

@gricha
Copy link
Copy Markdown
Owner

@gricha gricha commented Jan 23, 2026

Summary

Adds bearer token authentication support across the Perry stack, enabling secure API access for automated tools and integrations.

Changes

  • Server: Add server-side bearer token authentication with validation middleware
  • CLI: Add commands for token management (create, list, revoke)
  • Web UI: Integrate token handling for authenticated API requests
  • Mobile: Add token handling for mobile app authentication flow
  • Tests: Add comprehensive unit and integration tests for the auth flow

Benefits

  • Enables secure programmatic access to Perry APIs
  • Supports automation workflows and CI/CD integrations
  • Consistent authentication experience across all clients

Phase 1 implementation:
- Create auth.ts with checkAuth() and unauthorizedResponse() helpers
- Add AuthConfig type to AgentConfig
- Add auth middleware to fetch handler (after OPTIONS)
- Auth check covers WebSocket upgrade paths
- Update CORS headers to allow Authorization header
- Smart defaults: new installs auto-generate token, existing configs stay open
Phase 2 implementation:
- Add token field to ClientConfig type
- Update ApiClient to send Bearer token in requests
- Add getToken/setToken helpers to client config
- Add 'perry auth init' command to generate agent tokens
- Add 'perry config token' command to configure client tokens
- Update config display to show masked token
Phase 3 implementation:
- Add localStorage-based token storage (perry_auth_token)
- Export setToken/clearToken/getToken functions
- Update RPCLink to include Bearer token in requests
- initClient() recreates client when token changes
Phase 4 implementation:
- Add token field to ServerConfig interface
- Update saveServerConfig to accept optional token parameter
- Track currentToken in module state
- Update RPCLink to include Bearer token in requests
- Export getToken() for reading current token
Comment thread src/agent/run.ts
Comment on lines +140 to +143
const authResult = checkAuth(req, currentConfig);
if (!authResult.ok) {
return unauthorizedResponse();
}

This comment was marked as outdated.

Add WEB_UI_PATTERNS to bypass auth for /, /index.html, /assets/*, and
/favicon.ico so browsers can load the web UI before sending auth tokens.
Comment thread src/agent/run.ts
Comment on lines +267 to +274
const configExists = fs.existsSync(configPath);

const config = await loadAgentConfig(configDir);

if (!configExists && !config.auth?.token) {
const token = `perry-${crypto.randomBytes(16).toString('hex')}`;
config.auth = { ...config.auth, token };
await saveAgentConfig(config, configDir);

This comment was marked as outdated.

Static handler was serving index.html for /rpc/* and /health paths,
bypassing the auth middleware. Add isApiPath check to return null
for API prefixes, allowing proper auth handling.
@gricha gricha merged commit b02042a into main Jan 24, 2026
9 checks passed
@gricha gricha deleted the feat/auth-token-support branch January 24, 2026 01:40
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