Skip to content

Agent Mesh

mobile: wait for foreground before exchanging OIDC code

Antonio Ojeab91c717eaa40
diff --git a/mobile/sam-node-app/lib/main.dart b/mobile/sam-node-app/lib/main.dart
index 4e5870a8..f2f547f4 100644
--- a/mobile/sam-node-app/lib/main.dart
+++ b/mobile/sam-node-app/lib/main.dart
@@ -17,6 +17,7 @@ import 'sam_ffi.dart';
import 'mcp_server.dart';
import 'enroll_link.dart';
import 'scan_page.dart';
+import 'oidc.dart';
// Isolate.run lives in these top-level functions, not in State methods: a closure
// there shares its context with sibling setState closures, so `this` and its
@@ -504,16 +505,12 @@ class _NodeControlPageState extends State<NodeControlPage> {
_status = 'Exchanging code for token...';
});
- final tokenResponse = await http.post(
- Uri.parse(tokenUrl),
- headers: {'Content-Type': 'application/x-www-form-urlencoded'},
- body: {
- 'grant_type': 'authorization_code',
- 'client_id': clientId,
- 'code': code,
- 'redirect_uri': redirectUri,
- 'code_verifier': verifier,
- },
+ final tokenResponse = await exchangeAuthorizationCode(
+ tokenUrl: Uri.parse(tokenUrl),
+ clientId: clientId,
+ code: code,
+ redirectUri: redirectUri,
+ verifier: verifier,
);
if (tokenResponse.statusCode != 200) {
diff --git a/mobile/sam-node-app/lib/oidc.dart b/mobile/sam-node-app/lib/oidc.dart
new file mode 100644
index 00000000..f5fda4f7
--- /dev/null
+++ b/mobile/sam-node-app/lib/oidc.dart
@@ -0,0 +1,43 @@
+import 'dart:async';
+
+import 'package:flutter/widgets.dart';
+import 'package:http/http.dart' as http;
+
+Future<http.Response> exchangeAuthorizationCode({
+ required Uri tokenUrl,
+ required String clientId,
+ required String code,
+ required String redirectUri,
+ required String verifier,
+ http.Client? client,
+}) async {
+ final binding = WidgetsBinding.instance;
+ while (binding.lifecycleState != AppLifecycleState.resumed) {
+ final resumed = Completer<void>();
+ final listener = AppLifecycleListener(onResume: () {
+ if (!resumed.isCompleted) resumed.complete();
+ });
+ try {
+ await resumed.future;
+ } finally {
+ listener.dispose();
+ }
+ }
+
+ final requestClient = client ?? http.Client();
+ try {
+ return await requestClient.post(
+ tokenUrl,
+ headers: {'Content-Type': 'application/x-www-form-urlencoded'},
+ body: {
+ 'grant_type': 'authorization_code',
+ 'client_id': clientId,
+ 'code': code,
+ 'redirect_uri': redirectUri,
+ 'code_verifier': verifier,
+ },
+ );
+ } finally {
+ if (client == null) requestClient.close();
+ }
+}
diff --git a/mobile/sam-node-app/test/oidc_test.dart b/mobile/sam-node-app/test/oidc_test.dart
new file mode 100644
index 00000000..3d87e3d8
--- /dev/null
+++ b/mobile/sam-node-app/test/oidc_test.dart
@@ -0,0 +1,98 @@
+import 'package:flutter/widgets.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:http/http.dart' as http;
+import 'package:http/testing.dart';
+import 'package:sam_agent/oidc.dart';
+
+void main() {
+ Future<http.Response> exchange(http.Client client) =>
+ exchangeAuthorizationCode(
+ tokenUrl: Uri.parse('https://auth.example.test/token'),
+ clientId: 'test-client',
+ code: 'test-code',
+ redirectUri: 'http://127.0.0.1:13000/callback',
+ verifier: 'test-verifier',
+ client: client,
+ );
+
+ testWidgets('token exchange waits until the app resumes', (tester) async {
+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.hidden);
+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.paused);
+
+ final requests = <http.Request>[];
+ final client = MockClient((request) async {
+ requests.add(request);
+ return http.Response('{"id_token":"test-token"}', 200);
+ });
+ addTearDown(client.close);
+ final response = exchange(client);
+ await tester.pump();
+ expect(requests, isEmpty);
+
+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.hidden);
+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
+ await tester.pump();
+ expect(requests, isEmpty);
+
+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
+ await tester.pump();
+ expect((await response).statusCode, 200);
+ expect(requests, hasLength(1));
+ expect(requests.single.url, Uri.parse('https://auth.example.test/token'));
+ expect(requests.single.method, 'POST');
+ expect(requests.single.bodyFields, {
+ 'grant_type': 'authorization_code',
+ 'client_id': 'test-client',
+ 'code': 'test-code',
+ 'redirect_uri': 'http://127.0.0.1:13000/callback',
+ 'code_verifier': 'test-verifier',
+ });
+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
+ await tester.pump();
+ expect(requests, hasLength(1));
+ });
+
+ testWidgets('token exchange rechecks a short-lived resume', (tester) async {
+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.hidden);
+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.paused);
+ var requests = 0;
+ final client = MockClient((request) async {
+ requests++;
+ return http.Response('{}', 200);
+ });
+ addTearDown(client.close);
+ final response = exchange(client);
+
+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.hidden);
+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
+ await tester.pump();
+ expect(requests, 0);
+
+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
+ await tester.pump();
+ expect((await response).statusCode, 200);
+ expect(requests, 1);
+ });
+
+ testWidgets('token exchange proceeds when already resumed', (tester) async {
+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
+ var requests = 0;
+ final client = MockClient((request) async {
+ requests++;
+ return http.Response('{"error":"invalid_grant"}', 400);
+ });
+ addTearDown(client.close);
+ final response = await exchange(client);
+ expect(response.statusCode, 400);
+ expect(requests, 1);
+ });
+}