Node.js - 如何使用访问/授权令牌?

Node.js - How to use access / auth tokens?

我已经构建了我的第一个 Node.js 应用程序,它应该安装在 Shopify 商店中。如果你想看看我的实际代码是什么样的 (app.js),你可以查看它 here。这真的很基础,所以通读起来并不难。

我知道如何验证应用程序的安装(遵循 Shopify 说明)但我不知道如何验证所有后续请求 使用成功安装后提供给我的永久 access token

对于后续请求,我指的是呈现应用程序或安装应用程序的请求,即使应用程序已经安装。

现在,我存储商店的名称(唯一的)以及 Shopify 在我的数据库[=35]中发送给我的永久令牌=].但是我不知道是否有必要。如果我没记错的话,只需使用浏览器的会话就可以了?但是我该怎么做呢?以及每次请求通过时如何使用此令牌来检查它是否有效?

感谢您的任何 help/suggestions!

下面的代码是对 my actual code 的一种表示,目的是让您了解我的问题所在:

db.once('open', function(callback)
{  
   app.get('/', function (req, res)
   {
      var name = getNameFrom(req);

      if (existsInDB(name) && tokenExistsInDBfor(name))
      {
         res.redirect('/render');

         /*
            Is checking that the shop (along with a permanent token)
            exists in my DB enough ?
            Shouldn't I check whether the current request comes with 
            a token that is equal to the one in my DB ?
            What if the token received with this request is different       
            from the one stored in my DB ?
         */

      }
      else res.redirect('/auth');
   });

   app.get('/auth', function (req, res)
   {    
      if (authenticated(req))
      {
          var token = getPermanentToken(); 
          storeItInDB(nameFrom(req), token);
          res.redirect('/render');

          /*
            aren't I supposed to do anything more 
            with the token I've received ? send it
            back/store it in the browser session as well maybe?
            is storing it in the db necessary ?
          */
      }
   }); 

   app.get('/render', function (req, res)
   {   
      /*
      How do I check that this request is coming 
      from an authorised shop that has the necessary token ?
      Simply checking my DB will not do 
      because there might be some inconsistency correct ?
      */

      res.sendFile(*file that will build app on the client*);
   });
});

从 Shopify 获得 access token 是一次过程。

在你的数据库中保存access tokenshop's name,同时根据某种算法生成并保存'auth token'。 Return 为客户端生成了授权令牌。确保客户端在每个请求中发送此身份验证令牌。

现在当客户端访问您的服务器时验证身份验证令牌;验证后使用适当的 'access token' 和商店名称调用 Shopify API。

认证流程如下:

  • 从 Shopify 获取访问令牌
  • 为 Shopify 商店生成令牌(我将其称为身份验证令牌),参考 this
  • 现在将 shopify 的访问令牌、shopify 商店名称和您生成的令牌保存到数据库中
  • 现在将您生成的令牌发送给客户端(将其保存在 cookie 或本地存储中)

验证流程:

  • 客户端访问您的服务器以使用您的授权令牌获取数据
  • 在您的数据库中验证此授权令牌,并获取该授权令牌的访问令牌和商店名称
  • 现在使用此访问令牌和商店名称调用 Shopify API

希望这个方法有帮助