var passport = require('passport');
var wsfederationResponses = require('./wsfederation-responses');
var nconf = require('nconf');

exports.install = function (app) {

  if (nconf.get('AUTHENTICATION') === 'FORM') {
    app.get('/wsfed', 
      function (req, res, next) {
        if (req.session.user && req.query.wprompt !== 'consent') {
          req.user = req.session.user;
          return wsfederationResponses.token(req, res);
        }
        next();
      },
      function (req, res) {
        return res.render('login', {
          title: nconf.get('SITE_NAME'),
          errors: (req.session.messages || []).join('<br />')
        });
      });

    app.post('/wsfed', function (req, res, next) {
        passport.authenticate('WindowsAuthentication', {
          failureRedirect: req.url,
          failureMessage: "The username or password you entered is incorrect.",
          session: false
        })(req, res, next);
      }, function (req, res, next) {
        delete req.session.messages;
        req.session.user = req.user;
        next();
      }, wsfederationResponses.token);

    app.get('/logout', function (req, res) {
      delete req.session;
      res.send('bye');
    });
  }

  app.get('/wsfed/FederationMetadata/2007-06/FederationMetadata.xml',
    wsfederationResponses.metadata());
};
@@ -487,24 +487,131 @@
 req, res) %7B%0A
+        var messages = (req.session.messages %7C%7C %5B%5D).join('%3Cbr /%3E');%0A%0A        delete req.session.messages;%0A%0A
         retu
@@ -698,51 +698,16 @@
 rs: 
-(req.session.messages %7C%7C %5B%5D).join('%3Cbr /%3E')
+messages
 %0A   
@@ -1044,45 +1044,8 @@
 ) %7B%0A
-        delete req.session.messages;%0A
     

fix problem with errors in session