Why is user invalid on push replication

130 views
Skip to first unread message

Brian Hardy

unread,
May 22, 2014, 6:41:19 PM5/22/14
to mobile-c...@googlegroups.com


Hi.

I authenticate with couchbase. I thought things were good but when a delta is pushed I get invalid user.
I am using custom authentication. I get a cookie value from the sync gateway but I don't see anything in the sync gateway logs about user logging in as I did when I was using persona. 
Here is the logs from my android client

05-22 15:00:18.060: D/Syncer(18540): Putting cookie into replicator of type pull cookie = SyncGatewaySession=7dad25e46b2fed3f5ac9cfc0f09e6952232ad7e9; Expires=2014-05-23T22:00:19.008067287Z; Token=FFE4B8305E6EEEEEEEEE636184E5CC6; Username=brianh

05-22 15:00:18.060: D/Syncer(18540): Putting cookie into replicator of type push cookie = SyncGatewaySession=7dad25e46b2fed3f5ac9cfc0f09e6952232ad7e9; Expires=2014-05-23T22:00:19.008067287Z; Token= FFE4B8305E6EEEEEEEEE636184E5CC6; Username=brianh



05-22 15:01:18.665: E/Sync(18540): Exception getting status from {error=forbidden, id=brianh, reason=wrong user, status=403}
05-22 15:01:18.665: E/Sync(18540): java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
05-22 15:01:18.665: E/Sync(18540): at com.couchbase.lite.replicator.Pusher.statusFromBulkDocsResponseItem(Pusher.java:445)
05-22 15:01:18.665: E/Sync(18540): at com.couchbase.lite.replicator.Pusher.access$400(Pusher.java:44)
05-22 15:01:18.665: E/Sync(18540): at com.couchbase.lite.replicator.Pusher$3.onCompletion(Pusher.java:390)
05-22 15:01:18.665: E/Sync(18540): at com.couchbase.lite.support.RemoteRequest$2.run(RemoteRequest.java:277)
05-22 15:01:18.665: E/Sync(18540): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
05-22 15:01:18.665: E/Sync(18540): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-22 15:01:18.665: E/Sync(18540): at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
05-22 15:01:18.665: E/Sync(18540): at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
05-22 15:01:18.665: E/Sync(18540): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-22 15:01:18.665: E/Sync(18540): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-22 15:01:18.665: E/Sync(18540): at java.lang.Thread.run(Thread.java:841)


Code setting the cookie on the replications. The replicators are started shortly afterward.

   private void updateReplicators(Authenticator authenticator) {
               Database database = getDatabase(databaseName);
               String cookie = CookieManager.getInstance().getCookie(syncUrl.toExternalForm());
               boolean shouldUseCookies = !(authenticator instanceof BasicAuthenticator);
               if (shouldUseCookies && cookie == null) {
                       Log.d(TAG,  "Using cookie based authentication however no cookies have been set for key "+ syncUrl.toExternalForm());
               }
               List<Replication> replications = getAllReplications(database);
               if (replications != null) {
                       for (Replication replication : replications) {
                               if (shouldUseCookies) {
                                       String type = replication.isPull() ? "pull" : "push";
                                       Log.d(TAG, "Putting cookie into replicator of type "+ type +" cookie = "+ cookie);
                                       replication.getHeaders().put("Cookie", cookie);
                               } else {
                                       replication.deleteCookie(syncUrl.toExternalForm());
                                       replication.getHeaders().put("Cookie", null);
                                       CookieManager.getInstance().setCookie(syncUrl.toExternalForm(), null);
                               }
                               replication.setAuthenticator(authenticator);
                               replication.stop();
                       }
               }
       }


Here is my config.json - updated to simply check for userid of "brianh"

 {
    "interface": ":4984",
        "adminInterface": ":4985",
        "log": ["CRUD", "CRUD+", "HTTP", "HTTP+", "Access", "Cache", "Shadow", "Shadow+", "Changes", "Changes+"],
        "databases": {
        "bsg_sync": {
                    "server": "http://localhost:8091",
                    "bucket": "bsg_sync",
          "users": {
                          "GUEST": {"disabled": false, "admin_channels":["*"], "all_channels":["*"]}
          },
         
"sync": `function(doc, oldDoc) {
                                channel(doc.channels);
                if (doc.type == "profile") {
                   if (oldDoc) {
                       requireUser("brianh");
                   
}
               }
           }`
       
}
   }
}


Here's the data on the server

curl -X GET localhost:4985/bsg_sync/brianh

{"_id":"brianh","_rev":"297-51800495-1cfc-412e-88cd-e456803c7e34","mySchedule":["session:6696","session:6232","session:6370","session:5058","session:5314"],"type":"profile"}

Another item should be added to the list here but is not.

Brian Hardy

unread,
May 22, 2014, 7:04:30 PM5/22/14
to mobile-c...@googlegroups.com
Ok.

Fixed. I guess I have to use this line now rather than set the header values as before:

String value = getValue(cookie);  // from SyncGatewaySession=a;lkajsdkjaskfja;skfa;ksfjasf;ksajdf;k

replication.setCookie("SyncGatewaySession", value, null, null, false, false);

Brian Hardy

unread,
May 22, 2014, 8:37:53 PM5/22/14
to mobile-c...@googlegroups.com
Hmmm.

Unfortunately, calling that setCookie call seems to have stopped my pull replication from working. Any idea why?

Thanks

Brian Hardy

unread,
May 22, 2014, 8:58:28 PM5/22/14
to mobile-c...@googlegroups.com
I was trying to set the name of the cookie to the same value I used in the CookieManager and then set the value to the full cookie string. I thought that might be the problem. My pull replicator continues to work them but I have the push sync problem I originally wrote about so I assume the name should be "SyncGatewaySession" and the value should the the value from the SyncGatewaySession name value pair in the cookie. Any advice?

Thanks

Traun Leyden

unread,
May 27, 2014, 12:58:37 PM5/27/14
to mobile-c...@googlegroups.com
If you are using replication.setCookie() (which is the recommended approach), then you shouldn't need to call replication.getHeaders().put("Cookie", cookie); or any other API calls related to setting the cookie.

TodoLite-Android was recently updated to have the ability to call replication.setCookie().

If you change:

private AuthenticationType authenticationType = AuthenticationType.FACEBOOK;

to 

private AuthenticationType authenticationType = AuthenticationType.CUSTOM_COOKIE;

then when you login it will prompt you for a Cookie.  To create a valid session cookie, you can create a new session by using curl to make a new session on the Sync Gateway (sorry I don't have an example handy).

If you are still seeing the same problem with TodoLite-Android, then file an issue on github with exact steps to reproduce.  If not, then you should compare the code w/ your app to see what you are doing differently.










On Thu, May 22, 2014 at 5:58 PM, Brian Hardy <brian...@gmail.com> wrote:
I was trying to set the name of the cookie to the same value I used in the CookieManager and then set the value to the full cookie string. I thought that might be the problem. My pull replicator continues to work them but I have the push sync problem I originally wrote about so I assume the name should be "SyncGatewaySession" and the value should the the value from the SyncGatewaySession name value pair in the cookie. Any advice?

Thanks

--
You received this message because you are subscribed to the Google Groups "Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mobile-couchba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mobile-couchbase/1d41107b-38c3-4d6c-a9b4-ee3526740b31%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages