2015-08-30 49 views
0

我已经构建了我的第一个Node.js应用程序,该应用程序应该安装在Shopify商店中。如果你想看看我的实际代码是什么样的(app.js),你可以查看它here。这是非常基本的,所以阅读并不难。Node.js - 如何使用访问/授权令牌?

我知道如何验证安装应用程序的(继Shopify说明),但我没怎么使用永久access token,一个成功的安装为我提供了所有后续请求验证。

通过后续请求,我指的是呈现应用或请求安装应用的请求,即使应用已安装。

现在,我存储店铺的名字(这是唯一的)与永久性令牌Shopify将在我数据库一起。但我不知道这是否是必要的。如果我没有错,只需使用浏览器的会话就可以做到吗?但我该怎么做?我怎么在每次请求到来时使用这个令牌来检查它是否有效?

谢谢任何​​帮助/建议!

下面的代码是那种什么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*); 
    }); 
}); 

回答

1

获取access token从Shopify是一次时间过程。

access tokenshop's name保存在您的数据库中,并且还基于某种算法生成并保存'auth token'。将生成的授权令牌返回给客户端。确保客户端在每个请求中都发送此身份验证令牌。

现在,当客户端击中你的服务器验证身份验证令牌;一旦通过验证,就可以使用适当的“访问令牌”和商店名称致电Shopify API。

认证流程可能如下:

  • 从Shopify获得访问令牌
  • 生成令牌(我指此为验证令牌)的Shopify店,是指this
  • 现在保存shopify的访问令牌,将商店名称和生成的令牌Shoporder到数据库
  • 现在将您生成的令牌发送给客户端(保存在cookie或本地存储中)

验证流程:

  • 客户击中了你的服务器的身份验证令牌
  • 验证在你的数据库这个身份验证令牌,并获得访问令牌和店铺名称获取数据为身份验证令牌
  • 现在做使用此访问令牌和店铺名称

希望这种方法有助于

+0

感谢您的回复奇拉格调用Shopify API。尽管如此,我并不真正理解'access token'和'auth token'之间的区别。我在Shopify上得到的所有东西都是一个“临时标记”,然后我换成了“永久标记”,这是我在DB中保存的一个商标名称。所以我不知道如何获得你所指的'auth token'。我也不知道如何将'token'返回给客户端,就像你所建议的一样,我不知道如何在客户端发出的每个请求中都存在'check token'。我在OP中包含了一个到我的实际代码的链接,以防有所帮助。 –

+0

如果在客户端和服务器之间移动'永久令牌'不是好的做法,那我该怎么做呢?我如何生成你正在谈论的'auth token',我该怎么处理呢? –

+1

编辑我的答案详细的工作流程 –