Repository navigation
Auth module: Align Session.SignOut with the cross-SDK sign-out spec on an ended session, a missing session under the global scope and a refresh that fails before the request - #175
Conversation
…ec on an ended session, a missing session under the global scope and a refresh that fails before the request. Co-Authored-By: Claude <noreply@anthropic.com>
There was a problem hiding this comment.
The bullet point list above looks correct to me.
I got Claude to review the implementation, and it found some issues that also looked correct to me, so I got it to report them as well.
It is really great to have Go as a guinea pig for the spec, as it really illustrates why having a more comprehensible shared test suite would be very valuable
| if !errors.As(err, &ended) { | ||
| return nil, err | ||
| } | ||
| if ended.Status == session.SignedOut && ended.Cause != nil { |
There was a problem hiding this comment.
Repeat sign-out returns the old rejection. Say the token has expired and the refresh gets 400 refresh_token_not_found. The first SignOut() ends the session with EndedError{SignedOut, Cause: rejection} and returns the rejection. A retry, or a second goroutine calling SignOut(), then hits the s.status != Live branch in FreshToken, which returns the stored EndedError with Cause still set. That lands here and returns the rejection again. The doc says an already-ended session returns (&SignOutResult{}, nil). Could we add a test for signing out twice after a rejected refresh?
There was a problem hiding this comment.
Fixed in 35acf1d. SignOutToken returns the rejection itself and reserves *EndedError for a session that had already ended, so the global path reports the rejection once and success after that. The subtest now signs out a second time.
| bearer := held.AccessToken() | ||
| if held.ExpiresAt().Sub(s.client.now()) <= expiryMargin { | ||
| renewed, err := s.client.refreshGrant(ctx, held.RefreshToken()) | ||
| if err != nil { |
There was a problem hiding this comment.
A transient refresh failure leaves the server session unrevoked. Say the token has 20s left, so it's inside the margin but still valid, and the refresh gets a 503 or a network error. Before this change the still-valid held.AccessToken() went to /logout?scope=local. Now we return the error without sending anything. signOutLocally has already ended the session locally, so a retried SignOut(WithLocalScope()) takes the EndedError path and returns success. The refresh token stays valid on the server and nothing can retry it. Could we fall back to the captured bearer when it hasn't expired?
There was a problem hiding this comment.
Fixed in 35acf1d. The flowchart's branch is Access token expired?, so an unexpired token goes straight to /logout under the spec and the first version read it too narrowly. revokeHeld now sends the held token while it is unexpired on any failure other than the three already-signed-out codes.
| } | ||
| }) | ||
| if endingNow { | ||
| _ = s.perform(ctx, clearing) |
There was a problem hiding this comment.
Store-clear errors are dropped. _ = s.perform(ctx, clearing) discards the error here, and _, err = s.renew(...) discards the renew writeErr too. If the store's Delete fails, the session ends in memory but the store still holds it. SignOut returns only the rejection or ErrSessionExpired, and a later SessionFromStore restores the dead document. The SignOut doc says a store that can't be cleared is reported as the wrapped error.
There was a problem hiding this comment.
Fixed in 35acf1d for the call that performs the clear: the failure is returned beside the rejection, as the local path already does. A clear performed by a joined refresh still reaches no one, since only the starter writes the store.
| return token, err | ||
| } | ||
|
|
||
| _, err = s.renew(ctx, ending) |
There was a problem hiding this comment.
The result depends on concurrent calls. renew can join a refresh that another caller started, and that refresh ends the session with the starter's ending status, not ours. Example: a Token() refresh is in flight with ending=Expired, and a bare SignOut joins it. The server rejects the refresh and the session ends as Expired. signOutGlobally sees Status=Expired, returns success instead of the rejection, and the other sessions are never revoked. The reverse also happens: WithOthersScope joins a flight started by a global sign-out and returns ErrSignedOut instead of ErrSessionExpired.
There was a problem hiding this comment.
Fixed in 35acf1d. The flight carries the ending status, raised to signed out when a sign-out joins, so the read gets ErrSignedOut and the sign-out gets the rejection. A new subtest covers the join.
| }) | ||
| return token, err | ||
| } | ||
| if !s.definitive(err) { |
There was a problem hiding this comment.
A transient failure blocks sign-out while the token is still valid. With 30s left on the token and a 503 from /token, this returns the 503 and no logout is sent. Before this change, SignOut went through AccessToken(), got the unexpired token back, and the logout succeeded. So a /token outage now stops global and others-scope sign-outs even though the bearer would have worked. Maybe fall back to the current token when !definitive(err) and it hasn't expired?
There was a problem hiding this comment.
Fixed in 35acf1d, with the same correction as in revokeHeld: the sign-out path now takes its token exactly as AccessToken does, and others is AccessToken followed by the request.
| err = s.endedErrorLocked() | ||
| case s.state.expiresAt.Sub(s.now()) > s.expiryMargin: | ||
| token = s.state.accessToken | ||
| case s.rejected: |
There was a problem hiding this comment.
A remembered rejection ends a still-valid session. Say s.rejected is true and the token has 50s left. Then SignOut(WithOthersScope()) ends the current session as Expired and returns ErrSessionExpired without sending the logout. That contradicts "this one stays live". Token() would keep this session live until it actually expires, and the valid token could still have revoked the other sessions.
There was a problem hiding this comment.
Fixed in 35acf1d. A remembered rejection ends the session only once the token has expired, as Token already did, so an unexpired token is sent. A new subtest covers it.
| if !s.definitive(err) { | ||
| return "", err | ||
| } | ||
| // renew ended the session only if the token had expired. A rejection |
There was a problem hiding this comment.
nit/design: Two functions now decide whether to end the session. renew ends it on a rejection only when the token has expired, and FreshToken checks definitive(err) again and ends it itself. The *EndedError case only matches because Unwrap exposes the *Error cause. If renew's expiry rule or EndedError.Unwrap changes, this has to be updated by hand. Passing an "end on any rejection" flag to renew would let us drop the second lock block and the extra perform.
There was a problem hiding this comment.
Taken in 35acf1d. renew alone ends the session on a rejection, and the second lock block and its perform are gone.
| // as it was, and a rejection, remembered or answered now, ends the session | ||
| // with status ending and is returned as the [*EndedError] carrying it as | ||
| // Cause. An already ended session returns its [*EndedError]. | ||
| func (s *Session) FreshToken(ctx context.Context, ending Status) (string, error) { |
There was a problem hiding this comment.
nit/design: FreshToken repeats most of Token's structure: the lock, the status/margin/rejected switch, renew, then reading the state again under the lock. The two copies have already drifted. Token handles ctx.Err() and this doesn't, and revokeHeld uses <= expiryMargin where this uses >. Could the two share one helper?
There was a problem hiding this comment.
Taken in 35acf1d. Token and SignOutToken share one body, differing in the ending status and in whether an ended session's error replaces the rejection. revokeHeld stays separate because the spec makes its refresh a detached one.
| } | ||
| scope := signout.Resolve(options...).Scope() | ||
| if scope == "local" { | ||
| switch scope { |
There was a problem hiding this comment.
nit: "local", "others" and "global" appear as string literals in SignOut, signOutGlobally, revokeOthers, revokeHeld and nothingLeftToRevoke. Could these be constants shared with the signout package? A typo like nothingLeftToRevoke(err, "lcoal") would compile and silently turn session_not_found into a failure.
There was a problem hiding this comment.
Done in 35acf1d: one definition in auth/internal/scopes, read by signout and auth.
…expired token when its refresh fails, and report a rejected refresh once. Co-Authored-By: Claude <noreply@anthropic.com>
… to workaround boundaries. Co-Authored-By: Claude <noreply@anthropic.com>
|
Thanks for the review input, @cmelchior - Pushed 35acf1d for all nine threads. The sign-out path now takes its token the way |
🤖 Claude's Summary
Aligns
Session.SignOutwith the cross-SDK sign-out spec,sign_out.mdas merged in supabase/sdk#302, in the places the first implementation differed:ErrSignedOut, having no credential for the one effect it asks for.session_not_foundis a failure that keeps the session. The server answers it before the logout handler runs, so nothing was revoked, andWithLocalScopethen ends the session.user_not_foundis success.refresh_token_not_found,session_not_foundorsession_expiredis success, the session being gone already, and any other failure of an expired token is reported. Under the global scope a rejected refresh of an expired token ends the session as signed out and returns the server's rejection, under the others scope it ends the session as expired and returnsErrSessionExpired, and a transient failure keeps the session and returns the error under both.