lib/controllers/actions/reset.js
function validateToken(req, res, sails, params){
  var config = wl.config;
  var authConfig = wl.authConfig;
  if(params.token){
    try{
      // decode the token
      var _token = jwt.decode(params.token, config.jsonWebTokens.secret);

      // set the time of the request
      var _reqTime = Date.now();

      // If token is expired
      if(_token.exp <= _reqTime){
        return res.forbidden('Your token is expired.');
      }

      // If token is early
      if(_reqTime <= _token.nbf){
        return res.forbidden('This token is early.');
      }

      // If audience doesn't match
      if(config.jsonWebTokens.audience !== _token.aud){
        return res.forbidden('This token cannot be accepted for this domain.');
      }

      // If the subject doesn't match
      if('password reset' !== _token.sub){
        return res.forbidden('This token cannot be used for this request.');
      }

      sails.models.auth.findOne(_token.iss).populate('resetToken').exec(function(err, auth){
        if(typeof auth.resetToken === 'undefined' || params.token !== auth.resetToken.token){
          return res.forbidden('This token cannot be used.');
        }

        req.session.resetToken = auth.resetToken;

        if(authConfig.passwordReset.mail.forwardUrl){
          res.redirect(authConfig.passwordReset.mail.forwardUrl + '?token=' + auth.resetToken.token);
        }else{
          res.json(200);
        }

      });

    } catch(err){
      return res.serverError(err);
    }
  }else{
    //TODO limit attempts?
    req.session.resetToken = false;
    res.forbidden();
  }
}
function validateToken(req, res, sails, params){
  var config = wl.config;
  var authConfig = wl.authConfig;
  if(params.token){
    try{
      // decode the token
      var _token = jwt.decode(params.token, config.jsonWebTokens.secret);

      // set the time of the request
      var _reqTime = Date.now();

      // If token is expired
      if(_token.exp <= _reqTime){
        return res.forbidden('Your token is expired.');
      }

      // If token is early
      if(_reqTime <= _token.nbf){
        return res.forbidden('This token is early.');
      }

      // If audience doesn't match
      if(config.jsonWebTokens.audience !== _token.aud){
        return res.forbidden('This token cannot be accepted for this domain.');
      }

      // If the subject doesn't match
      if('password reset' !== _token.sub){
        return res.forbidden('This token cannot be used for this request.');
      }

      sails.models.auth.findOne(_token.iss).populate('resetToken').exec(function(err, auth){
        if(typeof auth.resetToken === 'undefined' || params.token !== auth.resetToken.token){
          return res.forbidden('This token cannot be used.');
        }

        req.session.resetToken = auth.resetToken;

        if(authConfig.passwordReset.mail.forwardUrl){
          res.redirect(authConfig.passwordReset.mail.forwardUrl + '?token=' + auth.resetToken.token);
        }else{
          res.json(200);
        }

      });

    } catch(err){
      return res.serverError(err);
    }
  }else{
    //TODO limit attempts?
    req.session.resetToken = false;
    res.forbidden();
  }
}
function validateToken ( req , res , sails , params ) { var config = wl . config ; var authConfig = wl . authConfig ; if ( params . token ) { try { // decode the token var _token = jwt . decode ( params . token , config . jsonWebTokens . secret ) ; // set the time of the request var _reqTime = Date . now ( ) ; // If token is expired if ( _token . exp <= _reqTime ) { return res . forbidden ( 'Your token is expired.' ) ; } // If token is early if ( _reqTime <= _token . nbf ) { return res . forbidden ( 'This token is early.' ) ; } // If audience doesn't match if ( config . jsonWebTokens . audience !== _token . aud ) { return res . forbidden ( 'This token cannot be accepted for this domain.' ) ; } // If the subject doesn't match if ( 'password reset' !== _token . sub ) { return res . forbidden ( 'This token cannot be used for this request.' ) ; } sails . models . auth . findOne ( _token . iss ) . populate ( 'resetToken' ) . exec ( function ( err , auth ) { if ( typeof auth . resetToken === 'undefined' || params . token !== auth . resetToken . token ) { return res . forbidden ( 'This token cannot be used.' ) ; } req . session . resetToken = auth . resetToken ; if ( authConfig . passwordReset . mail . forwardUrl ) { res . redirect ( authConfig . passwordReset . mail . forwardUrl + '?token=' + auth . resetToken . token ) ; } else { res . json ( 200 ) ; } } ) ; } catch ( err ) { return res . serverError ( err ) ; } } else { //TODO limit attempts? req . session . resetToken = false ; res . forbidden ( ) ; } }
validates a users token and redirects them to the new url if provided
@param  object req the express request object
@param  object res the express response object
@param  waterlineDAO User  the waterline User dao
@param  waterlineDAO ResetToken the waterline ResetToken dao
@param  object params all params for this request
validates a users token and redirects them to the new url if provided