Add more detail to error messages when token validation fails

refs #7163
fixes #7410
This commit is contained in:
Johannes Meyer 2014-10-21 16:10:21 +02:00
parent 8c62c66a4e
commit a5b10235d2

View File

@ -4,7 +4,9 @@
namespace Icinga\Web\Form\Validator; namespace Icinga\Web\Form\Validator;
use Exception;
use Zend_Validate_Abstract; use Zend_Validate_Abstract;
use Icinga\Util\File;
/** /**
* Validator that checks if a token matches with the contents of a corresponding token-file * Validator that checks if a token matches with the contents of a corresponding token-file
@ -27,10 +29,20 @@ class TokenValidator extends Zend_Validate_Abstract
{ {
$this->tokenPath = $tokenPath; $this->tokenPath = $tokenPath;
$this->_messageTemplates = array( $this->_messageTemplates = array(
'TOKEN_FILE_NOT_FOUND' => t('Cannot validate token, file could not be opened or does not exist.'), 'TOKEN_FILE_ERROR' => sprintf(
'TOKEN_FILE_EMPTY' => t('Cannot validate token, file is empty. Please define a token.'), t('Cannot validate token: %s (%s)'),
'TOKEN_FILE_PUBLIC' => t('Cannot validate token, file is publicly readable.'), $tokenPath,
'TOKEN_INVALID' => t('Invalid token supplied.') '%value%'
),
'TOKEN_FILE_EMPTY' => sprintf(
t('Cannot validate token, file "%s" is empty. Please define a token.'),
$tokenPath
),
'TOKEN_FILE_PUBLIC' => sprintf(
t('Cannot validate token, file "%s" must only be accessible by the webserver\'s user.'),
$tokenPath
),
'TOKEN_INVALID' => t('Invalid token supplied.')
); );
} }
@ -50,13 +62,15 @@ class TokenValidator extends Zend_Validate_Abstract
return false; return false;
} }
$expectedToken = @file_get_contents($this->tokenPath); try {
if ($expectedToken === false) { $file = new File($this->tokenPath);
$this->_error('TOKEN_FILE_NOT_FOUND'); $expectedToken = trim($file->fgets());
} catch (Exception $e) {
$msg = $e->getMessage();
$this->_error('TOKEN_FILE_ERROR', substr($msg, strpos($msg, ']: ') + 3));
return false; return false;
} }
$expectedToken = trim($expectedToken);
if (empty($expectedToken)) { if (empty($expectedToken)) {
$this->_error('TOKEN_FILE_EMPTY'); $this->_error('TOKEN_FILE_EMPTY');
return false; return false;
@ -68,4 +82,3 @@ class TokenValidator extends Zend_Validate_Abstract
return true; return true;
} }
} }