我在理解如何使用 oAuth2rize 和 Passport.js 实现资源所有者密码流程时遇到一些问题,特别是在 client_id 和 client_secret 的传输方面,以便我可以对客户端进行一些检查,以确保任何内容进入此端点(/使用特定“密码”授权类型的令牌)是专门的官方应用程序,没有其他基于 ID 和秘密的应用程序。
在构建解决方案时,我可以获得一个令牌,但那是在我尝试对客户端进行任何验证之前。当我尝试访问传递到密码交换策略的客户端变量(发布到端点)时,我收到基于文档的用户凭据(用户名、密码),但不是我在这里需要实现的。
我不知道如何获取实际的客户端凭据,我可以在密码函数源代码中看到,您可以提供其他选项来覆盖对 req['user'] 的默认分配,但这是否意味着我必须提供一些添加到 req 对象的代码类型?
我已经设置了一些集成测试,这是我调用端点的方式(使用 SuperTest):
request('http://localhost:43862')
.post('/oauth/token')
.type('form')
.send({ grant_type: 'password' })
.send({ client_id: 'goodClient' })
.send({ client_secret: 'asecret' })
.send({ username: '[email protected] /cdn-cgi/l/email-protection' })
.send({ password: 'goodpassword' })
.expect(200, done);
出于某种原因,我似乎完全过度思考了这一点,但出于某种原因,我完全被难住了......
正如预期的那样,这是一个理解问题,我们使用本地策略而不是 ClientPasswordStrategy,并且在发出令牌之前在密码交换中进行用户验证。
我们现在使用 ClientPasswordStrategy,并在 exchange.password 函数中调用我们的用户 api 的内部调用来验证用户凭据,如果可以,则颁发令牌。
passport.use(new ClientPasswordStrategy(
function(clientId, clientSecret, next){
Client.verify(clientId, clientSecret, function(err, verified){
if(!verified){
return next(null, false);
}
next(null, clientId);
});
}
));
passport.use(new BearerStrategy(
function(token, next) {
Token.getByToken(token, function(err, tokenObj){
if(err)
return next(err);
if(!tokenObj)
return next(null, false);
User.getByUsername(tokenObj.username, function(err, user){
return next(null, user, { scope: 'all' });
});
});
}
));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)