Skip to content

Commit

Permalink
Manage the authentication strategy keys for OAuth via a webpage.
Browse files Browse the repository at this point in the history
  • Loading branch information
sizzlemctwizzle committed Nov 14, 2013
1 parent 29d5b85 commit c3b6596
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 30 deletions.
22 changes: 14 additions & 8 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ var mongoose = require('mongoose');
var passport = require('passport');
var app = express();
var controllers = require('./controllers');
var authentication = require('./controllers/auth');
//var authentication = require('./controllers/auth');
var admin = require('./controllers/admin');
var settings = require('./models/settings.json');

app.configure(function(){
Expand All @@ -17,6 +18,11 @@ app.configure(function(){
app.use(express.session());
app.use(passport.initialize());
app.use(app.router);

// Set up the views
app.engine('html', require('./libs/muExpress').renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
});

if (process.env.NODE_ENV === 'production') {
Expand All @@ -31,22 +37,22 @@ db.once('open', function callback () {
app.listen(8080);
});

app.engine('html', require('./libs/muExpress').renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');

app.get('/', controllers.home);
app.get('/auth/:strategy?', authentication.auth);
/*app.get('/auth/:strategy?', authentication.auth);
app.post('/auth/', function(req, res) {
req.session.username = req.body.username;
res.redirect('/auth/' + req.body.auth);
});
app.get('/auth/:strategy/callback/', authentication.callback);
app.get('/logout', function(req, res) {
delete req.session.user;
res.redirect('/');
});
});*/

app.get('/auth/:strategy/callback/', authentication.callback);
app.get('/admin/user', admin.userAdmin);
app.get('/admin/api', admin.apiAdmin);
app.post('/admin/user/update', admin.userAdminUpdate);
app.post('/admin/api/update', admin.apiAdminUpdate);

app.use(express.static(__dirname + '/public'));
app.use(function(req, res, next){
Expand Down
86 changes: 86 additions & 0 deletions controllers/admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
var Strategy = require('../models/strategies.js').Strategy;
var User = require('../models/user.js').User;
var strategies = require('./strategies.json');

function userIsAdmin(req) {
return req.session.user && req.session.user.role < 3;
}

function getOAuthStrategies(stored) {
var oAuthStrats = [];
for (var i in strategies) {
var strategy = strategies[i];
if (strategy.oauth) {
oAuthStrats.push(stored[i] || { 'strat' : i, 'id' : '', 'key' : ''});
}
}

return oAuthStrats;
}

exports.userAdmin = function(req, res) {
if (!userIsAdmin(req)) res.redirect('/');

res.render('index', options, res);
};

exports.apiAdmin = function(req, res) {
//if (!userIsAdmin(req)) res.redirect('/');

Strategy.find({}, function(err, strats) {
var stored = {};
strats.forEach(function(strat) {
//strat.remove(function (err, product) {});
stored[strat.name] = { 'strat' : strat.name,
'id' : strat.id, 'key' : strat.key };
});

var strategies = getOAuthStrategies(stored);
var options = { 'strategies' : strategies };

res.render('apiAdmin', options, res);
});
};

exports.apiAdminUpdate = function(req, res) {
var postStrats = req.body;
var doneCount = 0;
function done() {
if (!(--doneCount)) res.redirect('/admin/api');
}

Strategy.find({}, function(err, strats) {
var stored = {};
strats.forEach(function(strat) {
stored[strat.name] = strat;
});

for (var i in postStrats) {
var postStrat = postStrats[i];
var strategy = null;
if (postStrat[0] && postStrat[1]) {
if (stored[i]) {
strategy = stored[i];
strategy.id = postStrat[0]
strategy.key = postStrat[1];
} else {
strategy = new Strategy({
'id' : postStrat[0],
'key' : postStrat[1],
'name' : i,
'display' : strategies[i].name
});
}

++doneCount;
strategy.save(function() { return done(); });
}
}

++doneCount;
done();
});
};

exports.userAdminUpdate = function(req, res) {
};
5 changes: 2 additions & 3 deletions controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
var User = require('../models/user').User;
var strategies = require('./strategies.json');
var prettystrategies = require('./prettyStrategies.json');

exports.home = function(req, res) {
var options = { 'title': 'Home page' };
var user = req.session.user;

if (!user) {
options.strategies = [{'strat' : '', 'pretty' : ''}];
/*options.strategies = [{'strat' : '', 'pretty' : ''}];
strategies.forEach(function(strat, index) {
options.strategies.push({
'strat' : strat, 'pretty' : prettystrategies[index]});
});
});*/
} else {
options.username = user.name;
}
Expand Down
5 changes: 0 additions & 5 deletions controllers/prettyStrategies.json

This file was deleted.

20 changes: 17 additions & 3 deletions controllers/strategies.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
[
"github", "facebook", "google", "paypal", "yahoo", "aol"
]
{
"github" : { "name" : "GitHub", "oauth" : true },
"facebook" : { "name" : "Facebook", "oauth" : true },
"google" : { "name" : "Google", "oauth" : false },
"paypal" : { "name" : "PayPal", "oauth" : false },
"yahoo" : { "name" : "Yahoo!", "oauth" : false },
"aol" : { "name" : "AOL", "oauth" : false },
"twitter" : { "name" : "Twitter", "oauth" : true },
"flickr" : { "name" : "Flickr", "oauth" : true },
"linkedin" : { "name" : "LinkedIn", "oauth" : true },
"netflix" : { "name" : "Netflix", "oauth" : true },
"reddit" : { "name" : "Reddit", "oauth" : true },
"imgur" : { "name" : "Imgur", "oauth" : true },
"amazon" : { "name" : "Amazon", "oauth" : true },
"windowslive" : { "name" : "Windows Live", "oauth" : true },
"openid" : { "name" : "OpenID", "oauth" : false }
}
11 changes: 0 additions & 11 deletions controllers/strategiesFinal.json

This file was deleted.

13 changes: 13 additions & 0 deletions models/strategies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var strategySchema = new Schema({
id: String,
key: String,
name: String,
display: String
});

var Strategy = mongoose.model('Strategy', strategySchema);

exports.Strategy = Strategy;
8 changes: 8 additions & 0 deletions models/userRoles.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
"Root",
"Founding Father",
"Admin",
"Moderator",
"Script Writer",
"User"
]
17 changes: 17 additions & 0 deletions views/apiAdmin.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<html>
<head>
<title>Edit Authenticator Settings</title>
</head>
<body>
<form method="post" action="/admin/api/update">
{{#strategies}}
<strong>{{strat}}</strong><br />
<strong>id: </strong>
<input type="text" name="{{strat}}[0]" value="{{id}}" /><br />
<strong>key: </strong>
<input type="text" name="{{strat}}[1]" value="{{key}}" /><br /><br />
{{/strategies}}
<input type="submit" value="Update" />
</form>
</body>
</html>

0 comments on commit c3b6596

Please sign in to comment.