-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
91 lines (76 loc) · 3.45 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
'use strict';
const unserialize = require('php-unserialize').unserialize;
const unserialize2 = require('php-serialization').unserialize;
const crypto = require('crypto');
const fs = require('fs');
module.exports = {
getAppKey: function (filepath) {
return new Promise(function (resolve, reject) {
fs.readFile(filepath, 'utf8', function (err, data) {
if (err != null) return reject(err);
let key = data.match(/APP_KEY.*/g);
if (key.length == 0) return reject('APP_KEY not found');
key = key[0];
key = key.split('=')[1].trim();
return resolve(key.replace('base64:', ''));
});
});
},
getSessionKey: function (laravelSession, laravelKey, keyLength) {
keyLength = keyLength || 32;
let cypher = 'aes-' + keyLength * 8 + '-cbc';
//Get session object
laravelSession = new Buffer(laravelSession, 'base64');
laravelSession = laravelSession.toString();
laravelSession = JSON.parse(laravelSession);
//Create key buffer
laravelKey = new Buffer(laravelKey, 'base64');
//crypto required iv in binary or buffer
laravelSession.iv = new Buffer(laravelSession.iv, 'base64');
//create decoder
let decoder = crypto.createDecipheriv(cypher, laravelKey, laravelSession.iv);
//add data to decoder and return decoded
let decoded = decoder.update(laravelSession.value, 'base64');
//unserialize
return unserialize(decoded);
},
getSessionFromFile: function (laravelSessionKey, filePath) {
return new Promise(function (resolve, reject) {
fs.readFile(filePath + '/' + laravelSessionKey, 'utf8', function (err, data) {
if (err != null) return reject(err);
return resolve(unserialize2(data));
});
});
},
getSessionFromRedis: function (laravelSessionKey, redisConnection, sessionPrefix='laravel_cache') {
return new Promise(function (resolve, reject) {
redisConnection.get(sessionPrefix + ':' + laravelSessionKey, function (err, value) {
if (err != null) return reject(err);
return resolve(unserialize2(unserialize2(value)));
});
});
},
getSessionFromMysql: function (laravelSessionKey, mySqlConnection, databaseTable) {
return new Promise(function (resolve, reject) {
databaseTable = databaseTable || 'sessions';
mySqlConnection.query('select payload from ' + databaseTable + ' where id = "' + laravelSessionKey + '"', function (err, rows, fields) {
if (err != null) return reject(err);
if (rows.length == 0) return reject('Session not found');
let session = new Buffer(rows[0].payload, 'base64').toString();
return resolve(unserialize(session));
});
});
},
getUserIdFromSession: function (session) {
var cookieKey = 'login_82e5d2c56bdd0811318f0cf078b78bfc';
if (session.hasOwnProperty(cookieKey)) {
return session[cookieKey];
}
for (var key in session) {
var matches = key.match(/login_(.*_)?([a-zA-Z0-9]+)/gi);
if (matches && matches.length > 0) {
return session[matches[0]];
}
}
}
};