有没有一种方法可以将Google身份令牌链接到数据库中的公司用户?

时间:2019-08-08 12:42:23

标签: oauth-2.0 actions-on-google google-authentication

我正在对使用OAuth和Google登录链接类型的Google项目进行设置。

以前,我使用在每个请求中发送的userId来在数据库中查找用户,以查看是否有可用的访问令牌和刷新令牌。但是由于不赞成使用userId,因此我正在寻找一种替代方法。

用户开始他/她的对话框,然后碰到这段代码:

app.intent('Give Color', async (conv, { color }) => {
  conv.data[Fields.COLOR] = color;
  if (conv.user.ref) {
    await conv.user.ref.set({ [Fields.COLOR]: color });
    conv.close(`I got ${color} as your favorite color.`);
    return conv.close('Since you are signed in, I\'ll remember it next time.');
  }
  return conv.ask(new SignIn(`To save ${color} as your favorite color for next time`));
});

用户要在其中选择正确的Google帐户的“要继续,将Test App链接到您的Google帐户”。然后,我的/ token端点在包含Google ID令牌(断言)的OAuth服务器上被调用,该令牌保存了所有用户数据。我解码它,在数据库中检查“ sub”是否已经存在,并且抛出以下异常:

return res.status(401).send({ error: 'user_not_found' });

然后开始执行普通的OAuth程序,在那里我将令牌传递给Google。旁注:这是我自己用NodeJS编写的OAuth服务器。我确定访问令牌和刷新令牌已交付给Google。

在令牌交付后,我收到了有关操作的新请求:

app.intent('Get Sign In', async (conv, params, signin) => {
  if (signin.status !== 'OK') {
    return conv.close('Let\'s try again next time.');
  }
  const color = conv.data[Fields.COLOR];
  await conv.user.ref.set({ [Fields.COLOR]: color });
  return conv.close(`I saved ${color} as your favorite color. `
    + 'Since you are signed in, I\'ll remember it next time.');
});

signin.status的值为“ OK”。但是conv.user对象是否不应该包含Google ID令牌,以便我可以将访问和刷新令牌以及Google ID令牌中的此“子”存储在数据库中?还是我出了点问题?

转换用户的内容如下:

User {raw: Object, storage: Object, _id: undefined, locale: "en-BE", verification: "VERIFIED", …}
_id: undefined
[[StableObjectId]]: 7
access: Access {token: "ACCT-ATlbRmcpMI545WJFssRSlK1Jcza46NIB"}
entitlements: Array(0) []
id: undefined
last: Last {seen: Thu Aug 08 2019 10:53:17 GMT+0200 (Central Europea…}
locale: "en-BE"
name: Name {display: undefined, family: undefined, given: undefined}
permissions: Array(0) []
profile: Profile {token: undefined}
raw: Object {accessToken: "ACCT-ATlbRmcpMI545WJFssRSlK1Jcza46NIB", locale: "en-BE", lastSeen: "2019-08-08T08:53:17Z", …}
storage: Object {}
verification: "VERIFIED"
__proto__: Object {constructor: , _serialize: , _verifyProfile: , …}
conv.user.id is *DEPRECATED*: Use conv.user.storage to store data instead

2 个答案:

答案 0 :(得分:1)

它不会包含该用户的Google ID,因为该用户尚未对其进行授权。

他们拥有的授权就是您要求他们通过OAuth服务器授权的任何内容。

因此,您将在conv.user.access中看到服务器已发送给Assistant的访问令牌,然后您可以使用此令牌查找数据库中的用户并采取相应的措施。

如果您特别想要他们的Google ID,则需要确保他们在与Action(通过语音,移动应用或网络应用)相同的项目上使用Google登录。

如果您只需要一个ID,以便稍后可以看到该用户何时回来,则可以使用从Google登录中获得的Google ID,或者仅生成一个ID并将其存储在conv.user.storage中。

答案 1 :(得分:0)

由于我只想拥有一个ID,因此我将使用它:

如果您只需要一个ID,以便稍后可以看到该用户何时返回,则可以使用从Google登录中获取的Google ID,或仅生成一个ID并将其存储在conv.user.storage中。

谢谢!