1 | /**
|
2 | * Store: Mongo
|
3 | * - Saving data using MongoDB
|
4 | **/
|
5 |
|
6 |
|
7 | var CRUD = function( options ){
|
8 |
|
9 | // use the provided db (error control?)
|
10 | this.db = options.db;
|
11 |
|
12 | }
|
13 |
|
14 | CRUD.prototype = {
|
15 |
|
16 | constructor: CRUD,
|
17 |
|
18 | create: function( data, callback ){
|
19 | // fallbacks
|
20 | data = data || {};
|
21 | var key = data._id || false;
|
22 | if( !key ) return callback(null, false);
|
23 | var expires = (data.expires_in) ? parseInt(data.expires_in) : 86400000; // default one day...
|
24 | // if refresh_token, add 60 days...
|
25 | if( data.refresh_token) expires += (60 * 86400000); // plus 60 days for refresh_token
|
26 | // convert ttl to seconds
|
27 | var ttl = Math.floor( expires / 1000 );
|
28 | // stringify data
|
29 | data = JSON.stringify(data);
|
30 | // connect to db
|
31 | this.db.setex( key, ttl, data, function(err, result){
|
32 | if(err) return callback(err);
|
33 | // error control?
|
34 | return callback( null, true );
|
35 | });
|
36 | },
|
37 |
|
38 | read: function( query, callback ){
|
39 | var key = query._id || false;
|
40 | if( !key ) return callback(null, false);
|
41 | // connect to db
|
42 | this.db.get( key, function(err, data){
|
43 | if(err) return callback(err);
|
44 | // parse data into an object
|
45 | data = JSON.parse( data.toString() );
|
46 | callback( null, data );
|
47 | });
|
48 | },
|
49 |
|
50 | destroy: function( item, callback ){
|
51 | var key = item._id || false;
|
52 | if( !key ) return callback(null, false);
|
53 | // connect to db
|
54 | this.db.del( key, function(err, data){
|
55 | if(err) return callback(err);
|
56 | callback( null, true );
|
57 | });
|
58 | },
|
59 | // FIX THIS: query not implemented for redis yet...
|
60 | query: function( query, callback ){
|
61 | var key = query._id || false;
|
62 | if( !key ) return callback(null, false);
|
63 | // connect to db
|
64 | this.db.get( key, function(err, data){
|
65 | if(err) return callback(err);
|
66 | // parse data into an object
|
67 | data = JSON.parse( data.toString() );
|
68 | callback( null, data );
|
69 | });
|
70 | }
|
71 | }
|
72 |
|
73 |
|
74 | module.exports = CRUD;
|
75 |
|
76 |
|
77 |
|
78 | /* DO NOT USE -*/
|
79 | /* This store needs revamping */
|
80 | /*
|
81 | var mongoose = require('mongoose'),
|
82 | Schema = mongoose.Schema,
|
83 | model = {};
|
84 |
|
85 |
|
86 |
|
87 | exports.addToDb = function(options, callback) {
|
88 | var res = false;
|
89 | var val = options.msg;
|
90 | var db = this.db;
|
91 | db.open(function(err, db) {
|
92 |
|
93 | db.collection('test', function(err, collection) {
|
94 | collection.count(function(err, count) {
|
95 | console.log('Total: ' + count);
|
96 | collection.find({'count': 'hi'}, function(err, cursor) {
|
97 | cursor.each(function(err, item) {
|
98 | if(item != null) {
|
99 | console.log(item);
|
100 | } else {
|
101 | console.log('No Items Found');
|
102 | }
|
103 | });
|
104 | });
|
105 |
|
106 | });
|
107 | collection.insert({'count':val});
|
108 | });
|
109 | });
|
110 |
|
111 |
|
112 | callback(null, res);
|
113 |
|
114 |
|
115 | };
|
116 |
|
117 | exports.addToDb.description = "Add data to the server";
|
118 | exports.addToDb.schema = {
|
119 | msg: {
|
120 | type: 'string',
|
121 | optional: false
|
122 | }
|
123 | };
|
124 |
|
125 | exports.getAllFromDb = function(options, callback) {
|
126 | var db = this.db;
|
127 | var res = [];
|
128 | db.open(function(err, db) {
|
129 | db.collection('test', function(err, collection) {
|
130 | collection.count(function(err, count) {
|
131 | console.log('Count: ' + count);
|
132 | collection.find(function(err, cursor) {
|
133 | cursor.toArray(function(err, docs) {
|
134 | docs.forEach(function(x) {
|
135 | res.push(x.count);
|
136 | });
|
137 | callback(null, res);
|
138 | if(docs != null) {
|
139 | res = docs;
|
140 | console.log(docs);
|
141 | } else {
|
142 | console.log('None');
|
143 | }
|
144 | db.close();
|
145 | });
|
146 | });
|
147 |
|
148 | });
|
149 | });
|
150 | });
|
151 | };
|
152 |
|
153 | exports.register = function(options, callback) {
|
154 | var db = new Db('trackRecords', new Server(host, port, {}), {native_parser: true});
|
155 | var name = options.name;
|
156 | db.open(function(err, db) {
|
157 | db.collection('domains', function(err, collection) {
|
158 | collection.insert({"name": name});
|
159 | db.close();
|
160 | console.log(options.name + ' Registered');
|
161 | callback(null, options);
|
162 |
|
163 | });
|
164 | });
|
165 |
|
166 | // callback(null, 'Could not update the data\n');
|
167 | };
|
168 | exports.register.description = "Register a domain on the Server";
|
169 | exports.register.schema = {
|
170 | name: {
|
171 | type: 'string',
|
172 | optional: false
|
173 | }
|
174 | };
|
175 |
|
176 |
|
177 | exports.trackHits = function(options, callback) {
|
178 | db = new Db('trackRecords', new Server(host, port, {}), {native_parser: true});
|
179 | db.open(function(err, db) {
|
180 | db.collection('domains', function(err, collection) {
|
181 | collection.find({"name":options.domain}, function(err, cursor) { //Getting the ID from the Domains Document
|
182 | cursor.nextObject(function(err, tuple) {
|
183 | if(tuple != null) {
|
184 | var hitCollection = 'hit_' + tuple._id; //Constructing the name of Hits document
|
185 | db.collection(hitCollection, function(err, collection) {
|
186 | collection.find({trackField:options.trackField}, function(err, cursor) {
|
187 | cursor.count(function(err, n) {
|
188 | var now = new Date();
|
189 | var trackField = options.trackField;
|
190 | if(n == 0) { //Insert
|
191 | collection.insert({trackField:trackField, count:1, 'timestamp':now.getTime()});
|
192 | console.log(options.domain + ':' + options.trackField + ' New Insert\n');
|
193 | db.close();
|
194 | } else { //Update
|
195 | var timestamps = []
|
196 | cursor.toArray(function(err, items) {
|
197 | for(i=0;i<n;i++) {
|
198 | timestamps.push(items[i].timestamp);
|
199 | }
|
200 | var max = Math.max.apply(0, timestamps);
|
201 | if((now.getTime() - max) < TIME_SLICE) { //Update the existing value
|
202 | collection.update({timestamp:max}, {$inc: {count: 1}});
|
203 | console.log(options.domain + ':' + options.trackField + ' Updated Existing\n');
|
204 | } else { //Insert a new row
|
205 | collection.insert({trackField:trackField, count:1, 'timestamp':now.getTime()});
|
206 | console.log(options.domain + ':' + options.trackField + ' Inserted after Time-Slice\n');
|
207 | }
|
208 | db.close();
|
209 | });
|
210 |
|
211 | }
|
212 |
|
213 | });
|
214 | });
|
215 | });
|
216 | }
|
217 | callback(null, tuple);
|
218 | });
|
219 | });
|
220 | });
|
221 | });
|
222 | };
|
223 | exports.trackHits.description = "Tracks the Click Hits on Sites";
|
224 | exports.trackHits.schema = {
|
225 | domain: {
|
226 | type: 'string',
|
227 | optional: false
|
228 | },
|
229 | trackField: {
|
230 | type: 'string',
|
231 | optional: false
|
232 | }
|
233 | };
|
234 |
|
235 | exports.getDomains = function(options, callback) {
|
236 | var db = new Db('trackRecords', new Server(host, port, {}), {native_parser: true});
|
237 |
|
238 | db.open(function(err, db) {
|
239 | db.collection('domains', function(err, collection) {
|
240 | collection.find(function(err, cursor) {
|
241 | cursor.toArray(function(err, items) {
|
242 | db.close();
|
243 | callback(null, items);
|
244 | });
|
245 | });
|
246 | });
|
247 | });
|
248 | };
|
249 |
|
250 | exports.getDomains.description = "Returns all the domains";
|
251 |
|
252 | exports.getHits = function(options, callback) {
|
253 | var db = new Db('trackRecords', new Server(host, port, {}), {native_parser: true});
|
254 | db.open(function(err, b) {
|
255 | db.collection('domains', function(err, collection) {
|
256 | collection.find({"name":options.domain}, function(err, cursor) { //Getting the ID from the Domains Document
|
257 | cursor.nextObject(function(err, tuple) {
|
258 | if(tuple != null) {
|
259 | var hitCollection = 'hit_' + tuple._id; //Constructing the name of Hits document
|
260 | db.collection(hitCollection, function(err, collection) {
|
261 | collection.find(function(err, cursor) {
|
262 | cursor.toArray(function(err, items) {
|
263 | callback(null, items);
|
264 | db.close();
|
265 | });
|
266 | });
|
267 | });
|
268 | }
|
269 | });
|
270 | });
|
271 | });
|
272 | });
|
273 | };
|
274 |
|
275 | exports.getHits.description = "Return the Hit data for a registered domain";
|
276 | exports.getHits.schema = {
|
277 | domain: {
|
278 | type: 'string',
|
279 | optional: false
|
280 | }
|
281 | };
|
282 |
|
283 | exports.savePattern = function(options, callback) {
|
284 | var db = new Db('trackRecords', new Server(host, port, {}), {native_parser: true});
|
285 | db.open(function(err, b) {
|
286 | db.collection('domains', function(err, collection) {
|
287 | collection.find({"name":options.domain}, function(err, cursor) { //Getting the ID from the Domains Document
|
288 | cursor.nextObject(function(err, tuple) {
|
289 | if(tuple != null) {
|
290 | var patternCollection = 'pattern_' + tuple._id; //Constructing the name of Hits document
|
291 | db.collection(patternCollection, function(err, collection) {
|
292 | var now = new Date();
|
293 | collection.insert({pattern: options.pattern, timestamp: now.getTime()});
|
294 | db.close();
|
295 | console.log('Pattern Saved for: ' + options.domain);
|
296 | callback(null, options);
|
297 | });
|
298 | }
|
299 | });
|
300 | });
|
301 | });
|
302 | });
|
303 |
|
304 | };
|
305 | exports.savePattern.description = "Saves pattern of a domain along with timestamp";
|
306 | exports.savePattern.schema = {
|
307 | domain: {
|
308 | type: 'string',
|
309 | optional: false
|
310 | },
|
311 | pattern: {
|
312 | type: 'string',
|
313 | optional: false
|
314 | }
|
315 | };
|
316 |
|
317 |
|
318 | exports.getPattern = function(options, callback) {
|
319 | var db = new Db('trackRecords', new Server(host, port, {}), {native_parser: true});
|
320 | db.open(function(err, b) {
|
321 | db.collection('domains', function(err, collection) {
|
322 | collection.find({"name":options.domain}, function(err, cursor) { //Getting the ID from the Domains Document
|
323 | cursor.nextObject(function(err, tuple) {
|
324 | if(tuple != null) {
|
325 | var patternCollection = 'pattern_' + tuple._id; //Constructing the name of Hits document
|
326 | db.collection(patternCollection, function(err, collection) {
|
327 | collection.find(function(err, cursor) {
|
328 | cursor.toArray(function(err, items) {
|
329 | callback(null, items);
|
330 | db.close();
|
331 | });
|
332 | });
|
333 | });
|
334 | }
|
335 | });
|
336 | });
|
337 | });
|
338 | });
|
339 | };
|
340 |
|
341 | exports.getPattern.description = "Return the pattern data for a registered domain";
|
342 | exports.getPattern.schema = {
|
343 | domain: {
|
344 | type: 'string',
|
345 | optional: false
|
346 | }
|
347 | };
|
348 |
|
349 |
|
350 | setInterval(function() {
|
351 | console.log('hello');
|
352 | }, 2000);
|
353 |
|
354 |
|
355 | //
|
356 | // Schemas definitions
|
357 | //
|
358 | var OAuthAccessTokensSchema = new Schema({
|
359 | accessToken: { type: String },
|
360 | clientId: { type: String },
|
361 | userId: { type: String },
|
362 | expires: { type: Date }
|
363 | });
|
364 |
|
365 | var OAuthRefreshTokensSchema = new Schema({
|
366 | refreshToken: { type: String },
|
367 | clientId: { type: String },
|
368 | userId: { type: String },
|
369 | expires: { type: Date }
|
370 | });
|
371 |
|
372 | var OAuthClientsSchema = new Schema({
|
373 | clientId: { type: String },
|
374 | clientSecret: { type: String },
|
375 | redirectUri: { type: String }
|
376 | });
|
377 |
|
378 | var OAuthUsersSchema = new Schema({
|
379 | username: { type: String },
|
380 | password: { type: String },
|
381 | firstname: { type: String },
|
382 | lastname: { type: String },
|
383 | email: { type: String, default: '' }
|
384 | });
|
385 |
|
386 | mongoose.model('OAuthAccessTokens', OAuthAccessTokensSchema);
|
387 | mongoose.model('OAuthRefreshTokens', OAuthRefreshTokensSchema);
|
388 | mongoose.model('OAuthClients', OAuthClientsSchema);
|
389 | mongoose.model('OAuthUsers', OAuthUsersSchema);
|
390 |
|
391 | var OAuthAccessTokensModel = mongoose.model('OAuthAccessTokens'),
|
392 | OAuthRefreshTokensModel = mongoose.model('OAuthRefreshTokens'),
|
393 | OAuthClientsModel = mongoose.model('OAuthClients'),
|
394 | OAuthUsersModel = mongoose.model('OAuthUsers');
|
395 |
|
396 | //
|
397 | // node-oauth2-server callbacks
|
398 | //
|
399 | model.getAccessToken = function (bearerToken, callback) {
|
400 | console.log('in getAccessToken (bearerToken: ' + bearerToken + ')');
|
401 |
|
402 | };
|
403 |
|
404 | model.getClient = function (clientId, clientSecret, callback) {
|
405 | console.log('in getClient (clientId: ' + clientId + ', clientSecret: ' + clientSecret + ')');
|
406 |
|
407 | ;
|
408 |
|
409 | // This will very much depend on your setup, I wouldn't advise doing anything exactly like this but
|
410 | // it gives an example of how to use the method to resrict certain grant types
|
411 | var authorizedClientIds = ['s6BhdRkqt3', 'toto'];
|
412 | model.grantTypeAllowed = function (clientId, grantType, callback) {
|
413 | console.log('in grantTypeAllowed (clientId: ' + clientId + ', grantType: ' + grantType + ')');
|
414 |
|
415 | if (grantType === 'password') {
|
416 | return callback(false, authorizedClientIds.indexOf(clientId) >= 0);
|
417 | }
|
418 |
|
419 | callback(false, true);
|
420 | };
|
421 |
|
422 | model.saveAccessToken = function (token, clientId, expires, userId, callback) {
|
423 | console.log('in saveAccessToken (token: ' + token + ', clientId: ' + clientId + ', userId: ' + userId + ', expires: ' + expires + ')');
|
424 |
|
425 | var accessToken = new OAuthAccessTokensModel({
|
426 | accessToken: token,
|
427 | clientId: clientId,
|
428 | userId: userId,
|
429 | expires: expires
|
430 | });
|
431 |
|
432 | accessToken.save(callback);
|
433 | };
|
434 |
|
435 | */
|