Skip to content

Commit

Permalink
Creating auth() and starting to refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Aug 3, 2014
1 parent 2444ce6 commit ce3615e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 20 deletions.
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = function (grunt) {
src : [
"src/intro.js",
"src/constructor.js",
"src/auth.js",
"src/bootstrap.js",
"src/error.js",
"src/factory.js",
Expand Down
41 changes: 31 additions & 10 deletions lib/tenso.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var TurtleIO = require( "turtle.io" ),
SERVER = "tenso/1.0.0",
CONFIG = require( __dirname + "/../config.json" ),
keigai = require( "keigai" ),
passport = require( "passport" ),
util = keigai.util,
array = util.array,
clone = util.clone,
Expand Down Expand Up @@ -59,6 +60,34 @@ Tenso.prototype.respond = function ( req, res, arg, status, headers ) {
this.server.respond( req, res, hypermedia( this.server, req, response( arg, status ), ref[0] ), status, ref[0] );
};

/**
* Setups up authentication
*
* @method auth
* @param {Object} config Tenso configuration
* @param {String} hostname Server hostname
* @return {Object} Updated Tenso configuration
*/
function auth ( config, hostname ) {
var obj;

if ( config.auth.type === "basic" ) {
obj = {};
obj[hostname] = {
authRealm: config.auth.realm || "Private",
authList: config.auth.list
};

config.auth = obj;
}
else if ( config.auth.type === "local" ) {
config.routes.post = config.routes.post || {};
config.routes.post["/login"] = passport.authenticate( "local", {successRedirect: "/", failureRedirect: "/login"} );
}

return config;
}

/**
* Bootstraps an instance of Tenso
*
Expand Down Expand Up @@ -123,7 +152,7 @@ function factory ( arg ) {
var HOSTNAME = arg ? arg.hostname || "localhost" : "localhost",
vhosts = {},
config = arg ? merge( clone( CONFIG, true ), arg ) : CONFIG,
auth, instance;
instance;

if ( !config.port ) {
console.error( "Invalid configuration" );
Expand All @@ -136,15 +165,7 @@ function factory ( arg ) {
config["default"] = HOSTNAME;

if ( config.auth !== null ) {
if ( config.auth.type === "basic" ) {
auth = {};
auth[HOSTNAME] = {
authRealm: config.auth.realm || "Private",
authList: config.auth.list || config.auth
};

config.auth = auth;
}
config = auth( config, HOSTNAME );
}

instance = new Tenso();
Expand Down
27 changes: 27 additions & 0 deletions src/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Setups up authentication
*
* @method auth
* @param {Object} config Tenso configuration
* @param {String} hostname Server hostname
* @return {Object} Updated Tenso configuration
*/
function auth ( config, hostname ) {
var obj;

if ( config.auth.type === "basic" ) {
obj = {};
obj[hostname] = {
authRealm: config.auth.realm || "Private",
authList: config.auth.list
};

config.auth = obj;
}
else if ( config.auth.type === "local" ) {
config.routes.post = config.routes.post || {};
config.routes.post["/login"] = passport.authenticate( "local", {successRedirect: "/", failureRedirect: "/login"} );
}

return config;
}
12 changes: 2 additions & 10 deletions src/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function factory ( arg ) {
var HOSTNAME = arg ? arg.hostname || "localhost" : "localhost",
vhosts = {},
config = arg ? merge( clone( CONFIG, true ), arg ) : CONFIG,
auth, instance;
instance;

if ( !config.port ) {
console.error( "Invalid configuration" );
Expand All @@ -22,15 +22,7 @@ function factory ( arg ) {
config["default"] = HOSTNAME;

if ( config.auth !== null ) {
if ( config.auth.type === "basic" ) {
auth = {};
auth[HOSTNAME] = {
authRealm: config.auth.realm || "Private",
authList: config.auth.list || config.auth
};

config.auth = auth;
}
config = auth( config, HOSTNAME );
}

instance = new Tenso();
Expand Down
1 change: 1 addition & 0 deletions src/intro.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var TurtleIO = require( "turtle.io" ),
SERVER = "tenso/{{VERSION}}",
CONFIG = require( __dirname + "/../config.json" ),
keigai = require( "keigai" ),
passport = require( "passport" ),
util = keigai.util,
array = util.array,
clone = util.clone,
Expand Down

0 comments on commit ce3615e

Please sign in to comment.