gpt4 book ai didi

node.js - 使用 "Resource Owner Password Flow"的 oAuth2orize 传递受信任的客户端信息

转载 作者:搜寻专家 更新时间:2023-10-31 23:13:52 27 4
gpt4 key购买 nike

我在理解如何使用 oAuth2rize 和 passport.js 实现资源所有者密码流程时遇到一些问题,特别是 client_id 和 client_secret 的传输,以便我可以对客户端进行一些检查以确保任何事情都发生在这一端使用特定“密码”授权类型的点 (/token) 是专门的官方应用程序,没有其他基于 id 和 secret 的应用程序。

构建解决方案时,我可以获得一个 token ,但那是在我尝试对客户端进行任何验证之前。当我尝试访问传递到密码交换策略的客户端变量(发布到端点)时,我收到了基于文档的用户凭据(用户名、密码),但不是我需要在此处实现的。

我不知道如何获得实际的客户端凭据,我可以在密码功能源代码中看到您可以提供其他选项来覆盖对 req['user'] 的默认分配,但这是否意味着我有提供某种代码以添加到请求对象?

我已经设置了一些集成测试,下面是我如何调用我的端点(使用 SuperTest):

                request('http://localhost:43862')
.post('/oauth/token')
.type('form')
.send({ grant_type: 'password' })
.send({ client_id: 'goodClient' })
.send({ client_secret: 'asecret' })
.send({ username: 'good@user.com' })
.send({ password: 'goodpassword' })
.expect(200, done);

出于某种原因,我似乎完全没有想到这一点,但由于某种原因,我完全被难住了......

最佳答案

正如预期的那样,这是一个理解问题,我们使用本地策略而不是 ClientPasswordStrategy,在颁发 token 之前在密码交换中进行用户验证。

我们现在使用 ClientPasswordStrategy 并在我们调用的 exchange.password 函数中和对我们的用户 api 的内部调用以验证用户凭据,如果正常,则颁发 token 。

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' });
});
});
}
));

关于node.js - 使用 "Resource Owner Password Flow"的 oAuth2orize 传递受信任的客户端信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16654078/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com