Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions packages/supabase_auth/lib/src/auth_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,28 @@ bool _holdSameTokens(Session? a, Session? b) =>
a.accessToken == b.accessToken &&
a.refreshToken == b.refreshToken;

/// Whether [current] is the server session [revoked] came from: it holds the
/// same tokens, or the same `session_id` claim, which a refresh keeps and a
/// new sign-in changes.
bool _isSameServerSession(Session revoked, Session? current) {
if (current == null) return false;
if (_holdSameTokens(revoked, current)) return true;
final sessionId = _sessionIdOf(revoked);
return sessionId != null && sessionId == _sessionIdOf(current);
}

String? _sessionIdOf(Session session) {
try {
final claims = decodeJwtPayload(session.accessToken).claims;
return switch (claims['session_id']) {
final String id => id,
_ => null,
};
} on AuthInvalidJwtException {
return null;
}
}

/// {@template auth_client}
/// API client to interact with the Supabase Auth server.
///
Expand Down Expand Up @@ -1608,14 +1630,12 @@ class AuthClient {
await _clearSignedOutState();
return;
}
// A sign-in or sign-out that happened while the session was refreshed or
// the request was in flight is newer than this sign-out.
final isCurrentSession = _holdSameTokens(session, _currentSession);
final versionBeforeRevoke = _sessionVersion;
await _revokeSessions(session, scope);
// A sign-in or sign-out that happened while the session was refreshed or
// the request was in flight is newer than this sign-out, while a refresh
// of the revoked session is not.
if (scope == SignOutScope.global &&
isCurrentSession &&
_sessionVersion == versionBeforeRevoke) {
_isSameServerSession(session, _currentSession)) {
await _signOutLocally(SignOutReason.userInitiated);
}
}
Expand Down
62 changes: 62 additions & 0 deletions packages/supabase_auth/test/sign_out_scope_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,68 @@ void main() {
expect(client.currentSession, session);
expect(events, isNot(contains(AuthChangeEvent.signedOut)));
});

test(
'signs out when the session was refreshed during the request',
() async {
await signInTestUser(client, claims: {'session_id': 'session-1'});
await pumpEventQueue();
events.clear();
final refresh = Completer<Response>();
final logout = Completer<Response>();
httpClient
..stubHandler(
(_) => refresh.future,
path: '/auth/v1/token',
query: {'grant_type': 'refresh_token'},
)
..stubHandler((_) => logout.future, path: '/auth/v1/logout');

final refreshed = client.refreshSession();
final signOut = client.signOut(scope: SignOutScope.global);
await pumpEventQueue();
refresh.complete(
jsonResponse(
testSessionResponseJson(
accessToken: unsignedTestJwt({
'sub': testUserId,
'session_id': 'session-1',
}),
refreshToken: 'rotated',
),
),
);
await refreshed;
logout.complete(Response('', 204));
await signOut;
await pumpEventQueue();

expect(client.currentSession, isNull);
expect(events, [
AuthChangeEvent.tokenRefreshed,
AuthChangeEvent.signedOut,
]);
expect(signOutReasons, [SignOutReason.userInitiated]);
},
);

test('keeps another session of the same user signed in', () async {
await signInTestUser(client, claims: {'session_id': 'session-1'});
final logout = Completer<Response>();
httpClient.stubHandler((_) => logout.future, path: '/auth/v1/logout');

final signOut = client.signOut(scope: SignOutScope.global);
await pumpEventQueue();
final session = await signInTestUser(
client,
claims: {'session_id': 'session-2'},
);
logout.complete(Response('', 204));
await signOut;
await pumpEventQueue();

expect(client.currentSession?.accessToken, session.accessToken);
});
});

group('signOut with the others scope', () {
Expand Down
Loading