<commit_before>/*
 Copyright 2015 Province of British Columbia

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and limitations under the License.
 */

var express = require('express');
var helmet = require('helmet');
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
var bodyParser = require('body-parser');
var logger = require('./common/logging.js').logger;
var config = require('config');
var passport = require('passport');
var GitHubStrategy = require('passport-github').Strategy;
var LinkedInStrategy = require('passport-linkedin-oauth2').Strategy;
var async = require('async');

// set up db connection
var db = require('./app/models/db');

// Passport session setup
passport.serializeUser(function(user, done) {
    done(null, user);
});

passport.deserializeUser(function(user, done) {
    done(null, user);
});

// use GitHubStrategy with Passport
passport.use(new GitHubStrategy({
        clientID: (process.env.GH_CLIENT_ID || config.github.clientID),
        clientSecret: (process.env.GH_CLIENT_SECRET || config.github.clientSecret),
        callbackURL: config.github.callbackURL,
        passReqToCallback: true
    },
    function(req, accessToken, refreshToken, extProfile, done) {
        passportStrategySetup(req, accessToken, refreshToken, extProfile, done);
    }
));


// use LinkedInStrategy with Passport
passport.use(new LinkedInStrategy({
    clientID: (process.env.LI_CLIENT_ID || config.linkedin.clientID),
    clientSecret: (process.env.LI_CLIENT_SECRET || config.linkedin.clientSecret),
    callbackURL: config.linkedin.callbackURL,
    passReqToCallback: true,
    scope: ['r_fullprofile'],
    state: true
}, function(req, accessToken, refreshToken, extProfile, done) {
        passportStrategySetup(req, accessToken, refreshToken, extProfile, done);
    }
));

function passportStrategySetup(req, accessToken, refreshToken, extProfile, done) {
    logger.info("logged in as " + extProfile.displayName + " from " + extProfile.provider);

    if (!req.user) {
        // not logged in
        // look for existing account

        db.getAccountByIdentity(extProfile.id, true,
            function (err, account){
                if (err) {
                    logger.error(err);
                    return done(err, null);
                }

                if (account) {

                    for( var i = 0; i < account.identities.length; i++ ) {
                        if ( account.identities[i].identifier === extProfile.id ) {
                            account.identities[i].accessToken = accessToken;
                            account.identities[i].refreshToken = refreshToken;
                            break;
                        }
                    }<commit_msg>fixed #24<commit_after>/*
 Copyright 2015 Province of British Columbia

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and limitations under the License.
 */

var express = require('express');
var helmet = require('helmet');
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
var bodyParser = require('body-parser');
var logger = require('./common/logging.js').logger;
var config = require('config');
var passport = require('passport');
var GitHubStrategy = require('passport-github').Strategy;
var LinkedInStrategy = require('passport-linkedin-oauth2').Strategy;
var async = require('async');

// set up db connection
var db = require('./app/models/db');

// Passport session setup
passport.serializeUser(function(user, done) {
    done(null, user);
});

passport.deserializeUser(function(user, done) {
    done(null, user);
});

// use GitHubStrategy with Passport
passport.use(new GitHubStrategy({
        clientID: (process.env.GH_CLIENT_ID || config.github.clientID),
        clientSecret: (process.env.GH_CLIENT_SECRET || config.github.clientSecret),
        callbackURL: config.github.callbackURL,
        passReqToCallback: true
    },
    function(req, accessToken, refreshToken, extProfile, done) {
        passportStrategySetup(req, accessToken, refreshToken, extProfile, done);
    }
));


// use LinkedInStrategy with Passport
passport.use(new LinkedInStrategy({
    clientID: (process.env.LI_CLIENT_ID || config.linkedin.clientID),
    clientSecret: (process.env.LI_CLIENT_SECRET || config.linkedin.clientSecret),
    callbackURL: config.linkedin.callbackURL,
    passReqToCallback: true,
    scope: ['r_fullprofile'],
    state: true
}, function(req, accessToken, refreshToken, extProfile, done) {
        passportStrategySetup(req, accessToken, refreshToken, extProfile, done);
    }
));

function passportStrategySetup(req, accessToken, refreshToken, extProfile, done) {

    if (!req.user) {
        // not logged in
        // look for existing account

        db.getAccountByIdentity(extProfile.id, true,
            function (err, account){
                if (err) {
                    logger.error(err);
                    return done(err, null);
                }

                if (account) {

                    for( var i = 0; i < account.identities.length; i++ ) {
                        if ( account.identities[i].identifier === extProfile.id ) {
                            account.identities[i].accessToken = accessToken;
                            account.identities[i].refreshToken = refreshToken;
                            break;
                        }
                    }
{'fraction_non_alphanumeric': 0.10959771935381692, 'fraction_numerical': 0.003484320557491289, 'mean_word_length': 2.9642184557438793, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1236848', 'n_tokens_mistral': 1697, 'n_tokens_neox': 1636, 'n_words': 477}