<commit_before>'use strict';

let getTokenDao = require('./token.dao.js');
let utils = require('../utils');
let uuid = require('node-uuid');
let tokenService, tokenDao;

module.exports = function(config) {
  tokenDao = getTokenDao(config);

  function save(tokenObj) {
    let id = uuid.v4().replace(new RegExp('-', 'g'), '');
    let token = uuid.v4().replace(new RegExp('-', 'g'), '');
    let baseTokenProps = {
      id,
      tokenEncrypted: utils.encrypt(token, config.crypto),
      expiresAt: Date.now() + config.tokens.timeToExpiry
    }
    let tokenProps = Object.assign(baseTokenProps, tokenObj);

    utils.appendCreatedAt(tokenProps);

    if (tokenProps.scopes && Array.isArray(tokenProps.scopes)) {
      tokenProps.scopes = JSON.stringify(tokenProps.scopes.sort());
    }
    
    return tokenDao.save(tokenProps)
    .then(() => id.concat('|', token));
  }

  function findOrSave(tokenObj) {
    return find(tokenObj)
    .then(token => {
      return token ? token : save(tokenObj);
    });
  }

  function find(tokenObj) {
    let tokenQueryCriteria = Object.assign({}, tokenObj);

    if (tokenQueryCriteria.scopes && Array.isArray(tokenQueryCriteria.scopes)) {
      tokenQueryCriteria.scopes = JSON.stringify(tokenQueryCriteria.scopes.sort());
    }

    return tokenDao.find(tokenQueryCriteria)
    .then((token) => {
      if (!token) {
        return null;
      }
      return formToken(token);
    })
  }

  function get(_token) {
    let tokenId = _token.split('|')[0];

    return tokenDao.get(tokenId)
    .then(token => {
      if (!token) {
        return null;
      }

      if (token.scopes) {
        token.scopes = JSON.parse(token.scopes);
      }

      token.tokenDecrypted = utils.decrypt(token.tokenEncrypted, config.crypto);
      delete token.tokenEncrypted;

      return token;
    })<commit_msg>added token service (#128)<commit_after>'use strict';

let getTokenDao = require('./token.dao.js');
let utils = require('../utils');
let uuid = require('node-uuid');
let tokenService, tokenDao;

module.exports = function(config) {
  tokenDao = getTokenDao(config);

  function save(tokenObj) {
    let id = uuid.v4().replace(new RegExp('-', 'g'), '');
    let token = uuid.v4().replace(new RegExp('-', 'g'), '');
    let baseTokenProps = {
      id,
      tokenEncrypted: utils.encrypt(token, config.crypto),
      expiresAt: Date.now() + config.tokens.timeToExpiry
    }
    let tokenProps = Object.assign(baseTokenProps, tokenObj);

    utils.appendCreatedAt(tokenProps);

    if (tokenProps.scopes && Array.isArray(tokenProps.scopes)) {
      tokenProps.scopes = JSON.stringify(tokenProps.scopes.sort());
    }
    
    return tokenDao.save(tokenProps)
    .then(() => id.concat('|', token));
  }

  function findOrSave(tokenObj) {
    return find(tokenObj)
    .then(token => {
      return token ? token : save(tokenObj);
    });
  }

  function find(tokenObj) {
    let tokenQueryCriteria = Object.assign({}, tokenObj);

    if (tokenQueryCriteria.scopes && Array.isArray(tokenQueryCriteria.scopes)) {
      tokenQueryCriteria.scopes = JSON.stringify(tokenQueryCriteria.scopes.sort());
    }

    return tokenDao.find(tokenQueryCriteria)
    .then((token) => {
      if (!token) {
        return null;
      }
      return formToken(token);
    })
  }


  function get(_token) {
    let tokenId = _token.split('|')[0];
    
    return tokenDao.get(tokenId)
    .then(token => {
      if (!token) {
        return null;
      }

      if (token.scopes) {
        token.scopes = JSON.parse(token.scopes);
      }

      token.tokenDecrypted = utils.decrypt(token.tokenEncrypted, config.crypto);
      delete token.tokenEncrypted;

      return token;
    })
{'fraction_non_alphanumeric': 0.15091792656587474, 'fraction_numerical': 0.002429805615550756, 'mean_word_length': 2.9967637540453076, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '3715928', 'n_tokens_mistral': 1191, 'n_tokens_neox': 1194, 'n_words': 215}