Code Monkey home page Code Monkey logo

persistentcookiejar's Introduction

PersistentCookieJar for OkHttp 3

A persistent CookieJar implementation for OkHttp 3 based on SharedPreferences.

If you're looking for a OkHttp 2/HTTPUrlConnection persistent CookieStore it can be found at this Gist.

Download

Step 1. Add the JitPack repository in your root build.gradle at the end of repositories:

allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

Step 2. Add the dependency

dependencies {
    compile 'com.github.franmontiel:PersistentCookieJar:v1.0.1'
}

Usage

Create an instance of PersistentCookieJar passing a CookieCache and a CookiePersistor:

ClearableCookieJar cookieJar =
                new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(context));

Then just add the CookieJar when building your OkHttp client:

OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .cookieJar(cookieJar)
                .build();

Features

This is a really simple library but here are some of the things that it provides:

  • Possibility to clear the jar: PersistentCookieJar implements ClearableCookieJar interface that declares a clear() method for removing all cookies from the jar.

  • Possibility to clear session cookies: PersistentCookieJar implements ClearableCookieJar interface that declares a clearSession() method for removing session cookies from the jar.

  • Decoupled and extensible: CookieCache and CookiePersistor are interfaces so you can provide your own implementation for each one.

    • CookieCache represents an in-memory cookie storage. SetCookieCache is the provided implementation that uses a Set to store the Cookies.
    • CookiePersistor represents a persistent storage. SharedPrefsCookiePersistor is the provided implementation that uses a SharedPreferences to persist the Cookies.
  • Thread-safe: PersistentCookieJar public methods are synchronized so there is no need to worry about threading if you need to implement a CookieCache or a CookiePersistor.

ProGuard

The following configuration is only needed for version 0.9.2 and below:

-dontwarn com.franmontiel.persistentcookiejar.**
-keep class com.franmontiel.persistentcookiejar.**

-keepclassmembers class * implements java.io.Serializable {  
    static final long serialVersionUID;  
    private static final java.io.ObjectStreamField[] serialPersistentFields;  
    !static !transient <fields>;  
    private void writeObject(java.io.ObjectOutputStream);  
    private void readObject(java.io.ObjectInputStream);  
    java.lang.Object writeReplace();  
    java.lang.Object readResolve();  
}

License

Copyright 2016 Francisco José Montiel Navarro

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

persistentcookiejar's People

Contributors

franmontiel avatar haogg avatar malvinstn avatar stkent avatar vovkab avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

persistentcookiejar's Issues

Proguard rules doesn't make sense?

Thanks for the beautiful implementation. But regarding the included proguard rules, they basically keep everything of your library, which I believe is redundant. Can you clarify more on your thought about that setup? Thanks.

Cookie not getting saved when used with kotlin and OKHTTP, while same works for Java and OKHTTPretrofit

Hi, I am using kotlin to do my android project. Im using this library on my other java projects on android and all of them works fine. But when I use this with kotlin the cookie is not getting saved. I want to know whether this has occured to you or do you know the work around for this issue?

I have manually checked using adb commands to read the shared prefrences. While on my java project the cokie is being set but here it isnt. Please respond fast

Upload to JCenter

I'm trying to use this library, but I have never added anything to the android classpath except from gradle.

So please help by

  • Upload to repo
  • Include how to add to project with maven and gradle.

clear session cookies

Hi,

thx again for the library :)

I added a clearSession method in PersistentCookieJar.. maybe also something useful for the lib.

public synchronized void clearSession() {
    this.cache.clear();
    this.cache.addAll(this.persistor.loadAll());
}

Could not find com.github.franmontiel:PersistentCookieJar:v1.0.1.

Hello,

I can't install this plugin for some reason

gradle - project


    repositories {
        jcenter()
        mavenCentral()
        maven { url "https://jitpack.io" }
    }

gradle - module

