Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow clients to fetch all cookies including HttpOnly. #773

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,21 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
case "cleanCache":
cleanCache(result);
break;
case "getAllCookies":
getAllCookies(call, result);
break;
default:
result.notImplemented();
break;
}
}

private void getAllCookies(MethodCall call, final MethodChannel.Result result){
if (webViewManager != null){
webViewManager.getAllCookies(call,result);
}
}

private void cleanCache(MethodChannel.Result result) {
webViewManager.cleanCache();
WebStorage.getInstance().deleteAllData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,13 @@ public void onReceiveValue(Boolean aBoolean) {
}
}

void getAllCookies(MethodCall call, final MethodChannel.Result result){
String url = call.argument("url");
CookieManager cookieManager = CookieManager.getInstance();
String cookieStr = cookieManager.getCookie(url);
result.success(cookieStr);
}

private void clearCache() {
webView.clearCache(true);
webView.clearFormData();
Expand Down
24 changes: 24 additions & 0 deletions ios/Classes/FlutterWebviewPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
[self onCanGoForward:call result:result];
} else if ([@"cleanCache" isEqualToString:call.method]) {
[self cleanCache:result];
} else if ([@"getAllCookies" isEqualToString:call.method]) {
[self getAllCookies:call completionHandler:^(NSString *cookies) {
result(cookies);
}];
} else {
result(FlutterMethodNotImplemented);
}
Expand Down Expand Up @@ -303,6 +307,26 @@ - (void)cleanCookies:(FlutterResult)result {
}
}

- (void)getAllCookies:(FlutterMethodCall*)call
completionHandler:(void (^_Nullable)(NSString * cookies))completionHandler {
if (self.webview != nil) {
NSString *url = call.arguments[@"url"];
WKHTTPCookieStore *cookieStore = self.webview.configuration.websiteDataStore.httpCookieStore;
[cookieStore getAllCookies:^(NSArray<NSHTTPCookie *> * _Nonnull cookies) {
NSString *allCookies = @"";
NSEnumerator *cookie_enum = [cookies objectEnumerator];
NSHTTPCookie *temp_cookie;
while (temp_cookie = [cookie_enum nextObject]) {
NSString *temp = [NSString stringWithFormat:@"%@=%@;",[temp_cookie name],[temp_cookie value]];
allCookies = [allCookies stringByAppendingString:temp];
}
completionHandler([NSString stringWithFormat:@"%@", allCookies]);
}];
} else {
completionHandler(nil);
}
}

- (void)cleanCache:(FlutterResult)result {
if (self.webview != nil) {
if (@available(iOS 9.0, *)) {
Expand Down
18 changes: 18 additions & 0 deletions lib/src/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,24 @@ class FlutterWebviewPlugin {
_instance = null;
}

// get all cookies including HttpOnly
// but for ios, it only support ios 11 or above
Future<Map<String, String>> getAllCookies(String url) async {
final cookiesString = await _channel.invokeMethod('getAllCookies', {'url': url});
final cookies = <String, String>{};

if (cookiesString?.isNotEmpty == true) {
cookiesString.split(';').forEach((String cookie) {
if (cookie.isNotEmpty && cookie.contains('=')) {
final split = cookie.split('=');
cookies[split[0].trim()] = split[1].trim();
}
});
}

return cookies;
}

Future<Map<String, String>> getCookies() async {
final cookiesString = await evalJavascript('document.cookie');
final cookies = <String, String>{};
Expand Down