2019-11-02 03:16:47 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2019-10-31 02:20:07 +00:00
|
|
|
import 'types.dart';
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
|
2020-05-11 00:48:41 +00:00
|
|
|
///Class that implements a singleton object (shared instance) which manages the shared HTTP auth credentials cache.
|
2019-11-10 13:11:30 +00:00
|
|
|
///On iOS, this class uses the [URLCredentialStorage](https://developer.apple.com/documentation/foundation/urlcredentialstorage) class.
|
2020-05-29 12:51:26 +00:00
|
|
|
///On Android, this class has a custom implementation using `android.database.sqlite.SQLiteDatabase` because
|
|
|
|
///[WebViewDatabase](https://developer.android.com/reference/android/webkit/WebViewDatabase)
|
2019-11-10 13:11:30 +00:00
|
|
|
///doesn't offer the same functionalities as iOS `URLCredentialStorage`.
|
2022-04-25 20:36:21 +00:00
|
|
|
///
|
|
|
|
///**Supported Platforms/Implementations**:
|
|
|
|
///- Android native WebView
|
|
|
|
///- iOS
|
2019-10-31 02:20:07 +00:00
|
|
|
class HttpAuthCredentialDatabase {
|
2021-01-28 16:10:15 +00:00
|
|
|
static HttpAuthCredentialDatabase? _instance;
|
2019-12-01 11:55:06 +00:00
|
|
|
static const MethodChannel _channel = const MethodChannel(
|
|
|
|
'com.pichillilorenzo/flutter_inappwebview_credential_database');
|
2019-10-31 02:20:07 +00:00
|
|
|
|
2019-11-10 13:11:30 +00:00
|
|
|
///Gets the database shared instance.
|
2019-10-31 02:20:07 +00:00
|
|
|
static HttpAuthCredentialDatabase instance() {
|
2021-01-28 16:10:15 +00:00
|
|
|
return (_instance != null) ? _instance! : _init();
|
2019-10-31 02:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static HttpAuthCredentialDatabase _init() {
|
|
|
|
_channel.setMethodCallHandler(_handleMethod);
|
2020-05-29 17:56:03 +00:00
|
|
|
_instance = HttpAuthCredentialDatabase();
|
2021-01-28 16:10:15 +00:00
|
|
|
return _instance!;
|
2019-10-31 02:20:07 +00:00
|
|
|
}
|
|
|
|
|
2019-12-01 11:55:06 +00:00
|
|
|
static Future<dynamic> _handleMethod(MethodCall call) async {}
|
2019-10-31 02:20:07 +00:00
|
|
|
|
2019-11-10 13:11:30 +00:00
|
|
|
///Gets a map list of all HTTP auth credentials saved.
|
2021-02-22 11:16:23 +00:00
|
|
|
///Each map contains the key `protectionSpace` of type [URLProtectionSpace]
|
2022-04-20 00:18:36 +00:00
|
|
|
///and the key `credentials` of type List<[URLCredential]> that contains all the HTTP auth credentials saved for that `protectionSpace`.
|
|
|
|
///
|
|
|
|
///**Supported Platforms/Implementations**:
|
|
|
|
///- Android native WebView
|
|
|
|
///- iOS ([Official API - URLCredentialStorage.allCredentials](https://developer.apple.com/documentation/foundation/urlcredentialstorage/1413859-allcredentials))
|
2021-02-22 11:16:23 +00:00
|
|
|
Future<List<URLProtectionSpaceHttpAuthCredentials>>
|
2020-06-21 22:09:35 +00:00
|
|
|
getAllAuthCredentials() async {
|
2019-10-31 02:20:07 +00:00
|
|
|
Map<String, dynamic> args = <String, dynamic>{};
|
2019-12-01 11:55:06 +00:00
|
|
|
List<dynamic> allCredentials =
|
|
|
|
await _channel.invokeMethod('getAllAuthCredentials', args);
|
2020-06-20 19:58:29 +00:00
|
|
|
|
2021-02-22 11:16:23 +00:00
|
|
|
List<URLProtectionSpaceHttpAuthCredentials> result = [];
|
2020-06-20 19:58:29 +00:00
|
|
|
|
2019-10-31 02:20:07 +00:00
|
|
|
for (Map<dynamic, dynamic> map in allCredentials) {
|
2021-02-22 22:54:09 +00:00
|
|
|
var element = URLProtectionSpaceHttpAuthCredentials.fromMap(
|
|
|
|
map.cast<String, dynamic>());
|
2021-02-22 11:16:23 +00:00
|
|
|
if (element != null) {
|
|
|
|
result.add(element);
|
|
|
|
}
|
2019-10-31 02:20:07 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-11-10 13:11:30 +00:00
|
|
|
///Gets all the HTTP auth credentials saved for that [protectionSpace].
|
2022-04-20 00:18:36 +00:00
|
|
|
///
|
|
|
|
///**Supported Platforms/Implementations**:
|
|
|
|
///- Android native WebView
|
|
|
|
///- iOS
|
2021-02-22 11:16:23 +00:00
|
|
|
Future<List<URLCredential>> getHttpAuthCredentials(
|
|
|
|
{required URLProtectionSpace protectionSpace}) async {
|
2019-10-31 02:20:07 +00:00
|
|
|
Map<String, dynamic> args = <String, dynamic>{};
|
|
|
|
args.putIfAbsent("host", () => protectionSpace.host);
|
|
|
|
args.putIfAbsent("protocol", () => protectionSpace.protocol);
|
|
|
|
args.putIfAbsent("realm", () => protectionSpace.realm);
|
|
|
|
args.putIfAbsent("port", () => protectionSpace.port);
|
2019-12-01 11:55:06 +00:00
|
|
|
List<dynamic> credentialList =
|
|
|
|
await _channel.invokeMethod('getHttpAuthCredentials', args);
|
2021-02-22 11:16:23 +00:00
|
|
|
List<URLCredential> credentials = [];
|
|
|
|
for (Map<dynamic, dynamic> map in credentialList) {
|
|
|
|
var credential = URLCredential.fromMap(map.cast<String, dynamic>());
|
|
|
|
if (credential != null) {
|
|
|
|
credentials.add(credential);
|
|
|
|
}
|
2019-10-31 02:20:07 +00:00
|
|
|
}
|
|
|
|
return credentials;
|
|
|
|
}
|
|
|
|
|
2019-11-10 13:11:30 +00:00
|
|
|
///Saves an HTTP auth [credential] for that [protectionSpace].
|
2022-04-20 00:18:36 +00:00
|
|
|
///
|
|
|
|
///**Supported Platforms/Implementations**:
|
|
|
|
///- Android native WebView
|
|
|
|
///- iOS ([Official API - URLCredentialStorage.set](https://developer.apple.com/documentation/foundation/urlcredentialstorage/1407227-set))
|
2019-12-01 11:55:06 +00:00
|
|
|
Future<void> setHttpAuthCredential(
|
2021-02-22 11:16:23 +00:00
|
|
|
{required URLProtectionSpace protectionSpace,
|
|
|
|
required URLCredential credential}) async {
|
2019-10-31 02:20:07 +00:00
|
|
|
Map<String, dynamic> args = <String, dynamic>{};
|
|
|
|
args.putIfAbsent("host", () => protectionSpace.host);
|
|
|
|
args.putIfAbsent("protocol", () => protectionSpace.protocol);
|
|
|
|
args.putIfAbsent("realm", () => protectionSpace.realm);
|
|
|
|
args.putIfAbsent("port", () => protectionSpace.port);
|
|
|
|
args.putIfAbsent("username", () => credential.username);
|
|
|
|
args.putIfAbsent("password", () => credential.password);
|
|
|
|
await _channel.invokeMethod('setHttpAuthCredential', args);
|
|
|
|
}
|
|
|
|
|
2019-11-10 13:11:30 +00:00
|
|
|
///Removes an HTTP auth [credential] for that [protectionSpace].
|
2022-04-20 00:18:36 +00:00
|
|
|
///
|
|
|
|
///**Supported Platforms/Implementations**:
|
|
|
|
///- Android native WebView
|
|
|
|
///- iOS ([Official API - URLCredentialStorage.remove](https://developer.apple.com/documentation/foundation/urlcredentialstorage/1408664-remove))
|
2019-12-01 11:55:06 +00:00
|
|
|
Future<void> removeHttpAuthCredential(
|
2021-02-22 11:16:23 +00:00
|
|
|
{required URLProtectionSpace protectionSpace,
|
|
|
|
required URLCredential credential}) async {
|
2019-10-31 02:20:07 +00:00
|
|
|
Map<String, dynamic> args = <String, dynamic>{};
|
|
|
|
args.putIfAbsent("host", () => protectionSpace.host);
|
|
|
|
args.putIfAbsent("protocol", () => protectionSpace.protocol);
|
|
|
|
args.putIfAbsent("realm", () => protectionSpace.realm);
|
|
|
|
args.putIfAbsent("port", () => protectionSpace.port);
|
|
|
|
args.putIfAbsent("username", () => credential.username);
|
|
|
|
args.putIfAbsent("password", () => credential.password);
|
|
|
|
await _channel.invokeMethod('removeHttpAuthCredential', args);
|
|
|
|
}
|
|
|
|
|
2019-11-10 13:11:30 +00:00
|
|
|
///Removes all the HTTP auth credentials saved for that [protectionSpace].
|
2022-04-20 00:18:36 +00:00
|
|
|
///
|
|
|
|
///**Supported Platforms/Implementations**:
|
|
|
|
///- Android native WebView
|
|
|
|
///- iOS
|
2019-12-01 11:55:06 +00:00
|
|
|
Future<void> removeHttpAuthCredentials(
|
2021-02-22 11:16:23 +00:00
|
|
|
{required URLProtectionSpace protectionSpace}) async {
|
2019-10-31 02:20:07 +00:00
|
|
|
Map<String, dynamic> args = <String, dynamic>{};
|
|
|
|
args.putIfAbsent("host", () => protectionSpace.host);
|
|
|
|
args.putIfAbsent("protocol", () => protectionSpace.protocol);
|
|
|
|
args.putIfAbsent("realm", () => protectionSpace.realm);
|
|
|
|
args.putIfAbsent("port", () => protectionSpace.port);
|
|
|
|
await _channel.invokeMethod('removeHttpAuthCredentials', args);
|
|
|
|
}
|
|
|
|
|
2019-11-10 13:11:30 +00:00
|
|
|
///Removes all the HTTP auth credentials saved in the database.
|
2022-04-20 00:18:36 +00:00
|
|
|
///
|
|
|
|
///**Supported Platforms/Implementations**:
|
|
|
|
///- Android native WebView
|
|
|
|
///- iOS
|
2019-10-31 02:20:07 +00:00
|
|
|
Future<void> clearAllAuthCredentials() async {
|
|
|
|
Map<String, dynamic> args = <String, dynamic>{};
|
|
|
|
await _channel.invokeMethod('clearAllAuthCredentials', args);
|
|
|
|
}
|
2019-12-01 11:55:06 +00:00
|
|
|
}
|