Skip to content

[webview_flutter_android] Set support for web authentication#11681

Open
creatorpiyush wants to merge 6 commits into
flutter:mainfrom
creatorpiyush:webview_flutter_android_set-Web-Authentication-Support
Open

[webview_flutter_android] Set support for web authentication#11681
creatorpiyush wants to merge 6 commits into
flutter:mainfrom
creatorpiyush:webview_flutter_android_set-Web-Authentication-Support

Conversation

@creatorpiyush
Copy link
Copy Markdown

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.
working-example.mp4

List which issues are fixed by this PR. You must list at least one issue.
flutter/flutter#186312

Pre-Review Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. 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.

Footnotes

  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. 2

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements Web Authentication support for the Android WebView, including the necessary Pigeon interface updates, native implementation, and example app integration. Feedback includes addressing a potential compatibility issue with Math.toIntExact on older Android APIs, renaming variables in documentation for clarity, and potentially using an enum to expose all support levels instead of a boolean toggle.

Comment thread packages/webview_flutter/webview_flutter_android/README.md Outdated
@SuppressLint("RequiresFeature")
@Override
public void setWebAuthenticationSupport(@NonNull WebSettings webSettings, long support) {
final int supportValue = Math.toIntExact(support);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Math.toIntExact was introduced in Android API level 24. Since this package supports API level 21, this call may cause a NoSuchMethodError on devices running API 21-23 unless Java 8 desugaring is guaranteed to be enabled and cover this specific method. Consider using a manual cast with an overflow check for broader compatibility, especially since the unit tests explicitly verify overflow behavior.

    final int supportValue = (int) support;
    if (supportValue != support) {
      throw new ArithmeticException("integer overflow");
    }

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minSdk in build.gradle.kts is 24

Comment thread packages/webview_flutter/webview_flutter_android/example/lib/readme_excerpts.dart Outdated
Comment on lines +896 to +911
Future<void> setWebAuthenticationEnabled(bool enabled) async {
if (!await isWebViewFeatureSupported(
WebViewFeatureType.webAuthentication,
)) {
return;
}

final int supportLevel = enabled
? WebAuthenticationSupportConstants.forApp
: WebAuthenticationSupportConstants.none;

await android_webview.WebSettingsCompat.setWebAuthenticationSupport(
_webView.settings,
supportLevel,
);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The setWebAuthenticationEnabled(bool enabled) method simplifies the API to a boolean toggle, which only covers the none and forApp support levels. Since the underlying Android API and the constants already defined in android_webkit_constants.dart also support forBrowser (intended for browser-like applications), consider using an enum to expose the full range of functionality provided by the platform.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@creatorpiyush creatorpiyush force-pushed the webview_flutter_android_set-Web-Authentication-Support branch from 9a2593d to 886d8da Compare May 11, 2026 12:33
@stuartmorgan-g
Copy link
Copy Markdown
Collaborator

[x] I followed the version and CHANGELOG instructions, using semantic versioning and the repository CHANGELOG style, or I have commented below to indicate which documented exception this PR falls under1.

Thanks for the contribution! You’ve checked boxes in the PR checklist above that are not reflected in this PR, so I’m assuming this is a work in progress and am marking it as a Draft. Please review the checklist, updating the PR as appropriate, and when the state of the PR as posted reflects the checklist please feel free to mark it as ready for review.

@stuartmorgan-g stuartmorgan-g marked this pull request as draft May 11, 2026 14:35
@creatorpiyush creatorpiyush marked this pull request as ready for review May 11, 2026 15:02
@creatorpiyush
Copy link
Copy Markdown
Author

Hello @stuartmorgan-g, PR is up for review; that part was missed to be pushed.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for configuring Web Authentication (WebAuthn) in the AndroidWebViewController via the setWebAuthenticationSupport method. The changes include updates to the Pigeon interface, Android native implementation using WebSettingsCompat, and the Dart API with a new WebAuthenticationSupport enum. A high-severity issue was identified regarding the use of Math.toIntExact, which requires API level 24 and would cause crashes on devices running Android 5.0 to 7.0 (API 21-23) supported by this plugin. Additionally, the WebAuthenticationSupportConstants class should use the @Deprecated annotation instead of just a doc comment for proper IDE and compiler support.

Comment on lines +33 to +55
/**
* This method should only be called if {@link WebViewFeatureProxyApi#isFeatureSupported(String)}
* with WEB_AUTHENTICATION returns true.
*
* <p>The {@code support} parameter is a {@code long} to accommodate Dart's integer type, but is
* safely converted to {@code int} for the underlying Android API call. {@link
* Math#toIntExact(long)} is used to verify the value fits in the {@code int} range and throw
* {@link ArithmeticException} if it overflows. This is safe because the valid support levels are
* constants (0, 1, 2) that well within the integer range.
*
* <p>Note: {@link Math#toIntExact(long)} requires API level 24 or higher. This is compatible with
* this plugin's minimum SDK version.
*
* @param webSettings the WebSettings instance
* @param support the WebAuthentication support level (0, 1, or 2)
* @throws ArithmeticException if {@code support} exceeds {@link Integer#MAX_VALUE}
*/
@SuppressLint("RequiresFeature")
@Override
public void setWebAuthenticationSupport(@NonNull WebSettings webSettings, long support) {
final int supportValue = Math.toIntExact(support);
WebSettingsCompat.setWebAuthenticationSupport(webSettings, supportValue);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The use of Math.toIntExact requires Android API level 24 or higher. Since webview_flutter_android supports API levels as low as 21, using this method will result in a NoSuchMethodError on devices running Android 5.0 to 7.0 (API 21-23) unless core library desugaring is enabled.

Given that the support values are expected to be small constants (0, 1, or 2) defined by the plugin, a simple cast to int is safer for compatibility. If overflow protection is desired, a manual check can be used to maintain compatibility with all supported API levels.

  /**
   * This method should only be called if {@link WebViewFeatureProxyApi#isFeatureSupported(String)}
   * with WEB_AUTHENTICATION returns true.
   *
   * <p>The {@code support} parameter is a {@code long} to accommodate Dart's integer type, but is
   * safely converted to {@code int} for the underlying Android API call. This is safe because the
   * valid support levels are constants (0, 1, 2) that are well within the integer range.
   *
   * @param webSettings the WebSettings instance
   * @param support the WebAuthentication support level (0, 1, or 2)
   */
  @SuppressLint("RequiresFeature")
  @Override
  public void setWebAuthenticationSupport(@NonNull WebSettings webSettings, long support) {
    if (support < Integer.MIN_VALUE || support > Integer.MAX_VALUE) {
      throw new ArithmeticException("integer overflow");
    }
    final int supportValue = (int) support;
    WebSettingsCompat.setWebAuthenticationSupport(webSettings, supportValue);
  }

Copy link
Copy Markdown
Author

@creatorpiyush creatorpiyush May 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minSdk in build.gradle.kts is 24, so I guess this comment is not valid for the current solution

@stuartmorgan-g stuartmorgan-g added the triage-android Should be looked at in Android triage label May 12, 2026
@mboetger mboetger requested review from a team and gmackall and removed request for a team May 12, 2026 20:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

p: webview_flutter platform-android triage-android Should be looked at in Android triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants