Skip to content

Commit c5443ad

Browse files
authored
[webview_flutter] Support for handling basic authentication requests (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.
1 parent 2e010d8 commit c5443ad

9 files changed

Lines changed: 134 additions & 4 deletions

File tree

packages/webview_flutter/webview_flutter_platform_interface/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## NEXT
1+
## 2.7.0
22

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

56
## 2.6.0

packages/webview_flutter/webview_flutter_platform_interface/lib/src/platform_navigation_delegate.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ typedef WebResourceErrorCallback = void Function(WebResourceError error);
3030
/// url of the web view.
3131
typedef UrlChangeCallback = void Function(UrlChange change);
3232

33+
/// Signature for callbacks that notify the host application of an
34+
/// authentication request.
35+
typedef HttpAuthRequestCallback = void Function(HttpAuthRequest request);
36+
3337
/// An interface defining navigation events that occur on the native platform.
3438
///
3539
/// The [PlatformWebViewController] is notifying this delegate on events that
@@ -132,4 +136,11 @@ abstract class PlatformNavigationDelegate extends PlatformInterface {
132136
'setOnUrlChange is not implemented on the current platform.',
133137
);
134138
}
139+
140+
/// Invoked when the web view is requesting authentication.
141+
Future<void> setOnHttpAuthRequest(HttpAuthRequestCallback onHttpAuthRequest) {
142+
throw UnimplementedError(
143+
'setOnHttpAuthRequest is not implemented on the current platform.',
144+
);
145+
}
135146
}

