Skip to content

Commit cb53af0

Browse files
[webview_flutter_android] Set support for web authentication (#11681)
This PR adds Android support for configuring WebView web authentication behavior through WebSettingsCompat.setWebAuthenticationSupport. - Added a new Pigeon API for WebSettingsCompat.setWebAuthenticationSupport. - Implemented the Android host-side proxy in WebSettingsCompatProxyApi. - Wired the generated Android and Dart Pigeon bindings to expose the new method. - Updated the Android controller flow to call the new API when the corresponding feature is supported. - Added/updated constants and example/docs to reflect the new support. - Added focused tests for the new Android host-side behavior. https://github.com/user-attachments/assets/cb0b1cfa-7a79-47c3-ad1b-d5230ce42b1d *List which issues are fixed by this PR. You must list at least one issue.* flutter/flutter#186312 ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent f2fd2b3 commit cb53af0

15 files changed

Lines changed: 497 additions & 1 deletion

File tree

‎packages/webview_flutter/webview_flutter_android/CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 4.14.0
2+
* Adds support for configuring Web Authentication in `AndroidWebViewController` with `setWebAuthenticationSupport` to enable Passkey and other related Authentication.
3+
14
## 4.13.0
25

36
* Adds new method for accessing a native `WebView` from a `FlutterPluginBinding`.

‎packages/webview_flutter/webview_flutter_android/README.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,33 @@ Add intent filters to your AndroidManifest.xml to discover and invoke Android pa
8484
</queries>
8585
```
8686

87+
## Enable Web Authentication in WebView
88+
89+
WebAuthentication (WebAuthn) can be configured by calling
90+
`AndroidWebViewController.setWebAuthenticationSupport` after checking
91+
`AndroidWebViewController.isWebViewFeatureSupported`.
92+
93+
The WebAuthentication support level can be set to one of three values:
94+
- **[WebAuthenticationSupport.none]**: Disables all WebAuthn requests
95+
- **[WebAuthenticationSupport.forApp]**: Allows WebAuthn for the embedded application (default)
96+
- **[WebAuthenticationSupport.forBrowser]**: Allows WebAuthn for any website (browser-like behavior)
97+
98+
<?code-excerpt "example/lib/readme_excerpts.dart (web_authentication_example)"?>
99+
```dart
100+
final bool webAuthenticationSupported = await androidController.isWebViewFeatureSupported(
101+
WebViewFeatureType.webAuthentication,
102+
);
103+
104+
if (webAuthenticationSupported) {
105+
// Enable WebAuthn for the embedded app
106+
await androidController.setWebAuthenticationSupport(WebAuthenticationSupport.forApp);
107+
// Or for browser-like behavior supporting any website:
108+
// await androidController.setWebAuthenticationSupport(
109+
// WebAuthenticationSupport.forBrowser,
110+
// );
111+
}
112+
```
113+
87114
## Fullscreen Video
88115

89116
To display a video as fullscreen, an app must manually handle the notification that the current page

‎packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/AndroidWebkitLibrary.g.kt‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6826,6 +6826,8 @@ abstract class PigeonApiWebSettingsCompat(
68266826
) {
68276827
abstract fun setPaymentRequestEnabled(webSettings: android.webkit.WebSettings, enabled: Boolean)
68286828

6829+
abstract fun setWebAuthenticationSupport(webSettings: android.webkit.WebSettings, support: Long)
6830+
68296831
companion object {
68306832
@Suppress("LocalVariableName")
68316833
fun setUpMessageHandlers(binaryMessenger: BinaryMessenger, api: PigeonApiWebSettingsCompat?) {
@@ -6854,6 +6856,30 @@ abstract class PigeonApiWebSettingsCompat(
68546856
channel.setMessageHandler(null)
68556857
}
68566858
}
6859+
run {
6860+
val channel =
6861+
BasicMessageChannel<Any?>(
6862+
binaryMessenger,
6863+
"dev.flutter.pigeon.webview_flutter_android.WebSettingsCompat.setWebAuthenticationSupport",
6864+
codec)
6865+
if (api != null) {
6866+
channel.setMessageHandler { message, reply ->
6867+
val args = message as List<Any?>
6868+
val webSettingsArg = args[0] as android.webkit.WebSettings
6869+
val supportArg = args[1] as Long
6870+
val wrapped: List<Any?> =
6871+
try {
6872+
api.setWebAuthenticationSupport(webSettingsArg, supportArg)
6873+
listOf(null)
6874+
} catch (exception: Throwable) {
6875+
AndroidWebkitLibraryPigeonUtils.wrapError(exception)
6876+
}
6877+
reply.reply(wrapped)
6878+
}
6879+
} else {
6880+
channel.setMessageHandler(null)
6881+
}
6882+
}
68576883
}
68586884
}
68596885

‎packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/WebSettingsCompatProxyApi.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,28 @@ public WebSettingsCompatProxyApi(@NonNull ProxyApiRegistrar pigeonRegistrar) {
2929
public void setPaymentRequestEnabled(@NonNull WebSettings webSettings, boolean enabled) {
3030
WebSettingsCompat.setPaymentRequestEnabled(webSettings, enabled);
3131
}
32+
33+
/**
34+
* This method should only be called if {@link WebViewFeatureProxyApi#isFeatureSupported(String)}
35+
* with WEB_AUTHENTICATION returns true.
36+
*
37+
* <p>The {@code support} parameter is a {@code long} to accommodate Dart's integer type, but is
38+
* safely converted to {@code int} for the underlying Android API call. {@link
39+
* Math#toIntExact(long)} is used to verify the value fits in the {@code int} range and throw
40+
* {@link ArithmeticException} if it overflows. This is safe because the valid support levels are
41+
* constants (0, 1, 2) that are well within the integer range.
42+
*
43+
* <p>Note: {@link Math#toIntExact(long)} requires API level 24 or higher. This is compatible with
44+
* this plugin's minimum SDK version.
45+
*
46+
* @param webSettings the WebSettings instance
47+
* @param support the WebAuthentication support level (0, 1, or 2)
48+
* @throws ArithmeticException if {@code support} exceeds {@link Integer#MAX_VALUE}
49+
*/
50+
@SuppressLint("RequiresFeature")
51+
@Override
52+
public void setWebAuthenticationSupport(@NonNull WebSettings webSettings, long support) {
53+
final int supportValue = Math.toIntExact(support);
54+
WebSettingsCompat.setWebAuthenticationSupport(webSettings, supportValue);
55+
}
3256
}

‎packages/webview_flutter/webview_flutter_android/android/src/test/java/io/flutter/plugins/webviewflutter/WebSettingsCompatTest.java‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
package io.flutter.plugins.webviewflutter;
66

7+
import static org.junit.Assert.assertThrows;
78
import static org.junit.Assert.fail;
89
import static org.mockito.Mockito.mock;
910
import static org.mockito.Mockito.mockStatic;
@@ -36,4 +37,38 @@ public void setPaymentRequestEnabled() {
3637
fail(e.toString());
3738
}
3839
}
40+
41+
@Test
42+
public void setWebAuthenticationSupport() {
43+
final PigeonApiWebSettingsCompat api =
44+
new TestProxyApiRegistrar().getPigeonApiWebSettingsCompat();
45+
46+
final WebSettings webSettings = mock(WebSettings.class);
47+
48+
try (MockedStatic<WebSettingsCompat> mockedStatic = mockStatic(WebSettingsCompat.class);
49+
MockedStatic<WebViewFeature> mockedWebViewFeature = mockStatic(WebViewFeature.class)) {
50+
mockedWebViewFeature
51+
.when(() -> WebViewFeature.isFeatureSupported(WebViewFeature.WEB_AUTHENTICATION))
52+
.thenReturn(true);
53+
api.setWebAuthenticationSupport(webSettings, 2L);
54+
mockedStatic.verify(() -> WebSettingsCompat.setWebAuthenticationSupport(webSettings, 2));
55+
}
56+
}
57+
58+
@Test
59+
public void setWebAuthenticationSupportWithLongOutsideIntRangeThrows() {
60+
final PigeonApiWebSettingsCompat api =
61+
new TestProxyApiRegistrar().getPigeonApiWebSettingsCompat();
62+
63+
final WebSettings webSettings = mock(WebSettings.class);
64+
65+
try (MockedStatic<WebViewFeature> mockedWebViewFeature = mockStatic(WebViewFeature.class)) {
66+
mockedWebViewFeature
67+
.when(() -> WebViewFeature.isFeatureSupported(WebViewFeature.WEB_AUTHENTICATION))
68+
.thenReturn(true);
69+
assertThrows(
70+
ArithmeticException.class,
71+
() -> api.setWebAuthenticationSupport(webSettings, Integer.MAX_VALUE + 1L));
72+
}
73+
}
3974
}

‎packages/webview_flutter/webview_flutter_android/example/lib/main.dart‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ enum MenuOptions {
329329
videoExample,
330330
logExample,
331331
basicAuthentication,
332+
webAuthentication,
332333
javaScriptAlert,
333334
viewportMeta,
334335
}
@@ -383,6 +384,8 @@ class SampleMenu extends StatelessWidget {
383384
_onLogExample();
384385
case MenuOptions.basicAuthentication:
385386
_promptForUrl(context);
387+
case MenuOptions.webAuthentication:
388+
_onWebAuthenticationExample(context);
386389
case MenuOptions.javaScriptAlert:
387390
_onJavaScriptAlertExample(context);
388391
case MenuOptions.viewportMeta:
@@ -443,6 +446,10 @@ class SampleMenu extends StatelessWidget {
443446
value: MenuOptions.basicAuthentication,
444447
child: Text('Basic Authentication Example'),
445448
),
449+
const PopupMenuItem<MenuOptions>(
450+
value: MenuOptions.webAuthentication,
451+
child: Text('Web Authentication Example'),
452+
),
446453
const PopupMenuItem<MenuOptions>(
447454
value: MenuOptions.javaScriptAlert,
448455
child: Text('JavaScript Alert Example'),
@@ -564,6 +571,30 @@ class SampleMenu extends StatelessWidget {
564571
);
565572
}
566573

574+
Future<void> _onWebAuthenticationExample(BuildContext context) async {
575+
final androidController = webViewController as AndroidWebViewController;
576+
final bool supported = await androidController.isWebViewFeatureSupported(
577+
WebViewFeatureType.webAuthentication,
578+
);
579+
580+
if (!supported) {
581+
if (context.mounted) {
582+
ScaffoldMessenger.of(context).showSnackBar(
583+
const SnackBar(content: Text('Web Authentication is not supported on this device.')),
584+
);
585+
}
586+
return;
587+
}
588+
589+
await androidController.setWebAuthenticationSupport(WebAuthenticationSupport.forApp);
590+
591+
if (context.mounted) {
592+
ScaffoldMessenger.of(
593+
context,
594+
).showSnackBar(const SnackBar(content: Text('Web Authentication enabled.')));
595+
}
596+
}
597+
567598
Future<void> _onDoPostRequest() {
568599
return webViewController.loadRequest(
569600
LoadRequestParams(

‎packages/webview_flutter/webview_flutter_android/example/lib/readme_excerpts.dart‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,26 @@ Future<void> enablePaymentRequest() async {
2020
// #enddocregion payment_request_example
2121
}
2222

23+
/// Example function for README demonstration of Web Authentication support.
24+
Future<void> enableWebAuthentication() async {
25+
final controller = PlatformWebViewController(AndroidWebViewControllerCreationParams());
26+
final androidController = controller as AndroidWebViewController;
27+
// #docregion web_authentication_example
28+
final bool webAuthenticationSupported = await androidController.isWebViewFeatureSupported(
29+
WebViewFeatureType.webAuthentication,
30+
);
31+
32+
if (webAuthenticationSupported) {
33+
// Enable WebAuthn for the embedded app
34+
await androidController.setWebAuthenticationSupport(WebAuthenticationSupport.forApp);
35+
// Or for browser-like behavior supporting any website:
36+
// await androidController.setWebAuthenticationSupport(
37+
// WebAuthenticationSupport.forBrowser,
38+
// );
39+
}
40+
// #enddocregion web_authentication_example
41+
}
42+
2343
/// Example function for README demonstration of geolocation permissions for
2444
/// a use case where the content is always trusted (for example, it only shows
2545
/// content from a domain controlled by the app developer) and geolocation

‎packages/webview_flutter/webview_flutter_android/lib/src/android_webkit.g.dart‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ class PigeonOverrides {
217217
/// Overrides [WebSettingsCompat.setPaymentRequestEnabled].
218218
static Future<void> Function(WebSettings, bool)? webSettingsCompat_setPaymentRequestEnabled;
219219

220+
/// Overrides [WebSettingsCompat.setWebAuthenticationSupport].
221+
static Future<void> Function(WebSettings, int)? webSettingsCompat_setWebAuthenticationSupport;
222+
220223
/// Overrides [WebViewFeature.isFeatureSupported].
221224
static Future<bool> Function(String)? webViewFeature_isFeatureSupported;
222225

@@ -232,6 +235,7 @@ class PigeonOverrides {
232235
flutterAssetManager_instance = null;
233236
webStorage_instance = null;
234237
webSettingsCompat_setPaymentRequestEnabled = null;
238+
webSettingsCompat_setWebAuthenticationSupport = null;
235239
webViewFeature_isFeatureSupported = null;
236240
}
237241
}
@@ -7188,6 +7192,35 @@ class WebSettingsCompat extends PigeonInternalProxyApiBaseClass {
71887192
_extractReplyValueOrThrow(pigeonVar_replyList, pigeonVar_channelName, isNullValid: true);
71897193
}
71907194

7195+
static Future<void> setWebAuthenticationSupport(
7196+
WebSettings webSettings,
7197+
int support, {
7198+
BinaryMessenger? pigeon_binaryMessenger,
7199+
PigeonInstanceManager? pigeon_instanceManager,
7200+
}) async {
7201+
if (PigeonOverrides.webSettingsCompat_setWebAuthenticationSupport != null) {
7202+
return PigeonOverrides.webSettingsCompat_setWebAuthenticationSupport!(webSettings, support);
7203+
}
7204+
final _PigeonInternalProxyApiBaseCodec pigeonChannelCodec = _PigeonInternalProxyApiBaseCodec(
7205+
pigeon_instanceManager ?? PigeonInstanceManager.instance,
7206+
);
7207+
final BinaryMessenger? pigeonVar_binaryMessenger = pigeon_binaryMessenger;
7208+
const pigeonVar_channelName =
7209+
'dev.flutter.pigeon.webview_flutter_android.WebSettingsCompat.setWebAuthenticationSupport';
7210+
final pigeonVar_channel = BasicMessageChannel<Object?>(
7211+
pigeonVar_channelName,
7212+
pigeonChannelCodec,
7213+
binaryMessenger: pigeonVar_binaryMessenger,
7214+
);
7215+
final Future<Object?> pigeonVar_sendFuture = pigeonVar_channel.send(<Object?>[
7216+
webSettings,
7217+
support,
7218+
]);
7219+
final pigeonVar_replyList = await pigeonVar_sendFuture as List<Object?>?;
7220+
7221+
_extractReplyValueOrThrow(pigeonVar_replyList, pigeonVar_channelName, isNullValid: true);
7222+
}
7223+
71917224
@override
71927225
WebSettingsCompat pigeon_copy() {
71937226
return WebSettingsCompat.pigeon_detached(

‎packages/webview_flutter/webview_flutter_android/lib/src/android_webkit_constants.dart‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,9 @@ class WebViewFeatureConstants {
126126
///
127127
/// See https://developer.android.com/reference/androidx/webkit/WebViewFeature#PAYMENT_REQUEST.
128128
static const String paymentRequest = 'PAYMENT_REQUEST';
129+
130+
/// This feature covers [WebSettingsCompat.setWebAuthenticationSupport].
131+
///
132+
/// See https://developer.android.com/reference/androidx/webkit/WebViewFeature#WEB_AUTHENTICATION.
133+
static const String webAuthentication = 'WEB_AUTHENTICATION';
129134
}

‎packages/webview_flutter/webview_flutter_android/lib/src/android_webview_controller.dart‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,10 +790,42 @@ class AndroidWebViewController extends PlatformWebViewController {
790790
Future<bool> isWebViewFeatureSupported(WebViewFeatureType featureType) {
791791
final String feature = switch (featureType) {
792792
WebViewFeatureType.paymentRequest => WebViewFeatureConstants.paymentRequest,
793+
WebViewFeatureType.webAuthentication => WebViewFeatureConstants.webAuthentication,
793794
};
794795
return android_webview.WebViewFeature.isFeatureSupported(feature);
795796
}
796797

798+
/// Sets the WebAuthentication support level for this WebView.
799+
///
800+
/// This method configures which contexts can use WebAuthn APIs in the WebView.
801+
/// Callers should check [isWebViewFeatureSupported] before calling this
802+
/// method.
803+
///
804+
/// **Parameters:**
805+
/// * [support] - The desired WebAuthentication support level:
806+
/// - [WebAuthenticationSupport.none]: Disables all WebAuthn requests
807+
/// - [WebAuthenticationSupport.forApp]: Allows WebAuthn for the embedded app
808+
/// - [WebAuthenticationSupport.forBrowser]: Allows WebAuthn for any website
809+
///
810+
/// **Example:**
811+
/// ```dart
812+
/// final isSupported = await controller
813+
/// .isWebViewFeatureSupported(WebViewFeatureType.webAuthentication);
814+
/// if (isSupported) {
815+
/// await controller.setWebAuthenticationSupport(
816+
/// WebAuthenticationSupport.forApp,
817+
/// );
818+
/// }
819+
/// ```
820+
///
821+
/// See https://developer.android.com/reference/androidx/webkit/WebSettingsCompat#setWebAuthenticationSupport.
822+
Future<void> setWebAuthenticationSupport(WebAuthenticationSupport support) async {
823+
await android_webview.WebSettingsCompat.setWebAuthenticationSupport(
824+
_webView.settings,
825+
support.value,
826+
);
827+
}
828+
797829
/// Sets whether the WebView should enable the Payment Request API.
798830
///
799831
/// This method uses [android_webview.WebSettingsCompat.setPaymentRequestEnabled]
@@ -960,6 +992,41 @@ enum WebViewFeatureType {
960992
///
961993
/// This feature covers [WebSettingsCompat.setPaymentRequestEnabled].
962994
paymentRequest,
995+
996+
/// Feature for isFeatureSupported.
997+
///
998+
/// This feature covers [WebSettingsCompat.setWebAuthenticationSupport].
999+
webAuthentication,
1000+
}
1001+
1002+
/// Support levels for [android_webview.WebSettingsCompat.setWebAuthenticationSupport].
1003+
///
1004+
/// This enum provides a type-safe way to specify the WebAuthentication support
1005+
/// level for a WebView.
1006+
///
1007+
/// See https://developer.android.com/reference/androidx/webkit/WebSettingsCompat#setWebAuthenticationSupport.
1008+
enum WebAuthenticationSupport {
1009+
/// Disables WebAuthn requests from WebView.
1010+
///
1011+
/// No WebAuthn APIs are available to web content in the WebView.
1012+
none(0),
1013+
1014+
/// Allows WebAuthn requests for the embedded app.
1015+
///
1016+
/// WebAuthn is available for Relying Party IDs that are registered for the
1017+
/// embedding application.
1018+
forApp(1),
1019+
1020+
/// Allows WebAuthn calls for any website.
1021+
///
1022+
/// WebAuthn is available for any Relying Party ID. This is the typical
1023+
/// configuration for a browser-like experience.
1024+
forBrowser(2);
1025+
1026+
const WebAuthenticationSupport(this.value);
1027+
1028+
/// The platform integer expected by `WebSettingsCompat`.
1029+
final int value;
9631030
}
9641031

9651032
/// Parameters received when the `WebView` should show a file selector.

0 commit comments

Comments
 (0)