node.js - Firebase Auth ID token 的 "aud"声明不正确

标签 node.js firebase firebase-authentication

我正在尝试验证 idToken 后端。用户已成功登录到 Firebase 客户端,但是当我尝试在后端验证 idToken 时,我收到这个不是很有帮助的错误消息

Firebase Auth ID token has incorrect "aud" claim

错误消息似乎变得更加有用,归结为在 auth 键中没有项目名称:

Error: Firebase ID token has incorrect "aud" (audience) claim. Expected "stripmall-0000" but got "617699194096-0aafcvsml0gke61d6077kkark051f3e1.apps.googleusercontent.com". Make sure the ID token comes from the same Firebase project as the service account used to authenticate this SDK. See https://firebase.google.com/docs/auth/server/verify-id-tokens for details on how to retrieve an ID token.

任何人都知道可能出了什么问题?我从客户端正确收到了 tokenId,所以这应该不是问题。如果以前有人问过这个问题或以任何其他方式微不足道,请真诚地道歉。

  firebase.initializeApp({
        serviceAccount: {
            "type": "service_account",
            "project_id": <project id here>,
            "private_key_id": <key id goes here>,
            "private_key": <key goes here>
            "client_email": <email goes here>,
            "client_id": <my client id>,
            "auth_uri": "https://accounts.google.com/o/oauth2/auth",
            "token_uri": "https://accounts.google.com/o/oauth2/token",
            "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
            "client_x509_cert_url": <url goes here>
        },
        databaseURL: <my db url here>
    });

    router.post("/verify", function (req, res) {
        firebase.auth().verifyIdToken(req.body.idToken).then(function (decodedToken) {
            var uid = decodedToken.sub;
            res.send(uid);
        }).catch(function (error, param2) {
            console.log(error);  // 'Firebase Auth ID token has incorrect "aud" claim'
        });

    });

最佳答案

您的问题可能是您尝试使用由 auth() 函数之一返回的 JWT token ,例如 firebaseRef.auth().signInWithPopup()。这些确实返回了一个 JWT token ,但是身份验证声明可能是错误的,并且不会通过 verifyIdToken 的验证。 Firebase 技术支持证实了这一点。

您必须使用 firebaseRef.auth().currentUser.getToken() 函数。该 token 将通过验证。

关于node.js - Firebase Auth ID token 的 "aud"声明不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38335127/

相关文章:

javascript - 在 Rails 服务器端运行 javascript

firebase - 有什么方法可以根据当前用户的UID将图像从Firebase加载到Flutter项目中

ios - 无法使用 APN 授权 key 发送推送通知 iOS

ios - 尝试自定义 FUIAuthPickerViewController 并在 AppDelegate 中出错

javascript - 将函数结果分配给变量与调用内联有什么好处?

node.js - Node 上的非阻塞 STDIN 检查?

angularjs - 无法使用 Appium for IOS 生成的代码运行 Protractor 脚本,它表示 findelement 不是函数

node.js - socket.io 握手中的 "t="查询参数是什么

android - 在后台从firebase数据库读取数据

javascript firebase.auth().signInWithEmailAndPassword 无法获得成功事件