ext {
    androidSupportVersion = '25.3.1'
    daggerVersion = '2.10'
    butterknifeVersion = '8.5.1'
    retrofitVersion = '2.2.0'
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile "com.android.support:appcompat-v7:$androidSupportVersion"
    compile "com.android.support:design:$androidSupportVersion"
    compile "com.google.dagger:dagger:$daggerVersion"
    annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion"
    compile "com.jakewharton:butterknife:$butterknifeVersion"
    annotationProcessor "com.jakewharton:butterknife-compiler:$butterknifeVersion"
    compile "com.squareup.retrofit2:retrofit:$retrofitVersion"
    compile "com.squareup.retrofit2:converter-gson:$retrofitVersion"
    compile 'com.github.franmontiel:PersistentCookieJar:v1.0.1'
}

What did I miss? Thanks a lot.

problem for SharedPrefsCookiePersistor#saveAll

Link

public void saveAll(Collection<Cookie> cookies) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        for (Cookie cookie : cookies) {
            if (cookie.persistent()) {
                editor.putString(createCookieKey(cookie), new SerializableCookie().encode(cookie));
            }
        }
        editor.apply();
    }

other:

public void add(HttpUrl url, Cookie cookie) {
        String name = getCookieToken(cookie);

        //将cookies缓存到内存中 如果缓存过期 就重置此cookie
        if (!cookie.persistent()) {
            if (!cookies.containsKey(url.host())) {
                cookies.put(url.host(), new ConcurrentHashMap<String, Cookie>());
            }
            cookies.get(url.host()).put(name, cookie);
        } else {
            if (cookies.containsKey(url.host())) {
                cookies.get(url.host()).remove(name);
            }
        }

        //讲cookies持久化到本地
        SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
        prefsWriter.putString(url.host(), TextUtils.join(",", cookies.get(url.host()).keySet()));
        prefsWriter.putString(name, encodeCookie(new SerializableOkHttpCookies(cookie)));
        prefsWriter.apply();
    }

Response Header:

Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=E8B4036464C98E104C35E246CA56FAFC; Path=/
Content-Type: application/json;charset=UTF-8
Content-Length: 537
Date: Thu, 04 Aug 2016 10:45:59 GMT
OkHttp-Sent-Millis: 1470307533457
OkHttp-Received-Millis: 1470307533489

