与Node.js Express共享Laravel 4会话 [英] Sharing laravel 4 session with nodejs express
本文介绍了与Node.js Express共享Laravel 4会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从nodejs头上的cookie中获取laravel会话ID.
I am trying to get the laravel session id from the cookie on the header on nodejs.
到目前为止,我已经尝试过:
I have tried so far:
function nodeDecrypt(data, key, iv) {
var decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
var chunks = []
chunks.push(decipher.update(chunk.toString(),'hex','binary'))
chunks.push(decipher.final('binary'))
return chunks.join('')
}
var cookie = JSON.parse(new Buffer(req.cookies.gjsess, 'base64'));
var iv = new Buffer(cookie.iv, 'base64');
var value = new Buffer(cookie.value, 'base64');
var dec = nodeDecrypt(value, 'YourSecretKey!!!', iv);
但是到目前为止,我一直得到Invalid IV length 32
.
But so far I keep getting Invalid IV length 32
.
YourSecretKey!!!
是laravel 4 app.php
上的密钥.
YourSecretKey!!!
is the key found on the app.php
of laravel 4.
Laravel加密机制:
Laravel encryption mech:
protected $cipher = 'rijndael-256';
protected $mode = 'cbc';
protected $block = 32;
...
$payload = $this->getJsonPayload($payload);
$value = base64_decode($payload['value']);
$iv = base64_decode($payload['iv']);
return unserialize($this->stripPadding($this->mcryptDecrypt($value, $iv)));
...
return mcrypt_decrypt($this->cipher, $this->key, $value, $this->mode, $iv);
...
$this->app->bindShared('encrypter', function($app)
{
return new Encrypter($app['config']['app.key']);
});
其他尝试
var cookie = JSON.parse(new Buffer(req.cookies.gjsess, 'base64'));
var iv = new Buffer(cookie.iv, 'base64');
var value = new Buffer(cookie.value, 'base64');
var MCrypt = require('mcrypt').MCrypt;
var desEcb = new MCrypt('rijndael-256', 'cbc');
desEcb.open('YourSecretKey!!!');
var plaintext = desEcb.decrypt(value, 'base64');
这不会产生错误,但仍会获取无用的数据.
This does not give an error but still getting useless data.
推荐答案
最后我也明白了!这是我的解决方案.对我来说很完美.
Finally I got it too! Here's my solution. Works perfectly for me.
// requirements
var PHPUnserialize = require('php-unserialize'); // npm install php-unserialize
var MCrypt = require('mcrypt').MCrypt; // npm install mcrypt
// helper function
function ord( string ) { // Return ASCII value of character
//
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
return string.charCodeAt(0);
}
function getSessionIdFromLaravelCookie() {
var cookie = JSON.parse(new Buffer(req.cookies.laravel_session, 'base64'));
var iv = new Buffer(cookie.iv, 'base64');
var value = new Buffer(cookie.value, 'base64');
var key = "_Encryption Key_";
var rijCbc = new MCrypt('rijndael-256', 'cbc');
rijCbc.open(key, iv); // it's very important to pass iv argument!
var decrypted = rijCbc.decrypt(value).toString();
var len = decrypted.length - 1;
var pad = ord(decrypted.charAt(len));
var sessionId = PHPUnserialize.unserialize(decrypted.substr(0, decrypted.length - pad));
return sessionId;
}
这篇关于与Node.js Express共享Laravel 4会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文