Skip to content

Conversation

marchrius
Copy link

@marchrius marchrius commented Mar 18, 2024

Supports to REST API flow (in body or parameter) instead of cookie.
The flow can be changed by hook 'jwt_auth_flow'.

  • Implement different flow
  • Edit tests to test the different flow
  • Update README.md

feat(refresh token): add hook to retrieve the refresh token from a custom filter
…efinition removing dependency on request variable
@rtorrente
Copy link

Hello @marchrius,

Very interested by your PR, i'm facing the same issue

Is there anything missing from this MR to send it in for review? Do you need help?

@marchrius
Copy link
Author

Hi @rtorrente,

the missing part is the README.md update with docs on new error codes and how to use the flow hook.

@sun sun self-requested a review April 30, 2024 13:53
Copy link
Collaborator

@sun sun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for contributing this enhancement! ❤️

From what I can see, the changes seem to be backwards-compatible with the current code in 3.x/master. Therefore, I believe we do not need to block the 3.0 release on this change.

However, before going into further details – there are a lot of coding style changes in this PR, which make it really hard to review the functional changes. So I'd like to ask you whether you can move all of the coding style changes into a separate PR (which we could merge fairly quickly), so that we can focus solely on the functional changes in this PR here?

This would be important, because we are touching security related functionality with this PR, and it would be embarrassing to introduce a security vulnerability just because a tiny but substantial change was hidden in a lot of code formatting changes.

@dominic-ks
Copy link
Collaborator

@marchrius are you able to have a look at @sun 's requested changes? It would be good to get this in as a few people are asking.

@marchrius
Copy link
Author

marchrius commented Sep 13, 2024

Hi @sun and @dominic-ks, I'll be happy to do that, can you give me some pointers on what might be the best way to split the two things?

I'm using PHPStorm as IDE and I have the option
PHP -> Framework -> WordPress -> Enable WordPress integration
to help me format the code automatically.

If you have some default rules I can use them for future commits and style changes.

@sun
Copy link
Collaborator

sun commented Sep 13, 2024

For the moment it would be best to disable all automated formattings and then revert all changes in this PR that are not functional changes.

In an issue separate from this here we can look into correcting the formatting – although I think we’ll rather look at automations like prettier and phpcs to do so, so that it doesn’t depend on anyone’s editor settings.

fix(refresh_token): revert logger integration (will be present in a different PR)
@marchrius
Copy link
Author

Hi @sun, I've removed all non-necessary integrations and style modifications.

Copy link
Collaborator

@sun sun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot – this at least allowed me to do a first round of review of the actual code changes. 👍

We should try to make this change in a backwards compatible way, so that existing production sites can update without headaches. We should also minimize the additional code introduced here by reusing the existing code. I added some insights on how we can achieve that, which should not require much effort.

And there are still a bunch of unnecessary code style changes in the surrounding files - if you can revert those, we can see and review the full picture with more certainty.

