<commit_before>var boom = require('boom')
var braveHapi = require('../brave-hapi')
var GitHub = require('github')
var Joi = require('joi')

var v1 = {}

/*
   GET /v1/login
 */

v1.login =
{ handler: function (runtime) {
  return async function (request, reply) {
    var debug = braveHapi.debug(module, request)
    var credentials = request.auth.credentials

    if (!request.auth.isAuthenticated) return reply(boom.forbidden())

    var gh = new GitHub({ version: '3.0.0', debug: false })
    gh.authenticate({ type: 'token', token: credentials.token })
    gh.user.getTeams({}, function (err, data) {
      if (err) return reply('Oops!')

      credentials.scope = []
      data.forEach(team => {
        if (team.organization.login === runtime.login.organization) credentials.scope.push(team.name)
      })
      if (credentials.scope.length === 0) {
        debug('failed ' + credentials.provider + ' ' + credentials.profile.email)
        return reply(boom.forbidden())
      }

      debug('login  ' + credentials.provider + ' ' + credentials.profile.email + ': ' + JSON.stringify(credentials.scope))

      request.auth.session.set(credentials)
      reply.redirect(runtime.login.world)
    })
  }
},

  auth: 'github',

  description: 'Logs the user into management operations',
  notes: 'This operation authenticates an administrative role ("devops") for the vault. The user is asked to authenticate their GitHub identity, and are assigned permissions based on team-membership. Operations are henceforth authenticated via an encrypted session cookie.',
  tags: [ 'api' ],

  validate:
    { query:
      { code: Joi.string().optional().description('an opaque string identifying an oauth flow'),
        state: Joi.string().optional().description('an opaque string')
      }
    }
}

/*
   GET /v1/logout
 */

v1.logout =
{ handler: function (runtime) {
  return async function (request, reply) {
    var debug = braveHapi.debug(module, request)
    var credentials = request.auth.credentials<commit_msg>update notes for login operation<commit_after>var boom = require('boom')
var braveHapi = require('../brave-hapi')
var GitHub = require('github')
var Joi = require('joi')

var v1 = {}

/*
   GET /v1/login
 */

v1.login =
{ handler: function (runtime) {
  return async function (request, reply) {
    var debug = braveHapi.debug(module, request)
    var credentials = request.auth.credentials

    if (!request.auth.isAuthenticated) return reply(boom.forbidden())

    var gh = new GitHub({ version: '3.0.0', debug: false })
    gh.authenticate({ type: 'token', token: <PASSWORD>.token })
    gh.user.getTeams({}, function (err, data) {
      if (err) return reply('Oops!')

      credentials.scope = []
      data.forEach(team => {
        if (team.organization.login === runtime.login.organization) credentials.scope.push(team.name)
      })
      if (credentials.scope.length === 0) {
        debug('failed ' + credentials.provider + ' ' + credentials.profile.email)
        return reply(boom.forbidden())
      }

      debug('login  ' + credentials.provider + ' ' + credentials.profile.email + ': ' + JSON.stringify(credentials.scope))

      request.auth.session.set(credentials)
      reply.redirect(runtime.login.world)
    })
  }
},

  auth: 'github',

  description: 'Logs the user into management operations',
  notes: 'This operation authenticates an administrative role for the server. The user is asked to authenticate their GitHub identity, and are assigned permissions based on team-membership. Operations are henceforth authenticated via an encrypted session cookie.',
  tags: [ 'api' ],

  validate:
    { query:
      { code: Joi.string().optional().description('an opaque string identifying an oauth flow'),
        state: Joi.string().optional().description('an opaque string')
      }
    }
}

/*
   GET /v1/logout
 */

v1.logout =
{ handler: function (runtime) {
  return async function (request, reply) {
    var debug = braveHapi.debug(module, request)
    var credentials = request.auth.credentials
{'fraction_non_alphanumeric': 0.13627353815659068, 'fraction_numerical': 0.0044598612487611496, 'mean_word_length': 3.359611231101512, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, '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': '5596860', 'n_tokens_mistral': 1215, 'n_tokens_neox': 1170, 'n_words': 314}