Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
517e9d4
Implement http basic auth
JeroenWeener Aug 10, 2023
967879b
Apply feedback
JeroenWeener Aug 28, 2023
c8624c0
Create HttpAuthHandlerTest.java
JeroenWeener Aug 29, 2023
4f3574d
Format java files
JeroenWeener Aug 29, 2023
1e7f785
Regenerate build_runner files
JeroenWeener Aug 29, 2023
131bc56
Implement feedback
JeroenWeener Sep 11, 2023
bc01be4
Remove redundant key in `Info.plist`
JeroenWeener Sep 11, 2023
67aa00e
Update example apps
JeroenWeener Sep 11, 2023
32496d0
Implement http basic auth
JeroenWeener Aug 10, 2023
aeb8708
Apply feedback
JeroenWeener Aug 28, 2023
fffc4c7
Create HttpAuthHandlerTest.java
JeroenWeener Aug 29, 2023
b455040
Format java files
JeroenWeener Aug 29, 2023
2aa9bfe
Regenerate build_runner files
JeroenWeener Aug 29, 2023
db286ba
Implement feedback
JeroenWeener Sep 11, 2023
467feb3
Remove redundant key in `Info.plist`
JeroenWeener Sep 11, 2023
dad8ae6
Update example apps
JeroenWeener Sep 11, 2023
6f3d802
Merge branch 'webview-auth-request' of https://github.com/andreisas06…
JeroenWeener Nov 1, 2023
0fef591
Add platform interface dev dependency to example
JeroenWeener Nov 1, 2023
b2a4fbb
Merge branch 'main' into webview-auth-request
JeroenWeener Nov 1, 2023
ce35d9b
Update packages/webview_flutter/webview_flutter_platform_interface/li…
bparrishMines Nov 8, 2023
fc32898
Merge branch 'main' of github.com:flutter/packages into webview-auth-…
bparrishMines Nov 8, 2023
4673e65
Merge branch 'webview-auth-request' of github.com:andreisas06/package…
bparrishMines Nov 8, 2023
580521a
Fix some lints, errros and call on errors
bparrishMines Nov 8, 2023
d1f305b
fix lints
bparrishMines Nov 8, 2023
a3f74be
fix tests
bparrishMines Nov 8, 2023
ed3798f
add onProceed and onCancel back
bparrishMines Nov 8, 2023
154c1f8
dont require realm to be nonnull
bparrishMines Nov 8, 2023
3c61dbc
add line back
bparrishMines Nov 8, 2023
7cf0d7e
Merge remote-tracking branch 'upstream/main' into webview-auth-request
JeroenWeener Nov 21, 2023
a21f29a
Update changelogs
JeroenWeener Nov 21, 2023
8f7603a
Merge main
JeroenWeener Dec 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implement feedback
  • Loading branch information
JeroenWeener committed Oct 26, 2023
commit 131bc5646d848df815ac421330f314da8a7cba5e
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:webview_flutter_android/webview_flutter_android.dart';
import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart';
import 'package:webview_flutter_wkwebview/webview_flutter_wkwebview.dart';

Future<void> main() async {
Expand All @@ -34,12 +33,22 @@ Future<void> main() async {
} else if (request.uri.path == '/favicon.ico') {
request.response.statusCode = HttpStatus.notFound;
} else if (request.uri.path == '/http-basic-authentication') {
final bool isAuthenticating = request.headers['Authorization'] != null;
if (isAuthenticating) {
request.response.writeln('Authorized');
final List<String>? authHeader =
request.headers[HttpHeaders.authorizationHeader];
if (authHeader != null) {
final String encodedCredential = authHeader.first.split(' ')[1];
final String credential =
String.fromCharCodes(base64Decode(encodedCredential));
if (credential == 'user:password') {
request.response.writeln('Authorized');
} else {
request.response.headers.add(
HttpHeaders.wwwAuthenticateHeader, 'Basic realm="Test realm"');
request.response.statusCode = HttpStatus.unauthorized;
}
} else {
request.response.headers
.add('WWW-Authenticate', 'Basic realm="Test realm"');
.add(HttpHeaders.wwwAuthenticateHeader, 'Basic realm="Test realm"');
request.response.statusCode = HttpStatus.unauthorized;
}
} else {
Expand Down Expand Up @@ -799,10 +808,15 @@ Future<void> main() async {
unawaited(
controller.setNavigationDelegate(
NavigationDelegate(
onHttpAuthRequest: (HttpAuthRequest request) => request.onProceed(
const WebViewCredential(user: 'user', password: 'password'),
onHttpAuthRequest: (HttpAuthRequest request) =>
request.onAuthenticate(
const WebViewCredential(
user: 'u2ser',
password: 'password',
),
),
onPageFinished: (_) => pageFinished.complete(),
onWebResourceError: (_) => fail('Authentication failed'),
),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import 'package:webview_flutter/webview_flutter.dart';
// #docregion platform_imports
// Import for Android features.
import 'package:webview_flutter_android/webview_flutter_android.dart';
import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart';
// Import for iOS features.
import 'package:webview_flutter_wkwebview/webview_flutter_wkwebview.dart';
// #enddocregion platform_imports
Expand Down Expand Up @@ -265,14 +264,14 @@ Page resource error:
// requests when a previous request is pending.
TextButton(
onPressed: () {
httpRequest.onCancel();
httpRequest.onAuthenticate(null);
Navigator.of(context).pop();
},
child: const Text('Cancel'),
),
TextButton(
onPressed: () {
httpRequest.onProceed(
httpRequest.onAuthenticate(
WebViewCredential(
user: usernameTextController.text,
password: passwordTextController.text,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class NavigationDelegate {
this.onProgress,
this.onWebResourceError,
void Function(UrlChange change)? onUrlChange,
this.onHttpAuthRequest,
HttpAuthRequestCallback? onHttpAuthRequest,
}) {
if (onNavigationRequest != null) {
platform.setOnNavigationRequest(onNavigationRequest!);
Expand All @@ -144,7 +144,7 @@ class NavigationDelegate {
platform.setOnUrlChange(onUrlChange);
}
if (onHttpAuthRequest != null) {
platform.setOnHttpAuthRequest(onHttpAuthRequest!);
platform.setOnHttpAuthRequest(onHttpAuthRequest);
}
}

Expand Down Expand Up @@ -174,7 +174,4 @@ class NavigationDelegate {

/// Invoked when a resource loading error occurred.
final WebResourceErrorCallback? onWebResourceError;

/// Invoked when a resource required HTTP authentication.
final HttpAuthRequestCallback? onHttpAuthRequest;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export 'package:webview_flutter_platform_interface/webview_flutter_platform_inte
WebResourceErrorCallback,
WebResourceErrorType,
WebViewCookie,
WebViewCredential,
WebViewPermissionResourceType,
WebViewPlatform;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,7 @@ Future<void> main() async {
);
await navigationDelegate.setOnPageFinished((_) => pageFinished.complete());
await navigationDelegate.setOnHttpAuthRequest(
(HttpAuthRequest request) => request.onProceed(
(HttpAuthRequest request) => request.onAuthenticate(
const WebViewCredential(user: 'user', password: 'password'),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,6 @@ Page resource error:
debugPrint('url change to ${change.url}');
})
..setOnHttpAuthRequest((HttpAuthRequest request) {
debugPrint(
'HTTP basic auth request with host ${request.host} and realm ${request.realm ?? '-'}');
openDialog(request);
}),
)
Expand Down Expand Up @@ -255,7 +253,7 @@ Page resource error:
actions: <Widget>[
TextButton(
onPressed: () {
httpRequest.onProceed(
httpRequest.onAuthenticate(
WebViewCredential(
user: usernameTextController.text,
password: passwordTextController.text,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart' show BinaryMessenger;

import 'package:flutter/widgets.dart' show WidgetsFlutterBinding;
import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart';

import 'android_webview.g.dart';
import 'android_webview_api_impls.dart';
Expand Down Expand Up @@ -1533,8 +1532,8 @@ class HttpAuthHandler extends JavaObject {

/// Instructs the WebView to proceed with the authentication with the provided
/// credentials.
Future<void> proceed(WebViewCredential credential) {
return api.proceedFromInstance(this, credential);
Future<void> proceed(String username, String password) {
return api.proceedFromInstance(this, username, password);
}

/// Gets whether the credentials stored for the current host are suitable for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import 'dart:ui';

import 'package:flutter/services.dart' show BinaryMessenger, Uint8List;
import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart'
show WebViewCredential;

import 'android_webview.dart';
import 'android_webview.g.dart';
Expand Down Expand Up @@ -1451,12 +1449,13 @@ class HttpAuthHandlerHostApiImpl extends HttpAuthHandlerHostApi {
/// Helper method to convert instance ids to objects.
Future<void> proceedFromInstance(
HttpAuthHandler instance,
WebViewCredential credential,
String username,
String password,
) {
return proceed(
_instanceManager.getIdentifier(instance)!,
credential.user,
credential.password,
username,
password,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1279,8 +1279,16 @@ class AndroidNavigationDelegate extends PlatformNavigationDelegate {
if (callback != null) {
callback(
HttpAuthRequest(
onProceed: httpAuthHandler.proceed,
onCancel: httpAuthHandler.cancel,
onAuthenticate: (WebViewCredential? credential) {
if (credential == null) {
httpAuthHandler.cancel();
} else {
httpAuthHandler.proceed(
credential.user,
credential.password,
);
}
},
host: host,
realm: realm,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
// 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 'package:flutter/foundation.dart';
import '../../webview_flutter_platform_interface.dart';

/// Defines the parameters of a pending HTTP authentication request received by
Expand Down Expand Up @@ -51,17 +50,15 @@ import '../../webview_flutter_platform_interface.dart';
class HttpAuthRequest {
/// Creates a [HttpAuthRequest].
const HttpAuthRequest({
required this.onProceed,
required this.onCancel,
required this.onAuthenticate,
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 callback to proceed with, or cancel an auth request.
///
/// If `credential` is `null`, the request will be canceled.
final void Function(WebViewCredential? credential) onAuthenticate;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the outcome of #4140 (comment) was that we were going to go back to onProceed and onCancel? I'll defer to @bparrishMines on the exact callback structure though.

Copy link
Contributor

Choose a reason for hiding this comment

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

Correct. I think we should separate this into onCancel and onProceed again. And then iOS can add its additional method.


/// The host requiring authentication.
final String host;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@ Future<void> main() async {
);
await navigationDelegate.setOnPageFinished((_) => pageFinished.complete());
await navigationDelegate.setOnHttpAuthRequest(
(HttpAuthRequest request) => request.onProceed(
(HttpAuthRequest request) => request.onAuthenticate(
const WebViewCredential(user: 'user', password: 'password'),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,14 @@ Page resource error:
// requests when a previous request is pending.
TextButton(
onPressed: () {
httpRequest.onCancel();
httpRequest.onAuthenticate(null);
Navigator.of(context).pop();
},
child: const Text('Cancel'),
),
TextButton(
onPressed: () {
httpRequest.onProceed(
httpRequest.onAuthenticate(
WebViewCredential(
user: usernameTextController.text,
password: passwordTextController.text,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,15 @@ class WebKitNavigationDelegate extends PlatformNavigationDelegate {
if (callback != null && host != null && realm != null) {
callback(
HttpAuthRequest(
onProceed: (WebViewCredential credential) {
onAuthenticate: (WebViewCredential? credential) {
if (credential == null) {
return completionHandler(
NSUrlSessionAuthChallengeDisposition
.cancelAuthenticationChallenge,
null,
);
}

return completionHandler(
NSUrlSessionAuthChallengeDisposition.useCredential,
NSUrlCredential.withUser(
Expand All @@ -976,13 +984,6 @@ class WebKitNavigationDelegate extends PlatformNavigationDelegate {
),
);
},
onCancel: () {
completionHandler(
NSUrlSessionAuthChallengeDisposition
.cancelAuthenticationChallenge,
null,
);
},
host: host,
realm: realm,
),
Expand Down