Couchbase facebook pull authenticator

8 views
Skip to first unread message

Muntianu Marius

unread,
Dec 7, 2015, 8:20:39 AM12/7/15
to Couchbase

I am using couchbase mobile for an application and I want to use facebook for authentication. As per documentation, couchbase offers it's own implementation for authentication, the only required thing would be the token which I retrieve from the android facebook login flow.


The code for Synchronize class looks something like this:


public class Synchronize {

public Replication pullReplication;
public Replication pushReplication;

public static class Builder {
    public Replication pullReplication;
    public Replication pushReplication;

    public Builder(Database database, String url, Boolean continuousPull) {

        if (pullReplication == null && pushReplication == null) {

            URL syncUrl;
            try {
                syncUrl = new URL(url);
            } catch (MalformedURLException e) {
                throw new RuntimeException(e);
            }

            pullReplication = database.createPullReplication(syncUrl);
            pullReplication.setContinuous(true);

            pushReplication = database.createPushReplication(syncUrl);
            pushReplication.setContinuous(true);
        }
    }

    public Builder facebookAuth(String token) {

        if (!TextUtils.isEmpty(token)) {
            Authenticator facebookAuthenticator = AuthenticatorFactory.createFacebookAuthenticator(token);

            pullReplication.setAuthenticator(facebookAuthenticator);
            pushReplication.setAuthenticator(facebookAuthenticator);
        }

        return this;
    }

    public Builder basicAuth(String username, String password) {

        Authenticator basicAuthenticator = AuthenticatorFactory.createBasicAuthenticator(username, password);

        pullReplication.setAuthenticator(basicAuthenticator);
        pushReplication.setAuthenticator(basicAuthenticator);

        return this;
    }

    public Builder addChangeListener(Replication.ChangeListener changeListener) {
        pullReplication.addChangeListener(changeListener);
        pushReplication.addChangeListener(changeListener);

        return this;
    }

    public Synchronize build() {
        return new Synchronize(this);
    }

}

private Synchronize(Builder builder) {
    pullReplication = builder.pullReplication;
    pushReplication = builder.pushReplication;
}

public void start() {
    pullReplication.start();
    pushReplication.start();
}

public void destroyReplications() {
    if (pullReplication != null && pushReplication != null) {
        pullReplication.stop();
        pushReplication.stop();
        pullReplication.deleteCookie("SyncGatewaySession");
        pushReplication.deleteCookie("SyncGatewaySession");
        pullReplication = null;
        pushReplication = null;
    }
}

}


And I use it like this:


...
public void startReplicationSync(String facebookAccessToken) {

    if (sync != null) {
        sync.destroyReplications();
    }

    final String url = BuildConfig.URL_HOST + ":" + BuildConfig.URL_PORT + "/" + DATABASE_NAME;
    sync = new Synchronize.Builder(databaseManager.getDatabase(), url, true)
            .facebookAuth(facebookAccessToken)
            .addChangeListener(getReplicationChangeListener())
            .build();
    sync.start();

}
...

My sync gateway json config file:

{
"interface":":4984",       
"adminInterface":":4985",
"log":["REST"],
"facebook":{
  "register" : true
},
"databases":{               
"sync_gateway":{
"server":"http://localhost:8091",
"bucket":"sync_gateway",
"users": {
    "GUEST": {"disabled": false}
},
"sync":`function(doc) {channel(doc.channels);}`
}
}
}


I also tried with "GUEST": {"disabled": true}, no luck

My problem is that if I do this:


pullReplication.setAuthenticator(facebookAuthenticator);
pushReplication.setAuthenticator(facebookAuthenticator);

Nothing will ever get replicated/pulled from the server. However if I don't set an authenticator, everything is pulled. Is it something I am doing wrong? I really need to use the authenticator in order to prevent some documents to not being replicated for non-authenticated users.


Note! The token is good, as if I am looking in the users section of sync gateway admin, I can see the right profile id of the logged in user token I passed to the couchbase facebook authenticator.

Reply all
Reply to author
Forward
0 new messages