-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtruevault_server.js
204 lines (162 loc) · 5.19 KB
/
truevault_server.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
TrueVault = {};
TrueVault.settings = {
"API_ENDPOINT": "https://api.truevault.com/v1",
};
TrueVault.config = function (configObject) {
TrueVault.settings = _.extend(TrueVault.settings, configObject);
};
TrueVault.urlVault = function () {
var settings = TrueVault.settings;
if(!settings["VAULT_ID"]){
throw new Meteor.Error(500, "VAULT_ID is not configured.");
} else {
return settings["API_ENDPOINT"] + '/vaults/' + settings["VAULT_ID"] + '/documents';
}
};
TrueVault.encode = function (obj) {
var objString = JSON.stringify(obj);
var base64 = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(objString));
return base64;
};
TrueVault.decode = function (base64) {
var baseParse = CryptoJS.enc.Base64.parse(base64);
var baseParse2 = CryptoJS.enc.Utf8.stringify(baseParse);
return JSON.parse(baseParse2);
};
TrueVault.insert = function (doc, userId) {
if(!doc || typeof doc !== "object"){
throw new Meteor.Error(400, "No updated object passed to TrueVault.update()");
}
var options = {
auth: TrueVault.settings["API_KEY"] + ":" + "",
data: {
document: TrueVault.encode(doc)
}
};
var result = HTTP.call("POST", TrueVault.urlVault(), options);
// console.log(result);
if(result.data.result === "success"){
// insert into collection
return Vault.insert({
userId: userId,
document_id: result.data.document_id,
version: 0
});
} else {
throw new Meteor.Error(result.statusCode, result.content);
}
};
TrueVault.findOne = function (docId) {
if(!docId){
throw new Meteor.Error(400, "No document_id passed to TrueVault.findOne()");
}
var url = TrueVault.urlVault() + "/" + docId;
var options = {
auth: TrueVault.settings["API_KEY"] + ":" + "",
};
var result = HTTP.call("GET", url, options);
// console.log(result);
if(result.statusCode === 200){
var decoded = TrueVault.decode(result.content);
// console.log(decoded);
return decoded;
} else {
throw new Meteor.Error(result.statusCode, result.error.messsage);
}
};
TrueVault.update = function (docId, doc) {
if(!docId || typeof docId !== "string"){
throw new Meteor.Error(400, "No document_id passed to TrueVault.update()");
}
if(!doc || typeof doc !== "object"){
throw new Meteor.Error(400, "No updated object passed to TrueVault.update()");
}
var url = TrueVault.urlVault() + "/" + docId;
var options = {
auth: TrueVault.settings["API_KEY"] + ":" + "",
data: {
document: TrueVault.encode(doc)
}
};
var result = HTTP.call("PUT", url, options);
if(result.statusCode === 200){
Vault.update({document_id: docId}, {$inc: {version: 1}});
}
return result.statusCode;
};
TrueVault.remove = function (docId) {
if(!docId || typeof docId !== "string"){
throw new Meteor.Error(400, "No document_id passed to TrueVault.remove()");
}
var url = TrueVault.urlVault() + "/" + docId;
var options = {
auth: TrueVault.settings["API_KEY"] + ":" + "",
};
var result = HTTP.call("DELETE", url, options);
// console.log(result);
// REMOVE DOCUMENT FROM DB ON SUCCESS
if(result.statusCode === 200){
Vault.remove({document_id: docId});
} else {
throw new Meteor.Error(result.statusCode, result.content);
}
};
TrueVault.fetch = function (arrayOfDocIds) {
console.log("TrueVault.fetch() is not currently implemented");
};
//
// HELPER FUNCTIONS FOR METHODS
//
// context is 'this' inside of the method
var checkForLoggedInUser = function (context) {
if(!context.userId){
throw new Meteor.Error(401, "There is not a logged in user.");
}
};
// return the existing vault or throw error
var findExistingVault = function (userId) {
var existingVault = Vault.findOne({userId: userId});
if(existingVault){
return existingVault;
} else {
throw new Meteor.Error(401, "This user does not have an existing record the Vault collection.");
}
};
Meteor.methods({
initializeTrueVault: function () {
checkForLoggedInUser(this);
var dataModel = TrueVault.settings["DATA_MODEL"];
var existingVault = Vault.find({userId: this.userId});
if(!dataModel){
throw new Meteor.Error(400, "No DATA_MODEL set on server for initializeTrueVault method to use.");
}
if(existingVault){
throw new Meteor.Error(400, "This user already has a record in the Vault collection.");
}
return TrueVault.insert(dataModel, this.userId);
}, //initializeTrueVault
// FIND LOGGED IN USERS TRUEVAULT RECORD
findOneTrueVault: function () {
checkForLoggedInUser(this);
var vault = findExistingVault(this.userId);
return TrueVault.findOne(vault.document_id);
}, //getTrueVault
// UPDATES THE LOGGED IN USERS TRUEVAULT RECORD WITH DOC THAT IS PASSED
updateTrueVault: function (doc) {
check(doc, Object);
checkForLoggedInUser(this);
if(!doc) {
throw new Meteor.Error(400, "No document passed to updateTrueVault method");
}
var vault = findExistingVault(this.userId);
return TrueVault.update(vault.document_id, doc);
}, //updateTrueVault
});
// PUBLISH THE VAULT INFORMATION FOR THE SIGNED IN USER
Meteor.publish(null, function () {
if(this.userId){
return Vault.find({userId: this.userId});
} else {
this.ready();
}
});