<commit_before>/**
 * SAML wrapper to make it compatible with hapijs
 */
"use strict";

var Boom = require('boom');

var Saml = require('./saml');

var wrapper = {};

wrapper.authenticate = function(options) {
  var saml = new Saml.SAML(options);

  options.samlFallback = options.samlFallback || 'login-request';

  // Optional verify function.
  // Returns profile as is by default
  options.verifyFunc = options.verifyFunc || function(profile, done) {
    return done(null, profile);
  };

  return function(request, reply) {
    function validateCallback(err, profile, loggedOut) {
      console.log(['debug', 'validateCallback'], profile);

      if (err) {
        return reply(Boom.internal(err));
      }

      if (loggedOut) {
        console.logout();
        if (profile) {
          request.samlLogoutRequest = profile;
          return saml.getLogoutResponseUrl(request, redirectIfSuccess);
        }
        return reply();
      }

      var verified = function (err, user, info) {
        console.log(['debug', 'verified', 'user'], user);
        console.log(['debug', 'verified', 'info'], info);

        if (err) {
          return reply(err);
        }

        if (!user) {
          return reply(Boom.badRequest(info));
        }

        reply(null, {
          credentials: user,
          info: info
        });
      };

      // Client verification of whether profile is
      // valid or not.
      console.log(['debug', 'pre-verifyFunc'], profile);
      request.info = request.info || {};
      request.info.test = "Testing";
      options.verifyFunc(profile, verified);
    }

    function redirectIfSuccess(err, url) {
      console.log(['debug', 'redirectIfSuccess'], url);

      if (err) {
        return reply(Boom.internal(err));
      } else {<commit_msg>Add support of saving user credentials to session<commit_after>/**
 * SAML wrapper to make it compatible with hapijs
 */
"use strict";

var Boom = require('boom');

var Saml = require('./saml');

var wrapper = {};

wrapper.authenticate = function(options) {
  var saml = new Saml.SAML(options);

  options.samlFallback = options.samlFallback || 'login-request';

  // Optional verify function.
  // Returns profile as is by default
  options.verifyFunc = options.verifyFunc || function(profile, done) {
    return done(null, profile);
  };

  return function(request, reply) {
    function validateCallback(err, profile, loggedOut) {
      console.log(['debug', 'validateCallback'], profile);

      if (err) {
        return reply(Boom.internal(err));
      }

      if (loggedOut) {
        console.logout();
        if (profile) {
          request.samlLogoutRequest = profile;
          return saml.getLogoutResponseUrl(request, redirectIfSuccess);
        }
        return reply();
      }

      var verified = function (err, user, info) {
        console.log(['debug', 'verified', 'user'], user);
        console.log(['debug', 'verified', 'info'], info);

        if (err) {
          return reply(err);
        }

        if (!user) {
          return reply(Boom.badRequest(info));
        }

        request.auth.session.set(user);
        return reply.redirect('/');
      };

      // Client verification of whether profile is
      // valid or not.
      console.log(['debug', 'pre-verifyFunc'], profile);
      request.info = request.info || {};
      request.info.test = "Testing";
      options.verifyFunc(profile, verified);
    }

    function redirectIfSuccess(err, url) {
      console.log(['debug', 'redirectIfSuccess'], url);

      if (err) {
        return reply(Boom.internal(err));
      } else {
{'fraction_non_alphanumeric': 0.14111787493082456, 'fraction_numerical': 0.0, 'mean_word_length': 2.4793070259865253, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2079923', 'n_tokens_mistral': 1053, 'n_tokens_neox': 1023, 'n_words': 257}