【问题标题】:In firebase - How to generate an idToken on the server for testing purposes?在 firebase - 如何在服务器上生成 idToken 用于测试目的?
【发布时间】:2018-01-15 18:08:06
【问题描述】:

我想测试一个创建用户的云功能。

在正常情况下,我在浏览器中生成一个idToken,然后通过标头将其发送到服务器:Authorization : Bearer etcIdToken

但是我想在没有浏览器的情况下测试这个功能。在我的摩卡测试中,我有:

before(done => {
   firebase = require firebase.. -- this is suppose to be like the browser lib.
   admin = require admin.. 

    idToken = null;
    uid = "AY8HrgYIeuQswolbLl53pjdJw8b2";
    admin.auth()
        .createCustomToken(uid)               -- admin creates a customToken
        .then(customToken => {
            return firebase.auth()            -- this is like browser code. customToken get's passed to the browser.
                .signInWithCustomToken(customToken)     -- browser signs in.
                .then(signedInUser => firebase.auth()             -- now i want to get an idToken. But this gives me an error.
                    .currentUser.getIdToken())
        })
        .then(idToken_ => {
            idToken = idToken_
            done();
        })
        .catch(err => done(err));
})

我得到的错误是:

firebase.auth(...).currentUser.getIdToken is not a function - getting the idToken like this works on client - and is documented here.

我直接尝试了signedInUser.getIdToken()。同样的问题:

signedInUser.getIdToken is not a function - 未记录。只是一个测试。

我认为这是因为 firebase 对象不适合 node.js 使用,就像我在这里所做的那样。登录时 - 东西被保存在浏览器本地存储中 - 也许这就是原因。

但问题仍然存在。如何在 node.js 中获取 idToken 以便能够进行测试:

return chai.request(myFunctions.manageUsers)
    .post("/create")
    .set("Authorization", "Bearer " + idToken)   --- i need the idToken here -  like would be if i'm getting it from the browser.
    .send({
          displayName: "jony",
          email: "jony@gmail.com",
          password: "123456"
    })

我是不是搞错了?我知道如果我能得到idToken 它将起作用。我需要浏览器吗?谢谢:)

【问题讨论】:

标签: javascript node.js firebase firebase-authentication google-cloud-functions


【解决方案1】:

从Exchange custom token for an ID and refresh token,您可以使用 api 将自定义令牌转换为 id 令牌。因此,您只需首先从 uid 生成自定义令牌,然后将其转换为自定义令牌。这是我的示例:

const admin = require('firebase-admin');
const config = require('config');
const rp = require('request-promise');

module.exports.getIdToken = async uid => {
  const customToken = await admin.auth().createCustomToken(uid)
  const res = await rp({
    url: `https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken?key=${config.get('firebase.apiKey')}`,
    method: 'POST',
    body: {
      token: customToken,
      returnSecureToken: true
    },
    json: true,
  });
  return res.idToken;
};

【讨论】:

  • 在 firebase auth 模拟器 url 中运行的任何人:http://localhost:9099/www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken?key=${environment.firebase.apiKey}
【解决方案2】:
curl 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=<FIREBASE_KEY>' -H 'Content-Type: application/json'--data-binary '{"email": "test@test.com","password":"test","returnSecureToken":true}'

如果此 curl 没有运行,请尝试在 Postman 上运行相同的操作。有效!

【讨论】:

    【解决方案3】:

    L.迈耶的回答对我有用。

    但是,rp npm 包已弃用且不再使用。 这是使用 axios 修改后的工作代码。

    const axios = require('axios').default;
    const admin = require('firebase-admin');
    const FIREBASE_API_KEY = 'YOUR_API_KEY_FROM_FIREBASE_CONSOLE';
    
    const createIdTokenfromCustomToken = async uid => {
      try {
        const customToken = await admin.auth().createCustomToken(uid);
    
        const res = await axios({
          url: `https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken?key=${FIREBASE_API_KEY}`,
          method: 'post',
          data: {
            token: customToken,
            returnSecureToken: true
          },
          json: true,
        });
    
        return res.data.idToken;
    
      } catch (e) {
        console.log(e);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-06-24
      • 1970-01-01
      • 1970-01-01
      • 2017-06-18
      • 2022-08-16
      • 1970-01-01
      • 2021-10-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多