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 refresh in the air cannot bring back a signed-out session

deee3595a72b195
diff --git a/mobile/lib/viewer/direct_auth.dart b/mobile/lib/viewer/direct_auth.dart
index f9571c916..30f2e89af 100644
--- a/mobile/lib/viewer/direct_auth.dart
+++ b/mobile/lib/viewer/direct_auth.dart
@@ -25,30 +25,44 @@ class DirectAuth implements AccessTokenSource {
Future<bool> hasSession() async =>
((await session.accessToken()) ?? '').isNotEmpty;
+ /// Which session [session] holds, counted: bumped every time it is replaced — a sign-in, a
+ /// sign-out. A refresh that set off under one count and lands under another answers for a
+ /// session this device no longer has, and must not touch the one it has now (see [_refresh]).
+ int _generation = 0;
+
Future<void> signIn(
IssuedTokens tokens, {
SessionIssuer issuer = SessionIssuer.sso,
- }) => session.saveLogin(
- token: tokens.token,
- refreshToken: tokens.refreshToken,
- autonomousEnv: tokens.autonomousEnv ?? api.config.autonomousEnv,
- expiresIn: tokens.expiresIn,
- issuer: issuer,
- );
+ }) {
+ _generation++;
+ return session.saveLogin(
+ token: tokens.token,
+ refreshToken: tokens.refreshToken,
+ autonomousEnv: tokens.autonomousEnv ?? api.config.autonomousEnv,
+ expiresIn: tokens.expiresIn,
+ issuer: issuer,
+ );
+ }
/// A session Harness issued itself is ended at the backend too, so its
/// refresh token is dead even if a copy of this phone's storage turns up.
/// Only briefly waited on: signing out never hangs on the network.
+ ///
+ /// ⚠️ Forgotten HERE before the backend is asked, not after. For the few
+ /// seconds the revoke may take, the session used to still be on disk — and a
+ /// socket redialling in that window refreshed it and saved the renewal, after
+ /// the person had signed out.
Future<void> signOut() async {
- if (await session.issuer() == SessionIssuer.harness) {
- final refreshToken = await session.refreshToken();
- if (refreshToken != null && refreshToken.isNotEmpty) {
- try {
- await api.revoke(refreshToken).timeout(const Duration(seconds: 3));
- } catch (_) {}
- }
- }
+ _generation++;
+ final revoke = await session.issuer() == SessionIssuer.harness
+ ? await session.refreshToken()
+ : null;
await session.clear();
+ if (revoke != null && revoke.isNotEmpty) {
+ try {
+ await api.revoke(revoke).timeout(const Duration(seconds: 3));
+ } catch (_) {}
+ }
}
@override
@@ -76,8 +90,16 @@ class DirectAuth implements AccessTokenSource {
expiresAt != null &&
expiresAt.isBefore(DateTime.now().toUtc().add(_refreshSkew));
+ /// ⚠️ **Nothing a refresh brings back is saved once the session it was for has gone.** It is
+ /// the one network call here that WRITES the session, and it can be in the air for as long as
+ /// the network takes: sign out meanwhile and its renewal put the session back (the next launch
+ /// walked straight past the sign-in screen); sign in as somebody else and it overwrote them. Its
+ /// refusal likewise cleared a session that was not the one refused. Either way, whoever asked is
+ /// answered for the session there is now.
Future<String> _refresh() async {
+ final generation = _generation;
final refreshToken = await session.refreshToken();
+ if (generation != _generation) return await _currentToken();
if (refreshToken == null || refreshToken.isEmpty) {
await session.clear();
throw const DirectAuthException(
@@ -95,6 +117,7 @@ class DirectAuth implements AccessTokenSource {
autonomousEnv: autonomousEnv,
),
};
+ if (generation != _generation) return await _currentToken();
await session.saveRefresh(
token: tokens.token,
refreshToken: tokens.refreshToken,
@@ -103,8 +126,18 @@ class DirectAuth implements AccessTokenSource {
);
return tokens.token;
} on DirectAuthException catch (error) {
+ if (generation != _generation) return await _currentToken();
if (error.signedOut) await session.clear();
rethrow;
}
}
+
+ /// The token of the session this device holds now, for a refresh that outlived its own.
+ Future<String> _currentToken() async {
+ final token = await session.accessToken();
+ if (token == null || token.isEmpty) {
+ throw const DirectAuthException('Not signed in.', signedOut: true);
+ }
+ return token;
+ }
}
diff --git a/mobile/test/viewer/direct_auth_test.dart b/mobile/test/viewer/direct_auth_test.dart
new file mode 100644
index 000000000..7ebe5ef1f
--- /dev/null
+++ b/mobile/test/viewer/direct_auth_test.dart
@@ -0,0 +1,366 @@
+import 'dart:async';
+import 'dart:convert';
+import 'dart:typed_data';
+
+import 'package:dio/dio.dart';
+import 'package:fake_async/fake_async.dart';
+import 'package:flutter_test/flutter_test.dart';
+
+import 'package:harness_mobile/auth/auth_session.dart';
+import 'package:harness_mobile/core/config.dart';
+import 'package:harness_mobile/viewer/direct_auth.dart';
+import 'package:harness_mobile/viewer/direct_auth_api.dart';
+import 'package:harness_mobile/viewer/email_code_api.dart';
+
+import '../voice_fakes.dart' show MemoryKeyValueStore;
+import 'fake_http.dart';
+
+/// The backend's `/api/auth/refresh`, held open until the test lets it answer — a refresh that is
+/// still in the air when something else happens to the session.
+class _HeldRefresh implements HttpClientAdapter {
+ final calls = <Map<String, Object?>>[];
+ final _answers = <Completer<FakeReply>>[];
+
+ Dio dio() => Dio(
+ BaseOptions(
+ baseUrl: 'https://fake.invalid',
+ validateStatus: (status) => status != null && status < 600,
+ ),
+ )..httpClientAdapter = this;
+
+ /// Resolves once a request is waiting here for its answer.
+ Future<void> arrived() async {
+ for (var i = 0; i < 100 && _answers.every((c) => c.isCompleted); i++) {
+ await Future<void>.delayed(Duration.zero);
+ }
+ }
+
+ /// Answers the oldest request still waiting — once it has got here: Dio takes a few hops of
+ /// its own between the call and the adapter.
+ Future<void> answer(FakeReply reply) async {
+ await arrived();
+ _answers.firstWhere((c) => !c.isCompleted).complete(reply);
+ }
+
+ @override
+ Future<ResponseBody> fetch(
+ RequestOptions options,
+ Stream<Uint8List>? requestStream,
+ Future<void>? cancelFuture,
+ ) async {
+ calls.add(Map<String, Object?>.from(options.data as Map));
+ final pending = Completer<FakeReply>();
+ _answers.add(pending);
+ final reply = await pending.future;
+ return ResponseBody.fromString(
+ jsonEncode(reply.body),
+ reply.status,
+ headers: {
+ Headers.contentTypeHeader: [Headers.jsonContentType],
+ },
+ );
+ }
+
+ @override
+ void close({bool force = false}) {}
+}
+
+FakeReply _renewed(String token, {String? refreshToken, int? expiresIn}) => (
+ status: 200,
+ body: {
+ 'success': true,
+ 'data': {
+ 'token': token,
+ 'refreshToken': ?refreshToken,
+ 'expiresIn': ?expiresIn,
+ },
+ },
+);
+
+void main() {
+ const config = AppConfig(apiBaseUrl: 'https://h.invalid');
+
+ late MemoryKeyValueStore storage;
+ late AuthSession session;
+ late _HeldRefresh backend;
+ late DirectAuth auth;
+
+ setUp(() {
+ storage = MemoryKeyValueStore();
+ session = AuthSession(storage: storage);
+ backend = _HeldRefresh();
+ auth = DirectAuth(
+ session: session,
+ api: DirectAuthApi(config: config, dio: backend.dio()),
+ emailCodes: EmailCodeApi(config: config, dio: FakeHttp({}).dio()),
+ );
+ });
+
+ /// A session whose access token has already lapsed, so the next ask refreshes it.
+ Future<void> signInStale({
+ String token = 'old',
+ String refreshToken = 'r1',
+ SessionIssuer issuer = SessionIssuer.sso,
+ }) async {
+ await auth.signIn(
+ IssuedTokens(token: token, refreshToken: refreshToken, expiresIn: 30),
+ issuer: issuer,
+ );
+ }
+
+ group('handing out a token', () {
+ test('no session is a sign-out, not a blip', () async {
+ expect(await auth.hasSession(), isFalse);
+ await expectLater(
+ auth.accessToken(),
+ throwsA(
+ isA<DirectAuthException>().having((e) => e.signedOut, 'out', true),
+ ),
+ );
+ });
+
+ test('a fresh token is handed out as it is, with no refresh', () async {
+ await auth.signIn(
+ const IssuedTokens(token: 't', refreshToken: 'r', expiresIn: 3600),
+ );
+ expect(await auth.hasSession(), isTrue);
+ expect(await auth.accessToken(), 't');
+ expect(backend.calls, isEmpty);
+ });
+
+ test('a token with no recorded expiry is taken at face value', () async {
+ await auth.signIn(const IssuedTokens(token: 't', refreshToken: 'r'));
+ expect(await auth.accessToken(), 't');
+ expect(backend.calls, isEmpty);
+ });
+
+ test('a token inside the last minute is renewed first', () async {
+ await signInStale();
+ final token = auth.accessToken();
+ await backend.arrived();
+ await backend.answer(_renewed('new', expiresIn: 3600));
+ expect(await token, 'new');
+ expect(backend.calls.single['refreshToken'], 'r1');
+ // The renewal is kept, and the refresh token the server did not rotate survives it.
+ expect(await session.accessToken(), 'new');
+ expect(await session.refreshToken(), 'r1');
+ });
+
+ test('two asks while one refresh is in the air share it', () async {
+ await signInStale();
+ final first = auth.accessToken();
+ final second = auth.accessToken(force: true, failedToken: 'old');
+ await backend.arrived();
+ await backend.answer(
+ _renewed('new', refreshToken: 'r2', expiresIn: 3600),
+ );
+ expect(await first, 'new');
+ expect(await second, 'new');
+ expect(backend.calls, hasLength(1));
+ expect(await session.refreshToken(), 'r2');
+ });
+
+ test(
+ 'a refused token that somebody already replaced is not refreshed again',
+ () async {
+ await auth.signIn(
+ const IssuedTokens(token: 'current', refreshToken: 'r'),
+ );
+ expect(
+ await auth.accessToken(force: true, failedToken: 'older'),
+ 'current',
+ );
+ expect(backend.calls, isEmpty);
+ },
+ );
+
+ test('a session with nothing to renew with ends, and says so', () async {
+ await auth.signIn(const IssuedTokens(token: 't', expiresIn: 30));
+ await expectLater(
+ auth.accessToken(),
+ throwsA(
+ isA<DirectAuthException>().having((e) => e.signedOut, 'out', true),
+ ),
+ );
+ expect(await session.accessToken(), isNull);
+ });
+
+ test('a dead refresh token clears the session', () async {
+ await signInStale();
+ final token = auth.accessToken();
+ await backend.arrived();
+ await backend.answer((
+ status: 401,
+ body: {
+ 'success': false,
+ 'error': {'code': 'UNAUTHORIZED'},
+ },
+ ));
+ await expectLater(
+ token,
+ throwsA(
+ isA<DirectAuthException>().having((e) => e.signedOut, 'out', true),
+ ),
+ );
+ expect(await session.accessToken(), isNull);
+ });
+
+ test('an outage during a refresh keeps the session', () async {
+ await signInStale();
+ final token = auth.accessToken();
+ await backend.arrived();
+ await backend.answer((status: 503, body: {'success': false}));
+ await expectLater(
+ token,
+ throwsA(
+ isA<DirectAuthException>().having((e) => e.signedOut, 'out', false),
+ ),
+ );
+ expect(await session.accessToken(), 'old');
+ expect(await session.refreshToken(), 'r1');
+ // And the next ask tries again rather than repeating the failure.
+ final retry = auth.accessToken();
+ await backend.arrived();
+ await backend.answer(_renewed('new', expiresIn: 3600));
+ expect(await retry, 'new');
+ });
+ });
+
+ group('the session changing under a refresh', () {
+ // ⚠️ The refresh was already in the air when the person signed out. Its answer belongs to a
+ // session that no longer exists on this phone, and saving it put that session back: the app
+ // went to the sign-in screen, and the next launch walked straight past it.
+ test(
+ 'signing out while a refresh is in the air stays signed out',
+ () async {
+ await signInStale();
+ final token = auth.accessToken();
+ await backend.arrived();
+ await auth.signOut();
+
+ await backend.answer(_renewed('resurrected', refreshToken: 'r2'));
+ await expectLater(
+ token,
+ throwsA(
+ isA<DirectAuthException>().having((e) => e.signedOut, 'out', true),
+ ),
+ );
+ expect(await auth.hasSession(), isFalse);
+ expect(await session.accessToken(), isNull);
+ expect(await session.refreshToken(), isNull);
+ },
+ );
+
+ test(
+ 'a refresh from the last session cannot overwrite the next one',
+ () async {
+ await signInStale(token: 'alice', refreshToken: 'alice-r');
+ final token = auth.accessToken();
+ await backend.arrived();
+ await auth.signOut();
+ await auth.signIn(
+ const IssuedTokens(
+ token: 'bob',
+ refreshToken: 'bob-r',
+ expiresIn: 3600,
+ ),
+ issuer: SessionIssuer.emailCode,
+ );
+
+ await backend.answer(
+ _renewed('alice-renewed', refreshToken: 'alice-r2'),
+ );
+ expect(await token, 'bob');
+ expect(await session.accessToken(), 'bob');
+ expect(await session.refreshToken(), 'bob-r');
+ expect(await session.issuer(), SessionIssuer.emailCode);
+ },
+ );
+
+ test(
+ 'the last session being refused cannot sign the next one out',
+ () async {
+ await signInStale(token: 'alice', refreshToken: 'alice-r');
+ final token = auth.accessToken();
+ await backend.arrived();
+ await auth.signOut();
+ await auth.signIn(
+ const IssuedTokens(
+ token: 'bob',
+ refreshToken: 'bob-r',
+ expiresIn: 3600,
+ ),
+ );
+
+ await backend.answer((status: 401, body: {'success': false}));
+ expect(await token, 'bob');
+ expect(await session.accessToken(), 'bob');
+ expect(await session.refreshToken(), 'bob-r');
+ },
+ );
+ });
+
+ group('signing out', () {
+ test('an SSO session is only forgotten here — no revoke call', () async {
+ await auth.signIn(const IssuedTokens(token: 't', refreshToken: 'r'));
+ await auth.signOut();
+ expect(backend.calls, isEmpty);
+ expect(await session.accessToken(), isNull);
+ });
+
+ test(
+ 'a Harness session with no refresh token has nothing to revoke',
+ () async {
+ await auth.signIn(
+ const IssuedTokens(token: 't'),
+ issuer: SessionIssuer.harness,
+ );
+ await auth.signOut();
+ expect(backend.calls, isEmpty);
+ expect(await session.accessToken(), isNull);
+ },
+ );
+
+ test(
+ 'nothing can renew the session while the backend is being told',
+ () async {
+ await signInStale(issuer: SessionIssuer.harness);
+ final signedOut = auth.signOut();
+ await backend.arrived();
+ expect(backend.calls.single, {'refreshToken': 'r1'});
+
+ // A socket redialling in the seconds the revoke may take.
+ await expectLater(
+ auth.accessToken(),
+ throwsA(
+ isA<DirectAuthException>().having((e) => e.signedOut, 'out', true),
+ ),
+ );
+ await backend.answer((status: 200, body: {'success': true}));
+ await signedOut;
+ expect(backend.calls, hasLength(1), reason: 'no refresh went out');
+ expect(storage.values, isEmpty);
+ },
+ );
+
+ test('a revoke that never answers does not hold the sign-out up', () {
+ fakeAsync((clock) {
+ var done = false;
+ auth
+ .signIn(
+ const IssuedTokens(token: 't', refreshToken: 'r'),
+ issuer: SessionIssuer.harness,
+ )
+ .then((_) => auth.signOut())
+ .then((_) => done = true);
+ clock.elapse(const Duration(milliseconds: 1));
+ expect(backend.calls.single, {'refreshToken': 'r'});
+ clock.elapse(const Duration(seconds: 2));
+ expect(done, isFalse, reason: 'it does wait for the backend, briefly');
+ clock.elapse(const Duration(seconds: 2));
+ expect(done, isTrue);
+ expect(storage.values, isEmpty);
+ });
+ });
+ });
+}