-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMasterController.js
239 lines (207 loc) · 9.35 KB
/
MasterController.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
var NodeRSA = require('node-rsa');
var couchbase = require('couchbase');
var cluster = new couchbase.Cluster(`couchbase://${process.env.COUCHBASE_LO}`);
cluster.authenticate(process.env.COUCHBASE_ADMIN_USERNAME, process.env.COUCHBASE_ADMIN_PASSWORD);
var openmoney_bucket = cluster.openBucket('openmoney_global');
var crypto = require('crypto');
require('dotenv').config();
if(typeof process.env.API_HOST === 'undefined'){
process.env.API_HOST = 'http://localhost';
}
if(typeof process.env.COUCHBASE_LO === 'undefined'){
console.error("Did you forget to set the COUCHBASE_LO environment variable?");
process.exit(1);
}
function getRandomstring(length) {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for(var i = 0; i < length; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
function getSymetricKey(key){
var randomRange = getRandomArbitrary(key.getMaxMessageSize()/2, key.getMaxMessageSize()-1);
return getRandomstring(randomRange);
}
function encryptSymetricKey(key, symetricKey) {
return key.encrypt(symetricKey, 'base64', 'utf8');
}
function getHash(value) {
return crypto.createHash('sha256').update(value).digest('base64');
}
exports.authorize = function(stewardname, authorizeHeader, authorizeCallback) {
if(authorizeHeader.match(/Basic /i)){
authorizeHeader = authorizeHeader.replace(/Basic /i,'');
var basic = new Buffer(authorizeHeader, 'base64').toString('ascii');
basic = basic.split(":");
var stewardnamebasic = basic[0];
var password = basic[1];
openmoney_bucket.get("stewards~" + stewardnamebasic.toLowerCase(), function(err, doc){
if(err){
authorizeCallback(err, false);
} else {
//check if resource path matches credentials
if(stewardname !== null && doc.value.stewardname.toLowerCase() !== stewardname.toLowerCase()){
var error = {};
error.status = 401;
error.code = 1002;
error.message = "Stewardname did not match.";
authorizeCallback(error, false);
} else {
//password is verified in passport
authorizeCallback(null, doc.value.publicKey);
}
}
});
} else {
if(authorizeHeader.match(/Bearer /i)){
authorizeHeader = authorizeHeader.replace(/Bearer /i,'');
}
openmoney_bucket.get("session~" + authorizeHeader, function (err, doc) {
if (err) {
authorizeCallback(err, false);
} else {
if (stewardname !== null && doc.value.stewardname != stewardname) {
var error = {};
error.status = 401;
error.code = 1002;
error.message = "Stewardname did not match.";
authorizeCallback(error, false);
} else if (doc.value.expires < new Date().getTime()) {
var error = {};
error.status = 401;
error.code = 1003;
error.message = "Access token has expired.";
authorizeCallback(error, false);
} else {
authorizeCallback(null, doc.value.publicKey);
}
}
});
}
};
exports.isStewardOfPublicKey = function(stewardname, publicKey, cb) {
openmoney_bucket.get(getHash(publicKey), function(err, doc){
if(err) {
cb(err, false);
} else {
cb(null, doc.value.stewardname == stewardname);
}
});
};
exports.accessTokenPost = function(request, accessTokenPostCallback) {
if(request.grant_type.toLowerCase() === 'password'){
//password has been verified earlier
//verify client credentials
}
openmoney_bucket.get("steward_bucket~" + getHash(request.publicKey), function(err,publicKeyDoc){
if(err) {
accessTokenPostCallback(err, false);
} else {
var session = {};
session.id = "session~" + getHash(request.proof_token);
session.token = request.proof_token;
session.expires = new Date().getTime() + 3600;
session.publicKey = request.publicKey;
openmoney_bucket.upsert(session.id, session, function(err, res){
if(err) {
accessTokenPostCallback(err, false);
} else {
var response = {};
response.token = getHash(request.proof_token);
response.expires = publicKeyDoc.value.expires;
response.scopes = ['read:stewards', 'write:stewards', 'read:accounts', 'write:accounts', 'read:currencies',
'write:currencies', 'manage:currencies', 'read:spaces', 'write:spaces', 'manage:spaces', 'read:journals', 'write:journals'];
response.token_type = 'password';
accessTokenPostCallback(null, response);
}
});
}
});
};
exports.authorizePost = function(request, authorizePostCallback) {
//check that public key exsits.
openmoney_bucket.get(getHash(request.publicKey), function(err,publicKeyDoc){
if(err) {
authorizePostCallback({status: 401, code: 1005, message: "That public key is not registered."}, false);
} else {
//encrypt random string with public key
var key = new NodeRSA();
key.importKey(request.publicKey, 'pkcs8-public');
if (!key.isPublic()) {
var err = {
"status": 401,
"code": 1004,
"message": 'Failed public key evaluation!'
};
authorizePostCallback(err, false);
} else {
var isSessionValid = false;
if (typeof publicKeyDoc != 'undefined' && typeof publicKeyDoc.value != 'undefined' && typeof publicKeyDoc.value.access_token_expiry != 'undefined' && typeof publicKeyDoc.value.access_token != 'undefined') {
if (publicKeyDoc.value.access_token_expiry > new Date().getTime()) {
//if access token still has time left re-issue the same token
isSessionValid = true;
}
if (publicKeyDoc.value.access_token_expiry <= new Date().getTime()) {
// session has expired
// delete session
}
}
if (isSessionValid) {
//return current session
var encryptedSymetricKey = encryptSymetricKey(key, publicKeyDoc.value.access_token);
var response = {};
response.challenge_token = encryptedSymetricKey;
response.cors_token = request.cors_token;
//response.session = publicKeyDoc.value.access_token;
authorizePostCallback(null, response);
} else {
var symetrickey = getSymetricKey(key);
var encryptedSymetricKey = encryptSymetricKey(key, symetrickey);
publicKeyDoc.value.access_token = symetrickey;
publicKeyDoc.value.access_token_expiry = new Date().getTime() + ( 15 * 60 * 1000 ); //fifteen minutes
openmoney_bucket.upsert(getHash(request.publicKey), publicKeyDoc.value, function (err, result) {
if (err) {
authorizePostCallback(err, false);
} else {
var response = {};
response.challenge_token = encryptedSymetricKey;
response.cors_token = request.cors_token;
var session = {};
session.id = "session~" + getHash(publicKeyDoc.value.access_token);
session.access_token = publicKeyDoc.value.access_token;
session.access_token_expiry = publicKeyDoc.value.access_token_expiry;
session.stewardname = publicKeyDoc.value.stewardname;
session.publicKey = request.publicKey;
openmoney_bucket.upsert(session.id, session, function(err, res){
if(err) {
authorizePostCallback(err, false);
} else {
authorizePostCallback(null, response);
}
});
}
});
}
}
}
});
};
exports.stewardsGetByPublicKey = function(publicKey, cb) {
openmoney_bucket.get("steward_bucket~" + getHash(publicKey), function(err, doc){
if(err) {
cb(err, null);
} else {
exports.stewardsGet(doc.value, function(err, steward){
if(err){
cb(err, null);
} else {
cb(null, steward);
}
});
}
});
};