summaryrefslogtreecommitdiff
path: root/src/node/db/SessionManager.js
blob: f8000e47ed98f573416a284088ba75690f7fd0a5 (plain)
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/**
 * The Session Manager provides functions to manage session in the database, it only provides session management for sessions created by the API
 */

/*
 * 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 

var ERR = require("async-stacktrace");
var customError = require("../utils/customError");
var randomString = require("../utils/randomstring");
var db = require("./DB").db;
var async = require("async");
var groupMangager = require("./GroupManager");
var authorMangager = require("./AuthorManager");
 
exports.doesSessionExist = function(sessionID, callback)
{
  //check if the database entry of this session exists
  db.get("session:" + sessionID, function (err, session)
  {
    if(ERR(err, callback)) return;
    callback(null, session != null);
  });
}
 
/**
 * Creates a new session between an author and a group
 */
exports.createSession = function(groupID, authorID, validUntil, callback)
{
  var sessionID;

  async.series([
    //check if group exists
    function(callback)
    {
      groupMangager.doesGroupExist(groupID, function(err, exists)
      {
        if(ERR(err, callback)) return;
        
        //group does not exist
        if(exists == false)
        {
          callback(new customError("groupID does not exist","apierror"));
        }
        //everything is fine, continue
        else
        {
          callback();
        }
      });
    },
    //check if author exists
    function(callback)
    {
      authorMangager.doesAuthorExists(authorID, function(err, exists)
      {
        if(ERR(err, callback)) return;
        
        //author does not exist
        if(exists == false)
        {
          callback(new customError("authorID does not exist","apierror"));
        }
        //everything is fine, continue
        else
        {
          callback();
        }
      });
    },
    //check validUntil and create the session db entry
    function(callback)
    {
      //check if rev is a number
      if(typeof validUntil != "number")
      {
        //try to parse the number
        if(!isNaN(parseInt(validUntil)))
        {
          validUntil = parseInt(validUntil);
        }
        else
        {
          callback(new customError("validUntil is not a number","apierror"));
          return;
        }
      }
      
      //ensure this is not a negativ number
      if(validUntil < 0)
      {
        callback(new customError("validUntil is a negativ number","apierror"));
        return;
      }
      
      //ensure this is not a float value
      if(!is_int(validUntil))
      {
        callback(new customError("validUntil is a float value","apierror"));
        return;
      }
    
      //check if validUntil is in the future
      if(Math.floor(new Date().getTime()/1000) > validUntil)
      {
        callback(new customError("validUntil is in the past","apierror"));
        return;
      }
      
      //generate sessionID
      sessionID = "s." + randomString(16);
      
      //set the session into the database
      db.set("session:" + sessionID, {"groupID": groupID, "authorID": authorID, "validUntil": validUntil});
      
      callback();
    },
    //set the group2sessions entry
    function(callback)
    {
      //get the entry
      db.get("group2sessions:" + groupID, function(err, group2sessions)
      {
        if(ERR(err, callback)) return;
        
        //the entry doesn't exist so far, let's create it
        if(group2sessions == null || group2sessions.sessionIDs == null)
        {
          group2sessions = {sessionIDs : {}};
        }
        
        //add the entry for this session
        group2sessions.sessionIDs[sessionID] = 1;
        
        //save the new element back
        db.set("group2sessions:" + groupID, group2sessions);
        
        callback();
      });
    },
    //set the author2sessions entry
    function(callback)
    {
      //get the entry
      db.get("author2sessions:" + authorID, function(err, author2sessions)
      {
        if(ERR(err, callback)) return;
        
        //the entry doesn't exist so far, let's create it
        if(author2sessions == null || author2sessions.sessionIDs == null)
        {
          author2sessions = {sessionIDs : {}};
        }
        
        //add the entry for this session
        author2sessions.sessionIDs[sessionID] = 1;
        
        //save the new element back
        db.set("author2sessions:" + authorID, author2sessions);
        
        callback();
      });
    }
  ], function(err)
  {
    if(ERR(err, callback)) return;
    
    //return error and sessionID
    callback(null, {sessionID: sessionID});
  })
}

