This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.js
198 lines (177 loc) · 5.14 KB
/
client.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
if (typeof GombotCrypto === 'undefined') {
var GombotCrypto = require('./crypto.js');
}
if (typeof URLParse === 'undefined') {
var URLParse = require('./urlparse.js');
}
(function() {
GombotClient = function(path, options) {
if (!options) options = {};
var url = URLParse(path);
this.scheme = url.scheme;
this.host = url.host;
this.port = url.port;
this.path = url.path || '';
this.user = options.user;
this.keys = options.keys;
};
var xhr = typeof jQuery !== 'undefined' ? jQuery.ajax : require('xhrequest');
function request(args, cb) {
var url = args.scheme ? args.scheme : 'http';
var method = args.method.toUpperCase();
url += '://' + args.host;
if (args.port) url += ':' + args.port;
url += args.path;
var req = {
url: url,
method: method,
type: method,
data: args.data,
//dataType: 'json',
//accepts: {json: 'application/json'},
headers: args.headers || {},
success: function(data, res, status) {
if (typeof data === 'string') {
try {
data = JSON.parse(data);
} catch (e) {
return cb && cb('Invalid JSON response: ' + e);
}
}
data.session_context = {};
if (cb) cb(null, data);
},
processData: false,
error: function(data, res, status) {
if (cb) cb('Error: ' + data + '\nStatus: ' + status);
},
complete: function () {
console.log('request complete');
}
};
if (method == 'PUT' || method == 'POST') {
req.contentType = req.headers['Content-Type'] = 'application/json';
}
xhr(url, req);
}
function authRequest(args, cb) {
var url = args.scheme ? args.scheme : 'http';
url += '://' + args.host;
if (args.port) url += ':' + args.port;
url += args.path;
// compute the authKey
GombotCrypto.sign({
email: args.email,
keys: args.keys,
url: url,
host: args.host,
port: args.port,
method: args.method,
nonce: args.nonce || 'unused',
date: args.date || new Date()
}, function(err, r) {
if (err) return cb(err, r);
args.headers = r;
// send request with authKey as the password
request(args, cb);
});
}
function mergeArgs(args, def) {
args.scheme = def.scheme;
args.host = args.host || def.host;
args.port = args.port || def.port;
args.keys = args.keys || def.keys;
args.email = args.email || def.user;
return args;
}
GombotClient.prototype = {
// get "session context" from the server
context: function(args, cb) {
if (typeof args === 'function') {
cb = args;
args = {};
}
args = mergeArgs(args, this);
args.method = 'get';
args.path = this.path + '/v1/context';
request(args, cb);
},
// create an account
account: function(args, cb) {
var self = this;
args = mergeArgs(args, this);
args.method = 'post';
args.path = this.path + '/v1/account';
// compute the authKey
var headers = GombotCrypto.derive({
email: args.email,
password: args.pass
}, function(err, r) {
self.keys = r;
self.user = args.email;
args.data = JSON.stringify({email: args.email, pass: GombotCrypto.hexToBase64(r.authKey), newsletter: args.newsletter});
// send request with authKey as the password
request(args, cb);
});
},
// check auth status
status: function(args, cb) {
args = mergeArgs(args, this);
args.method = 'get';
args.path = this.path + '/v1/status';
authRequest(args, cb);
},
// derives auth key using credentials then checks
// auth status using derived keys
signIn: function(args, cb) {
var self = this;
// compute the authKey
GombotCrypto.derive({
email: args.email,
password: args.pass
}, function(err, r) {
if (err) return cb(err);
var keys = args.keys = r;
self.status(args, function (err, r) {
if (!err && r.success) {
self.keys = keys;
self.user = args.email;
}
if (cb) cb(err, r);
});
});
},
storePayload: function(args, cb) {
args = mergeArgs(args, this);
args.method = 'put';
args.path = this.path + '/v1/payload';
GombotCrypto.encrypt(this.keys, JSON.stringify(args.payload), function (err, cipherText) {
if (err) return cb(err);
args.data = JSON.stringify({payload: cipherText});
authRequest(args, cb);
});
},
getPayload: function(args, cb) {
args = mergeArgs(args, this);
args.method = 'get';
args.path = this.path + '/v1/payload';
var keys = this.keys;
authRequest(args, function (err, data) {
if (err) return cb(err);
GombotCrypto.decrypt(keys, data.payload, function (err, plaintext) {
if (err) return cb(err);
cb(null, {success: data.success, payload: JSON.parse(plaintext), updated: data.updated});
});
});
},
getTimestamp: function(args, cb) {
args = mergeArgs(args, this);
args.method = 'get';
args.path = this.path + '/v1/payload/timestamp';
authRequest(args, cb);
}
};
})();
if (typeof module != 'undefined' && module.exports) {
module.exports = GombotClient;
}