NodeJS 未找到信封的pem:{“alg”:“RS 256”,“kid”:“5a 66482 db 3800 c83 c63”,“typ”:“JWT”}

yr9zkbsy  于 5个月前  发布在  Node.js
关注(0)|答案(4)|浏览(40)

我正在尝试验证和解码由前端发送的id令牌。我得到这个错误,当我运行verifyfunction。有时它可能会工作。

No pem found for envelope: {"alg":"RS256","kid":"53c666482db3800c83c63","typ":"JWT"}

字符串
这是我的代码

const ticket = await client.verifyIdToken({
        idToken: token,
        audience: '804312674051-5o4.apps.googleusercontent.com',
    });
    const payload = ticket.getPayload();

jobtbby3

jobtbby31#

今天终于找到了答案,Firebase工具会将原生Google连接到第三方登录token上,然后再封装一层,此时获得的token已经不再是Google给我们的原始token了。

  • A1:
  • 原始令牌:GoogleDesignInAccount Account = Task.getResult(ApiException.class);
  • Account.getidToken()//这是原始令牌
  • B1:
  • Firebase令牌:FireBaseUser currentUser = Mauth.getCurrentUser();
  • String token = currentUser.getIdToken(false).getResult().getToken();
  • A2:
  • 谷歌官方提供了一种验证令牌的方法
  • B2:
  • Firebase正式提供了身份验证令牌方法

以上四个数据点我们都使用了代码名,如果需要在后台验证token的合法性,它们必须相互对应,A1到A2,B1到B2,如果使用A2验证B1,则会失败

3xiyfsfu

3xiyfsfu2#

我也遇到了同样的问题.
我在谷歌src文件里找到的

if (!certs.hasOwnProperty(envelope.kid)) {
    // If this is not present, then there's no reason to attempt verification
    throw new Error('No pem found for envelope: ' + JSON.stringify(envelope));
  }

字符串
但我不知道那是什么意思。

hmae6n7t

hmae6n7t3#

如果您的目的是验证令牌,请使用此方法。

admin.auth().verifyIdToken(idToken)
    .then(function(decodedToken) {
        let uid = decodedToken.uid;
    }

字符串
或者这个:

const decodedToken = await admin.auth().verifyIdToken(idToken)
    let uid = decodedToken.uid;
    console.log(uid);

tgabmvqs

tgabmvqs4#

问题是使用的令牌。
您可以使用此示例生成令牌,仅更改content=“YOUR_CLIENT_ID.apps.googleusercontent.com“以获得有效的客户端ID google
使用console.log中生成的令牌进行测试

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="google-signin-client_id" content="YOUR_CLIENT_ID.apps.googleusercontent.com">
    <title>Demo Sing-In</title>
</head>
<body>
    <h1>Google Sing-In</h1>
    <div class="g-signin2" data-onsuccess="onSignIn"></div>
    <a href="#" onclick="signOut();">Sign out</a>
    <script src="https://apis.google.com/js/platform.js" async defer></script>
    <script>
        function onSignIn(googleUser) {
            var profile = googleUser.getBasicProfile();
            console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead.
            console.log('Name: ' + profile.getName());
            console.log('Image URL: ' + profile.getImageUrl());
            console.log('Email: ' + profile.getEmail()); // This is null if the 'email' scope is not present.
            var id_token = googleUser.getAuthResponse().id_token;
            console.log(id_token);
        }

        function signOut() {
            var auth2 = gapi.auth2.getAuthInstance();
            auth2.signOut().then(function () {
            console.log('User signed out.');
            });
        }
    </script>
</body>
</html>

字符串

相关问题