首页 > 解决方案 > 在 Google App Engine 上解码 Firebase idToken

问题描述

我的 firebase 用户正在向我的 API 服务器发送请求。我正在使用 Google Cloud Endpoints 中的安全规则对其进行验证。我在 Google App Engine 上不使用管理 SDK 就提取了他们的用户 ID。

通常,Google 建议在他们的示例 Firebase Cloud Functions 代码中通过此代码验证 HTTPS 请求的传入 id 令牌:

  admin.auth().verifyIdToken(idToken).then((decodedIdToken) => {
    console.log('ID Token correctly decoded', decodedIdToken);
    req.user = decodedIdToken;
    return next();
  }).catch((error) => {
    console.error('Error while verifying Firebase ID token:', error);
    res.status(403).send('Unauthorized');
  });

但是,在示例 Google App Engine 代码中,Google 在没有管理 SDK 的情况下解码令牌:

let authUser = { id: 'anonymous' };
const encodedInfo = req.get('X-Endpoint-API-UserInfo');
if (encodedInfo) {
  authUser = JSON.parse(Buffer.from(encodedInfo, 'base64'));
}

我正在使用 Google Cloud Endpoints 来保护托管在 Google App Engine 上的 API。我在云端点上设置了安全性,只允许 firebase 用户访问路由,但是,我只希望用户访问他们自己的数据,所以我需要解码他们的 id 令牌以检索他们的 userID。我想知道 Cloud Endpoints 是否在这里处理身份验证。我需要让管理员 SDK 验证令牌吗?还是谷歌示例中的简单解码足够安全,因为云端点已经负责验证 idToken?

标签: google-app-enginefirebase-authenticationgoogle-cloud-endpoints

解决方案


Admin SDK 正在做 Endpoints 代理在验证令牌时所做的事情。代理只是传递经过验证的令牌。只要 Endpoints 代理保留在您的应用程序前面,您就可以解码X-Endpoint-API-UserInfo令牌。


推荐阅读