<commit_before>
var cpaProtocol = {};
cpaProtocol.registration = {};

/**
 * Endpoints defined in the CPA spec
 */
cpaProtocol.config = {
  ap_register_url:  'register',
  ap_token_url:     'token',
  ap_associate_url: 'associate',

  // RadioTag spec
  sp_discover_url:  'tags'
};

var parseWwwAuthenticate = function(challenge) {
  var regex = /(?:(\w*)\=\"(.*?))*\"/g;
  var match = [], data = {};
  while (match = regex.exec(challenge)) {
    data[match[1]] = match[2];
  }

  var modesArray = data.modes.split(',');
  var modes = {
    client:  (modesArray.indexOf('client') !== -1),
    user:    (modesArray.indexOf('user') !== -1),
    anonymous: false
  };

  return { apBaseUrl: data.uri+'/', modes: modes };
};

/**
 *  Discover the responsible AP and the available modes for a domain
 */


cpaProtocol.getServiceInfos = function(domain, done) {

  var getServiceInfosCallback = function (jqXHR) {
    var challenge = jqXHR.getResponseHeader('WWW-Authenticate');
    if (!challenge) {
      done(new Error('Missing WWW-Authenticate header. Please, make sure CORS headers are correctly sent. ("Access-Control-Expose-Headers: WWW-Authenticate")'));
      return;
    }

    var spInfos = parseWwwAuthenticate(challenge);
    done(null, spInfos.apBaseUrl, spInfos.modes);
  };

  for (var station_name in config.domains) {
    if (config.domains[station_name] === domain) {
      requestHelper.get(domain + cpaProtocol.config.sp_discover_url)
        .done(function(body, textStatus, jqXHR) {
          getServiceInfosCallback(jqXHR);
        })
        .fail(function(jqXHR, textStatus, err) {
          getServiceInfosCallback(jqXHR);
        });
    }
  }
  done(new Error('Unable to find available modes for domain : ' + domain));
};

/**
 * Register the client with the Authentication Provider

 * done: function(err, status_code, body) {}
 *
 */
cpaProtocol.registerClient = function(APBaseUrl, clientName, softwareId, softwareVersion, done){

  var registrationBody = {
    client_name: clientName,
    software_id: softwareId,
    software_version: softwareVersion
  };

  Logger.info('CLIENT REGISTRATION');

  requestHelper.postJSON(APBaseUrl + cpaProtocol.config.ap_register_url, registrationBody)
    .success(function(data, textStatus, jqXHR) {

      if(jqXHR.status === 201) {
        Logger.info('Reply ' + jqXHR.status + '(' + textStatus + '): ', data);
        done(null, data.client_id, data.client_secret);
      } else {
        Logger.error('Reply ' + jqXHR.status + '(' + textStatus + '): ', 'wrong status code');
        done(new Error({message: 'wrong status code', 'jqXHR': jqXHR}), jqXHR.status, textStatus);
      }

    }).fail(function( jqXHR, textStatus ) {<commit_msg>Error reporting in auth mode query<commit_after>
var cpaProtocol = {};
cpaProtocol.registration = {};

/**
 * Endpoints defined in the CPA spec
 */
cpaProtocol.config = {
  ap_register_url:  'register',
  ap_token_url:     'token',
  ap_associate_url: 'associate',

  // RadioTag spec
  sp_discover_url:  'tags'
};

var parseWwwAuthenticate = function(challenge) {
  var regex = /(?:(\w*)\=\"(.*?))*\"/g;
  var match = [], data = {};
  while (match = regex.exec(challenge)) {
    data[match[1]] = match[2];
  }

  var modesArray = data.modes.split(',');
  var modes = {
    client:  (modesArray.indexOf('client') !== -1),
    user:    (modesArray.indexOf('user') !== -1),
    anonymous: false
  };

  return { apBaseUrl: data.uri+'/', modes: modes };
};

/**
 *  Discover the responsible AP and the available modes for a domain
 */


cpaProtocol.getServiceInfos = function(domain, done) {

  var getServiceInfosCallback = function (jqXHR) {
    var challenge = jqXHR.getResponseHeader('WWW-Authenticate');
    if (!challenge) {
      done(new Error('Missing WWW-Authenticate header. Please, make sure CORS headers are correctly sent. ("Access-Control-Expose-Headers: WWW-Authenticate")'));
      return;
    }

    var spInfos = parseWwwAuthenticate(challenge);
    done(null, spInfos.apBaseUrl, spInfos.modes);
  };

  var found = false;

  for (var station_name in config.domains) {
    if (config.domains[station_name] === domain) {
      found = true;
      requestHelper.get(domain + cpaProtocol.config.sp_discover_url)
        .done(function(body, textStatus, jqXHR) {
          getServiceInfosCallback(jqXHR);
        })
        .fail(function(jqXHR, textStatus, err) {
          getServiceInfosCallback(jqXHR);
        });
    }
  }

  if (!found) {
    done(new Error('Unable to find available modes for domain : ' + domain));
  }
};

/**
 * Register the client with the Authentication Provider

 * done: function(err, status_code, body) {}
 *
 */
cpaProtocol.registerClient = function(APBaseUrl, clientName, softwareId, softwareVersion, done){

  var registrationBody = {
    client_name: clientName,
    software_id: softwareId,
    software_version: softwareVersion
  };

  Logger.info('CLIENT REGISTRATION');

  requestHelper.postJSON(APBaseUrl + cpaProtocol.config.ap_register_url, registrationBody)
    .success(function(data, textStatus, jqXHR) {

      if(jqXHR.status === 201) {
        Logger.info('Reply ' + jqXHR.status + '(' + textStatus + '): ', data);
        done(null, data.client_id, data.client_secret);
      } else {
        Logger.error('Reply ' + jqXHR.status + '(' + textStatus + '): ', 'wrong status code');
        done(new Error({message: 'wrong status code', 'jqXHR': jqXHR}), jqXHR.status, textStatus);
      }

    }).fail(function( jqXHR, textStatus ) {
{'fraction_non_alphanumeric': 0.14709090909090908, 'fraction_numerical': 0.0025454545454545456, 'mean_word_length': 3.454251012145749, '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': '4407559', 'n_tokens_mistral': 1857, 'n_tokens_neox': 1778, 'n_words': 382}