<commit_before>'use strict';
/**
 * @file Define initial handlers,
 *
 * Trade grant_tokens for access_tokens.
 *
 */
var async = require('async');
var restify = require('restify');

var TempToken = require('../models/temp-token.js');
var Token = require('../models/token.js');

/**
 * This handler starts a new account for Cluestr.
 * We check the request is valid, create a new tempToken and call the handler for new account.
 * The tempToken is then saved.
 *
 * @param {Object} req Request object from the client
 * @param {Object} res Response we want to return
 * @param {Function} next Callback to call once res has been populated.
 */
module.exports.connect = function(initAccountCb) {
  return function (req, res, next) {
    if(!req.params.code) {
      return next(new restify.MissingParameterError("Missing code parameter."));
    }

    // Hold temporary state
    var tempToken = new TempToken({
      cluestrCode: req.params.code,
    });

    initAccountCb(req, function(err, preDatas, redirectUrl) {
      if(err) {
        return next(err);
      }

      tempToken.datas = preDatas;
      tempToken.save(function(err) {
        if(err) {
          return next(err);
        }

        res.send(302, null, {
          Location: redirectUrl
        });
        next();
      });
    });
  };
};

/**
 * The user is redirected to this handler after getting access to his provider.
 *
 * @param {Object} req Request object from the client
 * @param {Object} res Response we want to return
 * @param {Function} next Callback to call once res has been populated.
 */
exports.callback = function(connectAccountRetrievePreDatasIdentifier, connectAccountRetrieveAuthDatas, cluestrClient, connectUrl, redirectUrl) {
  return function (req, res, next) {
    async.waterfall([
      function(cb) {
        // Retrieve the previous tempToken
        connectAccountRetrievePreDatasIdentifier(req, cb);
      },
      function(hash, cb) {
        TempToken.findOne(hash, cb);
      },
      function(tempToken, cb) {
        if(!tempToken) {
          return cb(new Error("Unable to retrieve provider preDatas."));
        }

        // Create a new cluestr access-token with our grant
        cluestrClient.getAccessToken(tempToken.cluestrCode, connectUrl, function(err, cluestrAccessToken) {
          cb(err, tempToken, cluestrAccessToken);
        });
      },
      function(tempToken, cluestrAccessToken, cb) {
        // Retrieve datas from provider
        connectAccountRetrieveAuthDatas(req, tempToken.datas, function(err, providerDatas) {
          cb(err, tempToken, cluestrAccessToken, providerDatas);
        });
      },
      function(tempToken, cluestrAccessToken, providerDatas, cb) {
        // Create new token
        var token = new Token({
          cluestrToken: cluestrAccessToken,
          datas: providerDatas
        });

        token.save(function(err) {
          cb(err, tempToken);
        });
      },
      function(tempToken, cb) {
        tempToken.remove(cb);
      }
    ], function(err) {
      if(err) {
        return next(err);
      }
      
      res.send(302, null, {
        Location: redirectUrl
      });
<commit_msg>Finish provider init workflow before trading grant with anyfetch<commit_after>'use strict';
/**
 * @file Define initial handlers,
 *
 * Trade grant_tokens for access_tokens.
 *
 */
var async = require('async');
var restify = require('restify');

var TempToken = require('../models/temp-token.js');
var Token = require('../models/token.js');

/**
 * This handler starts a new account for Cluestr.
 * We check the request is valid, create a new tempToken and call the handler for new account.
 * The tempToken is then saved.
 *
 * @param {Object} req Request object from the client
 * @param {Object} res Response we want to return
 * @param {Function} next Callback to call once res has been populated.
 */
module.exports.connect = function(initAccountCb) {
  return function (req, res, next) {
    if(!req.params.code) {
      return next(new restify.MissingParameterError("Missing code parameter."));
    }

    // Hold temporary state
    var tempToken = new TempToken({
      cluestrCode: req.params.code,
    });

    initAccountCb(req, function(err, preDatas, redirectUrl) {
      if(err) {
        return next(err);
      }

      tempToken.datas = preDatas;
      tempToken.save(function(err) {
        if(err) {
          return next(err);
        }

        res.send(302, null, {
          Location: redirectUrl
        });
        next();
      });
    });
  };
};

/**
 * The user is redirected to this handler after getting access to his provider.
 *
 * @param {Object} req Request object from the client
 * @param {Object} res Response we want to return
 * @param {Function} next Callback to call once res has been populated.
 */
exports.callback = function(connectAccountRetrievePreDatasIdentifier, connectAccountRetrieveAuthDatas, cluestrClient, connectUrl, redirectUrl) {
  return function (req, res, next) {
    async.waterfall([
      function(cb) {
        // Retrieve the previous tempToken
        connectAccountRetrievePreDatasIdentifier(req, cb);
      },
      function(hash, cb) {
        TempToken.findOne(hash, cb);
      },
      function(tempToken, cb) {
        if(!tempToken) {
          return cb(new Error("Unable to retrieve provider preDatas."));
        }

        // Retrieve datas from provider
        connectAccountRetrieveAuthDatas(req, tempToken.datas, function(err, providerDatas) {
          cb(err, tempToken, providerDatas);
        });
      },
      function(tempToken, providerDatas, cb) {
        // Create a new cluestr access-token with our grant
        cluestrClient.getAccessToken(tempToken.cluestrCode, connectUrl, function(err, cluestrAccessToken) {
          cb(err, tempToken, cluestrAccessToken, providerDatas);
        });
      },
      function(tempToken, cluestrAccessToken, providerDatas, cb) {
        // Create new token
        var token = new Token({
          cluestrToken: cluestrAccessToken,
          datas: providerDatas
        });

        token.save(function(err) {
          cb(err, tempToken);
        });
      },
      function(tempToken, cb) {
        tempToken.remove(cb);
      }
    ], function(err) {
      if(err) {
        return next(err);
      }
      
      res.send(302, null, {
        Location: redirectUrl
      });

{'fraction_non_alphanumeric': 0.11123296255679148, 'fraction_numerical': 0.0018799937333542221, 'mean_word_length': 2.5925717501406864, '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': '394442', 'n_tokens_mistral': 1844, 'n_tokens_neox': 1807, 'n_words': 510}