doctrine / DoctrineModule

Doctrine Module for Laminas
http://www.doctrine-project.org/projects/doctrine-module.html
MIT License
398 stars 270 forks source link

AuthenticationService #454

Closed kukoman closed 2 years ago

kukoman commented 9 years ago

no matter what I do it seems like there is a whole entity saved as identity

doctrine config:

        'authentication' => array(
            'orm_default' => array(
                'object_manager' => 'Doctrine\ORM\EntityManager',
                'identity_class' => 'MyModule\V1\Rest\Users\Entity\Credential',
                'identity_property' => 'login',
                'credential_property' => 'password',
                'credential_callable' => function ($user, $passwordGiven) {
                    return (new Bcrypt())->verify($passwordGiven, $user->getPassword());
                },
            ),
        ),

factory in my module

    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'Zend\Authentication\AuthenticationService' => function($serviceManager) {
                    return $serviceManager->get('doctrine.authenticationservice.orm_default');
                 }
           ....

my apigility rpc class:

namespace Mcm\V1\Rpc\Auth;
class AuthController extends AbstractActionController
    public function loginAction()
    {
        $this->clearAction();

        $data = $this->bodyParams();

        $authService = $this->getServiceLocator()->get('Zend\Authentication\AuthenticationService');

        $adapter = $authService->getAdapter();
        $adapter->setIdentityValue($data['login']);
        $adapter->setCredentialValue($data['password']);
        // full entity
        $authService->getIdentity();
        $authResult = $adapter->authenticate(); // or $authService->authenticate()->getIdentity()
        // full entity
        $authResult->getIdentity();
}
...
}

I can see that ObjectRepository::write works correctly but somehow it will get rewritten when I call getIdentity()

anyone can help me to find this?

Ocramius commented 9 years ago

What is the storage in the $authService?

kukoman commented 9 years ago

DoctrineModule\Authentication\Storage\ObjectRepository // also full entity $authService->getStorage()->read()

Ocramius commented 9 years ago

So the auth adapter is not actually returning the entity? Can you check our existing tests? I can't see the fault in your code (if there is any)

kukoman commented 9 years ago

well, I feel little bit funny... I overlooked the read method :/ perhaps you can mention it in the documentation

        if (($identity = $this->options->getStorage()->read())) {
            return $this->options->getObjectRepository()->find($identity);
        }

thank you for your time

Ocramius commented 9 years ago

@kukoman that shouldn't be the default way of doing this. re-opening as this is indeed a bug.

kukoman commented 9 years ago

so just to sum it up; maybe the documentation need improving or I was doing something wrong but here is more detailed explanation:

    $authService = $this->getServiceLocator()->get('Zend\Authentication\AuthenticationService');
    $adapter = $authService->getAdapter();
    $adapter->setIdentityValue($data['login']);
    $adapter->setCredentialValue($data['password']);
    $authResult = $authService->authenticate();

    if ($authResult->isValid()) {
        ... some more magic with storage write
        $identity = $authResult->getIdentity();
        $identity->setPassword(null);
        $authService->getStorage()->write($identity);
     }

this will store only the entity identity key/value but from documentation I understood that also writing to the storage performs the same logic - however when I've done it I ended up with full entity in the session

if someone can reproduce it than it is indeed a bug :)

Ocramius commented 9 years ago

Well, I'd expect $authResult->getIdentity() to contain an object

TomHAnderson commented 6 years ago

I've seen the SessionStorageput the serialized getIdentity() object into the session too.

What is the correct solution? Unserialize the identity and call merge() and return the merged identity?