-
-
Notifications
You must be signed in to change notification settings - Fork 214
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
Changes from 6 commits
17bf76f
8045727
7e660c3
9b323b9
4205d8d
54ae4af
eefb5bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,7 @@ class RealtimeCloseEvent { | |
} | ||
|
||
class RealtimeClient { | ||
// This is named `accessTokenValue` in supabase-js | ||
String? accessToken; | ||
List<RealtimeChannel> channels = []; | ||
final String endPoint; | ||
|
@@ -89,6 +90,8 @@ class RealtimeClient { | |
}; | ||
int longpollerTimeout = 20000; | ||
SocketStates? connState; | ||
// This is called `accessToken` in realtime-js | ||
Future<String> Function()? customAccessToken; | ||
|
||
/// Initializes the Socket | ||
/// | ||
|
@@ -129,6 +132,7 @@ class RealtimeClient { | |
this.longpollerTimeout = 20000, | ||
RealtimeLogLevel? logLevel, | ||
this.httpClient, | ||
this.customAccessToken, | ||
}) : endPoint = Uri.parse('$endPoint/${Transports.websocket}') | ||
.replace( | ||
queryParameters: | ||
|
@@ -403,15 +407,43 @@ class RealtimeClient { | |
/// Sets the JWT access token used for channel subscription authorization and Realtime RLS. | ||
/// | ||
/// `token` A JWT strings. | ||
void setAuth(String? token) { | ||
accessToken = token; | ||
Future<void> setAuth(String? token) async { | ||
dshukertjr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
final tokenToSend = | ||
token ?? (await customAccessToken?.call()) ?? accessToken; | ||
|
||
if (tokenToSend != null) { | ||
Map<String, dynamic>? parsed; | ||
try { | ||
final decoded = | ||
utf8.decode(base64Url.decode(tokenToSend.split('.')[1])); | ||
parsed = json.decode(decoded); | ||
} catch (e) { | ||
// ignore parsing errors | ||
} | ||
if (parsed != null && parsed['exp'] != null) { | ||
final now = (DateTime.now().millisecondsSinceEpoch / 1000).floor(); | ||
final valid = now - parsed['exp'] < 0; | ||
if (!valid) { | ||
log( | ||
'auth', | ||
'InvalidJWTToken: Invalid value for JWT claim "exp" with value ${parsed['exp']}', | ||
null, | ||
Level.FINE, | ||
); | ||
throw FormatException( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the js client doesn't throw an exception, but logs the issue and then fails silently. I think it's better to not throw, because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great catch! I forgot to add, but the way js client handles the expired JWT changes in another PR though. supabase/realtime-js#439 |
||
'InvalidJWTToken: Invalid value for JWT claim "exp" with value ${parsed['exp']}'); | ||
} | ||
} | ||
} | ||
|
||
accessToken = tokenToSend; | ||
|
||
for (final channel in channels) { | ||
if (token != null) { | ||
channel.updateJoinPayload({'access_token': token}); | ||
if (tokenToSend != null) { | ||
channel.updateJoinPayload({'access_token': tokenToSend}); | ||
} | ||
if (channel.joinedOnce && channel.isJoined) { | ||
channel.push(ChannelEvents.accessToken, {'access_token': token}); | ||
channel.push(ChannelEvents.accessToken, {'access_token': tokenToSend}); | ||
} | ||
} | ||
} | ||
|
@@ -436,7 +468,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(); | ||
|
@@ -502,7 +534,7 @@ class RealtimeClient { | |
} | ||
|
||
@internal | ||
void sendHeartbeat() { | ||
Future<void> sendHeartbeat() async { | ||
if (!isConnected) { | ||
return; | ||
} | ||
|
@@ -524,6 +556,6 @@ class RealtimeClient { | |
payload: {}, | ||
ref: pendingHeartbeatRef!, | ||
)); | ||
setAuth(accessToken); | ||
await setAuth(accessToken); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,4 @@ dev_dependencies: | |
lints: ^3.0.0 | ||
mocktail: ^1.0.0 | ||
test: ^1.16.5 | ||
crypto: ^3.0.0 |
Uh oh!
There was an error while loading. Please reload this page.