<commit_before>// auth.js

var
    async = require('async');
var
    api = require('../api'),
    db = require('../db'),
    auth = require('./_auth'),
    utils = require('./_utils'),
    config = require('../config');

var
    User = db.user,
    AuthUser = db.authuser,
    warp = db.warp,
    next_id = db.next_id;

function getUsers(page, callback) {
    User.findNumber('count(*)', function(err, num) {
        if (err) {
            return callback(err);
        }
        page.totalItems = num;
        if (page.isEmpty) {
            return callback(null, {
                page: page,
                users: []
            });
        }
        User.findAll({
            offset: page.offset,
            limit: page.limit,
            order: 'created_at desc'
        }, function(err, users) {
            if (err) {
                return callback(err);
            }
            return callback(null, {
                page: page,
                users: users
            });
        });
    });
}

function getUser(id, tx, callback) {
    if (arguments.length===2) {
        callback = tx;
        tx = undefined;
    }
    User.find(id, tx, function(err, user) {
        if (err) {
            return callback(err);
        }
        if (user===null) {
            return callback(api.notFound('User'));
        }
        callback(null, user);
    });
}

function createAuthUser(user, authUser, callback) {
    warp.transaction(function(err, tx) {
        if (err) {
            return callback(err);
        }
        async.series({
            auth_user: function(callback) {
                AuthUser.create(authUser, tx, callback);
            },
            user: function(callback) {
                User.create(user, tx, callback);
            }
        }, function(err, result) {
            tx.done(err, function(err) {
                if (err) {
                    return callback(err);
                }
                return callback(null, result.auth_user, result.user);
            });
        });
    });
}

function processAuthentication(provider, authentication, callback) {
    console.log(JSON.stringify(authentication));
    var auth_id = provider + ':' + authentication.auth_id;
    AuthUser.find({
        where: 'auth_id=?',
        params: [auth_id]
    }, function(err, au) {
        if (err) {
            return callback(err);
        }
        if (au===null) {
            var user_id = next_id();
            var user = {
                id: user_id,
                email: user_id + '@itranswarp.org',
                name: authentication.name,
                passwd: '',
                image_url: '',
            };
            var authUser = {
                user_id: user_id,
                auth_provider: provider,
                auth_id: auth_id,
                auth_token: authentication.access_token,
                expires_at: Date.now() + authentication.expires_in
            };
            createAuthUser(user, authUser, function(err, au, u) {
                if (err) {
                    return callback(err);
                }
                callback(null, au, u);
            });
            return;
        }
        // update auth user:
        au.auth_token = authentication.access_token;
        au.expires_at = Date.now() + authentication.expires_in;
        au.update(function(err, result) {
            getUser(au.user_id, function(err, u) {
                if (err) {
                    return callback(err);
                }
                if (u.name===authentication.name && u.image_url===authentication.image_url) {
                    return callback(null, au, u);
                }
                // update user:
                u.name = authentication.name;
                u.image_url = authentication.image_url;
                u.update(function(err, u) {
                    if (err) {
                        return callback(err);
                    }
                    callback(null, au, u);
                });
            });
        });
    });
}

var httpPrefix = 'http://' + config.domain;
var httpsPrefix = 'https://' + config.domain;

