<commit_before>// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Flag to indicate whether or not to show offline hosts in the host list.
chromoting.showOfflineHosts = true;

// String to identify bad auth tokens.
var BAD_AUTH_TOKEN = 'bad_token';

// Number of days before auth token cookies expire.
// TODO(garykac): 21 days is arbitrary. Need to change this to the appropriate
// value from security review.
var AUTH_EXPIRES = 21;

function init() {
  updateLoginStatus();

  // Defer getting the host list for a little bit so that we don't
  // block the display of the extension popup.
  window.setTimeout(populateHostList, 100);

  var showOff = getCookie('offline');
  chromoting.showOfflineHosts = (!showOff || showOff == '1');
  updateShowOfflineCheckbox();
}

function updateShowOfflineHosts(cbox) {
  chromoting.showOfflineHosts = cbox.checked;

  // Save pref in cookie with long expiration.
  setCookie('offline', chromoting.showOfflineHosts ? '1' : '0', 1000);

  populateHostList();
}

function updateShowOfflineCheckbox() {
  var cbox = document.getElementById('show_offline');
  cbox.checked = chromoting.showOfflineHosts;
}

// Update the login status region (at the bottom of the popup) with the
// current account and links to sign in/out.
function updateLoginStatus() {
  var username = getCookie('username');

  var loginDiv = document.getElementById('login_div');
  clear(loginDiv);

  if (!username) {
    var signinLink = document.createElement('a');
    signinLink.setAttribute('href',
        "javascript:window.open('login.html', 'Sign In', " +
        "'width=400,height=200,scrollbars=no'); return false;");
    signinLink.appendChild(document.createTextNode('Sign In'));
    loginDiv.appendChild(signinLink);
  } else {
    var email = document.createElement('span');
    email.setAttribute('class', 'login_email');
    email.appendChild(document.createTextNode(username));
    loginDiv.appendChild(email);

    loginDiv.appendChild(document.createTextNode(' | '));

    var signoutLink = document.createElement('a');
    signoutLink.setAttribute('href', 'javascript:logout(this.form);');
    signoutLink.appendChild(document.createTextNode('Sign Out'));
    loginDiv.appendChild(signoutLink);
  }
}

// Sign out the current user by erasing the auth cookies.
function logout(form) {
  setCookie('username', '', AUTH_EXPIRES);
  setCookie('chromoting_auth', '', AUTH_EXPIRES);
  setCookie('xmpp_auth', '', AUTH_EXPIRES);

  updateLoginStatus();
  populateHostList();
}

function login(form) {
  var status = document.getElementById('login_status');
  clear(status);
  doLogin(form.username.value, form.password.value, checkLogin);
}

// Check to see if the login was successful.
function checkLogin() {
  var username = getCookie('username');
  var cauth = getCookie('chromoting_auth');
  var xauth = getCookie('xmpp_auth');

  // Verify login and show login status.
  var status = document.getElementById('login_status');
  if (cauth == BAD_AUTH_TOKEN || xauth == BAD_AUTH_TOKEN) {
    appendMessage(status, '', 'Sign in failed!');
    if (username) {
      setCookie('username', '', AUTH_EXPIRES);
    }
  } else {
    appendMessage(status, '', 'Successfully signed in as ' + username);
  }
}

function doLogin(username, password, done) {
  // Don't call |done| callback until both login requests have completed.
  var count = 2;
  var barrier = function() {
    count--;
    if (done && count == 0) {
      done();
    }
  }
  setCookie('username', username, AUTH_EXPIRES);
  doGaiaLogin(username, password, 'chromoting',
              function(cAuthToken) {
                setCookie('chromoting_auth', cAuthToken, AUTH_EXPIRES);
                barrier();
              });
  doGaiaLogin(username, password, 'chromiumsync',
              function(xAuthToken) {
                setCookie('xmpp_auth', xAuthToken, AUTH_EXPIRES);
                barrier();
              });
}

function doGaiaLogin(username, password, service, done) {
  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'https://www.google.com/accounts/ClientLogin', true);
  xhr.onreadystatechange = function() {
    if (xhr.readyState != 4) {
      return;
    }
    if (xhr.status = 200) {
      done(extractAuthToken(xhr.responseText));
    } else {
      console.log('Bad status on auth: ' + xhr.statusText);
    }
  };

  xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  xhr.send('accountType=HOSTED_OR_GOOGLE&Email=' + username + '&Passwd=' +
           password + '&service=' + service + '&source=chromoclient');
}

