/
sigrequest.js
105 lines (88 loc) · 3.01 KB
/
sigrequest.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
'use strict';
/**
* Snagged (and slightly modified) from official Avocado dev signature code
* https://github.com/AvocadoCorp/avocado-api-samples/blob/master/developer%20signatures/AvocadoSignTest.js
*/
/*global require:false, console:false, module:false */
var crypto = require('crypto');
var events = require('events');
var http = require('http');
var https = require('https');
var querystring = require('querystring');
var tty = require('tty');
var util = require('util');
var API_HOST = "avocado.io";
var API_PORT = 443;
var API_URL_BASE = "/api/";
var API_URL_LOGIN = API_URL_BASE + "authentication/login";
var API_URL_COUPLE = API_URL_BASE + "couple";
var AVOCADO_COOKIE_NAME = "user_email";
var AVOCADO_USER_AGENT = "Avocado Test Api Client v.1.0";
var ERROR_MSG = "FAILED. Signature was tested and failed. Try again and check the auth information.";
/**
* @constructor
* @extends {events.EventEmitter}
*/
function SignatureRequest(config) {
events.EventEmitter.apply(this);
this.config = config;
}
util.inherits(SignatureRequest, events.EventEmitter);
SignatureRequest.options = {
host: API_HOST,
port: API_PORT,
path: API_URL_LOGIN,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': AVOCADO_USER_AGENT
},
method: 'POST'
};
SignatureRequest.prototype.send = function() {
this.request = https.request(SignatureRequest.options,
this.onRequestSuccess.bind(this));
this.request.write(querystring.stringify({
'email' : this.config.email,
'password': this.config.password
}));
this.request.end();
this.request.on('error', this.onRequestError.bind(this));
};
SignatureRequest.prototype.onRequestSuccess = function(response) {
this.response = response;
this.response.on('data', this.parseResponse.bind(this));
};
SignatureRequest.prototype.onRequestError = function(e) {
this.emit(ApiRequestEvent.ERROR, e);
};
SignatureRequest.prototype.getUserCookieValue = function() {
var respCookieString = this.response.headers['set-cookie'][0];
var cookies = {};
respCookieString.split(';').forEach(function(cookie) {
var name = cookie.substring(0, cookie.indexOf('='));
var value = cookie.substring(name.length + 1, cookie.length);
cookies[name.trim()] = (value || '').trim();
});
this.config.cookies = cookies;
return cookies[AVOCADO_COOKIE_NAME];
};
SignatureRequest.prototype.getHashedUserToken = function() {
var hasher = crypto.createHash('sha256');
hasher.update(this.config.cookieValue + this.config.devKey);
return hasher.digest('hex');
};
SignatureRequest.prototype.parseResponse = function(data) {
if (data) data = data.toString();
if (this.response.statusCode != 200) {
this.emit(ApiRequestEvent.ERROR);
return;
}
this.config.cookieValue = this.getUserCookieValue();
this.config.signature = this.config.devId + ":" + this.getHashedUserToken();
this.emit(ApiRequestEvent.SUCCESS, data);
};
var ApiRequestEvent = {
SUCCESS: 'io.avocado.request.success',
ERROR: 'io.avocado.request.error'
};
module.exports = SignatureRequest;