fix: Fix Session.refresh() KeyError by sealing session client-side#673
fix: Fix Session.refresh() KeyError by sealing session client-side#673devin-ai-integration[bot] wants to merge 1 commit into
Conversation
The refresh method was sending a session/seal_session parameter to the /user_management/authenticate API endpoint expecting it to return a sealed_session field. The API does not support server-side sealing and never includes sealed_session in the response, causing a KeyError that was swallowed by the except block and surfaced as a bogus failure reason. Seal the session client-side after receiving the API response using the existing seal_session_from_auth_response helper, matching the approach used by the Node SDK. Fixes #666
Original prompt from garen.torikian
|
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
Greptile SummaryThis PR fixes a long-standing bug where
Confidence Score: 4/5The fix is correct and the core logic matches the Node SDK reference implementation; the refresh path is now functional and well-tested. The bug fix is straightforward and the new tests cover the happy path, impersonator case, and the regression scenario. The only open question is that both Session.refresh() and AsyncSession.refresh() decode the new access token with verify_aud=False and no iss check — a pattern also present in authenticate(), but now newly activated in the refresh path. The JWT decode options in src/workos/session.py (lines 368–374 and 551–557) are worth a second look before shipping. Important Files Changed
|
Description
Session.refresh()(andAsyncSession.refresh()) always failed withKeyError('sealed_session'), which was swallowed by the blanketexcept Exceptionand surfaced asreason="'sealed_session'"— not a validAuthenticateWithSessionCookieFailureReason.Root cause: The refresh method sent a
session: {seal_session: True, cookie_password: ...}parameter to the/user_management/authenticateAPI, expecting asealed_sessionfield in the response. The API does not support server-side session sealing and never returns this field. The hard key accessauth_response["sealed_session"]raisedKeyError.Fix: Seal the session client-side after receiving the API response, using the existing
seal_session_from_auth_response()helper. This matches the approach in the Node SDK, which strips thesessionparam before sending the request and seals locally viasealSessionDataFromAuthenticationResponse().Changes:
sessionbody param from both sync and asyncrefresh()(the API ignores it)seal_session_from_auth_response(access_token, refresh_token, user, impersonator, cookie_password)to produce the sealed cookiesessionis not sent to the APIFixes #666
Documentation
No docs changes needed — this is a bug fix to match the documented behavior of
Session.refresh().Link to Devin session: https://app.devin.ai/sessions/1f32a32e77304ede864acc4f6790bc29