<commit_before>// External modules.
var _ = require('lodash')
var Passport = require('passport')

module.exports = function (options) {
  var seneca = this
  var pp_auth = {}

  Passport.serializeUser(function (user, done) {
    done(null, user.user.id)
  })

  Passport.deserializeUser(function (id, done) {
    done(null)
  })

  function cmd_register_service (msg, respond) {
    seneca.log.info('registering auth service [' + msg.service + ']')
    Passport.use(msg.service, msg.plugin)
    registerService(msg.service, msg.conf)
    respond()
  }

  function registerService (service, conf) {
    seneca.add({role: 'auth', cmd: 'auth-' + service}, _login_service(service))
    seneca.add({role: 'auth', cmd: 'auth-' + service + '-callback'}, _service_callback(service))

    var map = {}
    map['auth-' + service] = {GET: true, POST: true, alias: '/' + service, responder: _blank_responder}
    map['auth-' + service + '-callback'] = {GET: true, POST: true, alias: '/' + service + '/callback'}

    seneca.act(
      "role:'web'",
      {
        plugin: 'auth',
        config: config,
        use: {
          prefix: options.prefix,
          pin: {role: 'auth', cmd: '*'},
          map: map
        }
      })

    seneca.add({role: 'auth', trigger: 'service-login-' + service}, trigger_service_login)
    configure_services(service, conf)
  }

  function configure_services (service, conf) {
    conf = conf || {}
    var func = null

    pp_auth[service] = function (req, res, next) {
      if (service !== 'local') {
        func = function (err, user, info) {
          if (err) {
            return afterlogin(err, next, req, res)
          }
          seneca.act('role: auth, trigger: service-login-' + service, {service: service, user: user},
            function (err, user) {
              if (err) {
                return afterlogin(err, next, req, res)
              }

              seneca.act('role: user, cmd: login', {nick: user.nick, auto: true}, function (err, out) {
                req.user = out
                afterlogin(err, next, req, res)
              })
            }
          )
        }
      }

      Passport.authenticate(service, conf, func)(req, res, next)
    }
  }

  function buildservice () {
    var pp_init = Passport.initialize()

    function init_session (req, res, done) {
      req.seneca.act("role: 'auth', get: 'token'", {tokenkey: options.tokenkey}, function (err, result) {
        if (err) return done(err)

        var token
        if (result) {
          token = result.token<commit_msg>Revert "fixes for express external auth strategy"<commit_after>// External modules.
var _ = require('lodash')
var Passport = require('passport')

module.exports = function (options) {
  var seneca = this
  var pp_auth = {}

  Passport.serializeUser(function (user, done) {
    done(null, user.user.id)
  })

  Passport.deserializeUser(function (id, done) {
    done(null)
  })

  function cmd_register_service (msg, respond) {
    seneca.log.info('registering auth service [' + msg.service + ']')
    Passport.use(msg.service, msg.plugin)
    registerService(msg.service, msg.conf)
    respond()
  }

  function registerService (service, conf) {
    seneca.add({role: 'auth', cmd: 'auth-' + service}, _login_service(service))
    seneca.add({role: 'auth', cmd: 'auth-' + service + '-callback'}, _service_callback(service))

    var map = {}
    map['auth-' + service] = {GET: true, POST: true, alias: '/' + service, responder: _blank_responder}
    map['auth-' + service + '-callback'] = {GET: true, POST: true, alias: '/' + service + '/callback'}

    seneca.act(
      "role:'web'",
      {
        plugin: 'auth',
        config: config,
        use: {
          prefix: options.prefix,
          pin: {role: 'auth', cmd: '*'},
          map: map
        }
      })

    seneca.add({role: 'auth', trigger: 'service-login-' + service}, trigger_service_login)
    configure_services(service, conf)
  }

  function configure_services (service, conf) {
    conf = conf || {}
    var func = null

    pp_auth[service] = function (req, res, next) {
      if (service !== 'local') {
        func = function (err, user, info) {
          if (err) {
            return afterlogin(err, next, req, res)
          }
          seneca.act("role: 'auth', trigger: 'service-login-' + service", {service: service, user: user},
            function (err, user) {
              if (err) {
                return afterlogin(err, next, req, res)
              }

              seneca.act("role: 'user', cmd: 'login'", {nick: user.nick, auto: true}, function (err, out) {
                req.user = out
                afterlogin(err, next, req, res)
              })
            }
          )
        }
      }

      Passport.authenticate(service, conf, func)(req, res, next)
    }
  }

  function buildservice () {
    var pp_init = Passport.initialize()

    function init_session (req, res, done) {
      req.seneca.act("role: 'auth', get: 'token'", {tokenkey: options.tokenkey}, function (err, result) {
        if (err) return done(err)

        var token
        if (result) {
          token = result.token
{'fraction_non_alphanumeric': 0.15073815073815075, 'fraction_numerical': 0.0, 'mean_word_length': 2.324080051646223, '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': '467567', 'n_tokens_mistral': 1624, 'n_tokens_neox': 1572, 'n_words': 403}