Passport-OAuth 2.0

OAuth 2.0

OAuth 2.0 (由 RFC 6749 正式指定) 提供的授权框架,它允许用户授权访问第三方应用程序。授权时,该应用程序发出的令牌作为身份验证凭据来使用。这有两个主要的安全优势:

  1. 应用程序并不需要存储用户的用户名和密码。 
  2. 令牌可以有一个受限制的范围(例如:只读访问)。 

这些好处是确保Web应用程序的安全性,使的OAuth2.0的API认证的主要标准尤为重要。 

当使用OAuth2.0保护的API端点,也有必须执行三个不同的步骤: 

  1. 应用程序向用户请求访问受保护资源的权限。 
  2. 则将令牌颁发给应用程序。
  3. 应用程序通过令牌获取受保护的资源。
Issuing Tokens

OAuth2orize, a sibling project to Passport, provides a toolkit for implementing OAuth 2.0 authorization servers.

授权是一个复杂的过程,涉及请求的应用程序和用户,以及提示权限用户,从而确保足够的细节使做出明智的决定。

此外,它是由实现者确定哪些限制可以放在有关访问范围的应用,以及随后实施这些限制。 

作为一个工具包,OAuth2orize并不试图做出执行决定。本指南不涉及这些问题,但强烈建议服务部署的OAuth2.0有涉及到安全方面的考虑一个完整的认识。

Authenticating Tokens

OAuth 2.0 provides a framework, in which an arbitrarily extensible set of token types can be issued. In practice, only specific token types have gained widespread use.

Bearer Tokens

Bearer tokens are the most widely issued type of token in OAuth 2.0. So much so, in fact, that many implementations assume that bearer tokens are the only type of token issued.

Bearer tokens can be authenticated using the passport-http-bearer module.

Install
$ npm install passport-http-bearer
Configuration
passport.use(new BearerStrategy(
  function(token, done) {
    User.findOne({ token: token }, function (err, user) {
      if (err) { return done(err); }
      if (!user) { return done(null, false); }
      return done(null, user, { scope: 'read' });
    });
  }
));

The verify callback for bearer tokens accepts the token as an argument. When invoking done, optionalinfo can be passed, which will be set by Passport at req.authInfo. This is typically used to convey the scope of the token, and can be used when making access control checks.

Protect Endpoints
app.get('/api/me', 
  passport.authenticate('bearer', { session: false }),
  function(req, res) {
    res.json(req.user);
  });

Specify passport.authenticate() with the bearer strategy to protect API endpoints. Sessions are not typically needed by APIs, so they can be disabled.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值