{"size": 993, "ext": "js", "max_stars_repo_path": "api/middleware/token.js", "max_stars_repo_name": "18F/acq-mo-medicaid-prototype-realtime-view", "max_stars_repo_stars_event_min_datetime": "2017-07-21T02:19:22.000Z", "max_stars_repo_stars_event_max_datetime": "2017-07-21T02:19:22.000Z", "max_issues_repo_path": "api/middleware/token.js", "max_issues_repo_name": "18F/acq-mo-medicaid-prototype-realtime-view", "max_issues_repo_issues_event_min_datetime": "2017-03-02T16:18:16.000Z", "max_issues_repo_issues_event_max_datetime": "2017-04-19T13:48:09.000Z", "max_forks_repo_path": "api/middleware/token.js", "max_forks_repo_name": "18F/cms-apd-prototype-budget-table", "max_forks_repo_forks_event_min_datetime": "2017-07-14T01:17:50.000Z", "max_forks_repo_forks_event_max_datetime": "2021-02-14T11:12:42.000Z"}
{"max_stars_count": 1, "max_issues_count": 11, "max_forks_count": 3, "avg_line_length": 33.1, "max_line_length": 111, "alphanum_fraction": 0.6213494461}
const tokens = require('../tokens');

module.exports = function token(app) {
  app.use((req, res, next) => {
    // Disable the reassignment rule here because we actually
    // do want to modify the req object.
    delete req.user; // eslint-disable-line no-param-reassign

    if (req.headers.authorization) {
      try {
        // Attach the user object to the request, so it can
        // easily be checked elsewhere.  Disable the eslint
        // rule because this is in fact what we want.
        req.user = tokens.getUserFromToken(req.headers.authorization); // eslint-disable-line no-param-reassign
      } catch (e) {
        // If there's an authorization token that we can't
        // verify, send back a 401 and bail out of the
        // request altogether.
        res.status(401).send('Invalid token');

        // [TODO] Remove this, replace with proper logging.
        console.log(e); // eslint-disable-line no-console

        return;
      }
    }
    next();
  });
};
