如何在服务器上创建Firebase令牌以用于单元测试? [英] How to create Firebase token on server for use with unit tests?

查看:76
本文介绍了如何在服务器上创建Firebase令牌以用于单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用node对Firebase用户进行身份验证,以便可以测试某些服务器端方法.对于每个受保护的请求,我使用以下命令验证Firebase令牌:

I need to authenticate a Firebase user using node so I can test some server side methods. For each protected request, I verify the Firebase token using:

firebase.auth().verifyIdToken(firebaseAccessToken).then(function(decodedToken) {
    // forward request
})

因此,在测试中,我从Firebase数据库中使用uid创建了一个令牌

So in my test I created a token with a uid from my Firebase database

firebase.auth().createCustomToken(uid).then(function(token) {
    //add to header for requests
})

后来我读到自定义令牌没有通过verifyIdToken方法验证,只有客户端生成的令牌得到了验证.

Later I read that custom tokens are not verified by the verifyIdToken method, only client generated ones.

我已经看过这个答案-

I've looked at this answer - server side verification of tokens in firebase

所以我将databaseAuthVariableOverride添加到了初始json

So I added databaseAuthVariableOverride to the init json

firebase.initializeApp({
  credential: firebase.credential.cert(serviceAccount),
  databaseURL: [dbURL],
  databaseAuthVariableOverride: {
    uid: [uid]
  }
});

仍然可以在测试中获得输出

Still getting the output in my tests

Error: expected 200 "OK", got 401 "Unauthorized"

和Firebase错误-

And the firebase error -

Error: Decoding Firebase ID token failed. Make sure you passed the entire string JWT which represents an ID token. See https://firebase.google.com/docs/auth/admin/verify-id-tokens for details on how to retrieve an ID token.

那我如何用当前设置模拟用户?

So how do I emulate a user with my current setup?

推荐答案

这是一个用于生成Firebase ID令牌(不是自定义令牌)的Python脚本.

python firebase_token_generator.py <UID>

可能有更简单的方法可以执行此操作,但是您可以从Node调用Python脚本.

There are probably easier ways to do this but you could call the Python script from Node.

这篇关于如何在服务器上创建Firebase令牌以用于单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