<commit_before>var Joi = require('joi');
var Hoek = require('hoek');
var async = require('async');
var bcrypt = require('bcrypt');
var config = require('../../config');


exports.register = function (plugin, options, next) {

    options = Hoek.applyToDefaults({ basePath: '' }, options);


    plugin.route({
        method: 'POST',
        path: options.basePath + '/login',
        config: {
            validate: {
                payload: {
                    username: Joi.string().token().required(),
                    password: Joi.string().required()
                }
            },
            pre: [{
                assign: 'abuseDetected',
                method: function (request, reply) {

                    var AuthAttempt = request.server.plugins.models.AuthAttempt;
                    var ip = request.info.remoteAddress;
                    var username = request.payload.username;

                    AuthAttempt.abuseDetected(ip, username, function (err, detected) {

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

                        if (detected) {
                            return reply({
                                message: 'Maximum number of auth attempts reached. Please try again later.'
                            }).takeover().code(400);
                        }

                        reply();
                    });
                }
            },{
                assign: 'user',
                method: function (request, reply) {

                    var User = request.server.plugins.models.User;
                    var username = request.payload.username;
                    var password = request.payload.password;

                    User.findByCredentials(username, password, function (err, user) {

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

                        reply(user);
                    });
                }
            },{
                assign: 'logAttempt',
                method: function (request, reply) {

                    if (request.pre.user) {
                        return reply();
                    }

                    var AuthAttempt = request.server.plugins.models.AuthAttempt;
                    var ip = request.info.remoteAddress;
                    var username = request.payload.username;

                    AuthAttempt.create(ip, username, function (err, authAttempt) {

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

                        return reply({
                            message: 'Username and password combination not found or account is inactive.'
                        }).takeover().code(400);
                    });
                }
            },{
                assign: 'session',
                method: function (request, reply) {

                    var Session = request.server.plugins.models.Session;

                    Session.create(request.payload.username, function (err, session) {

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

                        return reply(session);
                    });
                }
            }]
        },
        handler: function (request, reply) {

            reply({
                user: {
                    _id: request.pre.user._id,
                    username: request.pre.user.username,
                    email: request.pre.user.email,
                    roles: request.pre.user.roles
                },
                session: request.pre.session
            });
        }
    });


    plugin.route({
        method: 'POST',<commit_msg>added convenience auth header to login response<commit_after>var Joi = require('joi');
var Hoek = require('hoek');
var async = require('async');
var bcrypt = require('bcrypt');
var config = require('../../config');


exports.register = function (plugin, options, next) {

    options = Hoek.applyToDefaults({ basePath: '' }, options);


    plugin.route({
        method: 'POST',
        path: options.basePath + '/login',
        config: {
            validate: {
                payload: {
                    username: Joi.string().token().required(),
                    password: Joi.string().required()
                }
            },
            pre: [{
                assign: 'abuseDetected',
                method: function (request, reply) {

                    var AuthAttempt = request.server.plugins.models.AuthAttempt;
                    var ip = request.info.remoteAddress;
                    var username = request.payload.username;

                    AuthAttempt.abuseDetected(ip, username, function (err, detected) {

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

                        if (detected) {
                            return reply({
                                message: 'Maximum number of auth attempts reached. Please try again later.'
                            }).takeover().code(400);
                        }

                        reply();
                    });
                }
            },{
                assign: 'user',
                method: function (request, reply) {

                    var User = request.server.plugins.models.User;
                    var username = request.payload.username;
                    var password = request.payload.password;

                    User.findByCredentials(username, password, function (err, user) {

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

                        reply(user);
                    });
                }
            },{
                assign: 'logAttempt',
                method: function (request, reply) {

                    if (request.pre.user) {
                        return reply();
                    }

                    var AuthAttempt = request.server.plugins.models.AuthAttempt;
                    var ip = request.info.remoteAddress;
                    var username = request.payload.username;

                    AuthAttempt.create(ip, username, function (err, authAttempt) {

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

                        return reply({
                            message: 'Username and password combination not found or account is inactive.'
                        }).takeover().code(400);
                    });
                }
            },{
                assign: 'session',
                method: function (request, reply) {

                    var Session = request.server.plugins.models.Session;

                    Session.create(request.payload.username, function (err, session) {

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

                        return reply(session);
                    });
                }
            }]
        },
        handler: function (request, reply) {

            var credentials = request.pre.user.username + ':' + request.pre.session.key;
            var authHeader = 'Basic ' + new Buffer(credentials).toString('base64');

            reply({
                user: {
                    _id: request.pre.user._id,
                    username: request.pre.user.username,
                    email: request.pre.user.email,
                    roles: request.pre.user.roles
                },
                session: request.pre.session,
                authHeader: authHeader
            });
        }
    });


    plugin.route({
        method: 'POST',
{'fraction_non_alphanumeric': 0.0989556800815079, 'fraction_numerical': 0.001782985226693836, 'mean_word_length': 0.9936532114749936, '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': '3058397', 'n_tokens_mistral': 1827, 'n_tokens_neox': 1615, 'n_words': 384}