Repository navigation
Conversation
…false success switch_org() treated any status=='OK' response as a completed switch, but the server overloads that status for two different outcomes: a real switch (data has pk/org_name/organization_slug) and an SSO re-authentication challenge (data['idp'] populated) for members of SSO-protected orgs who aren't the owner/admin. The challenge case never actually updates the server-side active org, so treating it as success silently left callers on their previous org while locally claiming to have switched. Now raises with the idp names and a pointer to sso_login(org_name=...) as the alternative, instead of logging a false "Switched to organization" and setting self.session.org_name to a value the server never accepted.
_switch_org wrote _last_switched_org_token unconditionally after the POST, so a 403/500 or an SSO-challenge response (200 + data['idp']) got recorded as a completed switch. That poisoned memo made _maybe_switch_org short-circuit later, skipping switch_org() -- the only implementation that validates the response and raises on an SSO challenge. Now _switch_org returns without writing the memo on a non-2xx status or an idp challenge. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
…ogic ArrowUploader._switch_org and GraphistryClient.switch_org each POSTed to the same org-switch endpoint with their own copy of the HTTP-status, body-status, and SSO-idp-challenge checks. Extract the shared logic into switch_org_request() in graphistry/utils/requests.py, raising OrgSwitchError / OrgSwitchIdpChallenge. Each caller keeps its own success/failure policy (ArrowUploader logs+swallows, GraphistryClient raises) since that divergence is intentional API contract, not duplication. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TNGYwRNwAFQdYDucbwgtUm
…quests.py Type-hygiene guard flagged missing __init__ return annotations and a bare dict for OrgSwitchIdpChallenge's idp param. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GgDZr3AgU9uKz5EWcVZZVY
75c3b4f to
f043c72
Compare
Guard baseline already sat at max cast() count; cast() would've pushed it over. Use a localized type: ignore instead, per DEVELOP.md guidance. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GgDZr3AgU9uKz5EWcVZZVY
f043c72 to
4a9d63e
Compare
Adds direct unit tests for graphistry/utils/requests.py's switch_org_request (success, HTTP error, non-OK body w/ detail, unparseable JSON, SSO idp challenge) and ArrowUploader._switch_org (no-op guards, dedup cache, OrgSwitchError/OrgSwitchIdpChallenge/generic-exception swallow paths), raising changed-line coverage on the switch-org refactor. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GgDZr3AgU9uKz5EWcVZZVY
mock.patch("graphistry.utils.requests.requests.post") failed under the
full suite (AttributeError: module 'graphistry' has no attribute 'utils')
because some other test's import juggling leaves the parent package's
'utils' attribute unset. mock.patch.object against the imported module
sidesteps the string-resolution path entirely.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GgDZr3AgU9uKz5EWcVZZVY
95a9c9a to
510625a
Compare
…ent-only) Cache per-org verified JWTs (exp-aware) in ClientSession so switch_org() can no-op or swap in a cached token instead of re-triggering SSO, without any server-side changes. Multi-org: each org's token cached independently, so switching back to an org verified under an earlier (now-inactive) token still skips re-challenge as long as that token hasn't expired. - client_session.py: _verified_org_tokens dict (org -> (token, exp)), is_org_verified / mark_org_verified / get_verified_token, JWT exp read (no signature verification, token already trusted). - pygraphistry.py: switch_org() fast paths (same-token skip, cached-token swap-in); sso_login()/_handle_auth_url() record verification immediately on token issuance instead of waiting on a follow-up switch call. - arrow_uploader.py: _switch_org() uses the same cache API. - Tests added/migrated in test_pygraphistry.py, test_client_session.py, tests/utils/test_requests.py. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UnsBpuxHHTvC2bAUNaCfED
510625a to
f24fb45
Compare
The SSO token endpoint answers with a non-OK body status ("State is
invalid") on every poll until the browser login completes, so the poll
loop's blanket `except Exception` was load-bearing. But it also swallowed
permanent failures -- an org mismatch burned the full sso_timeout and then
reported a misleading "run sso_get_token()" message.
Type the retryable cases instead:
- New SsoPendingException, raised by ArrowUploader.sso_get_token for a
non-OK body status and for an unrecognized body with no 'status' key
(proxy error page, DRF detail). Nothing before the token is parsed can
be classified as permanent.
- The poll loop waits on SsoPendingException uncapped until sso_timeout,
retries RequestException/ValueError up to SSO_POLL_MAX_CONSECUTIVE_ERRORS
(ValueError covers both json and simplejson decode errors), and lets
everything else -- org mismatch, missing JWT in an OK response --
propagate on the first poll.
Also fix switch_org's cached-token fast path clearing _is_authenticated:
it went through the api_token() setter, whose side effect forced a
needless refresh() on the next authenticate(); that refresh minted a token
not verified for the org, reinstating the round trip the fast path exists
to skip. Assign the session field directly, and carry the org grant onto
the refreshed token in refresh() so the fast path stays hit.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AvLMs6S5UosfKf2YB2sCRS
B1 follow-up to #1721: refresh() carried the org grant from the pre-refresh token onto the newly reissued one, but the server resets the active org on every reissue -- so the carried grant let _maybe_switch_org() skip the required /switch/ POST, and plot() silently uploaded to the default org while session.org_name still showed the requested one. Deleting the carry-grant lines alone regresses further: switch_org()'s same-user fast path (added by 5cdd1d6 for B3) would then find the stale cache entry left over from before the rotation and swap that superseded, pre-refresh token back in as active -- no real switch request, and refresh()'s own return value reverted to the old token. Fix: ClientSession.forget_verified_org() drops the cache entry for the active org once refresh() confirms the token actually rotated (skipped on a no-op refresh, so it doesn't discard a still-valid verification), forcing a real switch_org() call with the new token. Tests: test_refresh_rotated_token_does_not_skip_switch pins the mocked-switch_org_request behavior; test_refresh_rotated_token_hits_real_switch_endpoint is the HTTP-level version (only requests.post mocked, real ArrowUploader.refresh()/switch_org_request() code paths run) -- both fail against the pre-fix code. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B1Pvaw1aZQvtraEWkEvdyt
albarralnunez
left a comment
There was a problem hiding this comment.
Thanks, this is close. The poll fix is the important part for graphistry#3028. Since 0.45.10 (#850), blocking sso_login() raises State is invalid on the first poll, before the user can finish the browser login. SsoPendingException fixes that, and #2083 keeps the loop bounded without IPython. #2083's own changes (bounded poll loop, user_id guard on the cached-token swap, forget_verified_org on refresh rotation) look correct to me; please merge it into this branch.
Two things to fix before merge, plus some small ones. Line numbers are at this PR's head 29174954.
Context (Hub server code): /switch/ stores the active org per token, in the JWT's sk server session. Uploads take org_name from the body, so they still land in the right org. But a skipped /switch/ leaves the token's server-side active org wrong. That active org is what /api-token-verify/, entitlements and GFQL remote see (graphistry#2933).
B-A: the SSO paths mark the org verified before any switch happened
graphistry/pygraphistry.py:259-261 and :370-372
sso_login() calls mark_org_verified(token, org) unconditionally, so the following _maybe_switch_org() always returns early. That is only safe when ArrowUploader.sso_get_token already switched successfully (Layers 1 and 3 call _switch_org, which marks on success). It breaks in two cases:
-
No
/switch/is ever sent:- Layer 2 (
arrow_uploader.py:477): the caller passedorg_nameand the server returned noactive_organization. - Org SSO where the server returns
SITE(older Hubs). The SITE handling turns this into Layer 2.
Repro: mock the jwt endpoint to return
{"status":"OK","data":{"token":...}}forsso_login(org_name="x"). Only the login and jwt calls happen. - Layer 2 (
-
An IdP-challenged switch gets marked verified anyway (
arrow_uploader.py:230correctly doesn't memoize it, thenpygraphistry.py:372does).
Fix:
- Drop both unconditional
mark_org_verifiedcalls insso_loginand rely on_switch_orgmarking on success. - In Layer 2, call
self._switch_org(self.org_name, token_value). It is already best-effort. - Add an end-to-end
sso_logintest that assertsPOST /o/<org>/switch/for L1, L2 and SITE-with-org, and that a challenged switch is not marked verified.
B-B: the per-org cache goes stale when the same token switches away
graphistry/client_session.py:127-138, graphistry/pygraphistry.py:2723 (and _maybe_switch_org, :813)
A plain switch doesn't reissue the token, so after switch_org("a"), then switch_org("b"), the cache still holds a -> T. A later switch_org("a") takes fast path 1 and skips the POST: session.org_name == "a" while the token's server-side org is still b. Repro: those three calls send 2 POSTs instead of 3. Master's single (org, token) slot didn't have this problem.
Fix: in mark_org_verified(token, org), first evict every other entry whose token is token (one active org per token), then add the new entry. Fast path 2 (swap in an older token verified for that org) is fine, because the active org is per token. Add an a, b, a test that expects 3 POSTs.
Small ones
graphistry/exceptions.py:8: keepSsoRetrieveTokenTimeoutException(#2083'sed013c71deletes it) as a deprecated, unused class for a release.from graphistry.exceptions import SsoRetrieveTokenTimeoutExceptionis public API.graphistry/utils/requests.py:76: a non-2xx response drops the server's message (a 403 "not a member" becomes just "failed with HTTP 403"). Fix: parse the body before the status check and passdetail=body.get('message').graphistry/arrow_uploader.py:438: a body with nostatus(proxy error page, DRF{"detail": ...}, 403 when SSO signup is off) is classified as pending, so it retries silently at debug level for the whole 50 s. Fix: only treat a non-OKstatusas pending. RaiseValueErroron a missingstatusso it goes through the consecutive-error limit and gets logged.graphistry/pygraphistry.py:243:sso_requested_org_nameis only cleared on blocking-poll success. Fix: clear it on timeout, error, the direct-token branch and insso_get_token()too (e.g. afinally).graphistry/arrow_uploader.py:237-240: thePyGraphistry.session is self._client_sessionbranch re-marks the same object. Fix: drop it.- CHANGELOG: please add an SSO entry under
[Development](### Fixed). Include: the poll fix (blockingsso_loginregression since 0.45.10), SITE handling,/switch/IdP-challenge detection (switch_org()now raisesOrgSwitchIdpChallengeinstead of silently "succeeding"; that's a behavior change), and the new/api/v2/my/organizations/personal-org lookup.
Once B-A and B-B are in, we'll do a live SSO test against a Hub (org SSO as a plain member, site-wide, switch round-trip, refresh) before merging.
…e org per token The server keeps one active org per token, but the client marked orgs verified without a switch and kept stale per-org entries, so later switches were skipped. - sso_login: drop unconditional mark_org_verified (direct-token and poll paths); only a confirmed /switch/ records verification, so a challenged or skipped switch is retried - ArrowUploader.sso_get_token Layer 2: request the switch (best-effort) - ClientSession.mark_org_verified: evict other orgs under the same token, so switch_org a -> b -> a sends 3 POSTs - switch_org_request: keep the server message on non-2xx responses - sso_get_token poll: a response with no status raises ValueError (consecutive error limit, logged) instead of retrying silently as pending - clear sso_requested_org_name when a login ends (blocking timeout/error, direct token, sso_get_token success); keep it while non-blocking is pending - drop redundant PyGraphistry.session re-mark in ArrowUploader._switch_org - end-to-end sso_login/switch_org tests; CHANGELOG entry Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Changelog
Summary
After SSO login on a site-wide SSO server, the session could end up bound to the reserved
SITEorg (or to no org at all), and subsequent dataset uploads failed because nothing can be created underSITE. This PR fixes that, and along the way hardens the surrounding org-switch and SSO-polling code that the fix depends on.Changes
1. SSO org resolution (
ArrowUploader.sso_get_token)Four-layer fallback for deciding which org the session is bound to after SSO:
active_organization: use it, but treat the reservedSITEsentinel as unbound (with a warning that the IdP org claim is likely mis-mapped server-side). Caller-suppliedorg_namemust match or be absent.org_name, server silent: preserve caller intent; membership validates lazily on later requests._personal_org_from_api()looks up the user's personal org viaGET /api/v2/my/organizations/filtered onis_personal. Replaces the earlier JWT-username-slugify reconstruction, which couldn't reproduce server-side slug-collision suffixing.2. Unified org-switch request (
graphistry/utils/requests.py)GraphistryClient.switch_organdArrowUploader._switch_orghad duplicate POST-and-validate code. Newswitch_org_request()is the single source of truth and raises:OrgSwitchError— non-2xx HTTP status or non-OK body status.OrgSwitchIdpChallenge— server quirk: for a plain member (not owner/admin) of an org with its own IdP, the switch endpoint returnsstatus: OKwithout actually switching and instead returns an SSO challenge indata['idp']. Previously this was silently treated as success, leaving the caller on their previous org.3. Per-org verified-token cache (
ClientSession)Replaces the single
_last_switched_org_tokentuple with_verified_org_tokens: Dict[org_name -> (token, exp)]plusis_org_verified()/mark_org_verified()/get_verified_token(). Entries self-expire based on the JWTexpclaim (read without signature verification — the token is already trusted, having come from the server via the auth flow just completed)._maybe_switch_orgnever re-triggers an IdP challenge.switch_org()gains two fast paths: already verified under the current token → skip the round trip; a still-unexpired older token was verified for that org → swap it back in as active.refresh()carries the org grant forward to the newly issued token.4. SSO polling robustness (
GraphistryClient.sso_login)New
SsoPendingExceptiondistinguishes "browser login not finished yet" from real failures. The poll loop now:sso_timeout;requests.RequestException/ JSONValueErrorup toSSO_POLL_MAX_CONSECUTIVE_ERRORS(3) consecutive times before giving up;SsoStateInvalidExceptionand other permanent errors immediately.The per-poll log is demoted from
errortodebugso it stops printing once a second during a normal login.5. Mid-switch org tracking
New
session.sso_requested_org_namerecords the org an in-flight SSO login was initiated for, sincesession.org_namestill holds the previous org until the switch is confirmed._sso_get_token()passes it explicitly toArrowUploader.6. Cleanup
Removed dead commented-out code (
sso_state/sso_auth_urlstubs,api_token_refresh_ms, old polling loop), return-type annotations and mypy fixes inutils/requests.py/switch_org.Tests
tests/utils/test_requests.py(new) —switch_org_requestsuccess, HTTP error, non-OK body, and IdP-challenge branches.test_arrow_uploader.py—_switch_orgmemoization/no-memoization on failure,SITEsentinel handling,_personal_org_from_apifallback,SsoPendingExceptionclassification.test_client_session.py— verified-token cache and JWTexpexpiry.test_pygraphistry.py—switch_orgfast paths, poll-loop failure classification, refresh carrying the org grant.🤖 Generated with Claude Code
https://claude.ai/code/session_01TyjxnzaqeB3SeirofUQhSj