<commit_before>/**
 * Module dependencies.
 */

var express = require('express');
var http = require('http');
var path = require('path');
var partials = require('express-partials');
var passport = require('passport');
// npm passport-openidconnect package is not up to date
var OpenidConnectStrategy = require('passport-openidconnect').Strategy;

var RP_DOMAIN_NAME = 'localhost.airydrive.org'; //'--insert-your-relying-party-domain-name-here--'

var CLIENT_NAME = 'AiryDrive - test2';
// your callback url
var CALLBACK_URL = '/auth/oidc/callback';
// your callback uris
var REDIRECT_URIS = ['http://' + RP_DOMAIN_NAME + CALLBACK_URL];
// scopes
var SCOPE = 'profile email';

// Configuration database with oidc and user tables
var Config = {oidc: [], user: []};

/* uncomment - if you want initialize Configuration database with Mitreid's data sample
// register a new client at e.g. https://mitreid.org/manage/dev/dynreg
var OP_DOMAIN_NAME = 'mitreid.org'; //'--insert-your-openid-provider-domain-name-here--'

var CLIENT_ID = 'abf911b1-47ba-4163-881d-2b995cba8a1d';//'--insert-oidc-client-id-here--';
var CLIENT_SECRET = '<KEY>';//'--insert-oidc-client-secret-here--';
 // OpenID Connect Provider endpoints e.g. from https://mitreid.org/.well-known/openid-configuration/
var AUTHORIZATION_URL = 'https://' + OP_DOMAIN_NAME + '/authorize';
var TOKEN_URL = 'https://' + OP_DOMAIN_NAME + '/token';
var USER_INFO_URL = 'https://' + OP_DOMAIN_NAME + '/userinfo';

Config.oidc.push({id: 1, provider: {
  issuer: 'https://' + OP_DOMAIN_NAME + '/',
  authorizationURL: AUTHORIZATION_URL,
  tokenURL: TOKEN_URL,
  userInfoURL: USER_INFO_URL,
  clientID: CLIENT_ID
}, reg: {
  clientSecret: CLIENT_SECRET
}});
*/

passport.serializeUser(function(user, done) {
    done(null, user);
});

passport.deserializeUser(function(user, done) {
    done(null, user);
});

function userFindOrCreate(issuer, sub, userInfo, provider, done) {
  var email = userInfo && userInfo.email ? userInfo.email : null;
  if (email) {
    findUserByEmail(email, function(issuer, userInfo, err, user) {
      if (!user) {
        var user_id = Config.user.length + 1;
        user = {id: user_id, oidc_id: null, sub: userInfo.sub, email: email, displayName: userInfo.displayName};
        Config.user.push(user);
      }
      findOidcByIssuer(issuer, function(user, err, oidc) {
        if (oidc) {
          user.oidc_id = oidc.id;
          done(null, user);
        } else {
          done('Cannot select oidc configuration', user);
        }
      }.bind(this, user));
    }.bind(this, issuer, userInfo));
  } else {
    done('Cannot select user', null);
  }
}

function findOidcById(id, fn) {
  for (var i = 0, len = Config.oidc.length; i < len; i++) {
    var oidc = Config.oidc[i];
    if (oidc.id === id) {
      return fn(null, oidc);
    }
  }
  return fn(null, null);
}

function findOidcByIssuer(iss, fn) {
  for (var i = 0, len = Config.oidc.length; i < len; i++) {
    var oidc = Config.oidc[i];
    if (oidc.provider.issuer === iss) {
      return fn(null, oidc);
    }
  }
  return fn(null, null);
}

