<commit_before>    
var $get = function(url, callback){
    fetch(url, {credentials : 'same-origin'}).then(function(response) {
        return response.json()
    }).then(function(json) {
        callback(json)
    }).catch(function(ex) {
        callback({ 'error': ex })
    })
}

var $post = function (url, body, callback) {
    fetch(url, {
        method  : 'post',
        credentials : 'same-origin',
        headers : {
            'Accept'       : 'application/json',
            'Content-Type' : 'application/json'
        },
        body    : JSON.stringify(body)
    }).then(function (response) {
        return response.json()
    }).then(function (json) {
        callback(json)
    }).catch(function (ex) {
        callback({ 'error': ex })
    })
}

var $id = function(id){
    return document.getElementById(id);
}


var Logger = function(id) {
    this.textarea = id;
}

Logger.prototype.log = function(text) {
    $logarea = $id(this.textarea);
    $logarea.value = '> ' + text + '\n' + $logarea.value;
    console.log(text);
}


var U2F_ERROR_CODES = {
    1 : 'OTHER_ERROR',
    2 : 'BAD_REQUEST',
    3 : 'CONFIGURATION_UNSUPPORTED',
    4 : 'DEVICE_INELIGIBLE',
    5 : 'TIMEOUT'
}

var locked = false;

var u2f_enroll = function(e) {
    if (e.preventDefault) e.preventDefault();


    var user = {
        'username': enroll_form.elements['username'].value,
        'password': enroll_form.elements['password'].value
    }


    var logger = new Logger('register_log');

    if(user['username'] && user['password'] && !locked){
        clear_textareas();

        $post('/register', user, function(response){
            if(response.status === 'success' || response.u2f_enroll_required){

                logger.log('Registering...');
                locked = true;
                $post('/login', user, function(response){
                    if(response.status !== 'failed'){

                        logger.log('Requesting challenge...');

                        $get('/enroll', function(serverreq){
                            logger.log('Registering...');
                            var req = serverreq.registerRequests[0];

                            // Getting AppID
                            var appid = req.appId;
                           
                            // Formating Challenge
                            var challenge = [{version: req.version, challenge: req.challenge}];

                            logger.log(req);
                            logger.log('Waiting for user action...');

                            u2f.register(appid, challenge, [], function(deviceResponse) {
                                locked = false;

                                if(deviceResponse.errorCode){
                                    logger.log('U2F ERROR: ' + U2F_ERROR_CODES[deviceResponse.errorCode]);
                                }else{
                                    logger.log(deviceResponse);
                                    logger.log('Verifying with server...');
                                    
                                    $post('/enroll', deviceResponse,  function(serverResonse){
                                        if(serverResonse.status === 'success'){
                                            logger.log('Success!');
                                        }else{
                                            logger.log('Fail! ' + serverResonse.error);
                                        }
                                    })
                                }

                            }, 10);
                        });
                    }else{
                        logger.log('Login Failed');
                    }
                });
            }else{
                logger.log('Failed');
                logger.log(response.error);
            }
        })
    }

    return false;
}

var u2f_sign = function(e) {
    if (e.preventDefault) e.preventDefault();


    var user = {
        'username': sign_form.elements['username'].value,
        'password': sign_form.elements['password'].value
    }


    var logger = new Logger('login_log');

    if(user['username'] && user['password'] && !locked){
        clear_textareas();
        
        logger.log('Loggin in...')

        $post('/login', user,function(response){
            locked = true;
            if(response.status === 'failed'){
                if(response['u2f_sign_required']){
                    logger.log('U2F Required')

                    $get('/sign', function(serverreq){
                        var req = serverreq.authenticateRequests[0];

                        // Getting AppID
                        var appid = req.appId;
                       
                        // Getting Challenge
                        var challenge = req.challenge;

                        // Formating keyhandle for U2F key
                        var registeredKeys = [{version: req.version, keyHandle: req.keyHandle}];

                        logger.log('Getting challenge...');
                        logger.log(req);
                        logger.log('Waiting for user action...');

                       
                        u2f.sign(appid, challenge, registeredKeys, function(deviceResponse) {
                            locked = false;
                            if(deviceResponse.errorCode){
                                logger.log('U2F ERROR: ' + U2F_ERROR_CODES[deviceResponse.errorCode]);
                            }else{
                                logger.log(deviceResponse);
                                logger.log('Verifying with server...');
                                
                                $post('/sign', deviceResponse, function(serverResonse){
                                    if(serverResonse.status === 'success'){
                                        logger.log('Success! Counter ' + serverResonse.counter);
                                    }else{
                                        logger.log('Fail! ' + serverResonse.error);
                                    }
                                })
                            }
                          
                            return
                        }, 10);
                    });
                }else{
                    logger.log('Fail');
                    logger.log(response.error);
                }
            }else{
                logger.log('Success');
                logger.log('No U2F required');
            }
        });
    }

    return false;
}

var clear_textareas = function() {
    var textareas = document.getElementsByTagName('textarea');
    
    for(var i = 0; i < textareas.length; i++)
        textareas[i].value = '';
}<commit_msg>Fixed incorrect expected responses<commit_after>    
var $get = function(url, callback){
    fetch(url, {credentials : 'same-origin'}).then(function(response) {
        return response.json()
    }).then(function(json) {
        callback(json)
    }).catch(function(ex) {
        callback({ 'error': ex })
    })
}