packages/webview_flutter/webview_flutter_platform_interface/lib/src/platform_webview_controller.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import 'webview_platform.dart' show WebViewPlatform;
1717
/// changes. Extending this class (using `extends`) ensures that the subclass
1818
/// will get the default implementation, while platform implementations that
1919
/// `implements` this interface will be broken by newly added
20-
/// [PlatformWebViewCookieManager] methods.
20+
/// [PlatformWebViewController] methods.
2121
abstract class PlatformWebViewController extends PlatformInterface {
2222
/// Creates a new [PlatformWebViewController]
2323
factory PlatformWebViewController(
@@ -267,7 +267,7 @@ abstract class PlatformWebViewController extends PlatformInterface {
267267
void Function(PlatformWebViewPermissionRequest request) onPermissionRequest,
268268
) {
269269
throw UnimplementedError(
270-
'setOnPermissionRequest is not implemented on the current platform',
270+
'setOnPlatformPermissionRequest is not implemented on the current platform',
271271
);
272272
}
273273

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/foundation.dart';
6+
import 'webview_credential.dart';
7+
8+
/// Defines the parameters of a pending HTTP authentication request received by
9+
/// the webview through a [HttpAuthRequestCallback].
10+
///
11+
/// Platform specific implementations can add additional fields by extending
12+
/// this class and providing a factory method that takes the [HttpAuthRequest]
13+
/// as a parameter.
14+
///
15+
/// This example demonstrates how to extend the [HttpAuthRequest] to provide
16+
/// additional platform specific parameters.
17+
///
18+
/// When extending [HttpAuthRequest], additional parameters should always accept
19+
/// `null` or have a default value to prevent breaking changes.
20+
///
21+
/// ```dart
22+
/// @immutable
23+
/// class WKWebViewHttpAuthRequest extends HttpAuthRequest {
24+
/// WKWebViewHttpAuthRequest._(
25+
/// HttpAuthRequest authRequest,
26+
/// this.extraData,
27+
/// ) : super(
28+
/// onProceed: authRequest.onProceed,
29+
/// onCancel: authRequest.onCancel,
30+
/// host: authRequest.host,
31+
/// realm: authRequest.realm,
32+
/// );
33+
///
34+
/// factory WKWebViewHttpAuthRequest.fromHttpAuthRequest(
35+
/// HttpAuthRequest authRequest, {
36+
/// String? extraData,
37+
/// }) {
38+
/// return WKWebViewHttpAuthRequest._(
39+
/// authRequest,
40+
/// extraData: extraData,
41+
/// );
42+
/// }
43+
///
44+
/// final String? extraData;
45+
/// }
46+
/// ```
47+
@immutable
48+
class HttpAuthRequest {
49+
/// Creates a [HttpAuthRequest].
50+
const HttpAuthRequest({
51+
required this.onProceed,
52+
required this.onCancel,
53+
required this.host,
54+
this.realm,
55+
});
56+
57+
/// The callback to authenticate.
58+
final void Function(WebViewCredential credential) onProceed;
59+
60+
/// The callback to cancel authentication.
61+
final void Function() onCancel;
62+
63+
/// The host requiring authentication.
64+
final String host;
65+
66+
/// The realm requiring authentication.
67+
final String? realm;
68+
}

packages/webview_flutter/webview_flutter_platform_interface/lib/src/types/types.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
export 'http_auth_request.dart';
56
export 'http_response_error.dart';
67
export 'javascript_console_message.dart';
78
export 'javascript_log_level.dart';
@@ -18,3 +19,4 @@ export 'platform_webview_widget_creation_params.dart';
1819
export 'url_change.dart';
1920
export 'web_resource_error.dart';
2021
export 'webview_cookie.dart';
22+
export 'webview_credential.dart';
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:meta/meta.dart';
6+
7+
import '../types/http_auth_request.dart';
8+
9+
/// Defines the response parameters of a pending [HttpAuthRequest] received by
10+
/// the webview.
11+
@immutable
12+
class WebViewCredential {
13+
/// Creates a [WebViewCredential].
14+
const WebViewCredential({
15+
required this.user,
16+
required this.password,
17+
});
18+
19+
/// The user name.
20+
final String user;
21+
22+
/// The password.
23+
final String password;
24+
}

packages/webview_flutter/webview_flutter_platform_interface/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ repository: https://github.com/flutter/packages/tree/main/packages/webview_flutt
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview_flutter%22
55
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
66
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
7-
version: 2.6.0
7+
version: 2.7.0
88

99
environment:
1010
sdk: ">=3.0.0 <4.0.0"

packages/webview_flutter/webview_flutter_platform_interface/test/platform_navigation_delegate_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,19 @@ void main() {
142142
throwsUnimplementedError,
143143
);
144144
});
145+
146+
test(
147+
'Default implementation of setOnHttpAuthRequest should throw unimplemented error',
148+
() {
149+
final PlatformNavigationDelegate callbackDelegate =
150+
ExtendsPlatformNavigationDelegate(
151+
const PlatformNavigationDelegateCreationParams());
152+
153+
expect(
154+
() => callbackDelegate.setOnHttpAuthRequest((HttpAuthRequest request) {}),
155+
throwsUnimplementedError,
156+
);
157+
});
145158
}
146159

147160
class MockWebViewPlatformWithMixin extends MockWebViewPlatform

packages/webview_flutter/webview_flutter_platform_interface/test/platform_webview_controller_test.mocks.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,15 @@ class MockPlatformNavigationDelegate extends _i1.Mock
124124
returnValue: _i4.Future<void>.value(),
125125
returnValueForMissingStub: _i4.Future<void>.value(),
126126
) as _i4.Future<void>);
127+
@override
128+
_i4.Future<void> setOnHttpAuthRequest(
129+
_i3.HttpAuthRequestCallback? onHttpAuthRequest) =>
130+
(super.noSuchMethod(
131+
Invocation.method(
132+
#setOnHttpAuthRequest,
133+
[onHttpAuthRequest],
134+
),
135+
returnValue: _i4.Future<void>.value(),
136+
returnValueForMissingStub: _i4.Future<void>.value(),
137+
) as _i4.Future<void>);
127138
}

0 commit comments

Comments
 (0)