apache / cordova-plugin-inappbrowser

Apache Cordova InAppBrowser Plugin
https://cordova.apache.org/
Apache License 2.0
1.12k stars 2.16k forks source link

add getCookies method #804

Open YazeedFares opened 3 years ago

YazeedFares commented 3 years ago

Feature Request

Motivation Behind Feature

We are looking for a way to get the cookies from the browser to maintain the session id cookie

Feature Description

We are developing a app that connects to an API that depends on the session ID to authenticate the user after SSO login with SAML provider.

We already implemented a method to do that but looking for official support from the plugin, we can provide our implementation too

samulla commented 3 years ago

Hi Yazeed, I have a similar requirement for the SSO login. Would you please share your method?

Best Regards, Samiulla

NiklasMerz commented 3 years ago

The session and cookies are shared between the Cordova main webview and the InAppBrowser.

I don't think it is and should be possible to read the cookies programatically other than standard browser methods.

YazeedFares commented 3 years ago

@NiklasMerz Not anymore after using WKWebview

NiklasMerz commented 3 years ago

@NiklasMerz Not anymore after using WKWebview

What do you mean? Cookies are not shared anymore? This will be fixed after these two bug fixes get released.

https://github.com/apache/cordova-plugin-inappbrowser/pull/825 https://github.com/apache/cordova-ios/pull/1031

This means when the new versions of cordova-ios and the inappbrowser plugin are out this should work again.

tinni95 commented 3 years ago

Hello, any news on this?

I am looking to share cookies session between system browser and cordova webview as well. This works out of the box in android, but it doesn't on IOS.

@YazeedFares could you share how you achieved it?

timbru31 commented 3 years ago

The required cordova-ios@6.2.0 release is now successfully published, we can now continue to work on a new InAppBrowser release (no ETA though)

adhishnigam01 commented 3 years ago

Hello All

Same issue with my application as well, With wkwebview not able to maintain the session cookies in inappbrowser(v3.2.0) because of this issue we need to login every time to open third party page ,with UIwebview (which is old one) it's working fine.

using v5 for cordova iOS

@NiklasMerz @timbru31 @samulla @YazeedFares @tinni95 Please help

Regards Adhish

YazeedFares commented 3 years ago

@tinni95 @samulla @adhishnigam01

What I did to share the cookie is to implement new methods in the "CDVWKInAppBrowser.m" file then exposed them in the related JS file "inappbrowser.js", then we managed the session cookie based on our need, here is the code I did:

In inappbrowser.js I added:

getCookies:function (url, successCallback, errorCallback) {
        exec(successCallback, errorCallback, 'InAppBrowser', 'getCookies', [url]);
 }

In CDVWKInAppBrowser.m we added the following methods:

- (void) getCookies:(CDVInvokedUrlCommand*)command{
    WKWebsiteDataStore* dataStore = [WKWebsiteDataStore defaultDataStore];
    WKHTTPCookieStore* cookieStore = dataStore.httpCookieStore;

    __block NSString *url = command.arguments[0];
    __block CDVPluginResult* pluginResult;
    __block bool cookieFound = NO;
    __block NSString* cookieValue = @"";

    [cookieStore getAllCookies:^(NSArray* cookies) {
        NSHTTPCookie* cookie;
        for(cookie in cookies){
            if([url containsString: cookie.domain]){
                if (cookie.value != nil && [cookie.name isEqual:@"SESSIONID"] && ![cookie.value isEqual:@""]) {
                    cookieFound = YES;
                    cookieValue = cookie.value;
                    break;
                }
            }
        }

        if(cookieFound){
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:@{@"cookieValue": cookieValue}];
        } else {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"No cookie found"];
        }

        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];

    }];
}
- (void) setCookieFor:(NSString *)url withCookieInfo:(NSString *)cookieInfo {
    WKWebsiteDataStore* dataStore = [WKWebsiteDataStore defaultDataStore];
    WKHTTPCookieStore* cookieStore = dataStore.httpCookieStore;
    NSMutableDictionary* cookieProperties = [NSMutableDictionary dictionary];

    NSArray *cookieParts = [cookieInfo componentsSeparatedByString:@"="];

    NSURL* urlObject = [NSURL URLWithString:url];
    NSString* reducedUrl = [NSString stringWithFormat:
        @"%@://%@/%@",
        urlObject.scheme,
        urlObject.host,
        urlObject.pathComponents[1]];

    //set rest of the properties
    if ([cookieParts count] > 1){
        [cookieProperties setObject:cookieParts[0] forKey:NSHTTPCookieName];
        [cookieProperties setObject:cookieParts[1] forKey:NSHTTPCookieValue];
        [cookieProperties setObject:@"/<path>" forKey:NSHTTPCookiePath];
        [cookieProperties setObject:[urlObject host] forKey:NSHTTPCookieDomain];
        //create a NSDate for some future time
        NSDate* expiryDate = [[NSDate date] dateByAddingTimeInterval:2629743];
        [cookieProperties setObject:expiryDate forKey:NSHTTPCookieExpires];
        [cookieProperties setObject:@"TRUE" forKey:NSHTTPCookieSecure];
        NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];

        [dataStore.httpCookieStore setCookie:cookie completionHandler:^{

        }];

        [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
    }
}
breautek commented 3 years ago

@adhishnigam01

inappbrowser(v3.2.0)

Version 3.2.0 is old. I'd recommend upgrading to 5.0.0 to see if it fixes your issue. Notably, it contains:

The InAppBrowser and main webview now share web resources again (like cookies, sessions etc.).

Which is a patch that I think you're after.

tinni95 commented 3 years ago

@YazeedFares thanks a lot for your answer!


getCookies:function (url, successCallback, errorCallback) {
        exec(successCallback, errorCallback, 'InAppBrowser', 'getCookies', [url]);
 }

I think in Inappbrowser.js you would add another method like this for setCookies? and also what would you give setCookies as a parameter, on ionic side? If you have some time to answer, that will be really appreciated :)

Thanks

adhishnigam01 commented 3 years ago

Hi @breautek

Thanks for reply and sorry for late responce ,I have tride with same version but unfortunately showing the same result for me :(

@YazeedFares and @tinni95 Thanks for the code ,but due to data security we can not pass the cookies manually. Do we have any plugin available which can overcome this issue ?

Thanks

nmanikiran commented 3 years ago

Hi @breautek

i am using InAppBrowser(v5.0.0) on iOS WKWebView , cookies are not getting shared

works fine on Android

some part of application i want to open in InAppBrowser but it is asking for login again

adhishnigam01 commented 3 years ago

Hi @breautek @NiklasMerz @nmanikiran

I am not able to open 3rd party website through Inappbrowser v5.0.0 and cordova iOS version 6.2.0(WKwebview) on iOS its asking for login .

I am facing same issue as @nmanikiran have, but still not got any solution for this, It would be great if some one help on this.