Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[security] serialize the user, again #24731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
johndodev opened this issue Oct 28, 2017 · 4 comments
Closed

[security] serialize the user, again #24731

johndodev opened this issue Oct 28, 2017 · 4 comments

Comments

@johndodev
Copy link

Q A
Bug report? no
Feature request? yes/no
BC Break report? no
RFC? no
Symfony version silex 2

One more time I have a "Class __PHP_Incomplete_Class has no unserializer" thanks to security component. It is probably caused by a doctrine relation, the security component wants to serialize a proxy or something, I don't know.

However I implemented \Serializable on my User, and serialize only the ID, so why implement serializable seems completely useless ?
As the doc say, This may or may not be needed depending on your setup, but it's probably a good idea. not very clear...

I still have to do a special object like SecurityUser to log my users, and have to surcharge "user" service, etc... and this solution is not in SF docs (but in some blogs).

After all those years, this serialize thing has always been a balls breaker thing to me. Could you try to simplify it ? Une fois pour toute qu'on en parle plus !

@linaori
Copy link
Contributor

linaori commented Oct 29, 2017

The __PHP_Incomplete_Class sounds like it can't find your class, perhaps something in the FQCN changed?

I agree that it should be simplified, but there's one thing you can do already: https://stovepipe.systems/post/decoupling-your-security-user

@johndodev
Copy link
Author

johndodev commented Oct 29, 2017

Yes that's the blog I was talking about and what I'm doing, but if it is the only solution to have a proper login system it should be on the official doc...

I don't know about my FQCN, I'm not doing any fancy things, just a user with some relations.

:(

@sashaaro
Copy link

sashaaro commented Oct 29, 2017

What about introduce to TokenSerializerInterface like that

interface TokenSerializerInterface {
    public function serialize(TokenInterface $token): mixed
    public function unserialize(mixed $data): TokenInterface
    public function supports(TokenInterface $token): bool
}

class StandardTokenSerializer implements TokenSerializerInterface {
    public function serialize(TokenInterface $token): mixed
    {
         return serialize($token);
    }
    public function unserialize(mixed $data): TokenInterface
    {
        return unserialize($data);
    }
    public function supports(TokenInterface $token): bool
    {
          return $token instanceof \Serializable
    }
}

class MyTokenSerializer implements TokenSerializerInterface {
     public function __construct(UserRepository $repository)
     {
            $this->repository = $repository
     }
     public function serialize(TokenInterface $token): mixed
     {
          if (!$token instanceof UserPasswordToken) { throw new UnsupportedToken() }
          return $token->getUser()->getId();
     }
     public function unserialize(mixed $data): TokenInterface
     {
         return new UserPasswordToken($this->repository->find($data));
     }
     public function supports(TokenInterface $token): bool
     {
          return $token instanceof UserPasswordToken
     }
}
class JWTTokenSerializer ....

@mvrhov
Copy link

mvrhov commented Oct 30, 2017

No workarounds serializer interfaces etc, the only proper solution is what @iltar is suggesting.

fabpot added a commit that referenced this issue Jan 8, 2018
…t be unserialized from the session (thewilkybarkid)

This PR was merged into the 2.7 branch.

Discussion
----------

[Security] Fail gracefully if the security token cannot be unserialized from the session

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

If the security token in the session can't be unserialized, an `E_NOTICE` is issued. This prevents it (and provides a better log message if it's not even a `__PHP_Incomplete_Class`).

This is similar to #24731, but I saw it triggered when changing OAuth library (elifesciences/journal#824), so the token class itself no longer exists. (I want to avoid having to manually invalidate all sessions, as not all sessions use that token class.)

Commits
-------

053fa43 [Security] Fail gracefully if the security token cannot be unserialized from the session
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants