Skip to content

Commit

Permalink
Adding OAuth2 & SAML authentication, fixes #10, & fixes #12
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Aug 26, 2014
1 parent c7e20eb commit ec46be0
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change Log

## 0.7.0
- Added `OAuth2`, & `SAML` authentication

## 0.6.1
- Upgrading turtle.io to 3.0.15 for etag middleware fix (out of order execution negated it)

Expand Down
12 changes: 12 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,23 @@
"client_secret": "",
"scope": null
},
"oauth2": {
"enabled": false,
"auth": null,
"auth_url": "",
"token_url": "",
"client_id": "",
"client_secret": ""
},
"local": {
"enabled": false,
"auth": null,
"middleware": null
},
"saml": {
"enabled": false,
"auth": null
},
"twitter": {
"enabled": false,
"auth": null,
Expand Down
57 changes: 53 additions & 4 deletions lib/tenso.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
* @license BSD-3 <https://raw.github.com/avoidwork/tenso/master/LICENSE>
* @link http://avoidwork.github.io/tenso
* @module tenso
* @version 0.6.0
* @version 0.6.1
*/
( function () {
"use strict";

var turtleio = require( "turtle.io" ),
SERVER = "tenso/0.6.0",
SERVER = "tenso/0.6.1",
CONFIG = require( __dirname + "/../config.json" ),
keigai = require( "keigai" ),
util = keigai.util,
Expand All @@ -31,6 +31,8 @@ var turtleio = require( "turtle.io" ),
FacebookStrategy = require( "passport-facebook" ).Strategy,
GoogleStrategy = require( "passport-google" ).Strategy,
LinkedInStrategy = require( "passport-linkedin" ).Strategy,
OAuth2Strategy = require( "passport-oauth2" ).Strategy,
SAMLStrategy = require( "passport-saml" ).Strategy,
TwitterStrategy = require( "passport-twitter" ).Strategy,
RedisStore = require( "connect-redis" )( session ),
REGEX_HYPERMEDIA = /_(guid|uuid|id|url|uri)$/,
Expand All @@ -52,7 +54,7 @@ function Tenso () {
this.rates = {};
this.server = turtleio();
this.server.tenso = this;
this.version = "0.6.0";
this.version = "0.6.1";
}

/**
Expand Down Expand Up @@ -457,6 +459,53 @@ function auth ( obj, config ) {
obj.server.get( "/auth/linkedin/callback", redirect );
}

if ( config.auth.oauth2.enabled ) {
passport.use( new OAuth2Strategy( {
authorizationURL: config.auth.oauth2.auth_url,
tokenURL : config.auth.oauth2.token_url,
clientID : config.auth.oauth2.client_id,
clientSecret : config.auth.oauth2.client_secret,
callbackURL : realm + "/auth/oauth2/callback"
}, function ( accessToken, refreshToken, profile, done ) {
config.auth.oauth2.auth( accessToken, refreshToken, profile, function ( err, user ) {
if ( err ) {
return done( err );
}

done( null, user );
} );
}
) );

obj.server.get( "/auth/oauth2", passport.authenticate( "oauth2" ) );
obj.server.get( "/auth/oauth2/callback", passport.authenticate( "oauth2", {failureRedirect: "/login"} ) );
obj.server.get( "/auth/oauth2/callback", redirect );
}

if ( config.auth.saml.enabled ) {
( function () {
var config = config.auth.saml;

config.callbackURL = realm + "/auth/saml/callback";
delete config.enabled;

passport.use( new SAMLStrategy( config, function ( accessToken, refreshToken, profile, done ) {
config.auth.saml.auth( accessToken, refreshToken, profile, function ( err, user ) {
if ( err ) {
return done( err );
}

done( null, user );
} );
}
) );
} )();

obj.server.get( "/auth/saml", passport.authenticate( "saml" ) );
obj.server.get( "/auth/saml/callback", passport.authenticate( "saml", {failureRedirect: "/login"} ) );
obj.server.get( "/auth/saml/callback", redirect );
}

if ( config.auth.twitter.enabled ) {
passport.use( new TwitterStrategy( {
consumerKey : config.auth.twitter.consumer_key,
Expand Down Expand Up @@ -493,7 +542,7 @@ function auth ( obj, config ) {
} )();
}

config.routes.get["/login"] = {login_uri: "/auth"};
config.routes.get["/login"] = config.auth.saml.enabled ? {login_uri: "/auth", instruction: "POST username/password to authenticate"} : {login_uri: "/auth"};
config.routes.get["/logout"] = function ( req, res ) {
if ( req.session.authorized || req.session.isAuthorized() ) {
req.session.destroy();
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@
"cookie-parser": "1.3.2",
"express-session": "^1.7.2",
"passport": "0.2.0",
"passport-oauth2": "1.1.2",
"passport-facebook": "1.0.3",
"passport-google": "0.3.0",
"passport-http": "0.2.2",
"passport-http-bearer": "1.0.1",
"passport-linkedin": "0.1.3",
"passport-saml": "0.5.2",
"passport-twitter": "1.0.2",
"connect-redis": "2.0.0",
"lusca": "1.0.1"
Expand Down
49 changes: 48 additions & 1 deletion src/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,53 @@ function auth ( obj, config ) {
obj.server.get( "/auth/linkedin/callback", redirect );
}

if ( config.auth.oauth2.enabled ) {
passport.use( new OAuth2Strategy( {
authorizationURL: config.auth.oauth2.auth_url,
tokenURL : config.auth.oauth2.token_url,
clientID : config.auth.oauth2.client_id,
clientSecret : config.auth.oauth2.client_secret,
callbackURL : realm + "/auth/oauth2/callback"
}, function ( accessToken, refreshToken, profile, done ) {
config.auth.oauth2.auth( accessToken, refreshToken, profile, function ( err, user ) {
if ( err ) {
return done( err );
}

done( null, user );
} );
}
) );

obj.server.get( "/auth/oauth2", passport.authenticate( "oauth2" ) );
obj.server.get( "/auth/oauth2/callback", passport.authenticate( "oauth2", {failureRedirect: "/login"} ) );
obj.server.get( "/auth/oauth2/callback", redirect );
}

if ( config.auth.saml.enabled ) {
( function () {
var config = config.auth.saml;

config.callbackURL = realm + "/auth/saml/callback";
delete config.enabled;

passport.use( new SAMLStrategy( config, function ( accessToken, refreshToken, profile, done ) {
config.auth.saml.auth( accessToken, refreshToken, profile, function ( err, user ) {
if ( err ) {
return done( err );
}

done( null, user );
} );
}
) );
} )();

obj.server.get( "/auth/saml", passport.authenticate( "saml" ) );
obj.server.get( "/auth/saml/callback", passport.authenticate( "saml", {failureRedirect: "/login"} ) );
obj.server.get( "/auth/saml/callback", redirect );
}

if ( config.auth.twitter.enabled ) {
passport.use( new TwitterStrategy( {
consumerKey : config.auth.twitter.consumer_key,
Expand Down Expand Up @@ -329,7 +376,7 @@ function auth ( obj, config ) {
} )();
}

config.routes.get["/login"] = {login_uri: "/auth"};
config.routes.get["/login"] = config.auth.saml.enabled ? {login_uri: "/auth", instruction: "POST username/password to authenticate"} : {login_uri: "/auth"};
config.routes.get["/logout"] = function ( req, res ) {
if ( req.session.authorized || req.session.isAuthorized() ) {
req.session.destroy();
Expand Down
2 changes: 2 additions & 0 deletions src/intro.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ var turtleio = require( "turtle.io" ),
FacebookStrategy = require( "passport-facebook" ).Strategy,
GoogleStrategy = require( "passport-google" ).Strategy,
LinkedInStrategy = require( "passport-linkedin" ).Strategy,
OAuth2Strategy = require( "passport-oauth2" ).Strategy,
SAMLStrategy = require( "passport-saml" ).Strategy,
TwitterStrategy = require( "passport-twitter" ).Strategy,
RedisStore = require( "connect-redis" )( session ),
REGEX_HYPERMEDIA = /_(guid|uuid|id|url|uri)$/,
Expand Down
1 change: 0 additions & 1 deletion www/css/style.css
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
html,body{min-height:100%}body{background:#fffff;color:#00000;font:12px Arial;margin:10px}
/*# sourceMappingURL=style.css.map */
7 changes: 0 additions & 7 deletions www/css/style.css.map

This file was deleted.

0 comments on commit ec46be0

Please sign in to comment.