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(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,
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)
));
}
});