firebase 验证 ID 令牌给出:Firebase ID 令牌具有不正确的“iss”

Psi*_*dom 4 python firebase firebase-authentication

如何验证idToken从服务器上的客户端发送?

我正在使用 python关注此处文档。基本上我在客户端上签字email,并password返回了idToken,然后我送这个idToken给服务器进行验证,但得到了如下错误。

ValueError:Firebase ID 令牌的“iss”(颁发者)声明不正确。预期“ https://securetoken.google.com/myproject ”但得到“ https://identitytoolkit.google.com/ ”。确保 ID 令牌与用于对此 SDK 进行身份验证的服务帐号来自同一 Firebase 项目。有关如何检索 ID 令牌的详细信息,请参阅 https://firebase.google.com/docs/auth/admin/verify-id-tokens

我很确定我在与客户端相同的项目中生成了服务帐户 json。有谁知道可能是什么问题?

我的服务器代码如下所示:

from firebase_admin import credentials, initialize_app, auth

def is_authenticated(id_token):
    cred = credentials.Certificate("service-account.json")
    app = initialize_app(cred)
    return auth.verify_id_token(id_token, app)
Run Code Online (Sandbox Code Playgroud)

All*_*oso 8

除了发送电子邮件和密码之外,您还必须将标志设置returnSecureTokentrue,否则您的 IdToken 将无效并且您也不会获得刷新令牌。

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}'
Run Code Online (Sandbox Code Playgroud)

文档


Psi*_*dom 0

看来我在客户端遵循了错误的文档;最终我遵循了这个文档,并使用以下代码提取了idToken我发送到后端进行验证的内容:

firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idToken) {
  // Send token to your backend via HTTPS
  // ...
}).catch(function(error) {
  // Handle error
});
Run Code Online (Sandbox Code Playgroud)