public function validate_refresh_token( $refresh_token_cookie, $device ) {
$parts = explode( '.', $refresh_token_cookie );
if ( count( $parts ) !== 2 || empty( intval( $parts[0] ) ) || empty( $parts[1] ) ) {
public function validate_refresh_token( $refresh_token, $return_response = true ) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. It seems you have changed the content of the refresh token - this means that all existing users will be logged-out when deploying the update. We can avoid that by implementing a backwards compatibility for the previous refresh tokens, so they are migrated individually to the new value when they are silently refreshed or the user re-authenticates. We can then safely remove this B/C layer in an upcoming release.
  2. Is this a copy of validate_token() now? We can avoid duplicate code by passing a parameter $type to validate_token() that contains either "refresh" or "access" (by default). Mainly seems to be necessary for hook names and error codes. However, let's keep the separate method for the temporary B/C layer and the backend/device validation.

As a result of these two suggestions, the amount of changes in this PR should reduce further.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you look into these points? Let me know if I can support.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Do you think the effort to create a backward code is worth it? If yes, how could be painless the backward implementation?
  2. No, the method for verification of refresh token is not exposed as API Rest. A common method that sorts the functionality and the different error code keys can be done. IMHO this will not reduce the the amount of changes but will increase it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider that the plugin is used in end-user / customer facing scenarios. Changing the refresh token value means that all users will get logged out 10 minutes after deploying the updated plugin, including users who just happened to log in afresh, leaving a negative brand and user experience. On our platform this would affect roughly ten to fifteen thousand users. Such incidents often go along with a spike of questions and complaints hitting the customer service, inducing further costs. And this is just talking about one site using the plugin.

It seems we can avoid this domino effect fairly easily by just simply supporting the previous refresh token value in addition to the new one; e.g., until the second to next release. In concrete terms, this would mean: check whether the flow is cookie and whether the value matches the previous format {userId}.{hash-64chars}, and if so, validate it.

*
* @return string
*/
public function generate_refresh_token( \WP_User $user, string $device = '' ): string {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto about generate_token() here (DRY)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can became a single function that generates a valid token without type in it and let the caller to set a specific type on it.

@marchrius marchrius force-pushed the feature/refresh_token_flow branch 4 times, most recently from 169f867 to 6934c57 Compare October 2, 2024 15:17
@marchrius marchrius force-pushed the feature/refresh_token_flow branch from 6934c57 to 25363ee Compare October 2, 2024 15:18
@napcoder
Copy link

napcoder commented Dec 2, 2024

Hello. My team and I are following this PR with great interest because this change is vital to unblock our mobile app to interact with a WP server. Are there any blockers we can help? I'm not into WP or PHP, but if there is anything I can do to help, please tell me and I will do it. I guess a lot of mobile devs are really interested in this very important PR (please leave a comment guys), we cannot use the plugin otherwise.

@rtorrente
Copy link

Hello. My team and I are following this PR with great interest because this change is vital to unblock our mobile app to interact with a WP server. Are there any blockers we can help? I'm not into WP or PHP, but if there is anything I can do to help, please tell me and I will do it. I guess a lot of mobile devs are really interested in this very important PR (please leave a comment guys), we cannot use the plugin otherwise.

Great interest too for the same reason of a mobile app. Glad to help if there is anything possible !

@tizianopitisci
Copy link

Hello. My team and I are following this PR with great interest because this change is vital to unblock our mobile app to interact with a WP server. Are there any blockers we can help? I'm not into WP or PHP, but if there is anything I can do to help, please tell me and I will do it. I guess a lot of mobile devs are really interested in this very important PR (please leave a comment guys), we cannot use the plugin otherwise.

I'm also very interested in this improvement otherwise we can't go ahead with our mobile app and our Wordpress based project. Thanks in advance to all guys that will solve this issue ì.

@el-lsan
Copy link

el-lsan commented Dec 8, 2024

Thanks for your works.
Any hope to get this merged?

@EleonoraCarrano
Copy link

Hello. My team and I are following this PR with great interest because this change is vital to unblock our mobile app to interact with a WP server. Are there any blockers we can help? I'm not into WP or PHP, but if there is anything I can do to help, please tell me and I will do it. I guess a lot of mobile devs are really interested in this very important PR (please leave a comment guys), we cannot use the plugin otherwise.

I'm very interested in this too

@n-ice-ch
Copy link

A new version 3.0.3 will be highly welcome to address the issue with "obsolete token" when re-send credentials and to have a JSON based token handling instead for cookie.

Is there anything we can help to ensure a fast updated version?

@sun
Copy link
Collaborator

sun commented Mar 4, 2025

This is still blocked on backwards compatibility; see #116 (comment)

@virajsoni06
Copy link

Has there been any update on this?

@midrocket
Copy link

@napcoder @EleonoraCarrano @tizianopitisci

Just to clarify, implementing this in a mobile app works fine even with the current setup, where refresh_token is sent as a cookie. We've successfully done it ourselves without needing any modifications or a new release.

In the end, a cookie is just a header — you can capture it from the response, store it securely on the device, and include it manually in the request headers when refreshing the token. No need for it to be in the body.

@dominic-ks
Copy link
Collaborator

As there has been no activity on this PR in some time, and I'm unclear on the way forward, I have created a newer one with fewer changes if anyone would care to review and provide feedback:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Give refresh_token in JSON response Allow to emit the refresh token in the response body instead of a cookie