Permalink
Browse files

SAML strategy

  • Loading branch information...
1 parent e71c638 commit c2853451b492bedcae49d251e212d09da461ed80 @gbraad committed Sep 15, 2014
Showing with 343 additions and 2 deletions.
  1. +3 −0 .bowerrc
  2. +10 −0 .drone.yml
  3. +3 −0 .gitignore
  4. +9 −0 .travis.yml
  5. +1 −0 Procfile
  6. +38 −2 README.md
  7. +67 −0 app.js
  8. +4 −0 app/views/404.jade
  9. +8 −0 app/views/500.jade
  10. +6 −0 app/views/error.jade
  11. +12 −0 app/views/home.jade
  12. +12 −0 app/views/layout.jade
  13. +12 −0 app/views/profile.jade
  14. +24 −0 bower.json
  15. +16 −0 config/config.js
  16. +31 −0 config/passport.js
  17. +55 −0 config/routes.js
  18. +32 −0 package.json
View
@@ -0,0 +1,3 @@
+{
+ "directory" : "public/bower_components"
+}
View
@@ -0,0 +1,10 @@
+image: bradrydzewski/node:0.10
+#env:
+script:
+ - npm install
+ - npm test
+#services:
+notify:
+ email:
+ recipients:
+ - me@gbraad.nl
View
@@ -0,0 +1,3 @@
+/node_modules
+/bower_components
+/public
View
@@ -0,0 +1,9 @@
+language: node_js
+node_js:
+ - "0.10"
+
+before_install:
+ - npm install -g npm
+
+script:
+ - npm test
View
@@ -0,0 +1 @@
+web: npm start
View
@@ -1,3 +1,39 @@
-PassportJS SAML-example
------------------------
+PassportJS SAML example
+=======================
+[![Build Status](https://travis-ci.org/gbraad/passport-saml-example.svg?branch=master)](https://travis-ci.org/gbraad/passport-saml-example)
+[![Code Climate](https://codeclimate.com/github/gbraad/passport-saml-example/badges/gpa.svg)](https://codeclimate.com/github/gbraad/passport-saml-example)
+
+
+Description
+-----------
+
+An example to demonstrate the use of the [Passport-SAML](https://github.com/bergie/passport-saml) Authentication provider for PassportJS.
+
+
+Usage
+-----
+
+```bash
+$ npm install
+$ bower install
+$ npm start
+```
+
+
+Authors
+-------
+
+Gerard Braad <me@gbraad.nl>
+
+
+License
+-------
+
+Licensed under the MIT license
+
+
+Note
+----
+
+Based on [PassportJS-Authentication](https://github.com/DanialK/PassportJS-Authentication) by [Danial Khosravi](http://danialk.github.io/)
View
@@ -0,0 +1,67 @@
+var express = require('express'),
+ http = require('http'),
+ path = require('path'),
+ passport = require("passport");
+
+var env = process.env.NODE_ENV || 'development',
+ config = require('./config/config')[env];
+
+require('./config/passport')(passport, config);
+
+
+var app = express();
+
+app.configure(function () {
+ app.set('port', config.app.port);
+ app.set('views', __dirname + '/app/views');
+ app.set('view engine', 'jade');
+ app.use(express.logger('dev'));
+ app.use(express.cookieParser());
+ app.use(express.bodyParser());
+ app.use(express.session(
+ {
+ secret: 'this shit hits'
+ }));
+ app.use(passport.initialize());
+ app.use(passport.session());
+ app.use(express.methodOverride());
+ app.use(app.router);
+ app.use(express.static(path.join(__dirname, 'public')));
+});
+
+app.configure('development', function () {
+ console.log ("Development mode.");
+ app.use(express.errorHandler());
+});
+app.configure ('production', function () {
+ console.log ("Production mode.");
+});
+
+app.use(function(err, req, res, next) {
+ res.status(err.status || 500);
+ res.render('500', { error: err });
+});
+
+app.use(function(req, res, next){
+ res.status(404);
+ if (req.accepts('html')) {
+ res.render('404',
+ {
+ url : req.url
+ });
+ return;
+ }
+ if (req.accepts('json')) {
+ res.send({ error: 'Not found' });
+ return;
+ }
+ res.type('txt').send('Not found');
+});
+
+
+require('./config/routes')(app, config, passport);
+
+
+http.createServer(app).listen(app.get('port'), function () {
+ console.log("Express server listening on port " + app.get('port'));
+});
View
@@ -0,0 +1,4 @@
+extends error
+
+block content
+ h2 Cannot find #{url}
View
@@ -0,0 +1,8 @@
+extends error
+
+block content
+ h1 Error: #{error.message}
+ if settings['verbose errors']
+ pre= error.stack
+ else
+ p An error ocurred!
@@ -0,0 +1,6 @@
+html
+ head
+ title Error
+ body
+ h1 An error occurred!
+ block content
View
@@ -0,0 +1,12 @@
+extends layout
+
+block content
+ if user !== null
+ h1 Hello, #{user.firstName}
+ a(href="/profile") Profile
+ br
+ a(href="/logout") Logout
+ else
+ h1 Welcome
+ br
+ a(href="/login") Login
View
@@ -0,0 +1,12 @@
+doctype html
+html
+ head
+ title PassportJS SAML example
+ block links
+ link(rel='stylesheet', href='bower_components/bootstrap/dist/css/bootstrap.css')
+ body
+ div.container
+ block content
+ script(src='bower_components/jquery/jquery.min.js')
+ script(src='bower_components/bootstrap/dist/js/bootstrap.min.js')
+ block scripts
@@ -0,0 +1,12 @@
+extends layout
+
+block content
+ h1 Profile
+
+ dl
+ dt Display name
+ dd #{user.displayName}
+ dt Email
+ dd #{user.email}
+
+ a(href="/logout") Logout
View
@@ -0,0 +1,24 @@
+{
+ "name": "passport-saml-example",
+ "version": "0.0.1",
+ "homepage": "https://github.com/gbraad/passport-saml-example",
+ "authors": [
+ "Gerard Braad <me@gbraad.nl>"
+ ],
+ "description": "Example for PassportJS SAML strategy",
+ "main": "app.js",
+ "moduleType": [
+ "node"
+ ],
+ "license": "MIT",
+ "ignore": [
+ "**/.*",
+ "node_modules",
+ "bower_components",
+ "test",
+ "tests"
+ ],
+ "dependencies": {
+ "bootstrap": "~3.2.0"
+ }
+}
View
@@ -0,0 +1,16 @@
+module.exports = {
+ development : {
+ app : {
+ name : 'Passport SAML strategy example',
+ port : process.env.PORT || 3000
+ },
+ passport: {
+ strategy : 'saml',
+ saml : {
+ path : '/login/callback',
+ entryPoint : 'https://openidp.feide.no/simplesaml/saml2/idp/SSOService.php',
+ issuer : 'passport-saml'
+ }
+ }
+ }
+}
View
@@ -0,0 +1,31 @@
+var SamlStrategy = require('passport-saml').Strategy
+
+module.exports = function (passport, config) {
+
+ passport.serializeUser(function(user, done) {
+ done(null, user);
+ });
+
+ passport.deserializeUser(function(user, done) {
+ done(null, user);
+ });
+
+ passport.use(new SamlStrategy(
+ {
+ path: config.passport.saml.path,
+ entryPoint: config.passport.saml.entryPoint,
+ issuer: config.passport.saml.issuer
+ },
+ function(profile, done) {
+ return done(null,
+ {
+ id : profile.uid,
+ email : profile.email,
+ displayName : profile.cn,
+ firstName : profile.givenName,
+ lastName : profile.sn
+ });
+ })
+ );
+
+}
View
@@ -0,0 +1,55 @@
+module.exports = function(app, config, passport) {
+ app.get("/", function(req, res) {
+ if(req.isAuthenticated()) {
+ res.render("home",
+ {
+ user : req.user
+ });
+ } else {
+ res.render("home",
+ {
+ user : null
+ });
+ }
+ });
+
+ app.get("/login",
+ passport.authenticate(config.passport.strategy,
+ {
+ successRedirect : "/",
+ failureRedirect : "/login",
+ })
+ );
+
+ app.post('/login/callback',
+ passport.authenticate(config.passport.strategy,
+ {
+ failureRedirect: '/',
+ failureFlash: true
+ }),
+ function(req, res) {
+ res.redirect('/');
+ }
+ );
+
+ app.get("/signup", function (req, res) {
+ res.render("signup");
+ });
+
+ app.get("/profile", function(req, res) {
+ if(req.isAuthenticated()){
+ res.render("profile",
+ {
+ user : req.user
+ });
+ } else {
+ res.redirect("/login");
+ }
+ });
+
+ app.get('/logout', function(req, res) {
+ req.logout();
+ // TODO: invalidate session on IP
+ res.redirect('/');
+ });
+}
View
@@ -0,0 +1,32 @@
+{
+ "name": "passport-saml-example",
+ "version": "0.0.1",
+ "description": "Example for PassportJS SAML strategy",
+ "author": {
+ "name": "Gerard Braad",
+ "email": "me@gbraad.nl",
+ "url": "http://gbraad.nl"
+ },
+ "keywords": [
+ "saml",
+ "sso"
+ ],
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/gbraad/passport-saml-example.git"
+ },
+ "scripts": {
+ "start": "nodemon app.js",
+ "test": ""
+ },
+ "dependencies": {
+ "express": "3.1.0",
+ "jade": "*",
+ "nodemon": "*",
+ "passport": "*",
+ "passport-saml": "*"
+ },
+ "engines": {
+ "node": ">= 0.10.0"
+ }
+}

0 comments on commit c285345

Please sign in to comment.