var $post = function (url, body, callback) {
    fetch(url, {
        method  : 'post',
        credentials : 'same-origin',
        headers : {
            'Accept'       : 'application/json',
            'Content-Type' : 'application/json'
        },
        body    : JSON.stringify(body)
    }).then(function (response) {
        return response.json()
    }).then(function (json) {
        callback(json)
    }).catch(function (ex) {
        callback({ 'error': ex })
    })
}

var $id = function(id){
    return document.getElementById(id);
}


var Logger = function(id) {
    this.textarea = id;
}

Logger.prototype.log = function(text) {
    $logarea = $id(this.textarea);
    $logarea.value = '> ' + text + '\n' + $logarea.value;
    console.log(text);
}


var U2F_ERROR_CODES = {
    1 : 'OTHER_ERROR',
    2 : 'BAD_REQUEST',
    3 : 'CONFIGURATION_UNSUPPORTED',
    4 : 'DEVICE_INELIGIBLE',
    5 : 'TIMEOUT'
}

var locked = false;

var u2f_enroll = function(e) {
    if (e.preventDefault) e.preventDefault();


    var user = {
        'username': enroll_form.elements['username'].value,
        'password': enroll_form.elements['password'].value
    }


    var logger = new Logger('register_log');

    if(user['username'] && user['password'] && !locked){
        clear_textareas();

        $post('/register', user, function(response){
            if(response.status === 'ok'){

                logger.log('Registering...');
                locked = true;
                $post('/login', user, function(response){
                    if(response.status !== 'failed'){

                        logger.log('Requesting challenge...');

                        $get('/enroll', function(serverreq){
                            logger.log('Registering...');
                            var req = serverreq.registerRequests[0];

                            // Getting AppID
                            var appid = req.appId;
                           
                            // Formating Challenge
                            var challenge = [{version: req.version, challenge: req.challenge}];

                            logger.log(req);
                            logger.log('Waiting for user action...');

                            u2f.register(appid, challenge, [], function(deviceResponse) {
                                locked = false;

                                if(deviceResponse.errorCode){
                                    logger.log('U2F ERROR: ' + U2F_ERROR_CODES[deviceResponse.errorCode]);
                                }else{
                                    logger.log(deviceResponse);
                                    logger.log('Verifying with server...');
                                    
                                    $post('/enroll', deviceResponse,  function(serverResonse){
                                        if(serverResonse.status === 'ok'){
                                            logger.log('Success!');
                                        }else{
                                            logger.log('Fail! ' + serverResonse.error);
                                        }
                                    })
                                }

                            }, 10);
                        });
                    }else{
                        logger.log('Login Failed');
                    }
                });
            }else{
                logger.log('Failed');
                logger.log(response.error);
            }
        })
    }

    return false;
}

var u2f_sign = function(e) {
    if (e.preventDefault) e.preventDefault();


    var user = {
        'username': sign_form.elements['username'].value,
        'password': sign_form.elements['password'].value
    }


    var logger = new Logger('login_log');

    if(user['username'] && user['password'] && !locked){
        clear_textareas();
        
        logger.log('Loggin in...')

        $post('/login', user,function(response){
            locked = true;
            if(response.status === 'failed'){
                if(response['u2f_sign_required']){
                    logger.log('U2F Required')

                    $get('/sign', function(serverreq){
                        var req = serverreq.authenticateRequests[0];

                        // Getting AppID
                        var appid = req.appId;
                       
                        // Getting Challenge
                        var challenge = req.challenge;

                        // Formating keyhandle for U2F key
                        var registeredKeys = [{version: req.version, keyHandle: req.keyHandle}];

                        logger.log('Getting challenge...');
                        logger.log(req);
                        logger.log('Waiting for user action...');

                       
                        u2f.sign(appid, challenge, registeredKeys, function(deviceResponse) {
                            locked = false;
                            if(deviceResponse.errorCode){
                                logger.log('U2F ERROR: ' + U2F_ERROR_CODES[deviceResponse.errorCode]);
                            }else{
                                logger.log(deviceResponse);
                                logger.log('Verifying with server...');
                                
                                $post('/sign', deviceResponse, function(serverResonse){
                                    if(serverResonse.status === 'ok'){
                                        logger.log('Success! Counter ' + serverResonse.counter);
                                    }else{
                                        logger.log('Fail! ' + serverResonse.error);
                                    }
                                })
                            }
                          
                            return
                        }, 10);
                    });
                }else{
                    logger.log('Fail');
                    logger.log(response.error);
                }
            }else{
                logger.log('Success');
                logger.log('No U2F required');
            }
        });
    }

    return false;
}

var clear_textareas = function() {
    var textareas = document.getElementsByTagName('textarea');
    
    for(var i = 0; i < textareas.length; i++)
        textareas[i].value = '';
}
{'fraction_non_alphanumeric': 0.11915301755939206, 'fraction_numerical': 0.0037627268702965914, 'mean_word_length': 1.0999225406661504, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 38, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1747787', 'n_tokens_mistral': 3343, 'n_tokens_neox': 3141, 'n_words': 576}