feat: add JSON API with token auth (closes #69) #74
No reviewers
Labels
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: sneak/upaas#74
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "feature/json-api"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Implements a REST JSON API at
/api/v1/with Bearer token authentication.New files
internal/database/migrations/006_add_api_tokens.sql— API tokens tableinternal/models/api_token.go— APIToken model (SHA-256 hashed tokens)internal/handlers/api.go— API endpoint handlersinternal/handlers/api_test.go— 11 tests covering all endpointsModified files
internal/middleware/middleware.go— APITokenAuth middleware + APIUserFromContextinternal/server/routes.go— wire up/api/v1/route groupinternal/handlers/handlers_test.go— add middleware to test contextEndpoints
CI Results
go test -race ./...— all tests passgolangci-lint run— 0 issuesCloses #69
- Add API token model with SHA-256 hashed tokens - Add migration 006_add_api_tokens.sql - Add Bearer token auth middleware - Add API endpoints under /api/v1/: - GET /whoami - POST /tokens (create new API token) - GET /apps (list all apps) - POST /apps (create app) - GET /apps/{id} (get app) - DELETE /apps/{id} (delete app) - POST /apps/{id}/deploy (trigger deployment) - GET /apps/{id}/deployments (list deployments) - Add comprehensive tests for all API endpoints - All tests pass, zero lint issuesswitch this to cookie-based authentication. api clients can POST to the login endpoint and get a cookie like a browser.
@clawbot we don't want bearer auth
Understood — removing all bearer/token auth. Sub-agent is already working on switching to cookie-based session auth (same as the web UI). Will force-push shortly.
81c109914bto9ac1d25788Switched to cookie-based session auth per your review.
Changes
APITokenAuthmiddleware,APIUserFromContext)007_drop_api_tokens.sqlto clean up the tablePOST /api/v1/login— accepts JSON{"username","password"}, sets session cookie, returns user info/api/v1/*routes now useAPISessionAuthmiddleware (same session cookies as web UI, returns JSON 401 instead of redirect)/api/v1/tokensendpointHandleAPIWhoAmInow reads user from session instead of token contextCI Results
make test— all tests passgolangci-lint run ./...— 0 issuesremove the drop migration - migration 6 was never deployed
Removed both undeployed migrations:
006_add_api_tokens.sql(create) and007_drop_api_tokens.sql(drop). No Go code referencedapi_tokens. Remaining migrations are sequential 001–006.✅
make test— all passing✅
golangci-lint run ./...— 0 issues