Skip to content

Commit 4cb3aa4

Browse files
committed
long road to auth
1 parent 23c8996 commit 4cb3aa4

File tree

3 files changed

+88
-6
lines changed

3 files changed

+88
-6
lines changed

lib/connection.js

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ var _ = require('underscore');
55
var EventEmitter = require('events');
66
var msgpack = require('msgpack');
77
var vow = require('vow');
8+
var crypto = require('crypto');
9+
var shasum = crypto.createHash('sha1');
10+
var xor = require('bitwise-xor');
11+
12+
var shatransform = function(t){
13+
var sha1 = crypto.createHash('sha1');
14+
sha1.update(t);
15+
return sha1.digest();
16+
};
817

918
var states = {
1019
CONNECTING: 0,
@@ -70,6 +79,8 @@ TarantoolConnection.prototype.onData = function(data){
7079
i--;
7180
}
7281
}
82+
console.log('connect data', data, data.toString(), data.slice(64, 108).toString(), data.length);
83+
this.salt = data.slice(64, 108).toString('utf8');
7384
this.state = states.CONNECTED;
7485
break;
7586
case states.CONNECTED:
@@ -148,13 +159,13 @@ TarantoolConnection.prototype._processResponse = function(buffer){
148159
})[0];
149160
var dfd = task[2];
150161
if (success)
151-
dfd.resolve(this._processResponseBody(task[0], obj[1]));
162+
dfd.resolve(this._processResponseBody(task[0], obj[1][tarantoolConstants.KeysCode.data]));
152163
else
153-
dfd.reject(obj[1]);
164+
dfd.reject(obj[1][tarantoolConstants.KeysCode.e]);
154165
} catch(e){
155166
console.log(e, e.stack);
156167
}
157-
}
168+
};
158169

159170
TarantoolConnection.prototype._processResponseBody = function(cmd, data){
160171
return data;
@@ -219,10 +230,79 @@ TarantoolConnection.prototype.select = function(spaceId, indexId, limit, offset,
219230
console.log(body, msgpack.unpack(body));
220231
console.log(buffered);
221232
this._request(header, body);
222-
this.commandsQueue.push([tarantoolConstants.RequestCode.rqPing, reqId, dfd]);
233+
this.commandsQueue.push([tarantoolConstants.RequestCode.rqSelect, reqId, dfd]);
234+
return dfd.promise();
235+
};
236+
237+
238+
TarantoolConnection.prototype.insert = function(spaceId, tuple){
239+
var dfd = vow.defer();
240+
if (Array.isArray(tuple)){
241+
console.log('start ping');
242+
var reqId = requestId.getId();
243+
var header = this._header(tarantoolConstants.RequestCode.rqInsert, reqId);
244+
console.log('header', header);
245+
var buffered = {
246+
spaceId: msgpack.pack(spaceId),
247+
tuple: msgpack.pack(tuple)
248+
};
249+
var body = Buffer.concat([new Buffer([0x82,tarantoolConstants.KeysCode.space_id]), buffered.spaceId,
250+
new Buffer([tarantoolConstants.KeysCode.tuple]), buffered.tuple]);
251+
console.log(body, msgpack.unpack(body));
252+
console.log(buffered);
253+
this._request(header, body);
254+
this.commandsQueue.push([tarantoolConstants.RequestCode.rqInsert, reqId, dfd]);
255+
}
256+
else
257+
dfd.reject(new Error('need array'));
223258
return dfd.promise();
224259
};
225260

261+
TarantoolConnection.prototype.auth = function(username, password){
262+
try{
263+
var dfd = vow.defer();
264+
console.log('auth');
265+
var reqId = requestId.getId();
266+
var header = this._header(tarantoolConstants.RequestCode.rqAuth, reqId);
267+
var buffered = {
268+
username: msgpack.pack(username)
269+
};
270+
271+
var body = Buffer.concat([new Buffer([0x82, tarantoolConstants.KeysCode.username]), buffered.username,
272+
new Buffer([0x21, 0x92]), tarantoolConstants.passEnter,
273+
msgpack.pack(scramble(password, this.salt).toString('ascii'))]);
274+
console.log(body, msgpack.unpack(body));
275+
console.log(buffered, msgpack.pack(scramble(password, this.salt)));
276+
this._request(header, body);
277+
this.commandsQueue.push([tarantoolConstants.RequestCode.rqAuth, reqId, dfd]);
278+
return dfd.promise();
279+
} catch(e){
280+
console.log(e);
281+
}
282+
283+
};
284+
285+
function scramble(password, salt){
286+
var encSalt = new Buffer(salt, 'base64').toString('ascii');
287+
console.log('encSalt', encSalt);
288+
var step1 = shatransform(password);
289+
console.log('step1',step1, step1.length);
290+
var step2 = shatransform(step1);
291+
var step3 = shatransform(salt, step2);
292+
console.log(password, step1, step2, step3, salt);
293+
console.log('salt', salt);
294+
var scramble = xor(step1, step3).toString('ascii');
295+
console.log('scramble', step1.length, step2.length, scramble, scramble.length);
296+
return scramble;
297+
}
298+
299+
function myXor(l, r){
300+
var x = [];
301+
for (var i=0; i<l.length; i++)
302+
x.push(l[i] ^ r[i]);
303+
return new Buffer(x);
304+
}
305+
226306

227307
TarantoolConnection.prototype._header = function(command, reqId){
228308
try {
@@ -248,7 +328,6 @@ TarantoolConnection.prototype._request = function(header, body){
248328
} catch (e){
249329
console.log(e, e.stack);
250330
}
251-
252331
};
253332

254333
TarantoolConnection.prototype.destroy = function(interupt){

lib/const.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var msgpack = require('msgpack');
12
// i steal it from go
23
const RequestCode = {
34
rqSelect: 0x01,
@@ -56,7 +57,8 @@ const ExportPackage = {
5657
OkCode: OkCode,
5758
NetErrCode: NetErrCode,
5859
TimeoutErrCode: TimeoutErrCode,
59-
PacketLengthBytes: 5
60+
PacketLengthBytes: 5,
61+
passEnter: msgpack.pack('chap-sha1')
6062
};
6163

6264
module.exports = ExportPackage;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
},
2323
"homepage": "https://github.com/KlonD90/node-tarantool-driver",
2424
"dependencies": {
25+
"bitwise-xor": "0.0.0",
2526
"msgpack5": "^2.0.0",
2627
"underscore": "^1.8.3",
2728
"vow": "^0.4.9"

0 commit comments

Comments
 (0)