Skip to content

Commit

Permalink
馃悰 Throw a proper error on invalid callback scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
LinusU committed Nov 1, 2022
1 parent 83fec04 commit 4f89925
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/flutter_web_auth.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:core';

import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart' show MethodChannel;
Expand All @@ -18,6 +19,7 @@ class _OnAppLifecycleResumeObserver extends WidgetsBindingObserver {

class FlutterWebAuth {
static const MethodChannel _channel = const MethodChannel('flutter_web_auth');
static RegExp _schemeRegExp = new RegExp(r"^[a-z][a-z0-9+.-]*$");

static final _OnAppLifecycleResumeObserver _resumedObserver = _OnAppLifecycleResumeObserver(() {
_cleanUpDanglingCalls(); // unawaited
Expand All @@ -30,6 +32,10 @@ class FlutterWebAuth {
/// [callbackUrlScheme] should be a string specifying the scheme of the url that the page will redirect to upon successful authentication.
/// [preferEphemeral] if this is specified as `true`, an ephemeral web browser session will be used where possible (`FLAG_ACTIVITY_NO_HISTORY` on Android, `prefersEphemeralWebBrowserSession` on iOS/macOS)
static Future<String> authenticate({required String url, required String callbackUrlScheme, bool? preferEphemeral}) async {
if (!_schemeRegExp.hasMatch(callbackUrlScheme)) {
throw ArgumentError.value(callbackUrlScheme, 'callbackUrlScheme', 'must be a valid URL scheme');
}

WidgetsBinding.instance.removeObserver(_resumedObserver); // safety measure so we never add this observer twice
WidgetsBinding.instance.addObserver(_resumedObserver);
return await _channel.invokeMethod('authenticate', <String, dynamic>{
Expand Down
7 changes: 7 additions & 0 deletions test/flutter_web_auth_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,11 @@ void main() {
'https://example.com/success',
);
});

test('invalid scheme', () async {
await expectLater(
FlutterWebAuth.authenticate(url: 'https://example.com/login', callbackUrlScheme: 'foobar://test'),
throwsA(isA<ArgumentError>()),
);
});
}

0 comments on commit 4f89925

Please sign in to comment.