function findUserByEmail(email, fn) {
  for (var i = 0, len = Config.user.length; i < len; i++) {
    var user = Config.user[i];
    if (user.email === email) {
      return fn(null, user);
    }
  }<commit_msg>issuer - remove trailing slashes<commit_after>/**
 * Module dependencies.
 */

var express = require('express');
var http = require('http');
var path = require('path');
var partials = require('express-partials');
var passport = require('passport');
// npm passport-openidconnect package is not up to date
var OpenidConnectStrategy = require('passport-openidconnect').Strategy;

var RP_DOMAIN_NAME = 'localhost.airydrive.org'; //'--insert-your-relying-party-domain-name-here--'

var CLIENT_NAME = 'AiryDrive - test2';
// your callback url
var CALLBACK_URL = '/auth/oidc/callback';
// your callback uris
var REDIRECT_URIS = ['http://' + RP_DOMAIN_NAME + CALLBACK_URL];
// scopes
var SCOPE = 'profile email';

// Configuration database with oidc and user tables
var Config = {oidc: [], user: []};

/* uncomment - if you want initialize Configuration database with Mitreid's data sample
// register a new client at e.g. https://mitreid.org/manage/dev/dynreg
var OP_DOMAIN_NAME = 'mitreid.org'; //'--insert-your-openid-provider-domain-name-here--'

var CLIENT_ID = 'abf911b1-47ba-4163-881d-2b995cba8a1d';//'--insert-oidc-client-id-here--';
var CLIENT_SECRET = '<KEY>';//'--insert-oidc-client-secret-here--';
 // OpenID Connect Provider endpoints e.g. from https://mitreid.org/.well-known/openid-configuration/
var AUTHORIZATION_URL = 'https://' + OP_DOMAIN_NAME + '/authorize';
var TOKEN_URL = 'https://' + OP_DOMAIN_NAME + '/token';
var USER_INFO_URL = 'https://' + OP_DOMAIN_NAME + '/userinfo';

Config.oidc.push({id: 1, provider: {
  issuer: 'https://' + OP_DOMAIN_NAME + '/',
  authorizationURL: AUTHORIZATION_URL,
  tokenURL: TOKEN_URL,
  userInfoURL: USER_INFO_URL,
  clientID: CLIENT_ID
}, reg: {
  clientSecret: CLIENT_SECRET
}});
*/

function removeTrailingSlashes(str)
{
  return str.replace(/\/+$/, "");
}

passport.serializeUser(function(user, done) {
    done(null, user);
});

passport.deserializeUser(function(user, done) {
    done(null, user);
});

function userFindOrCreate(issuer, sub, userInfo, provider, done) {
  var email = userInfo && userInfo.email ? userInfo.email : null;
  if (email) {
    findUserByEmail(email, function(issuer, userInfo, err, user) {
      if (!user) {
        var user_id = Config.user.length + 1;
        user = {id: user_id, oidc_id: null, sub: userInfo.sub, email: email, displayName: userInfo.displayName};
        Config.user.push(user);
      }
      findOidcByIssuer(issuer, function(user, err, oidc) {
        if (oidc) {
          user.oidc_id = oidc.id;
          done(null, user);
        } else {
          done('Cannot select oidc configuration', user);
        }
      }.bind(this, user));
    }.bind(this, issuer, userInfo));
  } else {
    done('Cannot select user', null);
  }
}

function findOidcById(id, fn) {
  for (var i = 0, len = Config.oidc.length; i < len; i++) {
    var oidc = Config.oidc[i];
    if (oidc.id === id) {
      return fn(null, oidc);
    }
  }
  return fn(null, null);
}

function findOidcByIssuer(iss, fn) {
  for (var i = 0, len = Config.oidc.length; i < len; i++) {
    var oidc = Config.oidc[i];
    if (removeTrailingSlashes(oidc.provider.issuer) === removeTrailingSlashes(iss)) {
      return fn(null, oidc);
    }
  }
  return fn(null, null);
}

function findUserByEmail(email, fn) {
  for (var i = 0, len = Config.user.length; i < len; i++) {
    var user = Config.user[i];
    if (user.email === email) {
      return fn(null, user);
    }
  }
{'fraction_non_alphanumeric': 0.16072489601901366, 'fraction_numerical': 0.0074272133095662505, 'mean_word_length': 3.4383651944627553, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 12, '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': '2626690', 'n_tokens_mistral': 2466, 'n_tokens_neox': 2391, 'n_words': 555}