<commit_before>/**
 * Part of the evias/nem-nodejs-bot package.
 *
 * NOTICE OF LICENSE
 *
 * Licensed under MIT License.
 *
 * This source file is subject to the MIT License that is
 * bundled with this package in the LICENSE file.
 *
 * @package    evias/nem-nodejs-bot
 * @author     <NAME> <<EMAIL>> (https://github.com/evias)
 * @license    MIT License
 * @copyright  (c) 2017, <NAME> <<EMAIL>>
 * @link       https://github.com/evias/nem-nodejs-bot
 */

(function() {

    var app = require('express')(),
        server = require('http').createServer(app),
        auth = require("http-auth"),
        bodyParser = require("body-parser"),
        fs = require("fs"),
        io = require('socket.io').listen(server);

    // configure database layer
    var models = require('./db/models.js');

    var NEMBot = function(config, logger, chainDataLayer) {
        this.config_ = config;
        this.blockchain_ = chainDataLayer;
        this.environment_ = process.env["APP_ENV"] || "development";

        this.db = new models.NEMBotDB(config, io, chainDataLayer);

        this.blockchain_.setDatabaseAdapter(this.db);
        this.blockchain_.setCliSocketIo(io);

        // define a helper for development debug of requests
        this.serverLog = function(req, msg, type) {
            var logMsg = "[" + type + "] " + msg + " (" + (req.headers ? req.headers['x-forwarded-for'] : "?") + " - " +
                (req.connection ? req.connection.remoteAddress : "?") + " - " +
                (req.socket ? req.socket.remoteAddress : "?") + " - " +
                (req.connection && req.connection.socket ? req.connection.socket.remoteAddress : "?") + ")";
            logger.info("NEMBot", __line, logMsg);
        };

        /**
         * Delayed route configuration. This will only be triggered when
         * the configuration file can be decrypted.
         *
         * Following is where we set our Bot's API endpoints. The API
         * routes list will change according to the Bot's "mode" config
         * value.
         */
        this.initBotAPI = function(config) {
            var self = this;

            // configure body-parser usage for POST API calls.
            app.use(bodyParser.urlencoded({ extended: true }));

            if (config.bot.protectedAPI === true) {
                // add Basic HTTP auth using nem-bot.htpasswd file

                var basicAuth = auth.basic({
                    realm: "This is a Highly Secured Area - Monkey at Work."
                    },(username, password, callback) => {  
                    // Custom authentication 
                    // Use callback(error) if you want to throw async error. 
                    callback(username === process.env["HTTP_AUTH_USERNAME"] &&
                             password === process.env["HTTP_AUTH_PASSWORD"]);
                }); 
                app.use(auth.connect(basicAuth));
            }<commit_msg>Update the HTTP Auth Logic<commit_after>/**
 * Part of the evias/nem-nodejs-bot package.
 *
 * NOTICE OF LICENSE
 *
 * Licensed under MIT License.
 *
 * This source file is subject to the MIT License that is
 * bundled with this package in the LICENSE file.
 *
 * @package    evias/nem-nodejs-bot
 * @author     <NAME> <<EMAIL>> (https://github.com/evias)
 * @license    MIT License
 * @copyright  (c) 2017, <NAME> <<EMAIL>>
 * @link       https://github.com/evias/nem-nodejs-bot
 */

(function() {

    var app = require('express')(),
        server = require('http').createServer(app),
        auth = require("http-auth"),
        bodyParser = require("body-parser"),
        fs = require("fs"),
        io = require('socket.io').listen(server);

    // configure database layer
    var models = require('./db/models.js');

    var NEMBot = function(config, logger, chainDataLayer) {
        this.config_ = config;
        this.blockchain_ = chainDataLayer;
        this.environment_ = process.env["APP_ENV"] || "development";

        this.db = new models.NEMBotDB(config, io, chainDataLayer);

        this.blockchain_.setDatabaseAdapter(this.db);
        this.blockchain_.setCliSocketIo(io);

        // define a helper for development debug of requests
        this.serverLog = function(req, msg, type) {
            var logMsg = "[" + type + "] " + msg + " (" + (req.headers ? req.headers['x-forwarded-for'] : "?") + " - " +
                (req.connection ? req.connection.remoteAddress : "?") + " - " +
                (req.socket ? req.socket.remoteAddress : "?") + " - " +
                (req.connection && req.connection.socket ? req.connection.socket.remoteAddress : "?") + ")";
            logger.info("NEMBot", __line, logMsg);
        };

        /**
         * Delayed route configuration. This will only be triggered when
         * the configuration file can be decrypted.
         *
         * Following is where we set our Bot's API endpoints. The API
         * routes list will change according to the Bot's "mode" config
         * value.
         */
        this.initBotAPI = function(config) {
            var self = this;

            // configure body-parser usage for POST API calls.
            app.use(bodyParser.urlencoded({ extended: true }));

            if (config.bot.protectedAPI === true) {
                // add Basic HTTP auth using nem-bot.htpasswd file
                if (process.env["HTTP_AUTH_USERNAME"] || process.env["HTTP_AUTH_PASSWORD"]){
                    var basicAuth = auth.basic({
                        realm: "This is a Highly Secured Area - Monkey at Work.",
                        },(username, password, callback) => {  
                        // Custom authentication 
                        // Use callback(error) if you want to throw async error. 
                        callback(username === process.env["HTTP_AUTH_USERNAME"] &&
                                password === process.env["HTTP_AUTH_PASSWORD"]);
                    }); 
                    app.use(auth.connect(basicAuth));
                }else{
                    var basicAuth = auth.basic({ 
                        realm: "This is a Highly Secured Area - Monkey at Work.", 
                        file: __dirname + "/../nem-bot.htpasswd" 
                    });
                    app.use(auth.connect(basicAuth));
                }
            }
{'fraction_non_alphanumeric': 0.12948616600790513, 'fraction_numerical': 0.0012648221343873518, 'mean_word_length': 1.8546931407942238, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 4, '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': '2916814', 'n_tokens_mistral': 1751, 'n_tokens_neox': 1670, 'n_words': 443}