<commit_before>/**
 * Created by a on 6/21/2015.
 */

module.exports= function(app,db) {

    var signindb = db.handler;
    // get the app environment from Cloud Foundry
    var cfenv = require('cfenv');
    var appEnv = cfenv.getAppEnv();

    var uuid = require('node-uuid');

    var survey = require('./surveymanager.js');

    //JSON structure of the documents in twitterusers signindb
    /*
    {
        user_id: 1123213    //twitter user_id as the primary key
        screen_name: votesavvyrhok  //the screen_name of the user
        user_tokem: uuid    // the user identifier in votesavvyrhok
    }
    */
    //the key of votesavvyrhok app under twitter account votesavvyrhok
    //in twitter application management

    var myConfig = {
        "consumerKey": "<KEY>",
        "consumerSecret": "<KEY>",
        "accessToken": "<KEY>",
        "accessTokenSecret": "<KEY>",
        "callBackUrl": appEnv.url + "/signintwitters/step2"
    };

    //stores the temporarily information of a user during the signing in procedure
    //for each sign in user, it stores the request token secret and generates a client token
    //the client token is a random number
    var oauthStore = {};

    var twitterLibrary = require('twitter-node-client');

    var twitterHdl = new twitterLibrary.Twitter(myConfig);

    app.get('/signintwitters/step1', function (req, res) {

        var signinUrl = 'https://api.twitter.com/oauth/authenticate?oauth_token=';

        console.log("you are at step 1 now");

        var usertoken={};

        //retrieve the oauth request token
        twitterHdl.getOAuthRequestToken(function (oauth) {
            if (oauth != null) {
                console.log(oauth);

                //store the user tokens in the oauthStore temporarily
                usertoken.token_secret = oauth.token_secret;
                //the client token is a uuid
                usertoken.client_token = uuid.v4();
                oauthStore[oauth.token] = usertoken;

                //redirect to the Twitter sign in URL
                signinUrl = signinUrl + oauth.token;
                console.log('sign in url: ' + signinUrl);
                console.log('oauthStore: ' + JSON.stringify(oauthStore));
                res.redirect(signinUrl);
            }
            else {
                console.log("error on retrieving request token");
                res.status(500).send("error on retrieving request token");

            }
        });
    });

    app.get('/signintwitters/user/:username', function (req, res) {

            var username = req.params.username;

            var user = twitterHdl.getUser({screen_name: username}, function (error) {
                res.status(500).send("error on retrieving user:" + username);
            }, function (body) {
                console.log(body);
                res.send(body);

            });
        }
    )

    var getOAuthAccessToken = function (oauth, next) {
        twitterHdl.oauth.getOAuthAccessToken(oauth.token, oauth.token_secret, oauth.verifier,
            function (error, oauth_access_token, oauth_access_token_secret, results) {
                if (error) {
                    console.log('ERROR: ' + error);
                    next();
                } else {
                    oauth.access_token = oauth_access_token;
                    oauth.access_token_secret = oauth_access_token_secret;

                    console.log('oauth.token: ' + oauth.token);
                    console.log('oauth.token_secret: ' + oauth.token_secret);
                    console.log('oauth.access_token: ' + oauth.access_token);
                    console.log('oauth.access_token_secret: ' + oauth.access_token_secret);
                    next(oauth,results);
                }
            }
        );
    };

    app.get('/signintwitters/step2', function (req, res) {
        var oauth_token = req.query.oauth_token;

        var oauth_verifier = req.query.oauth_verifier;

        var denied = req.query.denied;

        //the authentication is denied
        if (denied)
        {
            res.redirect('/');
            return;
        }

        console.log("you are at step 2 now");

        console.log("oauthStore: " + JSON.stringify(oauthStore));

        console.log("oauth_token " + oauth_token);
        console.log("oauth_verifier " + oauth_verifier);

        var oauthStep2 = {
            "token": oauth_token,
            "token_secret": oauthStore[oauth_token].token_secret,
            "verifier": oauth_verifier
        }

        console.log("oauthStep2: " + JSON.stringify(oauthStep2));

        getOAuthAccessToken(oauthStep2, function (oauth, results) {<commit_msg>processing cancel during signin<commit_after>/**
 * Created by a on 6/21/2015.
 */

module.exports= function(app,db) {

    var signindb = db.handler;
    // get the app environment from Cloud Foundry
    var cfenv = require('cfenv');
    var appEnv = cfenv.getAppEnv();

    var uuid = require('node-uuid');

    var survey = require('./surveymanager.js');

    //JSON structure of the documents in twitterusers signindb
    /*
    {
        user_id: 1123213    //twitter user_id as the primary key
        screen_name: votesavvyrhok  //the screen_name of the user
        user_tokem: uuid    // the user identifier in votesavvyrhok
    }
    */
    //the key of votesavvyrhok app under twitter account votesavvyrhok
    //in twitter application management

    var myConfig = {
        "consumerKey": "vf1TA6dx62BgDVIskWmILJKmb",
        "consumerSecret": "<KEY>",
        "accessToken": "<KEY>",
        "accessTokenSecret": "<KEY>",
        "callBackUrl": appEnv.url + "/signintwitters/step2"
    };

    //stores the temporarily information of a user during the signing in procedure
    //for each sign in user, it stores the request token secret and generates a client token
    //the client token is a random number
    var oauthStore = {};

    var twitterLibrary = require('twitter-node-client');

    var twitterHdl = new twitterLibrary.Twitter(myConfig);

    app.get('/signintwitters/step1', function (req, res) {

        var signinUrl = 'https://api.twitter.com/oauth/authenticate?oauth_token=';

        console.log("you are at step 1 now");

        var usertoken={};

        //retrieve the oauth request token
        twitterHdl.getOAuthRequestToken(function (oauth) {
            if (oauth != null) {
                console.log(oauth);

                //store the user tokens in the oauthStore temporarily
                usertoken.token_secret = oauth.token_secret;
                //the client token is a uuid
                usertoken.client_token = uuid.v4();
                oauthStore[oauth.token] = usertoken;

                //redirect to the Twitter sign in URL
                signinUrl = signinUrl + oauth.token;
                console.log('sign in url: ' + signinUrl);
                console.log('oauthStore: ' + JSON.stringify(oauthStore));
                res.redirect(signinUrl);
            }
            else {
                console.log("error on retrieving request token");
                res.status(500).send("error on retrieving request token");

            }
        });
    });

    app.get('/signintwitters/user/:username', function (req, res) {

            var username = req.params.username;

            var user = twitterHdl.getUser({screen_name: username}, function (error) {
                res.status(500).send("error on retrieving user:" + username);
            }, function (body) {
                console.log(body);
                res.send(body);

            });
        }
    )

    var getOAuthAccessToken = function (oauth, next) {
        twitterHdl.oauth.getOAuthAccessToken(oauth.token, oauth.token_secret, oauth.verifier,
            function (error, oauth_access_token, oauth_access_token_secret, results) {
                if (error) {
                    console.log('ERROR: ' + error);
                    next();
                } else {
                    oauth.access_token = oauth_access_token;
                    oauth.access_token_secret = oauth_access_token_secret;

                    console.log('oauth.token: ' + oauth.token);
                    console.log('oauth.token_secret: ' + oauth.token_secret);
                    console.log('oauth.access_token: ' + oauth.access_token);
                    console.log('oauth.access_token_secret: ' + oauth.access_token_secret);
                    next(oauth,results);
                }
            }
        );
    };

    app.get('/signintwitters/step2', function (req, res) {
        var oauth_token = req.query.oauth_token;

        var oauth_verifier = req.query.oauth_verifier;

        var denied = req.query.denied;

        //the authentication is denied
        if (denied)
        {
            res.redirect('/');
            return;
        }

        console.log("you are at step 2 now");

        console.log("oauthStore: " + JSON.stringify(oauthStore));

        var oauthStep2 = {
            "token": oauth_token,
            "token_secret": oauthStore[oauth_token].token_secret,
            "verifier": oauth_verifier
        }

        console.log("oauthStep2: " + JSON.stringify(oauthStep2));

        getOAuthAccessToken(oauthStep2, function (oauth, results) {
{'fraction_non_alphanumeric': 0.10832885545405696, 'fraction_numerical': 0.006878022568511553, 'mean_word_length': 1.9402843601895734, 'pattern_counts': {'":': 16, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 2, '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': '2098941', 'n_tokens_mistral': 2769, 'n_tokens_neox': 2592, 'n_words': 609}