Skip to content

feat: optional token auth middleware (AUTONOMOS_TOKEN) - #19

Merged
aterrylu merged 2 commits into
mainfrom
terry/auth-middleware
Mar 11, 2026
Merged

feat: optional token auth middleware (AUTONOMOS_TOKEN)#19
aterrylu merged 2 commits into
mainfrom
terry/auth-middleware

Conversation

@aterrylu

Copy link
Copy Markdown
Owner

Summary

  • Add opt-in token authentication via AUTONOMOS_TOKEN env var
  • When set, all /api/* and /ws/* routes require a valid token (cookie or Authorization: Bearer)
  • /auth?token=xxx endpoint sets an httpOnly cookie for browser sessions
  • Static assets remain public so the dashboard loads and can show an unauthenticated state
  • No token set = open access (unchanged behavior)

Security

  • Timing-safe comparison via crypto.timingSafeEqual
  • Strict Authorization: Bearer header parsing
  • secure cookie flag on non-localhost (proper hostname check via new URL())
  • Startup warns on empty/whitespace or short tokens
  • Token only accepted via query param on /auth endpoint, not on API routes

Usage

# No auth (default, unchanged)
make up

# With auth
AUTONOMOS_TOKEN=my-secret-token-here make up
# Server prints: http://localhost:3000/auth?token=my-secret-token-here
# Open that link once → cookie set → bookmarked / done

Test plan

  • No token set → all routes open (backward compatible)
  • Token set → API returns 401 without auth
  • Cookie auth works
  • Bearer header auth works
  • /auth?token=xxx sets cookie and redirects
  • Wrong token → 401
  • Query token on /api/* routes → 401 (not accepted)

Generated with Claude Code

Add opt-in authentication for the dashboard API and WebSocket routes.
When AUTONOMOS_TOKEN env var is set, all /api/* and /ws/* routes require
a valid token via cookie or Bearer header. Static assets remain public
so the dashboard can load and show an unauthenticated state.

- /auth?token=xxx endpoint sets httpOnly cookie (1 year, secure on HTTPS)
- Timing-safe token comparison via crypto.timingSafeEqual
- Scoped Hono middleware (only /api/* and /ws/*)
- Startup prints clickable auth link for easy browser setup
- Warns on empty/whitespace or short (<8 char) tokens

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@aterrylu
aterrylu enabled auto-merge (squash) March 11, 2026 03:54
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

@nox-0x nox-0x left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Clean auth middleware implementation. Timing-safe comparison, proper httpOnly cookie with conditional secure flag, and the Bearer/cookie dual-path is well-structured. The length short-circuit in safeEqual technically leaks token length but is a non-issue for a self-hosted personal tool. Startup URL log is intentional UX, not a concern here. LGTM — ship it.

@aterrylu
aterrylu merged commit 4e36e97 into main Mar 11, 2026
1 check failed
@aterrylu
aterrylu deleted the terry/auth-middleware branch March 11, 2026 03:58
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.

2 participants