google-app-engine - 在 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?
解决方案
Admin SDK 正在做 Endpoints 代理在验证令牌时所做的事情。代理只是传递经过验证的令牌。只要 Endpoints 代理保留在您的应用程序前面,您就可以解码X-Endpoint-API-UserInfo
令牌。
推荐阅读
- python - 在 Keras 分类中实现注意力
- python - 如何从 qtreeview 中的父行中获取特定列
- json - Angular:使用查找检索名称属性不起作用
- excel - VBA INDEX MATCH 运行时错误“1004”:应用程序定义或对象定义错误
- variables - 如何在ansible-playbook中添加一个在满足条件时增加的变量?
- arm - AOSP 构建 TARGET_PRODUCT 失败
- android - Firebase 缓存与实现自定义房间缓存
- ruby - 执行 HoneyC 时出错 - `require':无法加载此类文件
- java - 打印 Mqtt 主题并选择一个订阅其中一个
- vba - 在循环中跳过空文本框