1
0
Fork 0

Foxx: Some refinements for `.login` and `.logout`

This commit is contained in:
Lucas Dohmen 2013-09-04 13:01:56 +02:00
parent d9213af5ec
commit d1f7d25aa8
2 changed files with 132 additions and 32 deletions

View File

@ -164,7 +164,63 @@ function SetRoutesFoxxControllerSpec () {
} }
assertEqual(error, new Error("URL has to be a String")); assertEqual(error, new Error("URL has to be a String"));
assertEqual(routes.length, 0); 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"));
},
}; };
} }

View File

@ -36,13 +36,16 @@ var Controller,
extend = _.extend, extend = _.extend,
is = require("org/arangodb/is"), is = require("org/arangodb/is"),
internal = require("org/arangodb/foxx/internals"), internal = require("org/arangodb/foxx/internals"),
defaultsFor = {}; defaultsFor = {},
createStandardLoginHandler,
createStandardLogoutHandler;
defaultsFor.login = { defaultsFor.login = {
usernameField: "username", usernameField: "username",
passwordField: "password", passwordField: "password",
onSuccess: function (req, res) { onSuccess: function (req, res) {
'use strict';
res.json({ res.json({
user: req.user.identifier, user: req.user.identifier,
key: req.currentSession._key key: req.currentSession._key
@ -50,6 +53,7 @@ defaultsFor.login = {
}, },
onError: function (req, res) { onError: function (req, res) {
'use strict';
res.status(401); res.status(401);
res.json({ res.json({
error: "Username or Password was wrong" 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 = { defaultsFor.logout = {
onSuccess: function (req, res) { onSuccess: function (req, res) {
'use strict';
res.json({ res.json({
notice: "Logged out!", notice: "Logged out!",
}); });
}, },
onError: function (req, res) { onError: function (req, res) {
'use strict';
res.status(401); res.status(401);
res.json({ res.json({
error: "No session was found" 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 // --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 /// @fn JSF_foxx_controller_activateAuthentication
/// @brief Activate authentication for this app /// @brief Activate authentication for this app
@ -453,6 +515,7 @@ extend(Controller.prototype, {
/// @endcode /// @endcode
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
activateAuthentication: function (opts) { activateAuthentication: function (opts) {
'use strict';
var foxxAuthentication = require("org/arangodb/foxx/authentication"), var foxxAuthentication = require("org/arangodb/foxx/authentication"),
sessions, sessions,
cookieAuth, cookieAuth,
@ -538,23 +601,12 @@ extend(Controller.prototype, {
/// @endcode /// @endcode
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
login: function (route, opts) { login: function (route, opts) {
var foxxAuthentication = require("org/arangodb/foxx/authentication"), 'use strict';
auth = this.auth, this.post(route, createStandardLoginHandler(
users = new foxxAuthentication.Users(this.applicationContext), this.getAuth(),
options = _.defaults(opts || {}, defaultsFor.login); this.getUsers(),
_.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);
}
});
}, },
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -587,19 +639,11 @@ extend(Controller.prototype, {
/// @endcode /// @endcode
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
logout: function (route, opts) { logout: function (route, opts) {
var auth = this.auth, 'use strict';
options = _.defaults(opts || {}, defaultsFor.logout); this.post(route, createStandardLogoutHandler(
this.getAuth(),
this.post(route, function (req, res) { _.defaults(opts || {}, defaultsFor.logout)
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);
}
});
} }
}); });