Skip to content

fix(realtime_client): Prevent sending expired tokens #1095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
await all setAuth calls
  • Loading branch information
dshukertjr committed Dec 15, 2024
commit 4205d8d098ce48d929ca855095cfdf6ae39d4d39
6 changes: 4 additions & 2 deletions packages/realtime_client/lib/src/realtime_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,11 @@ class RealtimeChannel {

joinPush.receive(
'ok',
(response) {
(response) async {
final serverPostgresFilters = response['postgres_changes'];
if (socket.accessToken != null) socket.setAuth(socket.accessToken);
if (socket.accessToken != null) {
await socket.setAuth(socket.accessToken);
}

if (serverPostgresFilters == null) {
if (callback != null) {
Expand Down
6 changes: 3 additions & 3 deletions packages/realtime_client/lib/src/realtime_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ class RealtimeClient {
if (heartbeatTimer != null) heartbeatTimer!.cancel();
heartbeatTimer = Timer.periodic(
Duration(milliseconds: heartbeatIntervalMs),
(Timer t) => sendHeartbeat(),
(Timer t) async => await sendHeartbeat(),
);
for (final callback in stateChangeCallbacks['open']!) {
callback();
Expand Down Expand Up @@ -533,7 +533,7 @@ class RealtimeClient {
}

@internal
void sendHeartbeat() {
Future<void> sendHeartbeat() async {
if (!isConnected) {
return;
}
Expand All @@ -555,6 +555,6 @@ class RealtimeClient {
payload: {},
ref: pendingHeartbeatRef!,
));
setAuth(accessToken);
await setAuth(accessToken);
}
}
18 changes: 9 additions & 9 deletions packages/realtime_client/test/socket_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ void main() {
await Future.delayed(const Duration(milliseconds: 200));
expect(opens, 1);

socket.sendHeartbeat();
await socket.sendHeartbeat();
// need to wait for event to trigger
await Future.delayed(const Duration(seconds: 1));
expect(lastMsg['event'], 'heartbeat');
Expand Down Expand Up @@ -459,7 +459,7 @@ void main() {

test(
"sets access token, updates channels' join payload, and pushes token to channels",
() {
() async {
final mockedChannel1 = MockChannel();
when(() => mockedChannel1.joinedOnce).thenReturn(true);
when(() => mockedChannel1.isJoined).thenReturn(true);
Expand All @@ -484,7 +484,7 @@ void main() {
final channel1 = mockedSocket.channel(tTopic1);
final channel2 = mockedSocket.channel(tTopic2);

mockedSocket.setAuth(token);
await mockedSocket.setAuth(token);

expect(mockedSocket.accessToken, token);

Expand All @@ -498,7 +498,7 @@ void main() {

test(
"sets access token, updates channels' join payload, and pushes token to channels if is not a jwt",
() {
() async {
final mockedChannel1 = MockChannel();
final mockedChannel2 = MockChannel();
final mockedChannel3 = MockChannel();
Expand Down Expand Up @@ -537,7 +537,7 @@ void main() {
final pushPayload = {'access_token': token};
final updateJoinPayload = {'access_token': token};

mockedSocket.setAuth(token);
await mockedSocket.setAuth(token);

expect(mockedSocket.accessToken, token);

Expand Down Expand Up @@ -581,18 +581,18 @@ void main() {

//! Unimplemented Test: closes socket when heartbeat is not ack'd within heartbeat window

test('pushes heartbeat data when connected', () {
test('pushes heartbeat data when connected', () async {
mockedSocket.connState = SocketStates.open;

mockedSocket.sendHeartbeat();
await mockedSocket.sendHeartbeat();

verify(() => mockedSink.add(captureAny(that: equals(data)))).called(1);
});

test('no ops when not connected', () {
test('no ops when not connected', () async {
mockedSocket.connState = SocketStates.connecting;

mockedSocket.sendHeartbeat();
await mockedSocket.sendHeartbeat();
verifyNever(() => mockedSink.add(any()));
});
});
Expand Down
Loading