2009-11-06 03:23:21 +01:00
|
|
|
<?php
|
|
|
|
class PasswordEncryptorTest extends SapphireTest {
|
2012-04-07 04:59:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var Config
|
|
|
|
*/
|
|
|
|
private $config = null;
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
$this->config = clone(Config::inst());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
|
|
|
parent::tearDown();
|
|
|
|
Config::set_instance($this->config);
|
|
|
|
}
|
|
|
|
|
2009-11-06 03:23:21 +01:00
|
|
|
function testCreateForCode() {
|
2012-04-07 04:59:55 +02:00
|
|
|
Config::inst()->update('PasswordEncryptor', 'encryptors', array('test'=>array('PasswordEncryptorTest_TestEncryptor'=>null)));
|
2009-11-06 03:23:21 +01:00
|
|
|
$e = PasswordEncryptor::create_for_algorithm('test');
|
2012-04-07 04:59:55 +02:00
|
|
|
$this->assertType('PasswordEncryptorTest_TestEncryptor', $e );
|
2009-11-06 03:23:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException PasswordEncryptor_NotFoundException
|
|
|
|
*/
|
|
|
|
function testCreateForCodeNotFound() {
|
|
|
|
PasswordEncryptor::create_for_algorithm('unknown');
|
|
|
|
}
|
|
|
|
|
|
|
|
function testRegister() {
|
2012-04-07 04:59:55 +02:00
|
|
|
Config::inst()->update('PasswordEncryptor', 'encryptors', array('test'=>array('PasswordEncryptorTest_TestEncryptor'=>null)));
|
|
|
|
$encryptors = PasswordEncryptor::get_encryptors();
|
|
|
|
$this->assertContains('test', array_keys($encryptors));
|
|
|
|
$encryptor = $encryptors['test'];
|
|
|
|
$this->assertContains('PasswordEncryptorTest_TestEncryptor', key($encryptor));
|
2009-11-06 03:23:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function testUnregister() {
|
2012-04-07 04:59:55 +02:00
|
|
|
Config::inst()->update('PasswordEncryptor', 'encryptors', array('test'=>array('PasswordEncryptorTest_TestEncryptor'=>null)));
|
|
|
|
Config::inst()->remove('PasswordEncryptor', 'encryptors', 'test');
|
2009-11-06 03:23:21 +01:00
|
|
|
$this->assertNotContains('test', array_keys(PasswordEncryptor::get_encryptors()));
|
|
|
|
}
|
|
|
|
|
2012-05-02 03:51:29 +02:00
|
|
|
function testEncryptorPHPHashWithArguments() {
|
2012-04-07 04:59:55 +02:00
|
|
|
Config::inst()->update('PasswordEncryptor', 'encryptors', array('test_md5'=>array('PasswordEncryptor_PHPHash'=>'md5')));
|
2009-11-06 03:23:21 +01:00
|
|
|
$e = PasswordEncryptor::create_for_algorithm('test_md5');
|
|
|
|
$this->assertEquals('md5', $e->getAlgorithm());
|
|
|
|
}
|
|
|
|
|
2012-05-02 03:51:29 +02:00
|
|
|
function testEncryptorPHPHash() {
|
2012-04-07 04:59:55 +02:00
|
|
|
Config::inst()->update('PasswordEncryptor', 'encryptors', array('test_sha1'=>array('PasswordEncryptor_PHPHash'=>'sha1')));
|
2009-11-06 03:23:21 +01:00
|
|
|
$e = PasswordEncryptor::create_for_algorithm('test_sha1');
|
|
|
|
$password = 'mypassword';
|
|
|
|
$salt = 'mysalt';
|
|
|
|
$this->assertEquals(
|
|
|
|
hash('sha1', $password . $salt),
|
|
|
|
$e->encrypt($password, $salt)
|
|
|
|
);
|
|
|
|
}
|
2012-05-02 03:51:29 +02:00
|
|
|
|
|
|
|
function testEncryptorBlowfish() {
|
|
|
|
Config::inst()->update('PasswordEncryptor', 'encryptors', array('test_blowfish'=>array('PasswordEncryptor_Blowfish'=>'')));
|
|
|
|
$e = PasswordEncryptor::create_for_algorithm('test_blowfish');
|
|
|
|
$password = 'mypassword';
|
|
|
|
$salt = '10$mysaltmustbetwen2char';
|
|
|
|
$this->assertEquals(
|
|
|
|
crypt($password, '$2y$' . $salt),
|
|
|
|
'$2y$' . $salt . $e->encrypt($password, $salt)
|
|
|
|
);
|
|
|
|
}
|
2009-11-06 03:23:21 +01:00
|
|
|
|
2012-05-07 05:03:53 +02:00
|
|
|
function testEncryptorPHPHashCheck() {
|
2012-04-07 04:59:55 +02:00
|
|
|
Config::inst()->update('PasswordEncryptor', 'encryptors', array('test_sha1'=>array('PasswordEncryptor_PHPHash'=>'sha1')));
|
2009-11-06 03:23:21 +01:00
|
|
|
$e = PasswordEncryptor::create_for_algorithm('test_sha1');
|
2012-05-07 05:03:53 +02:00
|
|
|
$this->assertTrue($e->check(sha1('mypassword'), 'mypassword'));
|
|
|
|
$this->assertFalse($e->check(sha1('mypassword'), 'mywrongpassword'));
|
2009-11-06 03:23:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* See http://open.silverstripe.org/ticket/3004
|
|
|
|
*
|
|
|
|
* Handy command for reproducing via CLI on different architectures:
|
|
|
|
* php -r "echo(base_convert(sha1('mypassword'), 16, 36));"
|
|
|
|
*/
|
2012-05-07 05:03:53 +02:00
|
|
|
function testEncryptorLegacyPHPHashCheck() {
|
2012-04-07 04:59:55 +02:00
|
|
|
Config::inst()->update('PasswordEncryptor', 'encryptors', array('test_sha1legacy'=>array('PasswordEncryptor_LegacyPHPHash'=>'sha1')));
|
2009-11-06 03:23:21 +01:00
|
|
|
$e = PasswordEncryptor::create_for_algorithm('test_sha1legacy');
|
|
|
|
// precomputed hashes for 'mypassword' from different architectures
|
|
|
|
$amdHash = 'h1fj0a6m4o6k0sosks88oo08ko4gc4s';
|
|
|
|
$intelHash = 'h1fj0a6m4o0g04ocg00o4kwoc4wowws';
|
|
|
|
$wrongHash = 'h1fjxxxxxxxxxxxxxxxxxxxxxxxxxxx';
|
2012-05-07 05:03:53 +02:00
|
|
|
$this->assertTrue($e->check($amdHash, "mypassword"));
|
|
|
|
$this->assertTrue($e->check($intelHash, "mypassword"));
|
|
|
|
$this->assertFalse($e->check($wrongHash, "mypassword"));
|
2009-11-06 03:23:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class PasswordEncryptorTest_TestEncryptor extends PasswordEncryptor implements TestOnly {
|
|
|
|
function encrypt($password, $salt = null, $member = null) {
|
|
|
|
return 'password';
|
|
|
|
}
|
|
|
|
|
|
|
|
function salt($password, $member = null) {
|
|
|
|
return 'salt';
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|