Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/integrations/intervals_icu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,20 @@ class IntervalsIcu extends ChangeNotifier {
await _store(a);
}

// Nothing reports a closed in-app browser, so the caller ends a waiting
// sign-in with [cancelSignIn]; a new sign-in also ends the old one.
LoopbackAuth? _signIn;
void cancelSignIn() {
_signIn?.cancel();
_signIn = null; // also voids a sign-in already in the token exchange
}

/// OAuth PKCE sign-in; [open] shows the page in an in-app browser.
Future<void> signIn(Future<bool> Function(Uri) open) async {
if (!oauthAvailable) throw IntervalsException('intervals.icu sign-in is not available yet. Use an API key.');
final got = await LoopbackAuth(host: 'localhost').run(
_signIn?.cancel();
final attempt = _signIn = LoopbackAuth(host: 'localhost');
final got = await attempt.run(
open: open,
authorize: (redirect, state, challenge) => Uri.parse('$_base/oauth/authorize').replace(queryParameters: {
'client_id': kIntervalsClientId,
Expand All @@ -200,6 +210,7 @@ class IntervalsIcu extends ChangeNotifier {
'code_verifier': got.verifier,
'redirect_uri': got.redirectUri,
}));
if (!identical(_signIn, attempt)) throw IntervalsException('Sign-in was cancelled.');
if (r.statusCode >= 300) throw IntervalsException('intervals.icu sign-in failed (${r.statusCode}).');
await _store(parseIntervalsToken(r.body));
}
Expand Down
9 changes: 7 additions & 2 deletions lib/ui2/profile/connected_apps.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class _ConnectedAppsState extends State<ConnectedApps> {
@override
void dispose() {
_intervals.removeListener(_refresh);
_intervals.cancelSignIn();
super.dispose();
}

Expand Down Expand Up @@ -73,9 +74,13 @@ class _ConnectedAppsState extends State<ConnectedApps> {
}

/// One wrapper for every action on this screen: a spinner-free busy guard
/// and the failure said in the exception's own words.
/// and the failure said in the exception's own words. A tap while an
/// intervals.icu sign-in waits means the browser was closed: end it.
Future<void> _run(Future<void> Function() f) async {
if (_busy) return;
if (_busy) {
_intervals.cancelSignIn();
return;
}
setState(() => _busy = true);
try {
await f();
Expand Down
12 changes: 12 additions & 0 deletions test/integrations/intervals_icu_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
import 'package:openstrap_edge/integrations/intervals_icu.dart';
import 'package:openstrap_edge/integrations/oauth_loopback.dart';

void main() {
test('wellness body omits absent fields and converts units', () {
Expand Down Expand Up @@ -100,4 +101,15 @@ void main() {
await i.pushAfterDerive();
expect(calls, 0);
});

test('closing the browser: cancelSignIn ends a waiting sign-in, a new one ends the old', () async {
final i = IntervalsIcu(client: MockClient((r) async => http.Response('', 500)));
final first = i.signIn((_) async => true);
await Future<void>.delayed(const Duration(milliseconds: 50));
final second = i.signIn((_) async => true);
await expectLater(first, throwsA(isA<OAuthException>()));
await Future<void>.delayed(const Duration(milliseconds: 50));
i.cancelSignIn();
await expectLater(second, throwsA(isA<OAuthException>()));
});
}
Loading