kamermans / guzzle-oauth2-subscriber

OAuth 2.0 Client for Guzzle 4, 5, 6 and 7 with PHP 5.4 - PHP 8.0 - no more dependency hell!
MIT License
140 stars 31 forks source link

TokenPersistenceInterface->restoreToken always called with new RawToken #26

Closed thiakil closed 3 years ago

thiakil commented 3 years ago

I have a use case (Salesforce REST api) which pulls extra fields from the server's token response and stores it as part of the token. I store it because the extra info (a URL and an OpenID token) is used by some additional middleware.

Most of it works fine, I have a custom TokenFactory & a custom TokenInterface implementation.

My issue is that the persistence subsystem always tries to restore a RawToken, as restoreToken has a TokenInterface param - but it's only ever called with an argument of new RawToken in the codebase.

My workaround currently is to wrap the persist provider and override restoreToken to supply an instance of my token.

What are your thoughts on this? I'm not quite sure why it has the param to begin with (but I understand that changing it would likely be a breaking change)

kamermans commented 3 years ago

Good point, I'm not sure why I didn't abstract that here either. You could use a ClosureTokenPersistence, which overrides that method (along with a few others): https://github.com/kamermans/guzzle-oauth2-subscriber/blob/8741be9bddb88d049d433979676a95d7b994fc28/src/Persistence/ClosureTokenPersistence.php#L16-L22

Aside from that, to avoid a breaking change I would need some way to override the token constructor, so if it's not overridden you get a RawToken, and if it is, you can provide anything that satisfies the interface.

thiakil commented 3 years ago

Perhaps we can take a leaf from Java's book and have a NewTokenSupplier closure field which defaults to RawToken? This would be on the main oauth handler class

kamermans commented 3 years ago

Sure, something like that is fine with me - can you put in the PR?

kamermans commented 3 years ago

Thanks for the PR, I've tested it and released it in v1.0.8