<commit_before>var qs   = require('querystring');
var fs   = require('fs');
var jwt  = require('jsonwebtoken');

// Content
var index      = fs.readFileSync(__dirname+'/index.html');      // default page
var restricted = fs.readFileSync(__dirname+'/restricted.html'); // only show if JWT valid
var fail       = fs.readFileSync(__dirname+'/fail.html');       // auth fail

// secret
var secret = "CHANGE_THIS_TO_SOMETHING_RANDOM";

// show fail page (login)
function authFail(res) {
  res.writeHead(401, {'Content-Type': 'text/html'});
  res.end(fail);
}

function authSuccess(req, res) {
  // create JWT
  var token = jwt.sign({
    auth:  'magic',
    agent: req.headers['user-agent'],
    exp:   new Date().getTime() + 7*24*60*60*1000 // JS timestamp is ms...
  }, secret);

  res.writeHead(200, {
    'Content-Type': 'text/html',
    'x-access-token': token
  });
  res.end(restricted);
}

// lookup person in "database"
var db = { un: 'masterbuilder', pw: '<PASSWORD>' };

// handle authorisation requests
function authHandler(req,res){
  // >> lookup the actual user in our database in "real" app
  console.log("METHOD: "+req.method)
  if (req.method == 'POST') {
    var body = '';
    req.on('data', function (data) {
      body += data;
    }).on('end', function () {
      var post = qs.parse(body);
      // authentication success
      if(post.username && post.username === db.un && post.password && post.password === db.pw){
        authSuccess(req, res);
      } else {
        return authFail(res);
      }
    });
  } else {
    return authFail(res);
  }
}

function tokenValid(req, res) {
  console.log(req.headers)
  var token = req.headers['x-access-token'];
  jwt.verify(token, secret, function(err, decoded){
    if(err || !decoded || decoded.auth !== 'magic') {
      return authFail(res);
    } else {
      return privado(res, token);
    }
  });
}

function privado(res, token) {
  res.writeHead(200, {
    'Content-Type': 'text/html',
    'x-access-token': token
  });
  return res.end(restricted);
}

function exit(res) {
  res.writeHead(404, {'Content-Type': 'text/plain'});
  res.end('bye');
  process.exit(); // kill the server!
}

function notFound(res) {
  res.writeHead(404, {'Content-Type': 'text/plain'});
  res.end('Not Found');
}

function home(res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end(index);
}

function logout(res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Logged Out!');
}

module.exports = {
  fail : authFail,
  success : authSuccess,
  handler : authHandler,
  validate : tokenValid,
  notFound : notFound,
  exit: exit,
  home: home
}<commit_msg>update helpers to use views/*.html<commit_after>var qs   = require('querystring');
var fs   = require('fs');
var jwt  = require('jsonwebtoken');

// Content
var index      = fs.readFileSync(__dirname+'/views/index.html');      // default page
var restricted = fs.readFileSync(__dirname+'/views/restricted.html'); // only show if JWT valid
var fail       = fs.readFileSync(__dirname+'/views/fail.html');       // auth fail

// secret
var secret = "CHANGE_THIS_TO_SOMETHING_RANDOM";

// show fail page (login)
function authFail(res) {
  res.writeHead(401, {'Content-Type': 'text/html'});
  res.end(fail);
}

function authSuccess(req, res) {
  // create JWT
  var token = jwt.sign({
    auth:  'magic',
    agent: req.headers['user-agent'],
    exp:   new Date().getTime() + 7*24*60*60*1000 // JS timestamp is ms...
  }, secret);

  res.writeHead(200, {
    'Content-Type': 'text/html',
    'x-access-token': token
  });
  return res.end(restricted);
}

// lookup person in "database"
var db = { un: 'masterbuilder', pw: '<PASSWORD>' };

// handle authorisation requests
function authHandler(req,res){
  // >> lookup the actual user in our database in "real" app
  console.log("METHOD: "+req.method)
  if (req.method == 'POST') {
    var body = '';
    req.on('data', function (data) {
      body += data;
    }).on('end', function () {
      var post = qs.parse(body);
      // authentication success
      if(post.username && post.username === db.un && post.password && post.password === <PASSWORD>){
        authSuccess(req, res);
      } else {
        return authFail(res);
      }
    });
  } else {
    return authFail(res);
  }
}

function tokenValid(req, res) {
  console.log(req.headers)
  var token = req.headers['x-access-token'];
  jwt.verify(token, secret, function(err, decoded){
    if(err || !decoded || decoded.auth !== 'magic') {
      return authFail(res);
    } else {
      return privado(res, token);
    }
  });
}

function privado(res, token) {
  res.writeHead(200, {
    'Content-Type': 'text/html',
    'x-access-token': token
  });
  return res.end(restricted);
}

function exit(res) {
  res.writeHead(404, {'Content-Type': 'text/plain'});
  res.end('bye');
  process.exit(); // kill the server!
}

function notFound(res) {
  res.writeHead(404, {'Content-Type': 'text/plain'});
  return res.end('Not Found');
}

function home(res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  return res.end(index);
}

function logout(res) {
  // "destroy" (invalidate) the token
  res.writeHead(200, {'Content-Type': 'text/plain'});
  return res.end('Logged Out!');
}

module.exports = {
  fail : authFail,
  exit: exit,
  home: home,
  handler : authHandler,
  notFound : notFound,
  success : authSuccess,
  validate : tokenValid
}
{'fraction_non_alphanumeric': 0.17914042237865876, 'fraction_numerical': 0.011856243052982586, 'mean_word_length': 3.2578864353312302, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 10, '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': '2439524', 'n_tokens_mistral': 1902, 'n_tokens_neox': 1836, 'n_words': 418}