From 011d1c866fc5cf4b188da212022f0f207925b9ec Mon Sep 17 00:00:00 2001 From: Lorenzo Pichilli Date: Fri, 14 Oct 2022 01:02:39 +0200 Subject: [PATCH] CookieManager.deleteCookie and CookieManager.deleteCookies now have the domain argument optional and without a default value --- CHANGELOG.md | 4 ++ .../flutter_inappwebview/MyCookieManager.java | 33 ++++++++++++--- .../cookie_manager/set_get_delete.dart | 2 +- ios/Classes/MyCookieManager.swift | 42 ++++++++++++------- lib/src/cookie_manager.dart | 13 +----- 5 files changed, 60 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 06f78615..b1862844 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,10 @@ - Merged "Add directoryIndex and documentRoot to InAppLocalhostServer option" [#1319](https://github.com/pichillilorenzo/flutter_inappwebview/pull/1319) (thanks to [fa0311](https://github.com/fa0311)) - Merged "fix(ios): invoke onBrowserCreated when viewDidLoad is called with win…" [#1344](https://github.com/pichillilorenzo/flutter_inappwebview/pull/1344) (thanks to [perffecto](https://github.com/perffecto)) +### BREAKING CHANGES + +- `CookieManager.getCookie`, `CookieManager.deleteCookie` and `CookieManager.deleteCookies` have the `domain` argument optional and without a default value + ## 5.4.4+3 - Removed Android unsafe trust manager diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/MyCookieManager.java b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/MyCookieManager.java index 14444860..dde98e41 100755 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/MyCookieManager.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/MyCookieManager.java @@ -172,13 +172,16 @@ public class MyCookieManager extends ChannelDelegateImpl { }); cookieManager.flush(); } - else { + else if (plugin != null) { CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(plugin.applicationContext); cookieSyncMngr.startSync(); cookieManager.setCookie(url, cookieValue); result.success(true); cookieSyncMngr.stopSync(); cookieSyncMngr.sync(); + } else { + cookieManager.setCookie(url, cookieValue); + result.success(true); } } @@ -219,7 +222,12 @@ public class MyCookieManager extends ChannelDelegateImpl { cookieManager = getCookieManager(); if (cookieManager == null) return; - String cookieValue = name + "=; Path=" + path + "; Domain=" + domain + "; Max-Age=-1;"; + String cookieValue = name + "=; Path=" + path + "; Max-Age=-1"; + + if (domain != null) + cookieValue += "; Domain=" + domain; + + cookieValue += ";"; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { cookieManager.setCookie(url, cookieValue, new ValueCallback() { @@ -230,13 +238,16 @@ public class MyCookieManager extends ChannelDelegateImpl { }); cookieManager.flush(); } - else { + else if (plugin != null) { CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(plugin.applicationContext); cookieSyncMngr.startSync(); cookieManager.setCookie(url, cookieValue); result.success(true); cookieSyncMngr.stopSync(); cookieSyncMngr.sync(); + } else { + cookieManager.setCookie(url, cookieValue); + result.success(true); } } @@ -249,7 +260,7 @@ public class MyCookieManager extends ChannelDelegateImpl { String cookiesString = cookieManager.getCookie(url); if (cookiesString != null) { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP && plugin != null) { cookieSyncMngr = CookieSyncManager.createInstance(plugin.applicationContext); cookieSyncMngr.startSync(); } @@ -258,7 +269,14 @@ public class MyCookieManager extends ChannelDelegateImpl { for (String cookie : cookies) { String[] nameValue = cookie.split("=", 2); String name = nameValue[0].trim(); - String cookieValue = name + "=; Path=" + path + "; Domain=" + domain + "; Max-Age=-1;"; + + String cookieValue = name + "=; Path=" + path + "; Max-Age=-1"; + + if (domain != null) + cookieValue += "; Domain=" + domain; + + cookieValue += ";"; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) cookieManager.setCookie(url, cookieValue, null); else @@ -287,13 +305,16 @@ public class MyCookieManager extends ChannelDelegateImpl { }); cookieManager.flush(); } - else { + else if (plugin != null) { CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(plugin.applicationContext); cookieSyncMngr.startSync(); cookieManager.removeAllCookie(); result.success(true); cookieSyncMngr.stopSync(); cookieSyncMngr.sync(); + } else { + cookieManager.removeAllCookie(); + result.success(true); } } diff --git a/example/integration_test/cookie_manager/set_get_delete.dart b/example/integration_test/cookie_manager/set_get_delete.dart index 295acb1e..c9b1dce3 100644 --- a/example/integration_test/cookie_manager/set_get_delete.dart +++ b/example/integration_test/cookie_manager/set_get_delete.dart @@ -52,7 +52,7 @@ void setGetDelete() { cookie = await cookieManager.getCookie(url: url, name: "myCookie"); expect(cookie, isNull); - await cookieManager.deleteCookies(url: url); + await cookieManager.deleteCookies(url: url, domain: ".${TEST_CROSS_PLATFORM_URL_1.host}"); cookies = await cookieManager.getCookies(url: url); expect(cookies, isEmpty); }, skip: shouldSkip); diff --git a/ios/Classes/MyCookieManager.swift b/ios/Classes/MyCookieManager.swift index c5090e4b..182f31cf 100755 --- a/ios/Classes/MyCookieManager.swift +++ b/ios/Classes/MyCookieManager.swift @@ -62,15 +62,15 @@ public class MyCookieManager: ChannelDelegate { case "deleteCookie": let url = arguments!["url"] as! String let name = arguments!["name"] as! String - let domain = arguments!["domain"] as! String let path = arguments!["path"] as! String - MyCookieManager.deleteCookie(url: url, name: name, domain: domain, path: path, result: result) + let domain = arguments!["domain"] as? String + MyCookieManager.deleteCookie(url: url, name: name, path: path, domain: domain, result: result) break; case "deleteCookies": let url = arguments!["url"] as! String - let domain = arguments!["domain"] as! String let path = arguments!["path"] as! String - MyCookieManager.deleteCookies(url: url, domain: domain, path: path, result: result) + let domain = arguments!["domain"] as? String + MyCookieManager.deleteCookies(url: url, path: path, domain: domain, result: result) break; case "deleteAllCookies": MyCookieManager.deleteAllCookies(result: result) @@ -231,25 +231,30 @@ public class MyCookieManager: ChannelDelegate { } } - public static func deleteCookie(url: String, name: String, domain: String, path: String, result: @escaping FlutterResult) { + public static func deleteCookie(url: String, name: String, path: String, domain: String?, result: @escaping FlutterResult) { guard let httpCookieStore = MyCookieManager.httpCookieStore else { result(false) return } - + + var domain = domain httpCookieStore.getAllCookies { (cookies) in for cookie in cookies { - var originURL = "" + var originURL = url if cookie.properties![.originURL] is String { originURL = cookie.properties![.originURL] as! String } else if cookie.properties![.originURL] is URL { originURL = (cookie.properties![.originURL] as! URL).absoluteString } - if (!originURL.isEmpty && originURL != url) { - continue + if domain == nil, let domainUrl = URL(string: originURL) { + if #available(iOS 16.0, *) { + domain = domainUrl.host() + } else { + domain = domainUrl.host + } } - if (cookie.domain == domain || cookie.domain == ".\(domain)" || ".\(cookie.domain)" == domain) && cookie.name == name && cookie.path == path { + if let domain = domain, cookie.domain == domain, cookie.name == name, cookie.path == path { httpCookieStore.delete(cookie, completionHandler: { result(true) }) @@ -260,25 +265,30 @@ public class MyCookieManager: ChannelDelegate { } } - public static func deleteCookies(url: String, domain: String, path: String, result: @escaping FlutterResult) { + public static func deleteCookies(url: String, path: String, domain: String?, result: @escaping FlutterResult) { guard let httpCookieStore = MyCookieManager.httpCookieStore else { result(false) return } + var domain = domain httpCookieStore.getAllCookies { (cookies) in for cookie in cookies { - var originURL = "" + var originURL = url if cookie.properties![.originURL] is String { originURL = cookie.properties![.originURL] as! String } - else if cookie.properties![.originURL] is URL{ + else if cookie.properties![.originURL] is URL { originURL = (cookie.properties![.originURL] as! URL).absoluteString } - if (!originURL.isEmpty && originURL != url) { - continue + if domain == nil, let domainUrl = URL(string: originURL) { + if #available(iOS 16.0, *) { + domain = domainUrl.host() + } else { + domain = domainUrl.host + } } - if (cookie.domain == domain || cookie.domain == ".\(domain)" || ".\(cookie.domain)" == domain) && cookie.path == path { + if let domain = domain, cookie.domain == domain, cookie.path == path { httpCookieStore.delete(cookie, completionHandler: nil) } } diff --git a/lib/src/cookie_manager.dart b/lib/src/cookie_manager.dart index 9be6052b..29de8eba 100755 --- a/lib/src/cookie_manager.dart +++ b/lib/src/cookie_manager.dart @@ -372,7 +372,6 @@ class CookieManager { ///Removes a cookie by its [name] for the given [url], [domain] and [path]. /// ///The default value of [path] is `"/"`. - ///If [domain] is empty, its default value will be the domain name of [url]. /// ///[webViewController] is used for deleting the cookie (also session-only cookie) using JavaScript (cookie with `isHttpOnly` enabled cannot be deleted, see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies) ///from the current context of the [WebView] managed by that controller when you need to target iOS below 11 and Web platform. JavaScript must be enabled in order to work. @@ -392,12 +391,11 @@ class CookieManager { Future deleteCookie( {required Uri url, required String name, - String domain = "", String path = "/", + String? domain, @Deprecated("Use webViewController instead") InAppWebViewController? iosBelow11WebViewController, InAppWebViewController? webViewController}) async { - if (domain.isEmpty) domain = _getDomainName(url); assert(url.toString().isNotEmpty); assert(name.isNotEmpty); @@ -435,7 +433,6 @@ class CookieManager { ///Removes all cookies for the given [url], [domain] and [path]. /// ///The default value of [path] is `"/"`. - ///If [domain] is empty, its default value will be the domain name of [url]. /// ///[webViewController] is used for deleting the cookies (also session-only cookies) using JavaScript (cookies with `isHttpOnly` enabled cannot be deleted, see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies) ///from the current context of the [WebView] managed by that controller when you need to target iOS below 11 and Web platform. JavaScript must be enabled in order to work. @@ -454,12 +451,11 @@ class CookieManager { ///- Web Future deleteCookies( {required Uri url, - String domain = "", String path = "/", + String? domain, @Deprecated("Use webViewController instead") InAppWebViewController? iosBelow11WebViewController, InAppWebViewController? webViewController}) async { - if (domain.isEmpty) domain = _getDomainName(url); assert(url.toString().isNotEmpty); @@ -538,11 +534,6 @@ class CookieManager { return cookies; } - String _getDomainName(Uri url) { - String domain = url.host; - return domain.startsWith("www.") ? domain.substring(4) : domain; - } - Future _getCookieExpirationDate(int expiresDate) async { var platformUtil = PlatformUtil.instance(); var dateTime = DateTime.fromMillisecondsSinceEpoch(expiresDate).toUtc();