<commit_before>/**
 * Created by abousk01 on 20/07/2016.
 */
var api_controller = require(__dirname + '/../controllers/api');
var properties = require(__dirname + '/../properties/properties');
var restify = require('restify');
var utils = require(__dirname + '/../services/utils');
var logger = require(__dirname + '/../services/logger').getInstance();
var gcm = require('node-gcm');
var qrCode = require('qrcode-npm');
var sockets = require('../server/sockets');

// Set up the sender with you API key, prepare your recipients' registration tokens.
var sender = new gcm.Sender(properties.getMethodProperty('push', 'serverKey'), {'proxy': 'http://wwwcache.univ-lr.fr:3128'});

exports.name = "push";

exports.send_message = function (user, req, res, next) {
    user.push.code = utils.generate_digit_code(properties.getMethod('random_code').code_length);
    var message = new gcm.Message({
        priority: "high",
        data: {
            title: "Esup-OTP-Push",
            body: "Demande de connexion à votre compte",
            text: "Demande de connexion à votre compte",
            action: 'auth',
            uid: user.uid,
            code: user.push.code,
            lt: req.params.lt
        },
        to: user.push.device.gcm_id
    });

    var regTokens = [user.push.device.gcm_id];

    user.save( function () {
        sender.send(message, {registrationTokens: regTokens}, function (err, response) {
            if (err) {
                logger.info(err);
                res.send({
                    "code": "Error",
                    "message": JSON.stringify(err)
                });
            } else {
                res.send({
                    "code": "Ok",
                    "message": response
                });
            }
        });
    });
};

/**
 * Vérifie si l'otp fourni correspond à celui généré
 * si oui: on retourne un réponse positive
 * sinon: on renvoie une erreur 401 InvalidCredentialsError
 *
 * @param req requete HTTP contenant le nom la personne recherchee
 * @param res response HTTP
 * @param next permet d'appeler le prochain gestionnaire (handler)
 */
exports.verify_code = function (user, req, res, callbacks) {
    logger.debug(utils.getFileName(__filename) + ' ' + "verify_code: " + user.uid);
    if (user.push.code == req.params.otp) {
        delete user.push.code;
        user.save( function () {
            logger.info("Valid credentials by " + user.uid);
            res.send({
                "code": "Ok",
                "message": properties.getMessage('success', 'valid_credentials')
            });
        });
    } else {
        var next = callbacks.pop();
        next(user, req, res, callbacks)
    }
}


exports.generate_method_secret = function (user, req, res, next) {
    res.send({
        "code": "Error",
        "message": properties.getMessage('error', 'unvailable_method_operation')
    });
}

exports.delete_method_secret = function (user, req, res, next) {
    res.send({
        "code": "Error",
        "message": properties.getMessage('error', 'unvailable_method_operation')
    });
}

exports.get_method_secret = function (user, req, res, next) {
    res.send({
        "code": "Error",
        "message": properties.getMessage('error', 'unvailable_method_operation')
    });
}


exports.user_activate = function (user, req, res, next) {
    var activation_code = utils.generate_digit_code(6);
    user.push.activation_code = activation_code;
    var qr = qrCode.qrcode(4, 'M');
    var http = 'http://';
    if(req.isSecure())http="https://";
    qr.addData(http+req.headers.host+'/users/'+user.uid+'/methods/push/'+activation_code);
    qr.make();
    user.save( function () {
        res.send({
            "code": "Ok",
            "message1": properties.getMessage('success', 'push_confirmation1'),
            "message2": properties.getMessage('success', 'push_confirmation2'),
            "message3": properties.getMessage('success', 'push_confirmation3'),
            "message4": properties.getMessage('success', 'push_confirmation4'),
            "message5": properties.getMessage('success', 'push_confirmation5'),
            "qrCode" : qr.createImgTag(4),
            "activationCode" : activation_code
        });
    });
}<commit_msg>manage x-forwarded-host<commit_after>/**
 * Created by abousk01 on 20/07/2016.
 */