exports.getSessionInfo = function(sessionID, callback)
{
  //check if the database entry of this session exists
  db.get("session:" + sessionID, function (err, session)
  {
    if(ERR(err, callback)) return;
    
    //session does not exists
    if(session == null)
    {
      callback(new customError("sessionID does not exist","apierror"))
    }
    //everything is fine, return the sessioninfos
    else
    {
      callback(null, session);
    }
  });
}

/**
 * Deletes a session
 */
exports.deleteSession = function(sessionID, callback)
{
  var authorID, groupID;
  var group2sessions, author2sessions;

  async.series([
    function(callback)
    {
      //get the session entry
      db.get("session:" + sessionID, function (err, session)
      {
        if(ERR(err, callback)) return;
        
        //session does not exists
        if(session == null)
        {
          callback(new customError("sessionID does not exist","apierror"))
        }
        //everything is fine, return the sessioninfos
        else
        {
          authorID = session.authorID;
          groupID = session.groupID;
          
          callback();
        }
      });
    },
    //get the group2sessions entry
    function(callback)
    {
      db.get("group2sessions:" + groupID, function (err, _group2sessions)
      {
        if(ERR(err, callback)) return;
        group2sessions = _group2sessions;
        callback();
      });
    },
    //get the author2sessions entry
    function(callback)
    {
      db.get("author2sessions:" + authorID, function (err, _author2sessions)
      {
        if(ERR(err, callback)) return;
        author2sessions = _author2sessions;
        callback();
      });
    },
    //remove the values from the database
    function(callback)
    {
      //remove the session
      db.remove("session:" + sessionID);
      
      //remove session from group2sessions
      if(group2sessions != null) { // Maybe the group was already deleted
          delete group2sessions.sessionIDs[sessionID];
          db.set("group2sessions:" + groupID, group2sessions);
      }

      //remove session from author2sessions
      if(author2sessions != null) { // Maybe the author was already deleted
          delete author2sessions.sessionIDs[sessionID];
          db.set("author2sessions:" + authorID, author2sessions);
      }
      
      callback();
    }
  ], function(err)
  {
    if(ERR(err, callback)) return;
    callback();
  })
}

exports.listSessionsOfGroup = function(groupID, callback)
{
  groupMangager.doesGroupExist(groupID, function(err, exists)
  {
    if(ERR(err, callback)) return;
    
    //group does not exist
    if(exists == false)
    {
      callback(new customError("groupID does not exist","apierror"));
    }
    //everything is fine, continue
    else
    {
      listSessionsWithDBKey("group2sessions:" + groupID, callback);
    }
  });
}

exports.listSessionsOfAuthor = function(authorID, callback)
{  
  authorMangager.doesAuthorExists(authorID, function(err, exists)
  {
    if(ERR(err, callback)) return;
    
    //group does not exist
    if(exists == false)
    {
      callback(new customError("authorID does not exist","apierror"));
    }
    //everything is fine, continue
    else
    {
      listSessionsWithDBKey("author2sessions:" + authorID, callback);
    }
  });
}

//this function is basicly the code listSessionsOfAuthor and listSessionsOfGroup has in common
function listSessionsWithDBKey (dbkey, callback)
{
  var sessions;

  async.series([
    function(callback)
    {
      //get the group2sessions entry
      db.get(dbkey, function(err, sessionObject)
      {
        if(ERR(err, callback)) return;
        sessions = sessionObject ? sessionObject.sessionIDs : null;
        callback();
      });
    },
    function(callback)
    {           
      //collect all sessionIDs in an arrary
      var sessionIDs = [];
      for (var i in sessions)
      {
        sessionIDs.push(i);
      }
      
      //foreach trough the sessions and get the sessioninfos
      async.forEach(sessionIDs, function(sessionID, callback)
      {
        exports.getSessionInfo(sessionID, function(err, sessionInfo)
        {
          if (err == "apierror: sessionID does not exist")
          {
            console.warn("Found bad session " + sessionID + " in " + dbkey + ".");
          }
          else if(ERR(err, callback))
          {
            return;
          }

          sessions[sessionID] = sessionInfo;
          callback();
        });
      }, callback);
    }
  ], function(err)
  {
    if(ERR(err, callback)) return;
    callback(null, sessions);
  });
}

//checks if a number is an int
function is_int(value)
{ 
  return (parseFloat(value) == parseInt(value)) && !isNaN(value)
}