1
我想知道是否有可能通过用户的ID获得access_token。我使用nodejs服务器将通知推送到客户端,并且我需要验证连接的用户 - 好奇我是否可以通过使用由oauth2发送给用户的相同“承载...”访问令牌来完成此操作。Laravel Passport - 通过用户ID在控制器中获取访问令牌?
我想知道是否有可能通过用户的ID获得access_token。我使用nodejs服务器将通知推送到客户端,并且我需要验证连接的用户 - 好奇我是否可以通过使用由oauth2发送给用户的相同“承载...”访问令牌来完成此操作。Laravel Passport - 通过用户ID在控制器中获取访问令牌?
作为一种解决方案,我从控制器向节点服务器提供令牌,并且在客户端连接到节点服务器之后,客户端被请求发送持有者令牌 - 并使用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
});
...