cookie.persistent() Always returns false ? The link method is alway can`t save to SharedPrefs.
help me?

May I ask you something

I use retrofit with okhttp. And following interceptor:

public class SaveCookiesInterceptor implements Interceptor {
    private static final String TAG = "SaveCookiesInterceptor";

    @Override
    public Response intercept(Chain chain) throws IOException {
        Response originalResponse = chain.proceed(chain.request());

        if (!originalResponse.headers("Set-Cookie").isEmpty()) {
            HashSet<String> cookies = new HashSet<>();
            cookies.addAll(originalResponse.headers("Set-Cookie"));
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(App.context);
   //         if (sharedPreferences.getStringSet("cookies", null) == null) {
                Log.d(TAG, "intercept: " + cookies.toString());
                sharedPreferences.edit()
                        .putStringSet("cookies", cookies)
                        .apply();
  //          }
        }

        return originalResponse;
    }
}

...
public class ReadCookiesInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request.Builder builder = chain.request().newBuilder();
        Set<String> preferences =
                PreferenceManager.getDefaultSharedPreferences(App.context).getStringSet("cookies", new HashSet<String>());
        for (String cookie : preferences) {
            builder.addHeader("Cookie", cookie);
            Log.d("OkHttp", "intercept read Header: " + cookie); // This is done so I know which headers are being added; this interceptor is used after the normal logging of OkHttp
        }

        return chain.proceed(builder.build());
    }
}

...
            instance.client = new OkHttpClient.Builder()
                    .cookieJar(cookieJar)
                   .addInterceptor(new ReadCookiesInterceptor())
                   .addInterceptor(new SaveCookiesInterceptor())
                   .addInterceptor(interceptor).build();

Here is the problem:
I emulate login a website, it succeeds, but then I request my profile page, the response is the login page. After some trys, I add if (sharedPreferences.getStringSet("cookies", null) == null) { in SaveCookiesInterceptor. Then everthing works good.
So what happened in earth? Thx for your time in advance.

Cookies are either not persisting, or not being sent out

Hi,

We are using this library with OkHttp3 in 2 separate activities.
In the first activity, we are logging in via a POST, and then starting the second activity.
Once there, we request some information to the same server via a GET call (this is a secured endpoint).
This second call is failing saying the request is not authorized.
This system has been on production for years, so we know it's not the issue.

It looks like either the cookies are not being saved on the initial POST response, or sent out on the GET call. I was under the impression they are saved and sent out by this library, is there something extra that needs to be done?

Here's how we're setting up the client (in both activites):

// Have also tried ClearableCookieJar instead of CookieJar
CookieJar cookieJar =
     new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(this));

OkHttpClient client = new OkHttpClient.Builder()
     .cookieJar(cookieJar)
     .build();

[Question] PersistentCookieJar with multiple account

I know that when I Login all information of the session will save to Sharepreference of Persistent cookie. If I logout I will clear all data for the session in SharePreference.

Now I want use multiple account that mean:

  • When I switch to another account I don't need show login window
  • When one user logout only all data of the user clear other account still exist.

How do I use the library to implement that ?

will block the request queue

I use retrofit2.0 and okhttp3.0, all my request are use the call's method "enqueue()", and I try to response one request delay 5 seconds( just like sleep(5) in PHP), the other requests are waiting the blocked one, and when the blocked request response, the other request execute. locks like the request queue being blocked?

SetCookieCache may lead to ConcurrentModificationException

from code below may see SetCookieCache caches cookies in a HashSet, which is not thread-safe and gives rise to ConcurrentModificationException if accessed by multiple threads at the same time. Multi-thread accessing is not rare case in OkHttp.

private Set<IdentifiableCookie> cookies;

public SetCookieCache() {
    cookies = new HashSet<>();
}

This is a practical modification to make cookie container thread-safe:

cookies = Collections.newSetFromMap(new ConcurrentHashMap<IdentifiableCookie, Boolean>());

SharedPrefsCookiePersistor.loadAll() - SerializableCookie InvalidClassException

Hi,

thx for providing this cookie jar :)

i had some troubles with the SerializableCookie, because it has no serialVersionUID definied.
Therefore, if something in the class is changed (or in my case i updated gradle) the uid changed and the cookies cannot be deserialized.

Furthermore, in SharedPrefsCookiePersistor.loadAll a 'null' cookie is added to the list which causes a NullPointerException later on:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String okhttp3.Cookie.name()' on a null object reference at com.franmontiel.persistentcookiejar.cache.IdentifiableCookie.hashCode(IdentifiableCookie.java:65) at java.util.Collections.secondaryHash(Collections.java:3427) at java.util.HashMap.put(HashMap.java:385) at java.util.HashSet.add(HashSet.java:95) at java.util.AbstractCollection.addAll(AbstractCollection.java:76) at com.franmontiel.persistentcookiejar.cache.SetCookieCache.updateCookies(SetCookieCache.java:46) at com.franmontiel.persistentcookiejar.cache.SetCookieCache.addAll(SetCookieCache.java:36) at com.franmontiel.persistentcookiejar.PersistentCookieJar.<init>(PersistentCookieJar.java:38)

So i suggest to maybe add private static final long serialVersionUID = 1L; to SerializableCookie
and maybe something like this in SharedPrefsCookiePersistor.loadAll():

if (cookie != null) {
    cookies.add(cookie);
} else {
    //cannot decode cookies from sharedPrefs (serialize id changed e.g.); clear all
    clear();
    break;
}

Looking forward to hear your thoughts on this issue.

NullPointerException on IdentifiableCookie.hashCode

I received this stack trace from a user:

com.franmontiel.persistentcookiejar.cache.IdentifiableCookie.hashCode (IdentifiableCookie.java:65)
java.util.HashMap.remove (HashMap.java:624)
java.util.HashSet.remove (HashSet.java:173)
com.franmontiel.persistentcookiejar.cache.SetCookieCache.addAll (SetCookieCache.java:37)
com.franmontiel.persistentcookiejar.PersistentCookieJar.<init> (PersistentCookieJar.java:38)```

Release on JCenter?

My "MyOkhttp" library on jcenter which extend okhttp3. I want to dependency PersistentCookieJar with pom, but it only release on jitpack.
Can it release on jcenter? Or I fork it and publish with my jcenter account. (with group id 'com.franmontiel')

