<commit_before>const passport = require('passport');
const jsonwebtoken = require('jsonwebtoken');
const GithubStrategy = require('passport-github');

const Strategy = require('./Strategy');


const SECRET = 'secret';
const SUPER_SECRET = 'much more secret'
const COOKIE = {
  domain: process.env.COOKIE_DOMAIN,
  path: '/',
  secure: process.env.NODE_ENV === 'production'
}

passport.serializeUser((user, done) => done(null, user));
passport.deserializeUser((user, done) => done(null, user));

passport.use(
  new Strategy(
    {secret: SUPER_SECRET},
    (request, user, done) => done(null, user)));

function saveReferrer(req, res, next) {
  req.session.referrer = req.get('referrer');
  next();
}

function loadReferrer(req, res, next) {
  let {referrer} = req.session;
  req.session.referrer = void 0;
  req.session.save();

  res.redirect(referrer || '/')
}

module.exports = {
  SECRET,
  COOKIE,
  // route decorators
  requireUser (request, response, next) {
    passport.authenticate('cmyk', { session: false }, (err, user, info) =>
      ! user ? response.sendStatus(401)
      : request.login(user, (err) => err ? response.sendStatus(500) : next())
    )(request, response, next);
  },
  // Providers
  github: {
    connect(server) {
      passport.use(
        new GithubStrategy({
            clientID: process.env.GITHUB_CLIENT_ID,
            clientSecret: process.env.GITHUB_CLIENT_SECRET,
            callbackURL: `${server.locals.domain}/auth/github/callback`,
            scope: ['user', 'gist']
          },
          (accessToken, refreshToken, profile, done) =>
            done(null, {
              accessToken,
              refreshToken,
              profile: {...profile, _raw: false}})));

      server.get('/auth/github', saveReferrer, passport.authenticate('github'));
      server.get('/auth/github/callback', this.handleCallback, loadReferrer);
    },

    handleCallback (req, res, next) {
      passport.authenticate('github', function (err, user, info) {
        if (! err && user) try {
          req.session.authorization = jsonwebtoken.sign(user, SUPER_SECRET);
          res.cookie('authorization', req.session.authorization, COOKIE);
        } catch (e) { err = err || e }

        next(err);
      })(req, res, next);
    }
  }
}
<commit_msg>Set cookie max-age<commit_after>const passport = require('passport');
const jsonwebtoken = require('jsonwebtoken');
const GithubStrategy = require('passport-github');

const Strategy = require('./Strategy');


const SECRET = 'secret';
const SUPER_SECRET = 'much more secret'
const COOKIE = {
  domain: process.env.COOKIE_DOMAIN,
  maxAge: 1000 * 60 * 60 * 24 * 14,
  path: '/',
  secure: process.env.NODE_ENV === 'production'
}

passport.serializeUser((user, done) => done(null, user));
passport.deserializeUser((user, done) => done(null, user));

passport.use(
  new Strategy(
    {secret: SUPER_SECRET},
    (request, user, done) => done(null, user)));

function saveReferrer(req, res, next) {
  req.session.referrer = req.get('referrer');
  next();
}

function loadReferrer(req, res, next) {
  let {referrer} = req.session;
  req.session.referrer = void 0;
  req.session.save();

  res.redirect(referrer || '/')
}

module.exports = {
  SECRET,
  COOKIE,
  // route decorators
  requireUser (request, response, next) {
    passport.authenticate('cmyk', { session: false }, (err, user, info) =>
      ! user ? response.sendStatus(401)
      : request.login(user, (err) => err ? response.sendStatus(500) : next())
    )(request, response, next);
  },
  // Providers
  github: {
    connect(server) {
      passport.use(
        new GithubStrategy({
            clientID: process.env.GITHUB_CLIENT_ID,
            clientSecret: process.env.GITHUB_CLIENT_SECRET,
            callbackURL: `${server.locals.domain}/auth/github/callback`,
            scope: ['user', 'gist']
          },
          (accessToken, refreshToken, profile, done) =>
            done(null, {
              accessToken,
              refreshToken,
              profile: {...profile, _raw: false}})));

      server.get('/auth/github', saveReferrer, passport.authenticate('github'));
      server.get('/auth/github/callback', this.handleCallback, loadReferrer);
    },

    handleCallback (req, res, next) {
      passport.authenticate('github', function (err, user, info) {
        if (! err && user) try {
          req.session.authorization = jsonwebtoken.sign(user, SUPER_SECRET);
          res.cookie('authorization', req.session.authorization, COOKIE);
        } catch (e) { err = err || e }

        next(err);
      })(req, res, next);
    }
  }
}

{'fraction_non_alphanumeric': 0.15629742033383914, 'fraction_numerical': 0.0056362453934532845, 'mean_word_length': 3.040280210157618, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, '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': '1828374', 'n_tokens_mistral': 1525, 'n_tokens_neox': 1481, 'n_words': 308}