<commit_before>var passport = require('passport'),
    BasicStrategy = require('passport-http').BasicStrategy,
    BearerStrategy = require('passport-http-bearer').Strategy,
    FacebookTokenStrategy = require('passport-facebook-token'),
    ClientPasswordStrategy = require('passport-oauth2-client-password').Strategy;

// Find the src path
var src = process.cwd() + '/src/';

var config = require(src + 'helpers/conf'),
    log = require(src + 'helpers/log')(module);

var User = require(src + 'models/user'),
    Client = require(src + 'models/client'),
    AccessToken = require(src + 'models/accessToken');

passport.use(new BasicStrategy(
    function(username, password, done) {
        Client.findOne({ clientId: username }, function(err, client) {
            if (err) {
                return done(err);
            }
            if (!client) {
                return done(null, false);
            }
            if (client.clientSecret !== password) {
                return done(null, false);
            }
            return done(null, client);
        });
    }
));

passport.use(new ClientPasswordStrategy(
    function(clientId, clientSecret, done) {
        Client.findOne({ clientId: clientId }, function(err, client) {
            if (err) {
                return done(err);
            }
            if (!client) {
                return done(null, false);
            }
            if (client.clientSecret !== clientSecret) {
                return done(null, false);
            }
            return done(null, client);
        });
    }
));

passport.use(new BearerStrategy(
    function(accessToken, done) {
        AccessToken.findOne({ token: accessToken }, function(err, token) {
            if (err) {
                return done(err);
            }
            if (!token) {
                return done(null, false);
            }
            if (Math.round((Date.now() - token.created) / 1000) > config.get('security:tokenLife') ) {

                AccessToken.remove({ token: accessToken }, function (err) {
                    if (err) {
                        return done(err);
                    }
                });
                return done(null, false, { message: 'Token expired' });
            }
            User.findById(token.userId, function(err, user) {

                if (err) {
                    return done(err);
                }
                if (!user) {
                    return done(null, false, { message: 'Unknown user' });
                }
                done(null, user, { scope: '*' });
            });
        });
    }
));

passport.use(new FacebookTokenStrategy(config.get('auth:facebook'),
    function (accessToken, refreshToken, profile, done) {
        var email     = profile.emails[0].value,
            photo_url = profile.photos[0].value,
            username  = profile._json.link.split('/').slice(-1)[0] || profile.displayName;

        User.findOrCreate({ facebookId: profile.id },
            {username: username, email: email, photo_url: photo_url, socialAuth: true}, function (err, user) {

            if (!err) {
                AccessToken.remove({ $or: [{ userId: user.userId }, { userId: profile.id }] });

                var token = new AccessToken(
                    {userId: profile.id, clientId: config.get('default:client:clientId'), token: accessToken}
                );
                token.save();
                return done(null, user);
            }
            var errFields = [];

            for (var field in err.errors) {
                errFields.push(field);
            }
            log.error('Validation Error on Fields: ' + errFields.toString());
            return done(err, user);
        });
    }
));<commit_msg>Add log for facebook login<commit_after>var passport = require('passport'),
    BasicStrategy = require('passport-http').BasicStrategy,
    BearerStrategy = require('passport-http-bearer').Strategy,
    FacebookTokenStrategy = require('passport-facebook-token'),
    ClientPasswordStrategy = require('passport-oauth2-client-password').Strategy;

// Find the src path
var src = process.cwd() + '/src/';

var config = require(src + 'helpers/conf'),
    log = require(src + 'helpers/log')(module);

var User = require(src + 'models/user'),
    Client = require(src + 'models/client'),
    AccessToken = require(src + 'models/accessToken');

passport.use(new BasicStrategy(
    function(username, password, done) {
        Client.findOne({ clientId: username }, function(err, client) {
            if (err) {
                return done(err);
            }
            if (!client) {
                return done(null, false);
            }
            if (client.clientSecret !== password) {
                return done(null, false);
            }
            return done(null, client);
        });
    }
));

passport.use(new ClientPasswordStrategy(
    function(clientId, clientSecret, done) {
        Client.findOne({ clientId: clientId }, function(err, client) {
            if (err) {
                return done(err);
            }
            if (!client) {
                return done(null, false);
            }
            if (client.clientSecret !== clientSecret) {
                return done(null, false);
            }
            return done(null, client);
        });
    }
));

passport.use(new BearerStrategy(
    function(accessToken, done) {
        AccessToken.findOne({ token: accessToken }, function(err, token) {
            if (err) {
                return done(err);
            }
            if (!token) {
                return done(null, false);
            }
            if (Math.round((Date.now() - token.created) / 1000) > config.get('security:tokenLife') ) {

                AccessToken.remove({ token: accessToken }, function (err) {
                    if (err) {
                        return done(err);
                    }
                });
                return done(null, false, { message: 'Token expired' });
            }
            User.findById(token.userId, function(err, user) {

                if (err) {
                    return done(err);
                }
                if (!user) {
                    return done(null, false, { message: 'Unknown user' });
                }
                done(null, user, { scope: '*' });
            });
        });
    }
));

passport.use(new FacebookTokenStrategy(config.get('auth:facebook'),
    function (accessToken, refreshToken, profile, done) {
        var email     = profile.emails[0].value,
            photo_url = profile.photos[0].value,
            username  = profile._json.link.split('/').slice(-1)[0] || profile.displayName;

        User.findOrCreate({ facebookId: profile.id },
            {username: username, email: email, photo_url: photo_url, socialAuth: true}, function (err, user) {

            if (!err) {
                AccessToken.remove({ $or: [{ userId: user.userId }, { userId: profile.id }] });

                var token = new AccessToken(
                    {userId: profile.id, clientId: config.get('default:client:clientId'), token: accessToken}
                );
                token.save();
                return done(null, user);
            }
            if (err.name === 'ValidationError') {
                var errFields = [];

                for (var field in err.errors) {
                    errFields.push(field);
                }
                log.error('Validation Error on Fields: ' + errFields.toString());
            }
            return done(err, user);
        });
    }
));
{'fraction_non_alphanumeric': 0.13363264228179056, 'fraction_numerical': 0.0023768651789251288, 'mean_word_length': 1.6706629055007052, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7439649', 'n_tokens_mistral': 2046, 'n_tokens_neox': 1966, 'n_words': 437}