Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

fix(mobile): a rate-limited refresh no longer signs the phone out

dee1dea9303d526
diff --git a/mobile/lib/viewer/email_code_api.dart b/mobile/lib/viewer/email_code_api.dart
index 8db34351a..626705f2c 100644
--- a/mobile/lib/viewer/email_code_api.dart
+++ b/mobile/lib/viewer/email_code_api.dart
@@ -89,10 +89,18 @@ class EmailCodeApi {
} on DioException {
throw const DirectAuthException(_unreachable);
}
- if ((res.statusCode ?? 500) >= 500) {
+ // Neither is being told to slow down or to come back later (429, 408): that says nothing
+ // about the token. It is exactly what a phone hammering a refresh after a wake gets told.
+ final status = res.statusCode ?? 500;
+ if (status >= 500 || status == 429 || status == 408) {
+ throw const DirectAuthException(_unreachable);
+ }
+ // Nor is a page that is not this API's envelope at all: a gateway's error page, a captive
+ // portal's login. Only the service itself can say the token is no good.
+ final body = res.data;
+ if (body is! Map || !body.containsKey('status')) {
throw const DirectAuthException(_unreachable);
}
- final body = res.data is Map ? res.data as Map : const {};
final tokens = body['status'] == 1 ? _tokens(body['data']) : null;
return tokens ??
(throw const DirectAuthException(
diff --git a/mobile/test/viewer/email_code_api_test.dart b/mobile/test/viewer/email_code_api_test.dart
index 4704d6fb2..a1d31dd98 100644
--- a/mobile/test/viewer/email_code_api_test.dart
+++ b/mobile/test/viewer/email_code_api_test.dart
@@ -78,6 +78,84 @@ void main() {
expect(tokens.autonomousEnv, 'stag');
});
+ test('a refusal with no words of its own reads as unreachable', () async {
+ for (final body in <Object?>[
+ {'status': 0, 'message': ' '},
+ {'status': 0},
+ 'not an envelope',
+ ]) {
+ final http = FakeHttp({send: (status: 400, body: body)});
+ await expectLater(
+ api(http).sendCode('[email protected]'),
+ throwsA(
+ isA<DirectAuthException>().having(
+ (e) => e.message,
+ 'message',
+ startsWith('Could not reach the sign-in service'),
+ ),
+ ),
+ );
+ }
+ });
+
+ test('a dropped network reads as unreachable', () async {
+ await expectLater(
+ api(FakeHttp({})).sendCode('[email protected]'),
+ throwsA(isA<DirectAuthException>()),
+ );
+ });
+
+ test('a sign-in that yields no token is a failure, not a session', () async {
+ for (final data in <Object?>[
+ null,
+ {'access_token': ''},
+ {'access_token': 7},
+ ]) {
+ final http = FakeHttp({
+ signIn: (status: 200, body: {'status': 1, 'data': data}),
+ });
+ await expectLater(
+ api(http).signIn(email: '[email protected]', code: '1234'),
+ throwsA(isA<DirectAuthException>()),
+ );
+ }
+ });
+
+ test('odd token fields are dropped rather than trusted', () async {
+ final http = FakeHttp({
+ signIn: (
+ status: 200,
+ body: {
+ 'status': 1,
+ 'data': {'access_token': 'a', 'refresh_token': '', 'expire_in': -5},
+ },
+ ),
+ });
+ final tokens = await api(http).signIn(email: '[email protected]', code: '1');
+ expect(tokens.refreshToken, isNull);
+ expect(tokens.expiresIn, isNull);
+ });
+
+ test('a fractional expiry is kept, in whole seconds', () async {
+ final http = FakeHttp({
+ signIn: (
+ status: 200,
+ body: {
+ 'status': 1,
+ 'data': {'access_token': 'a', 'expire_in': 3600.9},
+ },
+ ),
+ });
+ expect((await api(http).signIn(email: 'a', code: '1')).expiresIn, 3600);
+ });
+
+ test('the default client is built without reaching anywhere', () {
+ expect(
+ EmailCodeApi(config: const AppConfig(apiBaseUrl: 'https://h.invalid')),
+ isA<EmailCodeApi>(),
+ );
+ });
+
group('refresh', () {
test('renews with the refresh grant', () async {
final http = FakeHttp({signIn: (status: 200, body: issued)});
@@ -101,6 +179,55 @@ void main() {
);
});
+ // ⚠️ Only the service ANSWERING that the token is no good may end a session: a sign-out
+ // deletes a refresh token nothing can bring back. Being told to slow down is not that
+ // answer, and neither is a page from something in front of the service.
+ test(
+ 'a rate limit or a stranger\'s page does not end the session',
+ () async {
+ for (final http in [
+ FakeHttp({
+ signIn: (
+ status: 429,
+ body: {'status': 0, 'message': 'Too many requests'},
+ ),
+ }),
+ FakeHttp({
+ signIn: (status: 408, body: {'status': 0}),
+ }),
+ // A gateway's or a captive portal's HTML, not the account API's envelope.
+ FakeHttp({signIn: (status: 403, body: '<html>Access denied</html>')}),
+ FakeHttp({signIn: (status: 200, body: const <String, Object?>{})}),
+ ]) {
+ await expectLater(
+ api(http).refresh('refresh-0'),
+ throwsA(
+ isA<DirectAuthException>().having(
+ (e) => e.signedOut,
+ 'out',
+ false,
+ ),
+ ),
+ );
+ }
+ },
+ );
+
+ test('a refusal with a 4xx of its own still ends the session', () async {
+ final http = FakeHttp({
+ signIn: (
+ status: 401,
+ body: {'status': 0, 'message': 'Invalid refresh token'},
+ ),
+ });
+ await expectLater(
+ api(http).refresh('refresh-0'),
+ throwsA(
+ isA<DirectAuthException>().having((e) => e.signedOut, 'out', true),
+ ),
+ );
+ });
+
test('an outage does not — the refresh token survives it', () async {
for (final http in [
FakeHttp({