【问题标题】:What to do with oauth2 access_token如何处理 oauth2 access_token
【发布时间】:2016-07-05 17:27:11
【问题描述】:

在经历了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 做什么呢?它是否保存到数据库中?应该加密吗?当我重定向时,是否将其添加为查询字符串?我是否将其存储在本地存储中?如果有,怎么做?

【问题讨论】:

    标签: node.js express oauth2


    【解决方案1】:

    首先,您的代码包含 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

    【讨论】:

      猜你喜欢
      • 2012-01-23
      • 2021-04-10
      • 2019-11-19
      • 1970-01-01
      • 2015-08-10
      • 2012-01-03
      • 2019-10-15
      • 2020-04-10
      相关资源
      最近更新 更多