How to get expiration/persistency from WebKit Cookiemanager cookies?

Viewed 157

I am reading authentication cookies from a webView and re-using them in my own connections for authentication. This works fine as long as I assume all cookies are persistent. However, the system I am connecting with distinguishes persistent ("keep me logged in") and session cookies, and I can't seem to get that information back from the WebKit CookieManager.

I have created a little sample application that illustrates the problem: https://github.com/Paul-JanPauptit/WebViewPersistentCookies

My WebView setup:

private void initializeDialog() {
    // Clear cookies (callback not handled in this test implementation)
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.removeAllCookies(null);

    WebView webView = new WebView(getContext());

    webView.setLayoutParams(new LinearLayout.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT));
    webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading (WebView view, WebResourceRequest request)
        {
            CookieManager cookieManager = CookieManager.getInstance();
            String cookiesString = cookieManager.getCookie(_url);

            if (cookiesString != null)
                Log.d(TAG, String.format("%s:\n%s", request.getUrl().toString(), cookiesString));

            return false;
        }
    });

    WebSettings settings = webView.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setJavaScriptCanOpenWindowsAutomatically(true);
    settings.setBuiltInZoomControls(true);
    setContentView(webView);

    webView.loadUrl(_url);
}

Fiddler response: Cookie in response, contains an expiration date

(notice how this does contain an expiry date, 5 days from now)

However, retrieving the cookies string from the WebKit CookieManager:

FedAuth=77u/PD9[Redacted, same string as in Fiddler] 1nPT08L1NQPg==;

(notice how this does not contain the expiry date bits)

When parsed into individual cookies, they end up with persisted=false, so I don't know which cookies to persist over app sessions.

Is this the expected behavior? Is there another way to retrieve the cookies from WebKit without having the expiration date stripped off?

0 Answers
Related