Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
mobile: pause device-code polling while backgrounded
Antonio Ojea9d3a85165e2b
diff --git a/mobile/sam-node-app/lib/main.dart b/mobile/sam-node-app/lib/main.dartindex f2f547f4..8472c6c7 100644--- a/mobile/sam-node-app/lib/main.dart+++ b/mobile/sam-node-app/lib/main.dart@@ -635,15 +635,13 @@ class _NodeControlPageState extends State<NodeControlPage> {if (!_devicePollingActive) break;try {- final response = await http.post(- Uri.parse(tokenUrl),- headers: {'Content-Type': 'application/x-www-form-urlencoded'},- body: {- 'grant_type': 'urn:ietf:params:oauth:grant-type:device_code',- 'device_code': deviceCode,- 'client_id': clientId,- },+ final response = await exchangeDeviceCode(+ tokenUrl: Uri.parse(tokenUrl),+ clientId: clientId,+ deviceCode: deviceCode,+ isActive: () => mounted && _devicePollingActive,);+ if (response == null || !mounted || !_devicePollingActive) break;if (response.statusCode == 200) {final data = jsonDecode(response.body);diff --git a/mobile/sam-node-app/lib/oidc.dart b/mobile/sam-node-app/lib/oidc.dartindex f5fda4f7..51ba4f00 100644--- a/mobile/sam-node-app/lib/oidc.dart+++ b/mobile/sam-node-app/lib/oidc.dart@@ -11,6 +11,40 @@ Future<http.Response> exchangeAuthorizationCode({required String verifier,http.Client? client,}) async {+ await _waitForAppResume();+ return _postToken(+ tokenUrl,+ {+ 'grant_type': 'authorization_code',+ 'client_id': clientId,+ 'code': code,+ 'redirect_uri': redirectUri,+ 'code_verifier': verifier,+ },+ client: client);+}++Future<http.Response?> exchangeDeviceCode({+ required Uri tokenUrl,+ required String clientId,+ required String deviceCode,+ required bool Function() isActive,+ http.Client? client,+}) async {+ if (!isActive()) return null;+ await _waitForAppResume();+ if (!isActive()) return null;+ return _postToken(+ tokenUrl,+ {+ 'grant_type': 'urn:ietf:params:oauth:grant-type:device_code',+ 'device_code': deviceCode,+ 'client_id': clientId,+ },+ client: client);+}++Future<void> _waitForAppResume() async {final binding = WidgetsBinding.instance;while (binding.lifecycleState != AppLifecycleState.resumed) {final resumed = Completer<void>();@@ -23,19 +57,19 @@ Future<http.Response> exchangeAuthorizationCode({listener.dispose();}}+}+Future<http.Response> _postToken(+ Uri tokenUrl,+ Map<String, String> body, {+ http.Client? client,+}) async {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,- },+ body: body,);} 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.dartindex 3d87e3d8..a5e570d4 100644--- a/mobile/sam-node-app/test/oidc_test.dart+++ b/mobile/sam-node-app/test/oidc_test.dart@@ -95,4 +95,90 @@ void main() {expect(response.statusCode, 400);expect(requests, 1);});++ testWidgets('device polling waits while verifying on this phone',+ (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 = exchangeDeviceCode(+ tokenUrl: Uri.parse('https://auth.example.test/token'),+ clientId: 'test-client',+ deviceCode: 'test-device-code',+ isActive: () => true,+ client: client,+ );+ await tester.pump(const Duration(seconds: 30));+ 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.bodyFields, {+ 'grant_type': 'urn:ietf:params:oauth:grant-type:device_code',+ 'device_code': 'test-device-code',+ 'client_id': 'test-client',+ });+ });++ testWidgets('canceling device login prevents a queued poll', (tester) async {+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.hidden);+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.paused);+ var active = true;+ var requests = 0;+ final client = MockClient((request) async {+ requests++;+ return http.Response('{}', 200);+ });+ addTearDown(client.close);+ final response = exchangeDeviceCode(+ tokenUrl: Uri.parse('https://auth.example.test/token'),+ clientId: 'test-client',+ deviceCode: 'test-device-code',+ isActive: () => active,+ client: client,+ );+ active = false;+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.hidden);+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);+ await tester.pump();+ expect(await response, isNull);+ expect(requests, 0);+ });++ for (final error in ['authorization_pending', 'slow_down', 'expired_token']) {+ testWidgets('foreground device poll preserves $error', (tester) async {+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);+ var requests = 0;+ final client = MockClient((request) async {+ requests++;+ return http.Response('{"error":"$error"}', 400);+ });+ addTearDown(client.close);+ final response = await exchangeDeviceCode(+ tokenUrl: Uri.parse('https://auth.example.test/token'),+ clientId: 'test-client',+ deviceCode: 'test-device-code',+ isActive: () => true,+ client: client,+ );+ expect(response?.statusCode, 400);+ expect(response?.body, '{"error":"$error"}');+ expect(requests, 1);+ });+ }}