feat(auth0-server-js): add revokeRefreshToken and logout revocation support - #222
Conversation
…upport (SDK-8850) Adds ServerClient.revokeRefreshToken() which reads the refresh token from the session and delegates to AuthClient.revokeToken(). Logout now always attempts revocation best-effort before clearing the session. In resolver mode, both revocation and session deletion are gated by a domain-match guard. Exports TokenRevocationError from the public surface.
📝 WalkthroughWalkthroughAdds ChangesRefresh token revocation
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant User
participant ExpressApp
participant ServerClient
participant Auth0
User->>ExpressApp: Submit POST /auth/revoke
ExpressApp->>ServerClient: revokeRefreshToken()
ServerClient->>Auth0: Revoke refresh token
Auth0-->>ServerClient: Success or TokenRevocationError
ServerClient-->>ExpressApp: Complete request
ExpressApp-->>User: Redirect to /private
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 6
🧹 Nitpick comments (1)
packages/auth0-server-js/src/server-client.spec.ts (1)
7022-7059: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winActually assert that revocation precedes deletion.
The test only checks that both operations occurred, so it would pass if their order were reversed. Record both callbacks and assert
['revoke', 'delete'].🤖 Prompt for 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. In `@packages/auth0-server-js/src/server-client.spec.ts` around lines 7022 - 7059, Update the “should revoke refresh token before clearing session on logout” test to record the operation order in both the revocation callback configured by setupRevocation and mockStateStore.delete. Assert the recorded sequence is ['revoke', 'delete'] instead of only checking that each operation occurred, while preserving the existing logout setup and expectations.
🤖 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 `@examples/example-express-web/src/auth0.ts`:
- Around line 87-90: Update the Auth0 authorizationParams configuration used by
the example’s login flow to include offline_access in the existing scope
alongside openid, profile, and email, so the /auth/revoke handler has a refresh
token available.
In `@packages/auth0-server-js/src/index.ts`:
- Line 5: Remove TokenRevocationError from the re-export in the package
entrypoint until `@auth0/auth0-auth-js` provides the revocation API. Keep
TokenExchangeError, MissingClientAuthError, and OrganizationValidationError
re-exported unchanged.
In `@packages/auth0-server-js/src/server-client.ts`:
- Around line 1136-1141: Update the logout revocation error handling around
revokeRefreshToken to detect and suppress the expected MissingSessionError
caused by absent refresh tokens, while continuing to console.warn for all other
revocation failures. Apply the same handling to the additional logout path noted
in the comment, preserving successful best-effort logout behavior.
- Around line 1082-1085: Update the JSDoc for the token-revocation method near
the explicit token parameter to state that explicit tokens bypass session lookup
only in static mode; in resolver mode, session state is consulted and revocation
may be skipped on a domain mismatch. Also update the corresponding example in
packages/auth0-server-js/EXAMPLES.md at lines 1501-1505 to document the same
resolver-mode lookup and mismatch behavior.
- Around line 1113-1120: Update the resolver-mode branch around
`#getSessionDomain` so that when stateData exists, its session domain must be
present and match resolvedDomain; return without revoking when the domain is
missing, uninferable, or different. Preserve the current resolved-domain
behavior only when no stateData is provided, and add a regression test covering
a session without domain or an inferable issuer.
- Around line 1094-1109: Update the refresh-token handling around options.token
and the fallback in the revocation flow to distinguish an omitted token from an
explicitly blank token. Reject an explicitly empty token before reading or using
stateData.refreshToken, while preserving the existing session-token fallback
only when token was not supplied.
---
Nitpick comments:
In `@packages/auth0-server-js/src/server-client.spec.ts`:
- Around line 7022-7059: Update the “should revoke refresh token before clearing
session on logout” test to record the operation order in both the revocation
callback configured by setupRevocation and mockStateStore.delete. Assert the
recorded sequence is ['revoke', 'delete'] instead of only checking that each
operation occurred, while preserving the existing logout setup and expectations.
🪄 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: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 96a1fd7b-1f08-4b72-b874-fb3fdb93f1c8
📒 Files selected for processing (7)
examples/example-express-web/src/auth0.tsexamples/example-express-web/views/private.ejspackages/auth0-server-js/EXAMPLES.mdpackages/auth0-server-js/src/index.tspackages/auth0-server-js/src/server-client.spec.tspackages/auth0-server-js/src/server-client.tspackages/auth0-server-js/src/types.ts
…trengthen revoke test assertion
… in resolver mode
…ty token guard, docs - Bump @auth0/auth0-auth-js dependency from ^1.11.0 to ^1.12.0 - Add explicit empty-string token guard in revokeRefreshToken (throws MissingRequiredArgumentError) - Add offline_access scope to example-express-web authorizationParams - Expand JSDoc and EXAMPLES.md to document resolver-mode behavior for explicit tokens - Assert revoke-before-delete ordering in logout test using ops array
95c8861 to
6c2e25b
Compare
Summary
ServerClient.revokeRefreshToken()— reads the refresh token from the session and delegates toAuthClient.revokeToken(), wrapping errors inTokenRevocationErrorlogout()now always attempts revocation best-effort before clearing the session; revocation failure never blocks logoutTokenRevocationErrorfrom the server-js public surface/auth/revokeroute to the Express example appDepends on
Requires #221 to be merged and released first.
Test plan
revokeRefreshToken— success, missing session, missing refresh token, wraps error asTokenRevocationErrorlogout(static mode) — revokes then deletes session; revocation failure does not block logoutlogout(resolver mode) — revokes and deletes when domains match; skips both when domains differ