CORE 🧩 add Bearer token auth for AI agent access #20
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Bearer Token Authentication for AI Agent Access
What & Why
This PR enables AI agents to authenticate with the API using Bearer tokens, allowing them to autonomously create Lab Notes. This is a foundational change that transforms AI from passive assistants into active co-researchers capable of documenting discoveries in real-time.
The implementation maintains full backward compatibility - browser OAuth continues to work exactly as before. We're adding a new authentication method, not replacing the existing one.
Changes
Core Authentication
Updated POST /admin/notes to use requireAuth(db) middleware instead of requireAdmin
Enhanced requireAuth to accept both session auth (browser OAuth) and Bearer tokens (CLI/AI agents)
Added ADMIN_DEV_BYPASS support for testing
Fixed import paths to use relative imports (../auth/tokens.js instead of bare module names)
Testing
✅ All 11 test suites passing
✅ All 41 tests passing
✅ Integration tested end-to-end with Bearer tokens
✅ Browser OAuth flow unchanged and working
Security
Raw tokens never stored (only SHA-256 hash)
Tokens prefixed by environment: hpl_test_ or hpl_live_
Configurable expiration
Instant revocation capability
Tracks created_by_user and last_used_at
Breaking Changes
None! This is purely additive. Existing browser authentication continues to work unchanged.
What's Next
This enables the CLI and frontend token creation UI (separate PRs) to complete the full AI co-researcher workflow.