From d1f7d25aa83e40b6a6cb3f6a1d8cbe96e30563ee Mon Sep 17 00:00:00 2001 From: Lucas Dohmen Date: Wed, 4 Sep 2013 13:01:56 +0200 Subject: [PATCH] Foxx: Some refinements for `.login` and `.logout` --- js/common/tests/shell-foxx.js | 58 +++++++++- .../modules/org/arangodb/foxx/controller.js | 106 +++++++++++++----- 2 files changed, 132 insertions(+), 32 deletions(-) diff --git a/js/common/tests/shell-foxx.js b/js/common/tests/shell-foxx.js index 0f67fa066c..51499cccc7 100644 --- a/js/common/tests/shell-foxx.js +++ b/js/common/tests/shell-foxx.js @@ -164,7 +164,63 @@ function SetRoutesFoxxControllerSpec () { } assertEqual(error, new Error("URL has to be a String")); assertEqual(routes.length, 0); - } + }, + + testAddALoginRoute: function () { + var myFunc = function () {}, + routes = app.routingInfo.routes; + + app.activateAuthentication({ + type: "cookie", + cookieLifetime: 360000, + cookieName: "my_cookie", + sessionLifetime: 400 + }); + app.login('/simple/route', myFunc); + assertEqual(routes[0].docs.httpMethod, 'POST'); + assertEqual(routes[0].url.methods, ["post"]); + }, + + testRefuseLoginWhenAuthIsNotSetUp: function () { + var myFunc = function () {}, + error; + + try { + app.login('/simple/route', myFunc); + } catch(e) { + error = e; + } + + assertEqual(error, new Error("Setup authentication first")); + }, + + testAddALogoutRoute: function () { + var myFunc = function () {}, + routes = app.routingInfo.routes; + + app.activateAuthentication({ + type: "cookie", + cookieLifetime: 360000, + cookieName: "my_cookie", + sessionLifetime: 400 + }); + app.logout('/simple/route', myFunc); + assertEqual(routes[0].docs.httpMethod, 'POST'); + assertEqual(routes[0].url.methods, ["post"]); + }, + + testRefuseLogoutWhenAuthIsNotSetUp: function () { + var myFunc = function () {}, + error; + + try { + app.logout('/simple/route', myFunc); + } catch(e) { + error = e; + } + + assertEqual(error, new Error("Setup authentication first")); + }, }; } diff --git a/js/server/modules/org/arangodb/foxx/controller.js b/js/server/modules/org/arangodb/foxx/controller.js index 0fe0c13f3e..082232bd21 100644 --- a/js/server/modules/org/arangodb/foxx/controller.js +++ b/js/server/modules/org/arangodb/foxx/controller.js @@ -36,13 +36,16 @@ var Controller, extend = _.extend, is = require("org/arangodb/is"), internal = require("org/arangodb/foxx/internals"), - defaultsFor = {}; + defaultsFor = {}, + createStandardLoginHandler, + createStandardLogoutHandler; defaultsFor.login = { usernameField: "username", passwordField: "password", onSuccess: function (req, res) { + 'use strict'; res.json({ user: req.user.identifier, key: req.currentSession._key @@ -50,6 +53,7 @@ defaultsFor.login = { }, onError: function (req, res) { + 'use strict'; res.status(401); res.json({ error: "Username or Password was wrong" @@ -57,14 +61,32 @@ defaultsFor.login = { } }; +createStandardLoginHandler = function (auth, users, options) { + 'use strict'; + return function (req, res) { + var username = req.body()[options.usernameField], + password = req.body()[options.passwordField]; + + if (users.isValid(username, password)) { + req.currentSession = auth.beginSession(req, res, username, {}); + req.user = users.get(req.currentSession.identifier); + options.onSuccess(req, res); + } else { + options.onError(req, res); + } + }; +}; + defaultsFor.logout = { onSuccess: function (req, res) { + 'use strict'; res.json({ notice: "Logged out!", }); }, onError: function (req, res) { + 'use strict'; res.status(401); res.json({ error: "No session was found" @@ -72,6 +94,21 @@ defaultsFor.logout = { } }; +createStandardLogoutHandler = function (auth, options) { + 'use strict'; + return function (req, res) { + if (is.existy(req.currentSession)) { + auth.endSession(req, res, req.currentSession._key); + req.user = null; + req.currentSession = null; + options.onSuccess(req, res); + } else { + options.onError(req, res); + } + }; +}; + + // ----------------------------------------------------------------------------- // --SECTION-- Controller // ----------------------------------------------------------------------------- @@ -426,6 +463,31 @@ extend(Controller.prototype, { }); }, +//////////////////////////////////////////////////////////////////////////////// +/// @fn JSF_foxx_controller_getUsers +/// @brief Get the users of this controller +//////////////////////////////////////////////////////////////////////////////// + getUsers: function () { + 'use strict'; + var foxxAuthentication = require("org/arangodb/foxx/authentication"), + users = new foxxAuthentication.Users(this.applicationContext); + + return users; + }, + +//////////////////////////////////////////////////////////////////////////////// +/// @fn JSF_foxx_controller_getAuth +/// @brief Get the auth object of this controller +//////////////////////////////////////////////////////////////////////////////// + getAuth: function () { + 'use strict'; + if (is.notExisty(this.auth)) { + throw new Error("Setup authentication first"); + } + + return this.auth; + }, + //////////////////////////////////////////////////////////////////////////////// /// @fn JSF_foxx_controller_activateAuthentication /// @brief Activate authentication for this app @@ -453,6 +515,7 @@ extend(Controller.prototype, { /// @endcode //////////////////////////////////////////////////////////////////////////////// activateAuthentication: function (opts) { + 'use strict'; var foxxAuthentication = require("org/arangodb/foxx/authentication"), sessions, cookieAuth, @@ -538,23 +601,12 @@ extend(Controller.prototype, { /// @endcode //////////////////////////////////////////////////////////////////////////////// login: function (route, opts) { - var foxxAuthentication = require("org/arangodb/foxx/authentication"), - auth = this.auth, - users = new foxxAuthentication.Users(this.applicationContext), - options = _.defaults(opts || {}, defaultsFor.login); - - this.post(route, function (req, res) { - var username = req.body()[options.usernameField], - password = req.body()[options.passwordField]; - - if (users.isValid(username, password)) { - req.currentSession = auth.beginSession(req, res, username, {}); - req.user = users.get(req.currentSession.identifier); - options.onSuccess(req, res); - } else { - options.onError(req, res); - } - }); + 'use strict'; + this.post(route, createStandardLoginHandler( + this.getAuth(), + this.getUsers(), + _.defaults(opts || {}, defaultsFor.login) + )); }, //////////////////////////////////////////////////////////////////////////////// @@ -587,19 +639,11 @@ extend(Controller.prototype, { /// @endcode //////////////////////////////////////////////////////////////////////////////// logout: function (route, opts) { - var auth = this.auth, - options = _.defaults(opts || {}, defaultsFor.logout); - - this.post(route, function (req, res) { - if (is.existy(req.currentSession)) { - auth.endSession(req, res, req.currentSession._key); - req.user = null; - req.currentSession = null; - options.onSuccess(req, res); - } else { - options.onError(req, res); - } - }); + 'use strict'; + this.post(route, createStandardLogoutHandler( + this.getAuth(), + _.defaults(opts || {}, defaultsFor.logout) + )); } });