【问题标题】:How do I implement JWT with pub sub push如何使用 pub sub push 实现 JWT
【发布时间】:2021-09-18 03:36:20
【问题描述】:

我使用推送方法 here 遵循了有关 pub/sub 通知的文档

而且我想通过 JWT 对我的通话进行身份验证。我查看了他们的 GitHub 示例here

app.post('/pubsub/authenticated-push', jsonBodyParser, async (req, res) => {
  // Verify that the request originates from the application.
  if (req.query.token !== PUBSUB_VERIFICATION_TOKEN) {
    res.status(400).send('Invalid request');
    return;
  }

  // Verify that the push request originates from Cloud Pub/Sub.
  try {
    // Get the Cloud Pub/Sub-generated JWT in the "Authorization" header.
    const bearer = req.header('Authorization');
    const [, token] = bearer.match(/Bearer (.*)/);
    tokens.push(token);

    // Verify and decode the JWT.
    // Note: For high volume push requests, it would save some network
    // overhead if you verify the tokens offline by decoding them using
    // Google's Public Cert; caching already seen tokens works best when
    // a large volume of messages have prompted a single push server to
    // handle them, in which case they would all share the same token for
    // a limited time window.
    const ticket = await authClient.verifyIdToken({
      idToken: token,
      audience: 'example.com',
    });

    const claim = ticket.getPayload();
    claims.push(claim);
  } catch (e) {
    res.status(400).send('Invalid token');
    return;
  }

  // The message is a unicode string encoded in base64.
  const message = Buffer.from(req.body.message.data, 'base64').toString(
    'utf-8'
  );

  messages.push(message);

  res.status(200).send();
});

但我有一些问题。

  1. 什么是 PUBSUB_VERIFICATION_TOKEN,我如何获取它并将其存储在我的环境中?

  2. const [, token] = bearer?.match(/Bearer (.*)/);抛出以下错误

    键入'RegExpMatchArray |空 | undefined' 必须有一个返回 iterator.ts(2488) 的 'Symbol.iterator' 方法

  3. 如果他们从不在此函数中检查该数组中是否存在已存在的令牌/声明,为什么还要将声明和令牌推送到数组中?

我正在尝试使用 Firebase Cloud Function 来实现这一点,这就是我所拥有的。甚至可以缓存令牌/声明吗?

//Service account auth client
const authClient = new google.auth.JWT({
    email: android_key.client_email,
    key: android_key.private_key,
    scopes: ["https://www.googleapis.com/auth/androidpublisher"]
});

export const handlePubSub = functions.region('europe-west1').https.onRequest(async (req, res) => {

    // What is PUBSUB_VERIFICATION_TOKEN???
    if (req.query.token !== PUBSUB_VERIFICATION_TOKEN) {
        res.status(400).send('Invalid request');
        return;
    }

    try {
        const bearer = req.header('Authorization');
        const [, token] = bearer?.match(/Bearer (.*)/); //Error Type 'RegExpMatchArray | null | undefined' must have a 'Symbol.iterator' method that returns an iterator.ts(2488)
        tokens.push(token); // Why do this? Can I do this in firebase cloud functions

        const ticket = await authClient.verifyIdToken({
            idToken: token,
        });

        const claim = ticket.getPayload();
        claims.push(claim);  // Why do this? Can I do this in firebase cloud functions
    } catch (e) {
        res.status(400).send('Invalid token');
        return;
    }

    const message = Buffer.from(req.body.message.data, 'base64').toString(
        'utf-8'
    );

    console.log(message);

    return res.status(200).json({
        statusCode: 200,
        method: req.method,
        message: 'Recieved successfully'
    });
});

【问题讨论】:

    标签: android node.js google-cloud-functions jwt google-cloud-pubsub


    【解决方案1】:

    什么是 PUBSUB_VERIFICATION_TOKEN 以及如何获取和存储它 在我的环境中?

    PUBSUB_VERIFICATION_TOKEN 可以是您想要的任何值。设置环境变量的最简单方法是在运行node时在命令行上:

    PUBSUB_VERIFICATION_TOKEN=whatevertoken node app.js
    

    比较的req.query.token也来自URL查询字符串。

    GET /whatever?token=whatevertoken
    

    键入'RegExpMatchArray |空 | undefined' 必须有一个 'Symbol.iterator' 方法返回一个 iterator.ts(2488)

    这是他们代码中的一个错误。 bearer.match 可以返回 undefined/null 不能传播到数组 [, token] 中。该示例仅在成功的正则表达式匹配时才有效。这将在纯 javascript 中解析,但 typescript 在编译时会突出显示此问题。

    const bearer = req.header('Authorization');
    const m = /Bearer (.*)/.exec(bearer)
    if (m) tokens.push(m[1])
    

    如果他们从不检查,为什么要将声明和令牌推送到数组中 此函数中的该数组用于已经存在的令牌/声明?

    示例 cmets // List of all messages received by this instance。 与其说是功能性的东西,不如说是一个调试商店。

    【讨论】:

    • 那么我该如何设置req.query.token呢?
    • 示例代码使用查询字符串...所以GET /whatever?token=whatevertoken
    猜你喜欢
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    • 2023-04-02
    • 2021-10-21
    • 2023-04-04
    • 2012-04-28
    • 1970-01-01
    相关资源
    最近更新 更多