1
0
Fork 0

Code style.

This commit is contained in:
Alan Plum 2014-07-09 14:42:54 +02:00
parent 00a0b4513a
commit e73ab38720
8 changed files with 93 additions and 97 deletions

View File

@ -1,12 +1,12 @@
/*jslint indent: 2, nomen: true, maxlen: 120, vars: true, es5: true */ /*jslint indent: 2, nomen: true, maxlen: 120, es5: true */
/*global require, applicationContext */ /*global require, applicationContext */
(function () { (function () {
'use strict'; 'use strict';
var _ = require('underscore'); var _ = require('underscore'),
var Foxx = require('org/arangodb/foxx'); Foxx = require('org/arangodb/foxx'),
var errors = require('./errors'); errors = require('./errors'),
var controller = new Foxx.Controller(applicationContext); controller = new Foxx.Controller(applicationContext),
var api = Foxx.requireApp(applicationContext.mount).sessionStorage; api = Foxx.requireApp(applicationContext.mount).sessionStorage;
controller.post('/', function (req, res) { controller.post('/', function (req, res) {
var session = api.create(req.body()); var session = api.create(req.body());
@ -31,8 +31,8 @@
.notes('Fetches the session with the given sid.'); .notes('Fetches the session with the given sid.');
controller.put('/:sid', function (req, res) { controller.put('/:sid', function (req, res) {
var body = JSON.parse(req.rawBody()); var body = JSON.parse(req.rawBody()),
var session = api.get(req.urlParameters.sid); session = api.get(req.urlParameters.sid);
session.set('sessionData', body); session.set('sessionData', body);
session.save(); session.save();
res.json(session.forClient()); res.json(session.forClient());
@ -48,8 +48,8 @@
.notes('Updates the session with the given sid by replacing the sessionData.'); .notes('Updates the session with the given sid by replacing the sessionData.');
controller.patch('/:sid', function (req, res) { controller.patch('/:sid', function (req, res) {
var body = JSON.parse(req.rawBody()); var body = JSON.parse(req.rawBody()),
var session = api.get(req.urlParameters.sid); session = api.get(req.urlParameters.sid);
_.extend(session.get('sessionData'), body); _.extend(session.get('sessionData'), body);
session.save(); session.save();
res.json(session.forClient()); res.json(session.forClient());

View File

@ -1,4 +1,4 @@
/*jslint indent: 2, nomen: true, maxlen: 120, vars: true, es5: true */ /*jslint indent: 2, nomen: true, maxlen: 120, es5: true */
/*global require, exports */ /*global require, exports */
(function () { (function () {
'use strict'; 'use strict';

View File

@ -1,9 +1,9 @@
/*jslint indent: 2, nomen: true, maxlen: 120, vars: true, es5: true */ /*jslint indent: 2, nomen: true, maxlen: 120, es5: true */
/*global require, applicationContext */ /*global require, applicationContext */
(function () { (function () {
'use strict'; 'use strict';
var db = require('org/arangodb').db; var db = require('org/arangodb').db,
var sessionsName = applicationContext.collectionName('sessions'); sessionsName = applicationContext.collectionName('sessions');
if (db._collection(sessionsName) === null) { if (db._collection(sessionsName) === null) {
db._create(sessionsName); db._create(sessionsName);

View File

@ -1,34 +1,31 @@
/*jslint indent: 2, nomen: true, maxlen: 120, vars: true, es5: true */ /*jslint indent: 2, nomen: true, maxlen: 120, es5: true */
/*global require, exports, applicationContext */ /*global require, exports, applicationContext */
(function () { (function () {
'use strict'; 'use strict';
var _ = require('underscore'); var _ = require('underscore'),
var internal = require('internal'); internal = require('internal'),
var arangodb = require('org/arangodb'); arangodb = require('org/arangodb'),
var db = arangodb.db; db = arangodb.db,
var addCookie = require('org/arangodb/actions').addCookie; addCookie = require('org/arangodb/actions').addCookie,
var crypto = require('org/arangodb/crypto'); crypto = require('org/arangodb/crypto'),
var Foxx = require('org/arangodb/foxx'); Foxx = require('org/arangodb/foxx'),
var errors = require('./errors'); errors = require('./errors'),
cfg = applicationContext.configuration,
var cfg = applicationContext.configuration; Session = Foxx.Model.extend({}, {
attributes: {
var Session = Foxx.Model.extend({}, { _key: {type: 'string', required: true},
attributes: { uid: {type: 'string', required: false},
_key: {type: 'string', required: true}, sessionData: {type: 'object', required: true},
uid: {type: 'string', required: false}, userData: {type: 'object', required: true},
sessionData: {type: 'object', required: true}, created: {type: 'integer', required: true},
userData: {type: 'object', required: true}, lastAccess: {type: 'integer', required: true},
created: {type: 'integer', required: true}, lastUpdate: {type: 'integer', required: true}
lastAccess: {type: 'integer', required: true}, }
lastUpdate: {type: 'integer', required: true} }),
} sessions = new Foxx.Repository(
}); applicationContext.collection('sessions'),
{model: Session}
var sessions = new Foxx.Repository( );
applicationContext.collection('sessions'),
{model: Session}
);
function generateSessionId() { function generateSessionId() {
var sid = ''; var sid = '';
@ -43,17 +40,17 @@
} }
function createSession(sessionData) { function createSession(sessionData) {
var sid = generateSessionId(cfg); var sid = generateSessionId(cfg),
var now = Number(new Date()); now = Number(new Date()),
var session = new Session({ session = new Session({
_key: sid, _key: sid,
sid: sid, sid: sid,
sessionData: sessionData || {}, sessionData: sessionData || {},
userData: {}, userData: {},
created: now, created: now,
lastAccess: now, lastAccess: now,
lastUpdate: now lastUpdate: now
}); });
sessions.save(session); sessions.save(session);
return session; return session;
} }
@ -106,11 +103,12 @@
} }
function fromCookie(req, cookieName, secret) { function fromCookie(req, cookieName, secret) {
var session = null; var session = null,
var value = req.cookies[cookieName]; value = req.cookies[cookieName],
signature;
if (value) { if (value) {
if (secret) { if (secret) {
var signature = req.cookies[cookieName + '_sig'] || ''; signature = req.cookies[cookieName + '_sig'] || '';
if (!crypto.constantEquals(signature, crypto.hmac(secret, value))) { if (!crypto.constantEquals(signature, crypto.hmac(secret, value))) {
return null; return null;
} }
@ -131,8 +129,8 @@
if (!cfg.timeToLive) { if (!cfg.timeToLive) {
return; return;
} }
var now = Number(new Date()); var now = Number(new Date()),
var prop = cfg.ttlType; prop = cfg.ttlType;
if (!prop || !this.get(prop)) { if (!prop || !this.get(prop)) {
prop = 'created'; prop = 'created';
} }
@ -141,8 +139,8 @@
} }
}, },
addCookie: function (res, cookieName, secret) { addCookie: function (res, cookieName, secret) {
var value = this.get('_key'); var value = this.get('_key'),
var ttl = cfg.timeToLive; ttl = cfg.timeToLive;
ttl = ttl ? Math.floor(ttl / 1000) : undefined; ttl = ttl ? Math.floor(ttl / 1000) : undefined;
addCookie(res, cookieName, value, ttl); addCookie(res, cookieName, value, ttl);
if (secret) { if (secret) {
@ -167,16 +165,16 @@
return session; return session;
}, },
save: function () { save: function () {
var session = this; var session = this,
var now = Number(new Date()); now = Number(new Date());
session.set('lastAccess', now); session.set('lastAccess', now);
session.set('lastUpdate', now); session.set('lastUpdate', now);
sessions.replace(session); sessions.replace(session);
return session; return session;
}, },
delete: function () { delete: function () {
var session = this; var session = this,
var now = Number(new Date()); now = Number(new Date());
session.set('lastAccess', now); session.set('lastAccess', now);
session.set('lastUpdate', now); session.set('lastUpdate', now);
try { try {

View File

@ -1,26 +1,26 @@
/*jslint indent: 2, nomen: true, maxlen: 120, vars: true, es5: true */ /*jslint indent: 2, nomen: true, maxlen: 120, es5: true */
/*global require, exports, applicationContext */ /*global require, exports, applicationContext */
(function () { (function () {
'use strict'; 'use strict';
var crypto = require('org/arangodb/crypto'); var crypto = require('org/arangodb/crypto'),
var cfg = applicationContext.configuration; cfg = applicationContext.configuration;
function verifyPassword(authData, password) { function verifyPassword(authData, password) {
if (!authData) { if (!authData) {
authData = {}; authData = {};
} }
var hashMethod = authData.method || cfg.hashMethod; var hashMethod = authData.method || cfg.hashMethod,
var salt = authData.salt || ''; salt = authData.salt || '',
var storedHash = authData.hash || ''; storedHash = authData.hash || '',
var generatedHash = crypto[hashMethod](salt + password); generatedHash = crypto[hashMethod](salt + password);
// non-lazy comparison to avoid timing attacks // non-lazy comparison to avoid timing attacks
return crypto.constantEquals(storedHash, generatedHash); return crypto.constantEquals(storedHash, generatedHash);
} }
function hashPassword(password) { function hashPassword(password) {
var hashMethod = cfg.hashMethod; var hashMethod = cfg.hashMethod,
var salt = crypto.genRandomAlphaNumbers(cfg.saltLength); salt = crypto.genRandomAlphaNumbers(cfg.saltLength),
var hash = crypto[hashMethod](salt + password); hash = crypto[hashMethod](salt + password);
return {method: hashMethod, salt: salt, hash: hash}; return {method: hashMethod, salt: salt, hash: hash};
} }

View File

@ -1,4 +1,4 @@
/*jslint indent: 2, nomen: true, maxlen: 120, vars: true, es5: true */ /*jslint indent: 2, nomen: true, maxlen: 120, es5: true */
/*global require, exports */ /*global require, exports */
(function () { (function () {
'use strict'; 'use strict';

View File

@ -1,9 +1,9 @@
/*jslint indent: 2, nomen: true, maxlen: 120, vars: true, es5: true */ /*jslint indent: 2, nomen: true, maxlen: 120, es5: true */
/*global require, applicationContext */ /*global require, applicationContext */
(function () { (function () {
'use strict'; 'use strict';
var db = require('org/arangodb').db; var db = require('org/arangodb').db,
var usersName = applicationContext.collectionName('users'); usersName = applicationContext.collectionName('users');
if (db._collection(usersName) === null) { if (db._collection(usersName) === null) {
db._create(usersName); db._create(usersName);

View File

@ -1,25 +1,23 @@
/*jslint indent: 2, nomen: true, maxlen: 120, vars: true, es5: true */ /*jslint indent: 2, nomen: true, maxlen: 120, es5: true */
/*global require, exports, applicationContext */ /*global require, exports, applicationContext */
(function () { (function () {
'use strict'; 'use strict';
var _ = require('underscore'); var _ = require('underscore'),
var arangodb = require('org/arangodb'); arangodb = require('org/arangodb'),
var db = arangodb.db; db = arangodb.db,
var Foxx = require('org/arangodb/foxx'); Foxx = require('org/arangodb/foxx'),
var errors = require('./errors'); errors = require('./errors'),
User = Foxx.Model.extend({}, {
var User = Foxx.Model.extend({}, { attributes: {
attributes: { user: {type: 'string', required: true},
user: {type: 'string', required: true}, authData: {type: 'object', required: true},
authData: {type: 'object', required: true}, userData: {type: 'object', required: true}
userData: {type: 'object', required: true} }
} }),
}); users = new Foxx.Repository(
applicationContext.collection('users'),
var users = new Foxx.Repository( {model: User}
applicationContext.collection('users'), );
{model: User}
);
function resolve(username) { function resolve(username) {
var user = users.firstExample({user: username}); var user = users.firstExample({user: username});