Skip to content

Commit

Permalink
[webview_flutter] Support for handling basic authentication requests …
Browse files Browse the repository at this point in the history
…(Platform Interface) (flutter#5362)

Adds the platform interface implementation for basic http authentication.

This PR is part of a series of PRs that aim to close flutter#83556.
The PR that contains all changes can be found at flutter/packages#4140.
  • Loading branch information
JeroenWeener authored Nov 20, 2023
1 parent 2e010d8 commit c5443ad
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
## 2.7.0

* Adds support for handling HTTP basic authentication requests. See `PlatformNavigationDelegate.setOnHttpAuthRequest`.
* Updates minimum supported SDK version to Flutter 3.10/Dart 3.0.

## 2.6.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ typedef WebResourceErrorCallback = void Function(WebResourceError error);
/// url of the web view.
typedef UrlChangeCallback = void Function(UrlChange change);

/// Signature for callbacks that notify the host application of an
/// authentication request.
typedef HttpAuthRequestCallback = void Function(HttpAuthRequest request);

/// An interface defining navigation events that occur on the native platform.
///
/// The [PlatformWebViewController] is notifying this delegate on events that
Expand Down Expand Up @@ -132,4 +136,11 @@ abstract class PlatformNavigationDelegate extends PlatformInterface {
'setOnUrlChange is not implemented on the current platform.',
);
}

/// Invoked when the web view is requesting authentication.
Future<void> setOnHttpAuthRequest(HttpAuthRequestCallback onHttpAuthRequest) {
throw UnimplementedError(
'setOnHttpAuthRequest is not implemented on the current platform.',
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import 'webview_platform.dart' show WebViewPlatform;
/// changes. Extending this class (using `extends`) ensures that the subclass
/// will get the default implementation, while platform implementations that
/// `implements` this interface will be broken by newly added
/// [PlatformWebViewCookieManager] methods.
/// [PlatformWebViewController] methods.
abstract class PlatformWebViewController extends PlatformInterface {
/// Creates a new [PlatformWebViewController]
factory PlatformWebViewController(
Expand Down Expand Up @@ -267,7 +267,7 @@ abstract class PlatformWebViewController extends PlatformInterface {
void Function(PlatformWebViewPermissionRequest request) onPermissionRequest,
) {
throw UnimplementedError(
'setOnPermissionRequest is not implemented on the current platform',
'setOnPlatformPermissionRequest is not implemented on the current platform',
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/foundation.dart';
import 'webview_credential.dart';

/// Defines the parameters of a pending HTTP authentication request received by
/// the webview through a [HttpAuthRequestCallback].
///
/// Platform specific implementations can add additional fields by extending
/// this class and providing a factory method that takes the [HttpAuthRequest]
/// as a parameter.
///
/// This example demonstrates how to extend the [HttpAuthRequest] to provide
/// additional platform specific parameters.
///
/// When extending [HttpAuthRequest], additional parameters should always accept
/// `null` or have a default value to prevent breaking changes.
///
/// ```dart
/// @immutable
/// class WKWebViewHttpAuthRequest extends HttpAuthRequest {
/// WKWebViewHttpAuthRequest._(
/// HttpAuthRequest authRequest,
/// this.extraData,
/// ) : super(
/// onProceed: authRequest.onProceed,
/// onCancel: authRequest.onCancel,
/// host: authRequest.host,
/// realm: authRequest.realm,
/// );
///
/// factory WKWebViewHttpAuthRequest.fromHttpAuthRequest(
/// HttpAuthRequest authRequest, {
/// String? extraData,
/// }) {
/// return WKWebViewHttpAuthRequest._(
/// authRequest,
/// extraData: extraData,
/// );
/// }
///
/// final String? extraData;
/// }
/// ```
@immutable
class HttpAuthRequest {
/// Creates a [HttpAuthRequest].
const HttpAuthRequest({
required this.onProceed,
required this.onCancel,
required this.host,
this.realm,
});

/// The callback to authenticate.
final void Function(WebViewCredential credential) onProceed;

/// The callback to cancel authentication.
final void Function() onCancel;

/// The host requiring authentication.
final String host;

/// The realm requiring authentication.
final String? realm;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

export 'http_auth_request.dart';
export 'http_response_error.dart';
export 'javascript_console_message.dart';
export 'javascript_log_level.dart';
Expand All @@ -18,3 +19,4 @@ export 'platform_webview_widget_creation_params.dart';
export 'url_change.dart';
export 'web_resource_error.dart';
export 'webview_cookie.dart';
export 'webview_credential.dart';
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:meta/meta.dart';

import '../types/http_auth_request.dart';

/// Defines the response parameters of a pending [HttpAuthRequest] received by
/// the webview.
@immutable
class WebViewCredential {
/// Creates a [WebViewCredential].
const WebViewCredential({
required this.user,
required this.password,
});

/// The user name.
final String user;

/// The password.
final String password;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ repository: https://github.com/flutter/packages/tree/main/packages/webview_flutt
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview_flutter%22
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 2.6.0
version: 2.7.0

environment:
sdk: ">=3.0.0 <4.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,19 @@ void main() {
throwsUnimplementedError,
);
});

test(
'Default implementation of setOnHttpAuthRequest should throw unimplemented error',
() {
final PlatformNavigationDelegate callbackDelegate =
ExtendsPlatformNavigationDelegate(
const PlatformNavigationDelegateCreationParams());

expect(
() => callbackDelegate.setOnHttpAuthRequest((HttpAuthRequest request) {}),
throwsUnimplementedError,
);
});
}

class MockWebViewPlatformWithMixin extends MockWebViewPlatform
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,15 @@ class MockPlatformNavigationDelegate extends _i1.Mock
returnValue: _i4.Future<void>.value(),
returnValueForMissingStub: _i4.Future<void>.value(),
) as _i4.Future<void>);
@override
_i4.Future<void> setOnHttpAuthRequest(
_i3.HttpAuthRequestCallback? onHttpAuthRequest) =>
(super.noSuchMethod(
Invocation.method(
#setOnHttpAuthRequest,
[onHttpAuthRequest],
),
returnValue: _i4.Future<void>.value(),
returnValueForMissingStub: _i4.Future<void>.value(),
) as _i4.Future<void>);
}

0 comments on commit c5443ad

Please sign in to comment.