【问题标题】:Authentication with Stripe Subscriptions Node.js使用 Stripe 订阅 Node.js 进行身份验证
【发布时间】:2020-10-22 16:42:03
【问题描述】:

我无法确定使用 Stripe 攻击订阅身份验证的最佳方法。我有一个预先存在的 MERN 堆栈,它使用 JWT 进行身份验证,存储订阅 ID。但是,要检测取消、发票不完整等变化,我想不出没有套接字的解决方案。

有什么想法吗?

【问题讨论】:

    标签: node.js mongodb mongoose jwt stripe-payments


    【解决方案1】:

    在您续订 JWT 之前,只需检查订阅状态 - 并确保到期时间足够短,让您在未付费访问窗口方面感到舒适。

    您还可以使用一些中间件来为每个 ajax 请求(或至少一个常见/频繁的 ajax 请求)执行此操作,如果订阅不再被视为“付费”,则返回 402 并使 JWT 无效。

    【讨论】:

    • 是的,所以我的想法是重新加载用户模式,例如,每 5 分钟(从 JWT 到期开始)并在此处比较状态是否存在争议(如果有效),发送一个刷新的用户对象。或者将一般身份验证和订阅保存在单独或嵌入的令牌中。我唯一担心通用身份验证令牌的有效期如此短是验证令牌的刷新
    • @NicholasDullam 这可能对你有用:auth0.com/blog/blacklist-json-web-token-api-keys
    【解决方案2】:

    查看用于接收事件通知的条带 webhook。

    https://stripe.com/docs/webhooks

    监听您的 Stripe 帐户上的事件,以便您的集成可以自动触发反应。

    当您的帐户发生事件时,Stripe 使用 webhook 通知您的应用程序。 Webhook 对于异步事件特别有用,例如当客户的银行确认付款、客户对收费提出异议或定期付款成功时。

    只需三个步骤即可开始在您的 Stripe 集成中使用 webhook:

    1.在您的服务器上创建一个 webhook 端点。

    2.使用 Stripe CLI 测试您的端点是否正常工作。

    3.向 Stripe 注册端点以上线。

    创建 webhook 的基本示例

    // This example uses Express to receive webhooks
    const app = require('express')();
    
    // Use body-parser to retrieve the raw body as a buffer
    const bodyParser = require('body-parser');
    
    // Match the raw body to content type application/json
    app.post('/webhook', bodyParser.raw({type: 'application/json'}), (request, response) => {
      let event;
    
      try {
        event = JSON.parse(request.body);
      } catch (err) {
        response.status(400).send(`Webhook Error: ${err.message}`);
      }
    
      // Handle the event
      switch (event.type) {
        case 'payment_intent.succeeded':
          const paymentIntent = event.data.object;
          // Then define and call a method to handle the successful payment intent.
          // handlePaymentIntentSucceeded(paymentIntent);
          break;
        case 'payment_method.attached':
          const paymentMethod = event.data.object;
          // Then define and call a method to handle the successful attachment of a PaymentMethod.
          // handlePaymentMethodAttached(paymentMethod);
          break;
        // ... handle other event types
        default:
          // Unexpected event type
          return response.status(400).end();
      }
    
      // Return a response to acknowledge receipt of the event
      response.json({received: true});
    });
    
    app.listen(8000, () => console.log('Running on port 8000'));
    

    【讨论】:

    • 是的,就数据库更改而言,我正计划使用网络挂钩。如果有意义的话,我更倾向于在不使用 Web 套接字的情况下刷新凭据客户端。感谢您的回复
    猜你喜欢
    • 2021-04-20
    • 2021-11-07
    • 2016-10-21
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-19
    相关资源
    最近更新 更多