function extractAuthToken(message) {
  var lines = message.split('\n');
  for (var i = 0; i < lines.length; i++) {
    if (lines[i].match('^Auth=.*')) {
      return lines[i].split('=')[1];
    }
  }
<commit_msg>Chromoting client extension: Properly encode ClientLogin request.<commit_after>// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Flag to indicate whether or not to show offline hosts in the host list.
chromoting.showOfflineHosts = true;

// String to identify bad auth tokens.
var BAD_AUTH_TOKEN = 'bad_token';

// Number of days before auth token cookies expire.
// TODO(garykac): 21 days is arbitrary. Need to change this to the appropriate
// value from security review.
var AUTH_EXPIRES = 21;

function init() {
  updateLoginStatus();

  // Defer getting the host list for a little bit so that we don't
  // block the display of the extension popup.
  window.setTimeout(populateHostList, 100);

  var showOff = getCookie('offline');
  chromoting.showOfflineHosts = (!showOff || showOff == '1');
  updateShowOfflineCheckbox();
}

function updateShowOfflineHosts(cbox) {
  chromoting.showOfflineHosts = cbox.checked;

  // Save pref in cookie with long expiration.
  setCookie('offline', chromoting.showOfflineHosts ? '1' : '0', 1000);

  populateHostList();
}

function updateShowOfflineCheckbox() {
  var cbox = document.getElementById('show_offline');
  cbox.checked = chromoting.showOfflineHosts;
}

// Update the login status region (at the bottom of the popup) with the
// current account and links to sign in/out.
function updateLoginStatus() {
  var username = getCookie('username');

  var loginDiv = document.getElementById('login_div');
  clear(loginDiv);

  if (!username) {
    var signinLink = document.createElement('a');
    signinLink.setAttribute('href',
        "javascript:window.open('login.html', 'Sign In', " +
        "'width=400,height=200,scrollbars=no'); return false;");
    signinLink.appendChild(document.createTextNode('Sign In'));
    loginDiv.appendChild(signinLink);
  } else {
    var email = document.createElement('span');
    email.setAttribute('class', 'login_email');
    email.appendChild(document.createTextNode(username));
    loginDiv.appendChild(email);

    loginDiv.appendChild(document.createTextNode(' | '));

    var signoutLink = document.createElement('a');
    signoutLink.setAttribute('href', 'javascript:logout(this.form);');
    signoutLink.appendChild(document.createTextNode('Sign Out'));
    loginDiv.appendChild(signoutLink);
  }
}

// Sign out the current user by erasing the auth cookies.
function logout(form) {
  setCookie('username', '', AUTH_EXPIRES);
  setCookie('chromoting_auth', '', AUTH_EXPIRES);
  setCookie('xmpp_auth', '', AUTH_EXPIRES);

  updateLoginStatus();
  populateHostList();
}

function login(form) {
  var status = document.getElementById('login_status');
  clear(status);
  doLogin(form.username.value, form.password.value, checkLogin);
}

// Check to see if the login was successful.
function checkLogin() {
  var username = getCookie('username');
  var cauth = getCookie('chromoting_auth');
  var xauth = getCookie('xmpp_auth');

  // Verify login and show login status.
  var status = document.getElementById('login_status');
  if (cauth == BAD_AUTH_TOKEN || xauth == BAD_AUTH_TOKEN) {
    appendMessage(status, '', 'Sign in failed!');
    if (username) {
      setCookie('username', '', AUTH_EXPIRES);
    }
  } else {
    appendMessage(status, '', 'Successfully signed in as ' + username);
  }
}

function doLogin(username, password, done) {
  // Don't call |done| callback until both login requests have completed.
  var count = 2;
  var barrier = function() {
    count--;
    if (done && count == 0) {
      done();
    }
  }
  setCookie('username', username, AUTH_EXPIRES);
  doGaiaLogin(username, password, 'chromoting',
              function(cAuthToken) {
                setCookie('chromoting_auth', cAuthToken, AUTH_EXPIRES);
                barrier();
              });
  doGaiaLogin(username, password, 'chromiumsync',
              function(xAuthToken) {
                setCookie('xmpp_auth', xAuthToken, AUTH_EXPIRES);
                barrier();
              });
}

function doGaiaLogin(username, password, service, done) {
  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'https://www.google.com/accounts/ClientLogin', true);
  xhr.onreadystatechange = function() {
    if (xhr.readyState != 4) {
      return;
    }
    if (xhr.status = 200) {
      done(extractAuthToken(xhr.responseText));
    } else {
      console.log('Bad status on auth: ' + xhr.statusText);
    }
  };

  xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  xhr.send('accountType=HOSTED_OR_GOOGLE&Email=' +
           encodeURIComponent(username) + '&Passwd=' +
           encodeURIComponent(password) + '&service=' +
           encodeURIComponent(service) + '&source=chromoclient');
}

function extractAuthToken(message) {
  var lines = message.split('\n');
  for (var i = 0; i < lines.length; i++) {
    if (lines[i].match('^Auth=.*')) {
      return lines[i].split('=')[1];
    }
  }

{'fraction_non_alphanumeric': 0.1348405329027049, 'fraction_numerical': 0.006459426725878078, 'mean_word_length': 3.754798464491363, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, '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': '230298', 'n_tokens_mistral': 3078, 'n_tokens_neox': 2985, 'n_words': 754}