Skip to content

Commit 4f89925

Browse files
committed
🐛 Throw a proper error on invalid callback scheme
1 parent 83fec04 commit 4f89925

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

lib/flutter_web_auth.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:async';
2+
import 'dart:core';
23

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

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

2224
static final _OnAppLifecycleResumeObserver _resumedObserver = _OnAppLifecycleResumeObserver(() {
2325
_cleanUpDanglingCalls(); // unawaited
@@ -30,6 +32,10 @@ class FlutterWebAuth {
3032
/// [callbackUrlScheme] should be a string specifying the scheme of the url that the page will redirect to upon successful authentication.
3133
/// [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)
3234
static Future<String> authenticate({required String url, required String callbackUrlScheme, bool? preferEphemeral}) async {
35+
if (!_schemeRegExp.hasMatch(callbackUrlScheme)) {
36+
throw ArgumentError.value(callbackUrlScheme, 'callbackUrlScheme', 'must be a valid URL scheme');
37+
}
38+
3339
WidgetsBinding.instance.removeObserver(_resumedObserver); // safety measure so we never add this observer twice
3440
WidgetsBinding.instance.addObserver(_resumedObserver);
3541
return await _channel.invokeMethod('authenticate', <String, dynamic>{

test/flutter_web_auth_test.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,11 @@ void main() {
2828
'https://example.com/success',
2929
);
3030
});
31+
32+
test('invalid scheme', () async {
33+
await expectLater(
34+
FlutterWebAuth.authenticate(url: 'https://example.com/login', callbackUrlScheme: 'foobar://test'),
35+
throwsA(isA<ArgumentError>()),
36+
);
37+
});
3138
}

0 commit comments

Comments
 (0)