var api_controller = require(__dirname + '/../controllers/api');
var properties = require(__dirname + '/../properties/properties');
var restify = require('restify');
var utils = require(__dirname + '/../services/utils');
var logger = require(__dirname + '/../services/logger').getInstance();
var gcm = require('node-gcm');
var qrCode = require('qrcode-npm');
var sockets = require('../server/sockets');

// Set up the sender with you API key, prepare your recipients' registration tokens.
var sender = new gcm.Sender(properties.getMethodProperty('push', 'serverKey'), {'proxy': 'http://wwwcache.univ-lr.fr:3128'});

exports.name = "push";

exports.send_message = function (user, req, res, next) {
    user.push.code = utils.generate_digit_code(properties.getMethod('random_code').code_length);
    var message = new gcm.Message({
        priority: "high",
        data: {
            title: "Esup-OTP-Push",
            body: "Demande de connexion à votre compte",
            text: "Demande de connexion à votre compte",
            action: 'auth',
            uid: user.uid,
            code: user.push.code,
            lt: req.params.lt
        },
        to: user.push.device.gcm_id
    });

    var regTokens = [user.push.device.gcm_id];

    user.save( function () {
        sender.send(message, {registrationTokens: regTokens}, function (err, response) {
            if (err) {
                logger.info(err);
                res.send({
                    "code": "Error",
                    "message": JSON.stringify(err)
                });
            } else {
                res.send({
                    "code": "Ok",
                    "message": response
                });
            }
        });
    });
};

/**
 * Vérifie si l'otp fourni correspond à celui généré
 * si oui: on retourne un réponse positive
 * sinon: on renvoie une erreur 401 InvalidCredentialsError
 *
 * @param req requete HTTP contenant le nom la personne recherchee
 * @param res response HTTP
 * @param next permet d'appeler le prochain gestionnaire (handler)
 */
exports.verify_code = function (user, req, res, callbacks) {
    logger.debug(utils.getFileName(__filename) + ' ' + "verify_code: " + user.uid);
    if (user.push.code == req.params.otp) {
        delete user.push.code;
        user.save( function () {
            logger.info("Valid credentials by " + user.uid);
            res.send({
                "code": "Ok",
                "message": properties.getMessage('success', 'valid_credentials')
            });
        });
    } else {
        var next = callbacks.pop();
        next(user, req, res, callbacks)
    }
}


exports.generate_method_secret = function (user, req, res, next) {
    res.send({
        "code": "Error",
        "message": properties.getMessage('error', 'unvailable_method_operation')
    });
}

exports.delete_method_secret = function (user, req, res, next) {
    res.send({
        "code": "Error",
        "message": properties.getMessage('error', 'unvailable_method_operation')
    });
}

exports.get_method_secret = function (user, req, res, next) {
    res.send({
        "code": "Error",
        "message": properties.getMessage('error', 'unvailable_method_operation')
    });
}


exports.user_activate = function (user, req, res, next) {
    var activation_code = utils.generate_digit_code(6);
    user.push.activation_code = activation_code;
    var qr = qrCode.qrcode(4, 'M');
    var http = 'http://';
    if(req.headers["x-forwarded-proto"])http=req.headers["x-forwarded-proto"]+"://";
    var host = req.headers.host;
    if(req.headers["x-forwarded-host"])host=req.headers["x-forwarded-host"].replace(/,.*/,'');
    qr.addData(http+host+'/users/'+user.uid+'/methods/push/'+activation_code);
    qr.make();
    user.save( function () {
        res.send({
            "code": "Ok",
            "message1": properties.getMessage('success', 'push_confirmation1'),
            "message2": properties.getMessage('success', 'push_confirmation2'),
            "message3": properties.getMessage('success', 'push_confirmation3'),
            "message4": properties.getMessage('success', 'push_confirmation4'),
            "message5": properties.getMessage('success', 'push_confirmation5'),
            "qrCode" : qr.createImgTag(4),
            "activationCode" : activation_code
        });
    });
}
{'fraction_non_alphanumeric': 0.15227900552486187, 'fraction_numerical': 0.006906077348066298, 'mean_word_length': 2.5321138211382115, 'pattern_counts': {'":': 37, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, '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': '3047572', 'n_tokens_mistral': 2618, 'n_tokens_neox': 2575, 'n_words': 567}