<commit_before>'use strict';

const db = require('../models/db');
var config = require('config');

module.exports = (req, res, next) => {
  const token = req.cookies["sdsession"];
  
  if (token && token != "null" && token != null) {
    db.Session.findOne({where: {token: token}})
      .then(session => {
        if (!session) {
          // session not found
          next();
        }
        else db.User.findOne({where: {_id: session.user_id}})
          .then(user => {
            if (!user) {
              var domain = (process.env.NODE_ENV == "production") ? new URL(config.get('endpoint')).hostname : req.headers.hostname;
              res.clearCookie('sdsession', { domain: domain });

              if (req.accepts("text/html")) {
                res.send("Please clear your cookies and try again.");
              } else if (req.accepts('application/json')) {
                res.status(403).json({
                  "error": "token_not_found"
                });
              } else {
                res.send("Please clear your cookies and try again.");
              }
              
            } else {
              req["token"] = token;
              req["user"] = user;
              next();
            }
          });
      })
      .error(err => {
        console.error("Session resolve error",err);
        next();
      });
  } else {
    next();
  }
}
<commit_msg>allow auth via api_token<commit_after>'use strict';

const db = require('../models/db');
var config = require('config');

module.exports = (req, res, next) => {

  // authentication via API token
  const api_token = req.headers["x-spacedeck-api-token"];

  if (api_token && api_token.length>7) {
    db.User.findOne({where: {api_token: api_token}}).then(user => {
      req.user = user;
      next();
    }).error(err => {
      res.status(403).json({
        "error": "invalid_api-token"
      });
      next();
    });

    return;
  }

  // authentication via session/cookie
  const token = req.cookies["sdsession"];

  if (token && token != "null" && token != null) {
    db.Session.findOne({where: {token: token}})
      .then(session => {
        if (!session) {
          // session not found
          next();
        }
        else db.User.findOne({where: {_id: session.user_id}})
          .then(user => {
            if (!user) {
              var domain = (process.env.NODE_ENV == "production") ? new URL(config.get('endpoint')).hostname : req.headers.hostname;
              res.clearCookie('sdsession', { domain: domain });

              if (req.accepts("text/html")) {
                res.send("Please clear your cookies and try again.");
              } else if (req.accepts('application/json')) {
                res.status(403).json({
                  "error": "token_not_found"
                });
              } else {
                res.send("Please clear your cookies and try again.");
              }

            } else {
              req["token"] = token;
              req["user"] = user;
              next();
            }
          });
      })
      .error(err => {
        console.error("Session resolve error",err);
        next();
      });
  } else {
    next();
  }
}
{'fraction_non_alphanumeric': 0.16390883546674992, 'fraction_numerical': 0.0031220730565095223, 'mean_word_length': 1.650124069478908, 'pattern_counts': {'":': 3, '<': 3, '<?xml version=': 0, '>': 14, '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': '623454', 'n_tokens_mistral': 928, 'n_tokens_neox': 909, 'n_words': 196}