Look forward to your soonest reply.

Get cookies data

I'm not sure how can I check the cookies that are storage by the library, I mean, to see which ones are being saves, and their data, like expiration time, etc

Another question it's that when you clear cookie session, what cookies are you really cleaning there?

Thanks in advance.

Google play rejected my app

I have implemented as you explain in readme. But Google play is rejected my app. That is bcz of using your dependency.

ClearableCookieJar cookieJar =
                new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(context));

OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .cookieJar(cookieJar)
                .build();

NPE on Cookie.name()

Hi,
I encountered the following exception:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String okhttp3.Cookie.name()' on a null object reference
          at com.franmontiel.persistentcookiejar.cache.IdentifiableCookie.hashCode(IdentifiableCookie.java:65)
          at java.util.Collections.secondaryHash(Collections.java:3427)
          at java.util.HashMap.remove(HashMap.java:616)
          at java.util.HashSet.remove(HashSet.java:173)
          at com.franmontiel.persistentcookiejar.cache.SetCookieCache.addAll(SetCookieCache.java:37)
          at com.franmontiel.persistentcookiejar.PersistentCookieJar.<init>(PersistentCookieJar.java:38)

Happened after leaving the app on the background for an extended time.

Repo is unavailable

Hello!
I've added repo and dependency exactly as described to my gradle project, when I build it I get such an error:

Could not resolve all dependencies for configuration ':compile'.
> Could not resolve com.github.franmontiel:PersistentCookieJar:v1.0.1.
  Required by:
      :libjam:unspecified
   > Could not resolve com.github.franmontiel:PersistentCookieJar:v1.0.1.
      > Could not get resource 'https://jitpack.io/com/github/franmontiel/PersistentCookieJar/v1.0.1/PersistentCookieJar-v1.0.1.pom'.
         > Could not GET 'https://jitpack.io/com/github/franmontiel/PersistentCookieJar/v1.0.1/PersistentCookieJar-v1.0.1.pom'.
            > peer not authenticated

That said, I can access the pom from my browser. What can it be?

About some of IdentifiableCookie

Hi~
PersistentCookieJar is helpful for me!
But once I found my request to get three cookie, save only the results of the two, both the cookie value is not the same, I find IdentifiableCookie:

 @Override
    public boolean equals(Object other) {
        if (!(other instanceof IdentifiableCookie)) return false;
        IdentifiableCookie that = (IdentifiableCookie) other;
        return that.cookie.name().equals(this.cookie.name())
                && that.cookie.domain().equals(this.cookie.domain())
                && that.cookie.path().equals(this.cookie.path())
                && that.cookie.secure() == this.cookie.secure()
                && that.cookie.hostOnly() == this.cookie.hostOnly();
    }

In okhttp3 the cookie inside the class methods:

@Override public boolean equals(Object other) {
    if (!(other instanceof Cookie)) return false;
    Cookie that = (Cookie) other;
    return that.name.equals(name)
        && that.value.equals(value)
        && that.domain.equals(domain)
        && that.path.equals(path)
        && that.expiresAt == expiresAt
        && that.secure == secure
        && that.httpOnly == httpOnly
        && that.persistent == persistent
        && that.hostOnly == hostOnly;
  }

I do not know which is better~
Thanks.

Kill the process from the background, from the new entry NullPointerException error

签名打包安装进入应用,从后台杀掉应用。从新进入就报NullPointerException error。cookie反序列化的时候失败,导致cookie为空。我打印了日志貌似从sp中获取到的字符串,就不是正常的cookie序列化后的字符串。烦请有知道原因的帮忙解决一下。多谢

