<commit_before>import {unauthorized, forbidden} from 'express-easy-helper';
import {verify} from '../../lib/token';
import {exits as rsExits, ttl as rsTtl} from '../../lib/redis/sessions';
import {get as ruGet} from '../../lib/redis/users';
import {decrypt} from '../../lib/utility/crypto';
import {hasRole} from '../../lib/utility/role';

// VerifyToken
export async function verifyToken(req, authOrSecDef, token, cb) {

  //these are the scopes/roles defined for the current endpoint
  let currentScopes = req.swagger.operation["x-security-scopes"];

  if (token) {
    // Bearer
    req.headers.authorization = `Bearer ${token}`;
    //Verify Token
    let decode = await verify(token);
    if (!decode)
      return cb(forbidden(req.res));

    let key = `${decode._id}:${decode._verify}`;

    // Verify if exits token in redis
    if (!await rsExits(key))
      return cb(unauthorized(req.res));

    // Extract info user from redis
    let _user = await ruGet(decode._id);
    if (!_user)
      return cb(unauthorized(req.res));

    //decrypt redis
    _user = await JSON.parse(decrypt(_user));

    //user is enabled?
    if (!_user.status)
      return cb(unauthorized(req.res));

    //set TTL
    _user.ttl = await rsTtl(key);
    //set key
    _user.key = key;
    //If id's not equals
    if (_user._id !== decode._id)
      return cb(forbidden(req.res));

    //Verify Roles
    if (currentScopes)
      if (!hasRole(currentScopes, _user.roles))
        return cb(forbidden(req.res));
  //Success
    req.user = _user;
    return cb(null);
  } else {
    return cb(forbidden(req.res));
  }
}<commit_msg>decode data from db<commit_after>import {unauthorized, forbidden} from 'express-easy-helper';
import {exits as rExits, ttl as rTtl} from '../../lib/redis';
import {hasRole} from '../../lib/utility/role';
import {verify} from '../../lib/token';
import User from '../../api/models/user';

// VerifyToken
export async function verifyToken(req, authOrSecDef, token, cb) {

  // these are the scopes/roles defined for the current endpoint
  let currentScopes = req.swagger.operation["x-security-scopes"];

  if (token) {
    // Bearer
    req.headers.authorization = `Bearer ${token}`;

    // Verify Token
    let decode = await verify(token);
    if (!decode)
      return cb(forbidden(req.res));

    // Key session in redis
    let key = `${decode._id}:${decode._verify}`;

    // Verify if exits token in redis
    if (!await rExits(key))
      return cb(unauthorized(req.res));

    // Extract info user from MongoDB
    let _user = await User.findById(decode._id).select('-social').exec();
    if (!_user)
      return cb(unauthorized(req.res));

    // If id's not equals
    if (_user._id.toString() !== decode._id.toString())
      return cb(forbidden(req.res));

    // User is enabled?
    if (!_user.status)
      return cb(unauthorized(req.res));

    // Verify Roles
    if (currentScopes)
      if (!hasRole(currentScopes, _user.roles))
        return cb(forbidden(req.res));

    // get current TTL
    let ttl = await rTtl(key);

    // Success
    req.user = Object.assign({
      key,
      ttl
    }, _user._doc);

    return cb(null);
  } else {
    return cb(forbidden(req.res));
  }
}
{'fraction_non_alphanumeric': 0.1555417572182552, 'fraction_numerical': 0.0, 'mean_word_length': 2.7728337236533958, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5877191', 'n_tokens_mistral': 1063, 'n_tokens_neox': 1059, 'n_words': 281}