Created
October 10, 2014 08:18
-
-
Save aliukevicius/c9c972a0b218cc7811f0 to your computer and use it in GitHub Desktop.
NodeJs get Larvel sessionId from session cookie
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// tested with Laravel 4.2.* | |
var PHPUnserialize = require('php-unserialize'); // npm install php-unserialize | |
var MCrypt = require('mcrypt').MCrypt; // npm install mcrypt | |
var encryptionKey = '---Laravel encription string---' | |
function ord( string ) { // Return ASCII value of character | |
return string.charCodeAt(0); | |
} | |
function getSessionIdFromLaravelCookie(encodedCookie, encryptionKey) { | |
var cookie = JSON.parse(new Buffer(encodedCookie, 'base64')); | |
var iv = new Buffer(cookie.iv, 'base64'); | |
var value = new Buffer(cookie.value, 'base64'); | |
var rijCbc = new MCrypt('rijndael-128', 'cbc'); | |
rijCbc.open(encryptionKey, iv); | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment