当前位置: 首页 > 知识库问答 >
问题:

如何使用firebase认证模拟器中存储的用户对firebase云功能进行认证?

薛经艺
2023-03-14

我的firebase cloud函数包含受保护的路由,只有在请求头中传递有效的IdToken时才能访问这些路由。云函数API如下所示

函数/index.js


const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp({
  credential: admin.credential.applicationDefault(),
  databaseURL: "DB_URL"
});

const express = require('express');
const app = express();


const authenticate = async (req, res, next) => {
  if (!req.headers.authorization || !req.headers.authorization.startsWith('Bearer ')) {
    res.status(403).send('Unauthorized');
    return;
  }
  const idToken = req.headers.authorization.split('Bearer ')[1];
  try {
    const decodedIdToken = await admin.auth().verifyIdToken(idToken);
    req.user = decodedIdToken;
    next();
    return;
  } catch(e) {
    res.status(403).send('Unauthorized');
    return;
  }
};

app.use(authenticate);


app.get('/protected', async (req, res) => {
    return res.send('OK');
});

exports.api = functions.https.onRequest(app);

最初,我使用Firebase身份验证创建新用户并获取IdToken

curl 'https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=[API_KEY]' \
-H 'Content-Type: application/json' \
--data-binary '{"email":"[user@example.com]","password":"[PASSWORD]","returnSecureToken":true}'
curl 'https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=[API_KEY]' \
-H 'Content-Type: application/json' \
--data-binary '{"email":"[user@example.com]","password":"[PASSWORD]","returnSecureToken":true}'
curl --location --request GET 'http://localhost:5001/abc-production/us-central1/api/protected/' \
--header 'Authorization: Bearer SECRET_ID_TOKEN'

我可以使用Auth emulator UI创建新用户,但是如何生成这些用户的访问令牌呢?是否有任何APIendpoint可以返回本地保存的用户的IdToken,这样我就可以测试受保护的API,而无需在生产中添加用户?

此外,当我运行auth模拟器时,使用production Firebase身份验证环境生成的idToken不能工作。

"code": "auth/argument-error",
        "message": "Firebase ID token has invalid signature. See https://firebase.google.com/docs/auth/admin/verify-id-tokens for details on how to retrieve an ID token."

共有1个答案

皇甫展
2023-03-14

模拟器只用于本地测试,而不用于生产,您不能使用模拟器发出的IdToken访问其他服务,如Firebase函数,因为正如文档所说:

出于安全原因,身份验证模拟器会发出未签名的ID令牌,当在云函数模拟器中运行时,这些令牌仅被接受为Firebase Admin SDK的verifyIdToken和createSessionCookie。

因此,这些令牌只在模拟器环境中有效,如果您想将它们与Firebase函数一起使用,则只能使用Functions模拟器。有关如何在本地运行这些函数,请参阅本指南。

 类似资料:
  • null 云存储桶是云功能工作所必需的吗?如果没有,我怎么能回到这些变化发生之前的地方。请引导我。

  • 我正在firebase中开发一个应用程序。创建订单时,我希望生成其发票。我已经实现了这个功能,但是URL有问题,因为pdf是创建的,但是正如你在图片中看到的,打开它的URL是错误的。 这是我在云函数index.js中的代码: 如你所见,pdf创建正确,但网址是错误的,我不能打开它。 如果有人帮助我,我将非常感激。

  • 我有两个存储桶,一个来自谷歌云存储,一个由firebase创建,firebase创建的存储桶是默认的,我想更改默认的存储桶并删除firebase创建的存储桶。

  • 使用新的firebase云函数,我决定将我的一些HTTPendpoint移动到firebase。一切都很顺利...但我有以下问题。我有两个endpoint由HTTP触发器构建(云函数) 用于创建用户并返回由Firebase Admin SDK生成的自定义令牌的APIendpoint。 获取某些用户详细信息的API终结点。 虽然第一个endpoint是好的,但是对于第二个endpoint,我希望只为

  • 我正在学习firebase,有一些关于Firebase存储的问题: < li >我可以使用的总文件大小的限制? < li >我想使用firebase datastorage使用avatar上传图像,然后使用该url将图像加载到带有glide或picasso的imageview。可能吗? < li >我想将我的应用程序与firebase authenticate配合使用,以使用登录、firebase

  • 我建立Android应用程序链接到谷歌云存储。我想允许访问GCS到我的Android应用程序只。 谷歌提供三种安全连接地面军事系统的解决方案: Oauth 2.0(谷歌账户也是如此) 资料来源:https://developers.google.com/storage/docs/authentication?hl=FR 是否有其他通过地面军事系统安全连接的解决方案?我希望通过这种方式在地面军事系统