2013-07-02 23:51:38 +00:00
|
|
|
/**
|
|
|
|
* Module Dependencies.
|
|
|
|
*/
|
|
|
|
|
2013-07-16 17:49:25 +00:00
|
|
|
var Model = require('../loopback').Model
|
|
|
|
, loopback = require('../loopback')
|
2013-07-12 22:47:58 +00:00
|
|
|
, crypto = require('crypto');
|
2013-07-02 23:51:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default Session properties.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var properties = {
|
2013-10-04 22:51:48 +00:00
|
|
|
id: {type: String, generated: true, id: 1},
|
2013-07-02 23:51:38 +00:00
|
|
|
uid: {type: String},
|
|
|
|
ttl: {type: Number, ttl: true}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-07-16 17:49:25 +00:00
|
|
|
* Extends from the built in `loopback.Model` type.
|
2013-07-02 23:51:38 +00:00
|
|
|
*/
|
|
|
|
|
2013-11-11 21:35:54 +00:00
|
|
|
var Session = module.exports = Model.extend('Session', properties);
|
2013-07-12 22:47:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a cryptographically random session id.
|
|
|
|
*
|
|
|
|
* @param {Function} callback
|
|
|
|
*/
|
|
|
|
|
|
|
|
Session.createSessionId = function (fn) {
|
|
|
|
crypto.randomBytes(this.settings.sessionIdLength || 64, function(err, buf) {
|
|
|
|
if(err) {
|
|
|
|
fn(err);
|
|
|
|
} else {
|
|
|
|
fn(null, buf.toString('base64'));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Hook to create session id.
|
|
|
|
*/
|
|
|
|
|
|
|
|
Session.beforeCreate = function (next, data) {
|
|
|
|
data = data || {};
|
|
|
|
|
|
|
|
Session.createSessionId(function (err, id) {
|
|
|
|
if(err) {
|
|
|
|
next(err);
|
|
|
|
} else {
|
|
|
|
data.id = id;
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
});
|
2013-11-11 21:35:54 +00:00
|
|
|
}
|