fix(auth): accept persistent session tokens in AuthMiddleware - #24
Conversation
FinalizeLogin issues bearer tokens from the persistent SessionStore, but AuthMiddleware only validated tokens against the OAuth login-state map. In dev the cookie path masked this; in the packaged Electron build (file://) there is no cookie, so every authenticated API call returned 401 and no accounts/characters were displayed. AuthMiddleware now checks the persistent session store first and falls back to the OAuth state lookup for legacy tokens. The session-validation handler already followed this order, which is why login appeared to succeed while data fetches failed. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
|
Warning Rate limit exceeded
To keep reviews running without waiting, you can enable usage-based add-on for your organization. This allows additional reviews beyond the hourly cap. Account admins can enable it under billing. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughA new ChangesBearer Token Authentication with Persistent Session Support
Sequence DiagramsequenceDiagram
actor Client
participant AuthMiddleware
participant PersistentValidator
participant LoginService
participant NextHandler
Client->>AuthMiddleware: Request with Bearer token
alt Persistent Validator Available
AuthMiddleware->>PersistentValidator: ValidateSession(token)
alt Session Valid
PersistentValidator-->>AuthMiddleware: true
AuthMiddleware->>NextHandler: Authenticate & Forward
NextHandler-->>Client: Response
else Session Invalid
PersistentValidator-->>AuthMiddleware: false
AuthMiddleware->>LoginService: ResolveAccountAndStatusByState(token)
LoginService-->>AuthMiddleware: account, status
alt callbackComplete
AuthMiddleware->>NextHandler: Authenticate & Forward
NextHandler-->>Client: Response
else Not Complete
AuthMiddleware-->>Client: 401 invalid or expired token
end
end
else No Persistent Validator
AuthMiddleware->>LoginService: ResolveAccountAndStatusByState(token)
LoginService-->>AuthMiddleware: account, status
alt callbackComplete
AuthMiddleware->>NextHandler: Authenticate & Forward
NextHandler-->>Client: Response
else Not Complete
AuthMiddleware-->>Client: 401 invalid or expired token
end
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
✨ Simplify code
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/http/middleware_test.go`:
- Around line 67-69: The test helper createTestRouter currently passes nil for
the new validator into AuthMiddleware, so add a small stub implementation of the
validator interface in this test file and update createTestRouter calls to
inject it; then add two tests: one that constructs a persistent-session bearer
token and ensures the validator's ValidateSession returns true so the request is
authenticated via ValidateSession, and one where ValidateSession returns false
for a persistent token and AuthMiddleware falls back to calling
ResolveAccountAndStatusByState on the mocked sessionService (assert the fallback
path is used and the account is resolved). Reference the AuthMiddleware,
ValidateSession (validator method), ResolveAccountAndStatusByState
(sessionService method) and createTestRouter to locate where to inject the stub
and add the new test cases.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: 7247ccde-2a89-4fcd-8b4a-b8fa8cbed9f1
📒 Files selected for processing (3)
internal/http/middleware.gointernal/http/middleware_test.gointernal/server/router.go
Adds a stub PersistentSessionValidator and two AuthMiddleware tests: one where the validator accepts the bearer token, and one where it rejects so the middleware falls back to ResolveAccountAndStatusByState. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Summary
FinalizeLoginreturns a bearer token issued by the persistentSessionStore, butAuthMiddlewareonly validated tokens against the OAuth login-state map — so in the packaged Electron build (file://, no cookie) every authenticated API call returned 401 and no accounts/characters showed up.AuthMiddlewarenow checks the persistent session store first and falls back to the OAuth state lookup for legacy tokens, mirroring whatGetSession/ValidateSessionalready do.Test plan
go build ./...go test ./internal/http/.../api/accountsreturns data without "Invalid or incomplete token" warnings.🤖 Generated with Claude Code
Summary by CodeRabbit