Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions jingo
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var express = require('express')
, Url = require('url')
, GoogleStrategy = require('passport-google').Strategy
, LocalStrategy = require('passport-local').Strategy
, FacebookStrategy = require('passport-facebook').Strategy
, yaml = require('yaml-js')
, Flash = require('connect-flash')
, program = require('commander');
Expand Down Expand Up @@ -97,11 +98,11 @@ if (!config.application || !config.server) {
process.exit(-1);
}

app.locals.authorization = config.authorization || { anonRead: false, validMatches: ".+" };
var auth = app.locals.authentication = config.authentication || { google: { enabled: true }, alone: { enabled: false } };

if ( (!auth.google || !auth.google.enabled) &&
(!auth.alone || !auth.alone.enabled) ) {
(!auth.alone || !auth.alone.enabled) &&
(!auth.facebook || !auth.facebook.appId || !auth.facebook.appSecret) ) {
console.log("Error: no authentication method provided. Cannot continue.");
process.exit(-1);
}
Expand Down Expand Up @@ -247,6 +248,20 @@ passport.use(new GoogleStrategy({
}
));

if (auth.facebook) {
passport.use(new FacebookStrategy({
clientID: auth.facebook.appId,
clientSecret: auth.facebook.appSecret,
callbackURL: app.locals.baseUrl + '/auth/facebook/return',
profileFields: ['displayName', 'emails', 'id']
},
function(accessToken, refreshToken, profile, done) {
usedAuthentication("facebook");
done(undefined, profile);
}
));
}

passport.use(new LocalStrategy(

function(username, password, done) {
Expand Down Expand Up @@ -321,6 +336,10 @@ app.get ("/auth/google", passport.authenticate('google'));
app.get ("/auth/google/return", passport.authenticate('google', { successRedirect: '/auth/done', failureRedirect: '/login' }));
app.get ("/auth/done", routes.authDone);

app.get ("/auth/facebook", passport.authenticate('facebook', { scope: ['email'] }));
app.get ("/auth/facebook/return", passport.authenticate('facebook', { successRedirect: '/auth/done', failureRedirect: '/login' }));
app.get ("/auth/done", routes.authDone);

app.all('*', routes.error404);

var listenAddr = process.env.NW_ADDR || "";
Expand Down
15 changes: 12 additions & 3 deletions lib/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,25 @@ var tools = {
return false;
}

if (!pattern || pattern.trim() == "") {
if (!pattern) {
return true;
}

if (!email.match(/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i)) {
return false;
}

var tests = pattern.split(",").map(function(str) { return str.trim(); });
var tests;
if (pattern instanceof Array) {
tests = pattern;
} else if (typeof pattern == "string") {
tests = pattern.split(",").map(function(str) { return str.trim(); });
}

if (tests.length === 0) {
return true;
}

var expr;
for (var i=0; i < tests.length; i++) {
try {
Expand All @@ -45,7 +55,6 @@ var tools = {
}

return expr;

},

hashify: function(str) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"jade": "*",
"passport": "*",
"passport-google": "*",
"passport-facebook": "*",
"passport-local": "*",
"iconv": "*",
"marked": ">= 0.2.x",
Expand Down
4 changes: 3 additions & 1 deletion views/layout.jade
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ html
a(id='login',href='/login?destination', title='Access login page') logged in
else
p.user
if user.email
if user.provider == 'facebook'
img(src="//graph.facebook.com/#{user.id}/picture?type=square", width=32)
else if user.email
img(src=gravatar().url("#{user.email}", {s:24}))
b &nbsp;#{user.displayName}&nbsp;
a(href='/logout', title='Become anonymous') [Logout]
Expand Down
6 changes: 6 additions & 0 deletions views/login.jade
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ block content
mixin saveAndCancel('Login')

hr

if (auth.facebook)
p
b Login using
a.btn.btn-primary(href="/auth/facebook") Facebook login
hr