为什么JWE解密失败?node.js中的Google完整性

toiithl6  于 2023-02-03  发布在  Node.js
关注(0)|答案(1)|浏览(201)

我在node.js/express中写了一个反向代理,让API调用更安全,下面是根据这个文档解码google发来的一个JWE的代码:

app.get('/checkGoogleToken', async (req, res) => {
    const tokens = fs.readdirSync('./tokens/')
    console.log('Checking Google Token')
    const token = fs.readFileSync('./tokens/' + tokens[0], 'utf-8')
    console.log(token)
    const { plaintext, protectedHeader } = await jose.compactDecrypt(
        token,
        Buffer.from(process.env.DECRYPTION_KEY, "base64"
        )); /* integrity_token is the token got from Integrity API response in the app. 
    DECRYPTION_KEY is found from Google Play Console */

    console.log(protectedHeader);
    console.log(new TextDecoder().decode(plaintext));

    const { payload, Header = protectedHeader } = await jose.compactVerify(
        plaintext,
        crypto.createPublicKey("-----BEGIN PUBLIC KEY-----\n" +
            process.env.VERIFICATION_KEY +
            "\n-----END PUBLIC KEY-----")
    )

我在这里找到了代码:Decrypt and verify locally Play Integrity API Token using NodeJS
然而,即使DECRYPTION_KEY是Google提供的,解密也会失败。/tokens/中的文件是包含JWE令牌的纯文本文件,我的测试手机在收到它们后保存了这些文件。不过,有一个小细节。到目前为止,我通过Metro从Expo执行应用程序,不是通过正确的安装通过播放存储(为了加快测试)...这会导致一个不同的密钥被用来加密到JWE?非常感谢!

vmjh9lq9

vmjh9lq91#

解决方案:对于将来遇到此问题的任何人:上面的代码和链接问题中的代码是100%正确的。如果通过Play商店安装,Google Play Integrity只能按照您在Play控制台中定义的方式工作(不过,这也适用于测试版本)。因此,如果您需要摆弄Integrity,我的建议是在开发过程的最后阶段进行,然后通过测试分发进行。

相关问题