Couchbase facebook 拉验证器

Couchbase facebook pull authenticator

我正在使用 couchbase 移动应用程序,我想使用 facebook 进行身份验证。根据文档,couchbase 提供了它自己的身份验证实现,唯一需要的是我从 android facebook 登录流程中检索到的令牌。

同步 class 的代码如下所示:

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;
    }
}

}

我是这样使用的:

...
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();

}
...

我的同步网关 json 配置文件:

{
"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);}`
}
}
}

我也试过 "GUEST": {"disabled": true}, 运气不好

我的问题是,如果我这样做

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

replicated/pulled 永远不会从服务器获取任何内容。但是,如果我不设置身份验证器,一切都会被拉走。是我做错了什么吗?我真的需要使用身份验证器来防止某些文档无法为未经过身份验证的用户复制。

注意!令牌很好,就像我正在查看同步网关管理员的用户部分一样,我可以看到我传递给 couchbase facebook 身份验证器的已登录用户令牌的正确个人资料 ID。

在您提供的 Sync Gateway 配置中,Sync Function 是 function(doc, oldDoc) {channel(doc.channels);} 这意味着如果 Sync Gateway 处理的文档在 channels 字段下包含一个字符串,则该文档将映射到 this/these 个频道。让我们假设以下配置文件:

{
  "log": ["CRUD"],
  "databases": {
    "db": {
      "server": "walrus:",
      "users": {
        "GUEST": {"disabled": false, "admin_channels": ["*"]}
      },
      "sync": `
        function sync(doc, oldDoc) {
          channel(doc.channels);
        }
      `
    }
  }
}

如果频道字段不存在,则文档将映射到名为 undefined 的频道。但是 GUEST 帐户可以访问 * 频道(代表所有频道的占位符)。因此,所有未经身份验证的复制都将提取所有文档。现在让我们介绍配置文件中的 facebook 登录字段。这一次,使用 facebook 令牌进行身份验证的复制代表默认情况下只能访问 ! 频道的新用户(观看此截屏视频以了解 ! 频道,a.k.a public频道https://www.youtube.com/watch?v=DKmb5mj9pMI). To give a user access to other channels, you must use the access API call in the Sync Function (read more about all Sync Function API calls here).

在facebook认证的情况下,使用用户的facebook ID来表示用户名。假设文档有一个 属性 保存用户的 facebook ID (user_id: FACEBOOK_ID),您可以将文档映射到一个频道并授予用户访问权限。新的同步功能如下所示:

function(doc, oldDoc) {
  channel(doc._id);
  access(doc.user_id, doc._id);
}

您可以使用 Facebook Android SDK 检索用户的 Facebook ID 并保存在文档字段中。