exports = module.exports = {

    getUser: getUser,

    getUsers: getUsers,

    'GET /auth/signout': function(req, res, next) {
        res.clearCookie(utils.SESSION_COOKIE_NAME);
        var referer = req.get('referer') || '/';
        if (referer.indexOf('/manage/')>=0 || referer.indexOf('/auth/')>=0) {
            referer = '/';
        }
        res.redirect(referer);
    },

    'GET /auth/': function(req, res, next) {
        /**
         * Display authentication page.
         */
        res.render('auth/signin.html', {});
    },

    'GET /auth/from/:name': function(req, res, next) {
        var provider = auth[req.params.name];
        if (provider) {
            var redirect = req.get('referer');
            if (redirect.indexOf('/auth/')>=0 || redirect.indexOf('/manage')>=0) {
                redirect = '/';
            }
            var redirect_uri = 'http://' + req.host + '/auth/callback/' + req.params.name + '?redirect=' + encodeURIComponent(redirect);
            var jscallback = req.query.jscallback;
            if (jscallback) {
                redirect_uri = redirect_uri + '&jscallback=' + jscallback;
            }
            return res.redirect(provider.getAuthenticateURL({
                redirect_uri: redirect_uri
            }));
        }
        return res.send(404, 'Not Found');
    },

    'GET /auth/callback/:name': function(req, res, next) {
        var providerName = req.params.name;
        var provider = auth[req.params.name];
        if (! provider) {
            return res.send(404, 'Not Found');
        }
        var code = req.query.code;
        if (! code) {

        }
        provider.getAuthentication({
            code: code
        }, function(err, authentication) {
            if (err) {
                res.send(err);
            }
            // check AuthUser:
            processAuthentication(providerName, authentication, function(err, authUser, user) {
                var cookieStr = utils.makeSessionCookie(providerName, authUser.id, authUser.auth_token, authUser.expires_at);
                res.cookie(utils.SESSION_COOKIE_NAME, cookieStr, {
                    path: '/',
                    expires: new Date(authUser.expires_at)
                });
                var jscallback = req.query.jscallback;
                if (jscallback) {
                    return res.send('<html><body><script> window.opener.' + jscallback + '(' + JSON.stringify({
                        id: user.id,
                        name: user.name,
                        image_url: user.image_url<commit_msg>fix signin failed<commit_after>// auth.js

var
    async = require('async');
var
    api = require('../api'),
    db = require('../db'),
    auth = require('./_auth'),
    utils = require('./_utils'),
    config = require('../config');

var
    User = db.user,
    AuthUser = db.authuser,
    warp = db.warp,
    next_id = db.next_id;

function getUsers(page, callback) {
    User.findNumber('count(*)', function(err, num) {
        if (err) {
            return callback(err);
        }
        page.totalItems = num;
        if (page.isEmpty) {
            return callback(null, {
                page: page,
                users: []
            });
        }
        User.findAll({
            offset: page.offset,
            limit: page.limit,
            order: 'created_at desc'
        }, function(err, users) {
            if (err) {
                return callback(err);
            }
            return callback(null, {
                page: page,
                users: users
            });
        });
    });
}

function getUser(id, tx, callback) {
    if (arguments.length===2) {
        callback = tx;
        tx = undefined;
    }
    User.find(id, tx, function(err, user) {
        if (err) {
            return callback(err);
        }
        if (user===null) {
            return callback(api.notFound('User'));
        }
        callback(null, user);
    });
}

function createAuthUser(user, authUser, callback) {
    warp.transaction(function(err, tx) {
        if (err) {
            return callback(err);
        }
        async.series({
            auth_user: function(callback) {
                AuthUser.create(authUser, tx, callback);
            },
            user: function(callback) {
                User.create(user, tx, callback);
            }
        }, function(err, result) {
            tx.done(err, function(err) {
                if (err) {
                    return callback(err);
                }
                return callback(null, result.auth_user, result.user);
            });
        });
    });
}

function processAuthentication(provider, authentication, callback) {
    console.log(JSON.stringify(authentication));
    var auth_id = provider + ':' + authentication.auth_id;
    AuthUser.find({
        where: 'auth_id=?',
        params: [auth_id]
    }, function(err, au) {
        if (err) {
            return callback(err);
        }
        if (au===null) {
            var user_id = next_id();
            var user = {
                id: user_id,
                email: user_id + '@<EMAIL>',
                name: authentication.name,
                passwd: '',
                image_url: '',
            };
            var authUser = {
                user_id: user_id,
                auth_provider: provider,
                auth_id: auth_id,
                auth_token: authentication.access_token,
                expires_at: Date.now() + authentication.expires_in
            };
            createAuthUser(user, authUser, function(err, au, u) {
                if (err) {
                    return callback(err);
                }
                callback(null, au, u);
            });
            return;
        }
        // update auth user:
        au.auth_token = authentication.access_token;
        au.expires_at = Date.now() + authentication.expires_in;
        au.update(function(err, result) {
            getUser(au.user_id, function(err, u) {
                if (err) {
                    return callback(err);
                }
                if (u.name===authentication.name && u.image_url===authentication.image_url) {
                    return callback(null, au, u);
                }
                // update user:
                u.name = authentication.name;
                u.image_url = authentication.image_url;
                u.update(function(err, u) {
                    if (err) {
                        return callback(err);
                    }
                    callback(null, au, u);
                });
            });
        });
    });
}

var httpPrefix = 'http://' + config.domain;
var httpsPrefix = 'https://' + config.domain;

exports = module.exports = {

    getUser: getUser,

    getUsers: getUsers,

    'GET /auth/signout': function(req, res, next) {
        res.clearCookie(utils.SESSION_COOKIE_NAME);
        var referer = req.get('referer') || '/';
        if (referer.indexOf('/manage/')>=0 || referer.indexOf('/auth/')>=0) {
            referer = '/';
        }
        res.redirect(referer);
    },

    'GET /auth/': function(req, res, next) {
        /**
         * Display authentication page.
         */
        res.render('auth/signin.html', {});
    },

    'GET /auth/from/:name': function(req, res, next) {
        var provider = auth[req.params.name];
        if (provider) {
            var redirect = req.get('referer');
            if (redirect.indexOf('/auth/')>=0 || redirect.indexOf('/manage')>=0) {
                redirect = '/';
            }
            var redirect_uri = 'http://' + req.host + '/auth/callback/' + req.params.name + '?redirect=' + encodeURIComponent(redirect);
            var jscallback = req.query.jscallback;
            if (jscallback) {
                redirect_uri = redirect_uri + '&jscallback=' + jscallback;
            }
            return res.redirect(provider.getAuthenticateURL({
                redirect_uri: redirect_uri
            }));
        }
        return res.send(404, 'Not Found');
    },

    'GET /auth/callback/:name': function(req, res, next) {
        var providerName = req.params.name;
        var provider = auth[req.params.name];
        if (! provider) {
            return res.send(404, 'Not Found');
        }
        var code = req.query.code;
        if (! code) {
            // something error:
            console.log('oauth signin failed...');
            var jscallback = req.query.jscallback;
            if (jscallback) {
                return res.send('<html><body><script> self.close(); </script></body></html>');
            }
            return res.redirect(req.query.redirect || '/');
        }
        provider.getAuthentication({
            code: code
        }, function(err, authentication) {
            if (err) {
                res.send(err);
            }
            // check AuthUser:
            processAuthentication(providerName, authentication, function(err, authUser, user) {
                var cookieStr = utils.makeSessionCookie(providerName, authUser.id, authUser.auth_token, authUser.expires_at);
                res.cookie(utils.SESSION_COOKIE_NAME, cookieStr, {
                    path: '/',
                    expires: new Date(authUser.expires_at)
                });
                var jscallback = req.query.jscallback;
                if (jscallback) {
                    return res.send('<html><body><script> window.opener.' + jscallback + '(' + JSON.stringify({
                        id: user.id,
                        name: user.name,
                        image_url: user.image_url
{'fraction_non_alphanumeric': 0.13189448441247004, 'fraction_numerical': 0.0015987210231814548, 'mean_word_length': 1.4305898975626987, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 24, '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': '3181320', 'n_tokens_mistral': 3892, 'n_tokens_neox': 3778, 'n_words': 749}