2016-07-05 28 views
0

經歷了OAuth2的多個步驟後,一旦接收到access_token後應該如何處理?如何處理oauth2 access_token

app.get('/oauth2', function(req, res) { 
    var code = req.query.code; 

    var url = "https://.../oauth/access_token"; 
    var options = { 
    url: url, 
    method: "POST", 
    form: { 
     client_id: '...', 
     client_secret: '...', 
     grant_type: 'authorization_code', 
     redirect_uri: 'http://localhost:8080/oauth2', 
     code: code, 
    }, 
    json: true 
    } 

    request(options, function(err, response, body) { 

    // I need to save the user in database if she doesn't exist 
    // Then redirect, but should I pass the access_token to the redirect? 
    res.redirect('/'); // or res.redirect('/?access_token=zzz') 
    } 

    // Also, should the access_token be encrypted 
    // Does it need to be saved in database? 
    // Does it go in local storage? 

}); 

我想要一些我在響應中收到的信息,所以它需要存儲在數據庫中。但是,我具體做什麼access_token?它會被保存到數據庫嗎?它應該被加密嗎?當我重定向時,是否將它作爲查詢字符串添加?我是否將其存儲在本地存儲中?如果是這樣,怎麼樣?

回答

0

首先,您的代碼包含json: true,但RFC 6749 4.1.3. Access Token Request指出應使用application/x-www-form-urlencoded格式發送參數。

其次,來自令牌端點的響應格式爲JSON。詳情請參閱4.1.4. Access Token Response

第三,獲取訪問令牌後,應將其保存以備後用。保存訪問令牌的地方取決於您。數據庫,內存中以及任何你喜歡的地方。如果您希望在保存訪問令牌時對其進行加密,請根據需要進行操作。

最後,訪問令牌用於調用資源服務器的Web API。在正常情況下,Web API的實現以RFC 6750中定義的方式接受訪問令牌。在說明書中,定義了以下三種方式。

  1. In Authorization header
  2. As a Form parameter
  3. As a Query parameter
相關問題