public function testCanRetrieveEntityFromObjectRepositoryStorage() { // Identifier is considered to be username here $entity = new IdentityObject(); $entity->setUsername('a username'); $entity->setPassword('a password'); $objectRepository = $this->getMock('Doctrine\\Common\\Persistence\\ObjectRepository'); $objectRepository->expects($this->exactly(1))->method('find')->with($this->equalTo('a username'))->will($this->returnValue($entity)); $metadata = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata'); $metadata->expects($this->exactly(1))->method('getIdentifierValues')->with($this->equalTo($entity))->will($this->returnValue($entity->getUsername())); $storage = new ObjectRepositoryStorage(array('objectRepository' => $objectRepository, 'classMetadata' => $metadata, 'storage' => new NonPersistentStorage())); $storage->write($entity); $this->assertFalse($storage->isEmpty()); $result = $storage->read(); $this->assertEquals($entity, $result); $key = $storage->readKeyOnly(); $this->assertEquals('a username', $key); }
public function testCanValidateWithSpecialCrypt() { $hash = '$2y$07$usesomesillystringforsalt$'; $entity = new IdentityObject(); $entity->setUsername('username'); // Crypt password using Blowfish $entity->setPassword(crypt('password', $hash)); $objectRepository = $this->getMock('Doctrine\\Common\\Persistence\\ObjectRepository'); $objectRepository->expects($this->exactly(2))->method('findOneBy')->with($this->equalTo(array('username' => 'username')))->will($this->returnValue($entity)); $adapter = new ObjectRepositoryAdapter(); $adapter->setOptions(array('object_repository' => $objectRepository, 'credential_property' => 'password', 'identity_property' => 'username', 'credential_callable' => function (IdentityObject $identity, $credentialValue) use($hash) { return $identity->getPassword() === crypt($credentialValue, $hash); })); $adapter->setIdentityValue('username'); $adapter->setCredentialValue('password'); $result = $adapter->authenticate(); $this->assertTrue($result->isValid()); $adapter->setCredentialValue('wrong password'); $result = $adapter->authenticate(); $this->assertFalse($result->isValid()); }
public function testWillNotCastAuthCredentialValue() { $objectRepository = $this->getMock('Doctrine\\Common\\Persistence\\ObjectRepository'); $adapter = new ObjectRepositoryAdapter(); $entity = new IdentityObject(); $entity->setPassword(0); $adapter->setOptions(array('object_repository' => $objectRepository, 'credential_property' => 'password', 'identity_property' => 'username')); $adapter->setIdentity('a username'); $adapter->setCredential('00000'); $objectRepository->expects($this->once())->method('findOneBy')->with($this->equalTo(array('username' => 'a username')))->will($this->returnValue($entity)); $this->assertFalse($adapter->authenticate()->isValid()); }
public function testCanValidateWithSpecialCrypt() { $hash = '$2a$07$usesomesillystringforsalt$'; $entity = new IdentityObject(); $entity->setUsername('username'); // Crypt password using Blowfish $entity->setPassword(crypt('password', $hash)); $objectRepository = $this->getMock('Doctrine\\Common\\Persistence\\ObjectRepository'); $objectRepository->expects($this->exactly(2))->method('findOneBy')->with($this->equalTo(array('username' => 'username')))->will($this->returnValue($entity)); $adapter = new ObjectRepositoryAdapter($objectRepository, __NAMESPACE__ . '\\TestAsset\\IdentityObject'); $adapter->setIdentityValue('username'); $adapter->setCredentialValue('password'); // enforced type hinting to verify that closure is invoked correctly $adapter->setCredentialCallable(function (IdentityObject $identity, $credentialValue) use($hash) { return $identity->getPassword() === crypt($credentialValue, $hash); }); $result = $adapter->authenticate(); $this->assertTrue($result->isValid()); $adapter->setCredentialValue('wrong password'); $result = $adapter->authenticate(); $this->assertFalse($result->isValid()); }