我想知道是否有可能通过用户的id获取access_token。我正在使用nodejs服务器将通知推送到客户端,我需要验证连接用户 - 如果我能使用oauth2发送给用户的相同'Bearer ...'访问令牌,那就很好奇。
答案 0 :(得分:0)
作为一种解决方案,我正在从控制器向节点服务器提供令牌,并且在客户端连接到节点服务器之后,请求客户端发送承载令牌 - 并使用jsonwebtoken包从中提取jti,用于与收到的令牌进行比较来自控制器。
以下是代码:
<强>控制器强>
$userTokenReceiver = $pm->receiver->tokens()->where('revoked', 'false')
->select('id')->get()->sortBy('created_at', true)->first();
if ($userTokenReceiver && $userTokenReceiver->toArray()['id'])
$dataRedis->receiver_token = $userTokenReceiver->toArray()['id'];
节点服务器
var jwt = require('jsonwebtoken');
...
io.on('connection', function(socket) {
socket.waitingAuthorize = true;
// E> authentication_request: after connection asking client to provide token
socket.emit('authentication_request');
// O> authorize: waiting for client's response to auth request
socket.on('authorize', function(token) {
var decoded = jwt.decode(token);
if ( !decoded || !decoded.jti ) {
socket.disconnect(true);
return;
}
socket.waitingAuthorize = false;
socket.token = decoded.jti; // this is compared to token received from controller
});
...