{"size": 3450, "ext": "js", "max_stars_repo_path": "app/authentication/init.js", "max_stars_repo_name": "pindinz/cardiophylla", "max_stars_repo_stars_event_min_datetime": "2019-05-27T13:41:50.000Z", "max_stars_repo_stars_event_max_datetime": "2019-05-27T13:41:50.000Z", "max_issues_repo_path": "app/authentication/init.js", "max_issues_repo_name": "pindinz/cardiophylla", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "app/authentication/init.js", "max_forks_repo_name": "pindinz/cardiophylla", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 33.8235294118, "max_line_length": 155, "alphanum_fraction": 0.5147826087}
(function () {
    'use strict';

    const JWT_COOKIE_NAME = 'jwt';
    const AUTHENTICATION_TIMEOUT = 900; //seconds

    const crypto = require('crypto');
    const fs = require('fs');
    const hbs = require('hbs');
    const jwt = require('jwt-express');
    const jwtSecret = process.env.JWT_SECRET || crypto.randomBytes(256).toString('hex');
    const jwtOptions = {
        cookie: JWT_COOKIE_NAME,
        cookieOptions: {httpOnly: true, 'Max-Age': AUTHENTICATION_TIMEOUT},
        stales: 1000 * AUTHENTICATION_TIMEOUT
    };
    const passport = require('passport');
    const passportJwt = require('passport-jwt');
    const JwtStrategy = passportJwt.Strategy;
    const ExtractJwt = passportJwt.ExtractJwt;
    const jwtStrategyOptions = {
        jwtFromRequest: ExtractJwt.fromExtractors([jwtFromRequestCookie]),
        secretOrKey: jwtSecret,
        ignoreExpiration: false
    };
    const logger = require('winston');
    const contextFactory = require('../util/contextFactory');
    const userService = require('../user/service');


    function init(app) {

        app.use(jwt.init(jwtSecret, jwtOptions));

        passport.use(new JwtStrategy(jwtStrategyOptions, jwtVerify));

        app.use(passport.initialize());

        app.use('*',
            function (req, res, next) {
                passport.authenticate('jwt',
                    function (err, user, info) {
                        if (info && info.name === 'JsonWebTokenError') {
                            logger.warn('Unauthorized: Invalid JWT');
                            jwt.clear();
                            const context = contextFactory.createContext(req, res, {messageKey: 'authentication.authenticationFailed.message.jwtInvalid'});
                            return res.status(401).render('authentication/authenticationFailed', context);
                        }
                        if (err) {
                            logger.error(err);
                            return next(err);
                        }
                        if (!user) {
                            user = {};
                        }
                        req.user = user;
                        return next(null, req, res);
                    })(req, res, next);
            });


        // error handler
        app.use(function (err, req, res, next) {
            console.error(err);
            if (err.name === 'UnauthorizedError' && err.message === 'invalid signature') {
                logger.warn('Unauthorized: Invalid JWT');
                logger.debug(err);
                jwt.clear();
                res.redirect('/');
            }
            else {
                next(err, req, res, next);
            }
        });

        hbs.registerPartial('logout', fs.readFileSync(__dirname + '/logout.hbs', 'utf8'));

        require('./routes').init(app);
    }

    function jwtFromRequestCookie(req) {
        let token = null;
        if (req && req.cookies && req.cookies[JWT_COOKIE_NAME]) {
            token = req.cookies[JWT_COOKIE_NAME];
        }
        return token;
    }

    function jwtVerify(jwtPayload, done) {
        userService.loadById(jwtPayload.sub)
            .then(function (user) {
                console.log(user);
                return done(null, user);
            })
            .catch(function (err) {
                return done(null, {});
            });
    }

    module.exports = {init: init};

})();