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

add getCookies method #804

Open
YazeedFares opened this issue Oct 20, 2020 · 13 comments
Open

add getCookies method #804

YazeedFares opened this issue Oct 20, 2020 · 13 comments

Comments

@YazeedFares
Copy link

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
Copy link

samulla commented Jan 4, 2021

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

Best Regards,
Samiulla

@NiklasMerz
Copy link
Member

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
Copy link
Author

@NiklasMerz Not anymore after using WKWebview

@NiklasMerz
Copy link
Member

@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.

#825
apache/cordova-ios#1031

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

@tinni95
Copy link

tinni95 commented Feb 5, 2021

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
Copy link
Member

timbru31 commented Feb 8, 2021

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
Copy link

adhishnigam01 commented Feb 18, 2021

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
Copy link
Author

YazeedFares commented Feb 18, 2021

@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
Copy link
Contributor

@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
Copy link

tinni95 commented Feb 26, 2021

@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
Copy link

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
Copy link

nmanikiran commented Apr 14, 2021

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants