如何构建我的应用程序以使用 Firebase、Braintree、Ionic/AngularJS 和最小的 Nodejs 服务器

2024-01-10

参考这个问题:Braintree Dropin UI 无法与 Ionic Framework 一起使用,除非强制刷新 https://stackoverflow.com/questions/32531662/braintree-dropin-ui-does-not-work-with-ionic-framework-unless-force-refresh/32571410#32571410

我当前的 Ionic / Angular / Firebase + 一个非常简单的 Node 服务器应用程序在使用 Braintree 向用户信用卡收费时存在安全问题。根据 @RaymondBerg 的说法,问题在于客户端可以发布任何 customerId 并创建 Braintree 令牌并向该客户收费。由于我所有的用户授权都发生在 Firebase / Angular - 客户端。因此,当用户从我的 AngularJS/Ionic 到我的 Node 服务器执行 $HTTP.post 时,我不想再次授权它们(因为我什至不知道如何做到这一点,所以我使用 Firebase)。

那么设置 Firebase 和我的 Node 服务器以与 Braintree 等支付系统配合使用的策略是什么?

我能想到的一件事是,首先在 http 请求之前在我的 firebase 中创建一个节点,然后在客户端(Ionic 应用程序)中传入客户端 $id 进行请求:

$scope.getToken = function () {
       var ref = new Firebase('[FirebaseURL]/braintreePaymentToken');
       var tokenObj = $firebaseObject(ref.child(posterId));
       tokenObj.tokenGenerated = true; 
       tokenObj.$save().then(function(){
        $http({
          method: 'POST',
          url: 'http://localhost:3000/api/v1/token',
          data: {
            //user $id from Firebase
            userId: snapshot.key(),
          }
        })
       }

在 Firebase 中,我将安全规则设置为:

  "braintreePayment": {
      ".read": false,
      ".write": false,
    },
   "braintreePaymentToken": {
      "$uid": {
       ".read": "auth != null",
       ".write": "auth != null && auth.uid == $uid",
      }
    },

这样,临时节点 BraintreePaymentToken 只能由应用程序中的当前登录用户写入。其他登录用户(恶意用户)不能在此节点上写入,因为他们的auth.uid将不等于posterId,其中posterId是需要付费的用户。

在服务器端,我使用一次来查看是否可以找到该值:

var ref = new Firebase('[FirebaseURL]');
app.post('/api/v1/token', jsonParser, function (request, response) {

  var userId = request.body.userId;
  console.log (userId);

  //customerId from braintree is stored here so no one except the server can read it
  ref.child('braintreePayment').child(userId).once("value", function(snapshot){
    var exists = (snapshot.val() !== null);
    console.log (exists);
    if (exists) {
    console.log ("using exsiting customer!");

    //If braintreePaymentToken with userId child exsited, it mean this request is come from my Ionic client, not from anywhere else.

    ref.child('braintreePaymentToken').child(userId).once("value", function(snap) {
    if (snap.val()) {
      gateway.clientToken.generate({
        customerId: snapshot.val().customerId
      }, function (err, res) {
        if (err) throw err;
        response.json({
          "client_token": res.clientToken
        });
        //After I return the clientToken, I delete the braintreePaymentToken node. It is like using Firebase to send email with Zaiper. More secue I guess?
        ref.child('braintreePaymentToken').child(userId).remove();
      });
    else {
      response.json({
          "client_token": "Unauthorized Access!"
        });
    }
 } else {
    console.log ("using no customer!");
    gateway.clientToken.generate({}, function (err, res) {
      if (err) throw err;
      response.json({
        "client_token": res.clientToken
      });
    });
 }
 }); 
});

当用户点击我的客户端(离子应用程序)上的支付按钮时,我再次执行 Firebase Once 请求以查看 customerId 是否已在我的 firebase/braintreePayment 中。如果没有,我们用 Braintree 创建的返回交易 customerId 保存一个。

app.post('/api/v1/process', jsonParser, function (request, response) {

  var transaction = request.body;
  ref.child('braintreePayment').child(transaction.userId).once("value",        function(snapshot){
 var exists = (snapshot.val() !== null);
 console.log (exists);
 if (exists) {
    console.log ("Return customer!");
    gateway.transaction.sale({
      amount: transaction.amount,
      paymentMethodNonce: transaction.payment_method_nonce,
      options: {
        submitForSettlement: true
      },

    }, function (err, result) {
      if (err) throw err;
      response.json(result);
    });
 } else {
    console.log ("First time customer!");
    gateway.transaction.sale({
      amount: transaction.amount,
      paymentMethodNonce: transaction.payment_method_nonce,

      options: {
        store_in_vault_on_success: true,
        submitForSettlement: true
      },

    }, function (err, result) {
      if (err) throw err;

      console.log ("Customer Id: " + result.transaction.customer.id);
      var customerId = result.transaction.customer.id;

           ref.child('braintreePayment').child(transaction.userId).update({customerId: customerId});

      response.json(result);
    });
    }
   });

  });

如您所见,这确实很复杂。但我不知道有更好、更安全的方法来做到这一点...... 这是 Firebase、Node 和 Braintree 之间构建的最佳方式吗?这是否解决了 OWASP 安全问题?有没有办法改进这段代码,或者有更好的方法来做到这一点?

Thanks!


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何构建我的应用程序以使用 Firebase、Braintree、Ionic/AngularJS 和最小的 Nodejs 服务器 的相关文章

  • 替换 firebase 键中无效字符的好方法?

    我的用例是保存用户的信息 当我尝试使用用户的电子邮件地址作为密钥将数据保存到 Firebase 时 Firebase 会引发以下错误 错误 密钥无效 电子邮件受保护 cdn cgi l email protection 不能包含 因此 显然
  • 我应该开发一个单独的 Express 服务器,还是处理 next.js 应用程序中的所有 API 调用?

    我的网站将执行 CRUD 操作 并将与 MongoDB 和 Firebase 存储 身份验证配合使用 开发单独的 Express 服务器而不是将所有内容集成到我的 next js 应用程序中的原因 优势是什么 据我所知 这一切都可以在我的
  • 使用 Bower 添加 ui.bootstrap 依赖项

    我正在尝试将 ui bootstrap 依赖项添加到我的角度项目中 我正在使用 yeoman bower 我这样做了 哟有角 Bower 安装 Angular UI 然后我添加到我的 index html 文件中 和 咕噜发球 然后我将 u
  • 单击菜单外部以关闭菜单时,Bootstrap 导航栏“闪烁”

    我遇到了导航栏问题 当您单击菜单外部以关闭菜单时 导航栏会出现 闪烁 如果在单击菜单外时按住鼠标 则闪烁会持续存在 如下所示 我认为这可能与角度有关 而不是与CSS有关 主要是因为其他人未能在小提琴上复制它 上一个问题在这里 https s
  • Angularjs 在生产中禁用调试数据

    我正在尝试按照角度文档中的建议禁用生产服务器中的调试数据here https docs angularjs org guide production 补充一点 我并没有真正看到性能和加载时间有任何改进 这是我的代码在 app js 中的样子
  • 如何检查摘要周期是否稳定(又名“Angular 完成编译了吗?”)

    tl dr 最初的问题是 如何在每个摘要周期触发回调 但潜在的问题更有趣 因为这回答了两个问题 所以我继续修改了标题 Context 在解决了所有依赖项 nginclude API 调用等之后 我试图控制 Angular 何时完成 HTML
  • firebase批量更新和onWrite触发同步

    我在同步两个 Firebase 云函数时遇到问题 第一个函数对多个文档执行批量更新 第二个函数由onWrite触发这些文档之一 为了便于说明 假设我有两个文档A and B 在两个单独的集合中 第一个云功能更新两个文档A and B有消防库
  • Angular2 RxJS从地图函数调用类函数

    我是 Angular 2 和 Observables 的新手 所以如果我的问题微不足道 我深表歉意 无论如何 我正在尝试使用 RxJS 测试 Angular 2 HTTP 客户端 虽然我让它工作了 但我需要向我当前正在处理的服务添加更多逻辑
  • 将 Angular Material 与 Twitter Bootstrap 相结合,不会发生冲突

    我想将 Twitter Bootstrap 与 Angular 材料结合起来 我发现引导材料设计https github com FezVrasta bootstrap material design https github com Fe
  • 是否可以使用 Firebase 动态链接指定短链接?

    这个问题是关于我认为 REST API 无论如何来自文档 和通过 Firebase 控制台提供的行为之间的差异 这是控制台 然而当我看到REST API 文档 https firebase google com docs reference
  • Firebase 查询 Or'ing whereEqualTo 以获得可能值的列表

    我见过之前针对早期版本的 Firebase 提出过这个问题 https stackoverflow com questions 26700924 query based on multiple where clauses in fireba
  • 独立的开发和生产 Firebase 环境

    我正在考虑使用 Firebase 作为 MBaaS 但是我找不到任何可靠的解决方案来解决以下问题 我想设置两个单独的 Firebase 环境 一个用于开发 一个用于生产 但我不想在开发和生产之间手动复制功能 例如远程配置设置 通知规则等 环
  • 如何验证最终用户经过身份验证的令牌(使用 Firebase 身份验证)来调用 google cloud run 端点?

    请帮助使用 firebase 身份验证在云运行中进行最终用户身份验证 简短的介绍 我从 firebase 函数提交 Authorization Bearer idToken 标头 并使用电子邮件 密码 firebase 用户进行身份验证的
  • 编辑模板身份验证 Firebase

    您好 我使用 Firebase 启动了一个新应用程序 然后执行身份验证方法 但我需要编辑电子邮件地址验证和更改电子邮件地址的模板 这两个选项无法编辑 但重置密码后可以编辑模板 字段 消息 该消息仅在选项 电子邮件地址验证和更改电子邮件地址
  • Angular - 将焦点放在动态创建的输入字段上

    我如何将焦点添加到新创建的字段 参见到目前为止的示例 http jsfiddle net aERwc 165 http jsfiddle net aERwc 165 scope addField function console log h
  • Angular2、ZoneJS 和外部更改的 DOM

    我需要一些有关 Angular2 RC1 Web 应用程序中外部更改的 DOM 的帮助 场景很简单 我确实有一个带有相应模板的组件 其中包含一个具有如下 ID 的空 div div div typescripted 组件有一个 ngOnIn
  • Ionic 3 更新后 WebpackJsonp 丢失

    我最近从 2 升级到 ionic 3 我可以为 iOS 构建应用程序 但 ionic 服务现在失败并出现以下错误 在为 iOS 构建之前 我必须手动将 main prod ts 和 main dev ts 替换为 main ts 您需要更多
  • angularjs - 当 $interval 触发时 ng-show 不会更新类

    尝试使用 Angular 中的 interval 来使用 ng show 更改列表中当前可见的项目 检查 html 我注意到角度将 ng show 从 true false 更改 但它并没有删除 ng hide 类 html 很简单 h1
  • AngularJS 服务在控制器之间传递数据

    当使用 AngularJS 服务尝试在两个控制器之间传递数据时 我的第二个控制器在尝试从服务访问数据时总是收到未定义的消息 我猜这是因为第一个服务执行了 window location href 并且我认为这是清除服务中的数据 有没有办法将
  • Firebase HTTP 云函数 HTTP 错误代码 403

    自 2020 年 3 月 28 日起 我的所有 HTTP 云函数都出现错误 在我上次更新之前 它们运行良好 我只更改了一些内容 在上次部署后我收到了此错误 h1 Error Forbidden h1 h2 Your client does

随机推荐