<commit_before>'use strict';

require('isomorphic-fetch');

const crypto = require('crypto');
const base64url = require('base64url');
const config = require('./config');

var locals = { tokenTime: 0 };

const refreshToken = () => {
  if (locals.uuid && locals.secret) {
    return refreshDeviceToken();
  } else if (locals.networkUuid && locals.apiKey) {
    return refreshNetworkToken();
  } else if (locals.username && locals.password && locals.organization) {
    return refreshUserToken();
  } else {
    return Promise.reject('No token available.');
  }
};

const refreshUserToken = () => {
  return fetch(config.host + '/api/auth/login', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      username: locals.username,
      password: <PASSWORD>,
      org: locals.organization
    })
  }).then(response => {
    if (!response.ok) return Promise.reject('Authentication failed');
    return response.json().then(body => {
      locals.token = body.token;
      return locals.token;
    });
  });
};

const refreshDeviceToken = () => {
  var header = JSON.stringify({ alg: 'HS256', 'typ': 'JWT' });
  var body = JSON.stringify({ uuid: locals.uuid, deviceUuid: locals.uuid });
  var hmac = crypto.createHmac('sha256', locals.secret);
  var message = base64url.encode(header) + '.' + base64url.encode(body);
  locals.token = message + '.' + base64url.encode(hmac.update(message).digest());
  locals.tokenTime = new Date();
  return Promise.resolve(locals.token);
};

const refreshNetworkToken = () => {
  var header = JSON.stringify({ alg: 'HS256', 'typ': 'JWT' });
  var body = JSON.stringify({ networkUuid: locals.networkUuid });
  var hmac = crypto.createHmac('sha256', locals.apiKey);
  var message = base64url.encode(header) + '.' + base64url.encode(body);
  locals.token = message + '.' + base64url.encode(hmac.update(message).digest());
  locals.tokenTime = new Date();
  return Promise.resolve(locals.token);
};

module.exports.setDeviceCredentials = (uuid, secret) => {
  module.exports.clear();
  locals.uuid = uuid;
  locals.secret = secret;
};

module.exports.setNetworkCredentials = (uuid, apiKey) => {
  module.exports.clear();
  locals.networkUuid = uuid;
  locals.apiKey = apiKey;
};

module.exports.setUserCredentials = (username, password, organization) => {
  module.exports.clear();
  locals.username = username;
  locals.password = password;
  locals.organization = organization;
};

module.exports.setToken = token => {
  module.exports.clear();
  locals.token = token;
  locals.tokenTime = Infinity;
};

module.exports.getToken = () => {
  if (locals.token && (new Date() - locals.tokenTime) < 3600 * 1000) {
    return Promise.resolve(locals.token);
  }<commit_msg>Begin support for pairing device flag.<commit_after>'use strict';

require('isomorphic-fetch');

const crypto = require('crypto');
const base64url = require('base64url');
const config = require('./config');

var locals = { tokenTime: 0 };

const refreshToken = () => {
  if (locals.uuid && locals.secret) {
    return refreshDeviceToken();
  } else if (locals.networkUuid && locals.apiKey) {
    return refreshNetworkToken();
  } else if (locals.username && locals.password && locals.organization) {
    return refreshUserToken();
  } else {
    return Promise.reject('No token available.');
  }
};

const refreshUserToken = () => {
  return fetch(config.host + '/api/auth/login', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      username: locals.username,
      password: <PASSWORD>,
      org: locals.organization
    })
  }).then(response => {
    if (!response.ok) return Promise.reject('Authentication failed');
    return response.json().then(body => {
      locals.token = body.token;
      return locals.token;
    });
  });
};

const refreshDeviceToken = () => {
  var header = JSON.stringify({ alg: 'HS256', 'typ': 'JWT' });
  var body = JSON.stringify({ uuid: locals.uuid, deviceUuid: locals.uuid, allowPairing: locals.allowPairing });
  var hmac = crypto.createHmac('sha256', locals.secret);
  var message = base64url.encode(header) + '.' + base64url.encode(body);
  locals.token = message + '.' + base64url.encode(hmac.update(message).digest());
  locals.tokenTime = new Date();
  return Promise.resolve(locals.token);
};

const refreshNetworkToken = () => {
  var header = JSON.stringify({ alg: 'HS256', 'typ': 'JWT' });
  var body = JSON.stringify({ networkUuid: locals.networkUuid });
  var hmac = crypto.createHmac('sha256', locals.apiKey);
  var message = base64url.encode(header) + '.' + base64url.encode(body);
  locals.token = message + '.' + base64url.encode(hmac.update(message).digest());
  locals.tokenTime = new Date();
  return Promise.resolve(locals.token);
};

module.exports.setDeviceCredentials = (uuid, secret, allowPairing) => {
  module.exports.clear();
  locals.uuid = uuid;
  locals.secret = secret;
  locals.allowPairing = allowPairing;
};

module.exports.setNetworkCredentials = (uuid, apiKey) => {
  module.exports.clear();
  locals.networkUuid = uuid;
  locals.apiKey = apiKey;
};

module.exports.setUserCredentials = (username, password, organization) => {
  module.exports.clear();
  locals.username = username;
  locals.password = password;
  locals.organization = organization;
};

module.exports.setToken = token => {
  module.exports.clear();
  locals.token = token;
  locals.tokenTime = Infinity;
};

module.exports.getToken = () => {
  if (locals.token && (new Date() - locals.tokenTime) < 3600 * 1000) {
    return Promise.resolve(locals.token);
  }
{'fraction_non_alphanumeric': 0.16471006759160442, 'fraction_numerical': 0.013162575595873355, 'mean_word_length': 3.971706454465075, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 27, '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': '3580064', 'n_tokens_mistral': 1785, 'n_tokens_neox': 1767, 'n_words': 364}