06-26 10:08:00.289 18000-18000/? E/HuiMai.go: crash result ==java.lang.ExceptionInInitializerError
at cn.hhealth.shop.net.k.(Novate.java:104)
at cn.hhealth.shop.net.c.(BaseRequest.java:24)
at cn.hhealth.shop.d.l.(CommonRequest.java:27)
at cn.hhealth.shop.service.SettingService.onCreate(SettingService.java:54)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2990)
at android.app.ActivityThread.access$1900(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5542)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:962)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String okhttp3.Cookie.name()' on a null object reference
at cn.hhealth.shop.net.a.e.hashCode(IdentifiableCookie.java:57)
at java.util.Collections.secondaryHash(Collections.java:3405)
at java.util.HashMap.remove(HashMap.java:616)
at java.util.HashSet.remove(HashSet.java:173)
at java.util.Collections$SynchronizedCollection.remove(Collections.java:422)
at cn.hhealth.shop.net.a.c.a(CookieCacheImpl.java:36)
at cn.hhealth.shop.net.a.f.(NovateCookieManager.java:28)
at cn.hhealth.shop.net.s.a(RetrofitBuilder.java:85)
at cn.hhealth.shop.net.s.(RetrofitBuilder.java:51)
at cn.hhealth.shop.net.s.(RetrofitBuilder.java:39)
at cn.hhealth.shop.net.k.(Novate.java:104) 
at cn.hhealth.shop.net.c.(BaseRequest.java:24) 
at cn.hhealth.shop.d.l.(CommonRequest.java:27) 
at cn.hhealth.shop.service.SettingService.onCreate(SettingService.java:54) 
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2990) 
at android.app.ActivityThread.access$1900(ActivityThread.java:162) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5542) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:962) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) 
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String okhttp3.Cookie.name()' on a null object reference
at cn.hhealth.shop.net.a.e.hashCode(IdentifiableCookie.java:57)
at java.util.Collections.secondaryHash(Collections.java:3405)
at java.util.HashMap.remove(HashMap.java:616)
at java.util.HashSet.remove(HashSet.java:173)
at java.util.Collections$SynchronizedCollection.remove(Collections.java:422)
at cn.hhealth.shop.net.a.c.a(CookieCacheImpl.java:36)
at cn.hhealth.shop.net.a.f.(NovateCookieManager.java:28)
at cn.hhealth.shop.net.s.a(RetrofitBuilder.java:85)
at cn.hhealth.shop.net.s.(RetrofitBuilder.java:51)
at cn.hhealth.shop.net.s.(RetrofitBuilder.java:39)
at cn.hhealth.shop.net.k.(Novate.java:104)
at cn.hhealth.shop.net.c.(BaseRequest.java:24)
at cn.hhealth.shop.d.l.(CommonRequest.java:27)
at cn.hhealth.shop.service.SettingService.onCreate(SettingService.java:54)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2990)
at android.app.ActivityThread.access$1900(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:

Issue with implementation of the Secure flag in cookies

Hello,

I am using the PersistentCookieJar library in my project and I am noticing some incorrect behavior when there are subsequent requests that modify already existing cookies in some very particular cases, which are:

  1. When a cookie is created with the "Secure" flag set and without an expiration date, and then a subsequent request does not include the Secure flag on the cookie, and changes the value of the cookie to a non-empty value, and sets the cookie to be expired, then the saved value of that cookie does not change.

Example:
First request:

header("Set-Cookie: BAD_cookie_with_secure_flag_next_request_unsecure_nonempty_expire=initial_value; path=/; Secure", false);

Second request:

$expiredDate = new \DateTime('now');
$expiredDate->modify('-1 day');
header("Set-Cookie: BAD_cookie_with_secure_flag_next_request_unsecure_nonempty_expire=nonempty_changed_value; expires={$expiredDate->format(DATE_COOKIE)}; path=/;", false);

In this example, after the second request, the value of BAD_cookie_with_secure_flag_next_request_unsecure_nonempty_expire remains as initial_value even though it should have been deleted and not sent at all on the next request, since the cookie is expired.

  1. When a cookie is created with the "Secure" flag set and without an expiration date, and then a subsequent request does not include the Secure flag on the cookie, and changes the value of the cookie to empty, and sets the cookie to be expired, then the saved value of that cookie does not change.

Example:
First request:

header("Set-Cookie: BAD_cookie_with_secure_flag_next_request_unsecure_empty_expire=initial_value; path=/; Secure", false);

Second request:

$expiredDate = new \DateTime('now');
$expiredDate->modify('-1 day');
header("Set-Cookie: BAD_cookie_with_secure_flag_next_request_unsecure_empty_expire=; expires={$expiredDate->format(DATE_COOKIE)}; path=/;", false);

In this example, after the second request, the value of BAD_cookie_with_secure_flag_next_request_unsecure_empty_expire remains as initial_value even though it should have been deleted and not sent at all on the next request, since the cookie is expired.

  1. When a cookie is created without the "Secure" flag set and without an expiration date, and then a subsequent request does include the Secure flag on the cookie, and changes the value of the cookie to empty, and sets the cookie to be expired, then the saved value of that cookie does not change.

Example:
First request:

header("Set-Cookie: BAD_cookie_without_secure_flag_next_request_secure_empty_expire=initial_value; path=/;", false);

Second request:

$expiredDate = new \DateTime('now');
$expiredDate->modify('-1 day');
header("Set-Cookie: BAD_cookie_without_secure_flag_next_request_secure_empty_expire=; expires={$expiredDate->format(DATE_COOKIE)}; Secure; path=/;", false);

In this example, after the second request, the value of BAD_cookie_without_secure_flag_next_request_secure_empty_expire remains as initial_value even though it should have been deleted and not sent at all on the next request, since the cookie is expired.

In all three of these cases, the tests were accessed via HTTPS. Chrome on Android deleted the cookies in all three cases. Desktop Chrome and Safari on Mac OS also deleted the cookies in all cases.

It looks to me like in these cases there is something going wrong in com.franmontiel.persistentcookiejar.PersistentCookieJar#saveFromResponse because the persistor seems to contain non-persistent cookies (without an expiration date) when it should not contain them.

I think this might be happening because there might duplicate cookies entries getting created because of the Secure flag getting added into these requests. I see that this is the case in com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor#createCookieKey. Based on the logic in that function, it seems that there is one cookie created for HTTP and another one for HTTPS. The expected behavior is for only one cookie to be created, not dependent upon the Secure flag or protocol.

For reference, the spec for the Secure attribute is here: https://tools.ietf.org/html/rfc6265#section-4.1.2.5

using ipv6 ip will cause IllegalArgumentException("unexpected domain")

I've faced a exception when using ipv6 ip address.
I'm using okhttp3 3.6.0 with PresistentCookieJar.
When try to connect directly using ivp6 address ex: 2001:b011:400a:156e:211:32ff:fe0f:c06b
The exception occur when SerializableCookie try to read cooke from an inputStream through readObject function

final String domain = (String) in.readObject();
builder.domain(domain);`

It using Cookie.Builder to set domain into cookie.
And inside builder.domain, it checks invalidHostname which can not contains ":"

  private static boolean containsInvalidHostnameAsciiCodes(String hostnameAscii) {
    for (int i = 0; i < hostnameAscii.length(); i++) {
      char c = hostnameAscii.charAt(i);
      // The WHATWG Host parsing rules accepts some character codes which are invalid by
      // definition for OkHttp's host header checks (and the WHATWG Host syntax definition). Here
      // we rule out characters that would cause problems in host headers.
      if (c <= '\u001f' || c >= '\u007f') {
        return true;
      }
      // Check for the characters mentioned in the WHATWG Host parsing spec:
      // U+0000, U+0009, U+000A, U+000D, U+0020, "#", "%", "/", ":", "?", "@", "[", "\", and "]"
      // (excluding the characters covered above).
      if (" #%/:?@[\\]".indexOf(c) != -1) {
        return true;
      }
    }
    return false;
  }

ref:
https://github.com/square/okhttp/blob/master/okhttp/src/main/java/okhttp3/internal/Util.java#L386

But ipv6 address contains colon anyway.
How can I fix this?
Thanks

Share cookie with Webview

I there a way to pass the cookie,which is stored for each request to a webview so that webview will also consume the same session

How to remove cookies for a target url?

There is a function to load all cookies for a target url:

@Override
synchronized public List<Cookie> loadForRequest(HttpUrl url) { ... }

It would be nice if the cookie jar has a function to remove all cookies for a target url too, e.g.:

@Override
synchronized public void removeForRequest(HttpUrl url) { ... }

What's the correct way to remove cookies from the cache and persistent storage at the moment? The only way I can imagine is to clone the cookie list for a given url and change all the expiresAt values to -1.

But that's a pretty shit solution because there is no reason to re-construct the whole list as only one variable needs to be changed in order for the cookie jar to remove those cookies at the next loadForRequest call (then all of those would be expired).

But making the expiresAt variable public would be no pretty implementation, so a separate function to remove all cookies for a given url (as suggested above) would be nice!

Duplicated cookie

I have requested to login using basic authentication, it redirects to user details.
After it finished, I have checked cookie.

wfwaf-authcookie-c448b9945c37d9cc4b94b09f3d21632a=27419%7Cadministrator%7Cbfbc704c50089e8b848ca10c14b1b74ad4a309992102e3faf98bdd782d6aeb47; expires=Wed, 09 Nov 2016 14:19:08 GMT; path=/; httponly
wfvt_4021301540=5822879cee322; expires=Wed, 09 Nov 2016 02:49:08 GMT; path=/; httponly
wfvt_4021301540=5822879e5f5cc; expires=Wed, 09 Nov 2016 02:49:10 GMT; path=/; httponly

I found wfvt_4021301540 duplicated.

I think one is for example.com, other for www.example.com.
I have requested to example.com.

How to read cookies?

Please guide me how to read cookies by domain & path...

//I'm using like below
ClearableCookieJar cookieJar = new PersistentCookieJar(
new SetCookieCache(), new SharedPrefsCookiePersistor(context));
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.cookieJar(cookieJar)
.build();

Thanks.

When is removeAll method called?

I was using the Cookie Persistor, I noticed that the removeAll method is being called if there are no cookies on the REST return. Is that correct? This prevents me from using the cookies previously stored.

I am using okhttp3 followed by retrofit2 and the next time cookies are lost

I am using okhttp3 followed by retrofit2 and the next time cookies are lost below is my code and

public static ClearableCookieJar cookieManager = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(context)); public static ClearableCookieJar getCookieManager() { return cookieManager; }

`OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(NETWORK_TIMEOUT, TimeUnit.SECONDS);
builder.readTimeout(READ_TIMEOUT, TimeUnit.MINUTES);
builder.cookieJar(TrestApplication.getCookieManager());

    OkHttpClient clientOkHttp = builder.build();

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(baseUrl)
            .client(clientOkHttp)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    return retrofit;`

Invalid Cast

Hi! Whenever I try and use your library i always get the following issue

java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
at com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor.loadAll(SharedPrefsCookiePersistor.java:48)
at com.franmontiel.persistentcookiejar.PersistentCookieJar.<init>(PersistentCookieJar.java:38)

I have no idea what is going on and I was wondering if anyone could help me out.
To provide some context the following is the HTTP class I use to preform requests...

public class HTTP {

    // Cookie manager
    OkHttpClient client;
    ClearableCookieJar cookieJar;

    public HTTP(SharedPreferences preferences) {
        this.cookieJar =
                new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(preferences));

        // Set up the okhttp client
        this.client = new OkHttpClient.Builder()
                .cookieJar(cookieJar)
                .build();
    }

    public Response performRequest(Request request) throws Exception {
        // Perform the request
        Response response = client.newCall(request).execute();
        // Save the cookies
        return response;
    }
}

There is only one param but I had set all cookies in loadForRequest(HttpUrl url).

Cookie mi=a8f64fbd22f8f70afee8a48c2c18f202
This is my request cookie data.
@OverRide
synchronized public List loadForRequest(HttpUrl url) {
List cookiesToRemove = new ArrayList<>();
List validCookies = new ArrayList<>();

    for (Iterator<Cookie> it = cache.iterator(); it.hasNext(); ) {
        Cookie currentCookie = it.next();

        if (isCookieExpired(currentCookie)) {
            cookiesToRemove.add(currentCookie);
            it.remove();

        } else if (currentCookie.matches(url)) {
            validCookies.add(currentCookie);
        }
    }

    persistor.removeAll(cookiesToRemove);

    return validCookies;
}

I use your method for add request cookie. But there is only one param when I request server,and in the debug I had ensure the return validCookies is a unbroken cookie.
It gives me a headache,Have you met this situation???
Please help me!!!

cookie disappeared

Hello, when APP clear the background, restart again, cookie disappeared, and how should I do? Thank you very much!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.