-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add token authentication to the installation wizard
resolves #6135
- Loading branch information
Johannes Meyer
committed
May 23, 2014
1 parent
df02e55
commit ad4f7de
Showing
2 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
// {{{ICINGA_LICENSE_HEADER}}} | ||
// {{{ICINGA_LICENSE_HEADER}}} | ||
|
||
namespace Icinga\Web\Form\Validator; | ||
|
||
use Zend_Validate_Abstract; | ||
|
||
/** | ||
* Validator that checks if a token matches with the contents of a corresponding token-file | ||
*/ | ||
class TokenValidator extends Zend_Validate_Abstract | ||
{ | ||
/** | ||
* The path to the token file | ||
* | ||
* @var string | ||
*/ | ||
protected $tokenPath; | ||
|
||
/** | ||
* Create a new TokenValidator | ||
* | ||
* @param string $tokenPath The path to the token-file | ||
*/ | ||
public function __construct($tokenPath) | ||
{ | ||
$this->tokenPath = $tokenPath; | ||
$this->_messageTemplates = array( | ||
'TOKEN_FILE_NOT_FOUND' => t('Cannot validate token, file could not be opened or does not exist.'), | ||
'TOKEN_FILE_EMPTY' => t('Cannot validate token, file is empty. Please define a token.'), | ||
'TOKEN_FILE_PUBLIC' => t('Cannot validate token, file is publicly readable.'), | ||
'TOKEN_INVALID' => t('Invalid token supplied.') | ||
); | ||
} | ||
|
||
/** | ||
* Validate the given token with the one in the token-file | ||
* | ||
* @param string $value The token to validate | ||
* @param null $context The form context (ignored) | ||
* | ||
* @return bool | ||
*/ | ||
public function isValid($value, $context = null) | ||
{ | ||
$tokenStats = stat($this->tokenPath); | ||
if (($tokenStats['mode'] & 4) === 4) { | ||
$this->_error('TOKEN_FILE_PUBLIC'); | ||
return false; | ||
} | ||
|
||
$expectedToken = @file_get_contents($this->tokenPath); | ||
if ($expectedToken === false) { | ||
$this->_error('TOKEN_FILE_NOT_FOUND'); | ||
return false; | ||
} | ||
|
||
$expectedToken = trim($expectedToken); | ||
if (empty($expectedToken)) { | ||
$this->_error('TOKEN_FILE_EMPTY'); | ||
return false; | ||
} elseif ($value !== $expectedToken) { | ||
$this->_error('TOKEN_INVALID'); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |