首页 文章

在Google App Engine上解码Firebase idToken

提问于
浏览
0

我的firebase用户正在向我的API服务器发送请求 . 我正在使用Google Cloud Endpoints中的安全规则验证它们 . 我在Google App Engine上不使用管理员SDK来提取他们的用户ID .

通常,Google建议通过此代码 in their example Firebase Cloud Functions code 验证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');
  });

但是, in the example Google App Engine code ,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 . 我在 Cloud endpoints 上设置了安全性,只允许firebase用户访问路由,但是,我只希望用户访问自己的数据,因此我需要解码他们的id令牌以检索他们的userID . 我想知道Cloud Endpoints是否在这里处理身份验证 . Do I need to have admin SDK verify the token? or the simple decoding in Google's example is secure enough because cloud endpoints already takes care of verifying the idToken?

1 回答

  • 1

    Admin SDK正在执行 endpoints 代理在验证令牌时所执行的操作 . 代理只是传递经过验证的令牌 . 只要 endpoints 代理保留在您的应用程序前面,您就可以解码 X-Endpoint-API-UserInfo 令牌 .

相关问题