Skip to content
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

[Subscriptions] Add/redirect url once token is set in cookie #33437

Merged
merged 4 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: enhancement

[Subscriptions] Hide the token from the URL
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ abstract class Token_Subscription_Service implements Subscription_Service {
public function initialize() {
$token = $this->token_from_request();
if ( null !== $token ) {
$this->set_token_cookie( $token );
$this->set_token_cookie( $token, true );
}
}

Expand All @@ -62,7 +62,7 @@ public function visitor_can_view_content( $valid_plan_ids, $access_level ) {
// URL token always has a precedence, so it can overwrite the cookie when new data available.
$token = $this->token_from_request();
if ( $token ) {
$this->set_token_cookie( $token );
$this->set_token_cookie( $token, false );
} else {
$token = $this->token_from_cookie();
}
Expand Down Expand Up @@ -202,15 +202,22 @@ private function token_from_cookie() {
* Store the auth cookie.
*
* @param string $token Auth token.
* @param bool $redirect_when_cookie_is_set If true, we'll force-reload the page to "hide" the token from the URL.
* @return void
*/
private function set_token_cookie( $token ) {
private function set_token_cookie( $token, $redirect_when_cookie_is_set = false ) {
if ( defined( 'TESTING_IN_JETPACK' ) && TESTING_IN_JETPACK ) {
return;
}

if ( ! empty( $token ) && false === headers_sent() ) {
setcookie( self::JWT_AUTH_TOKEN_COOKIE_NAME, $token, 0, '/', COOKIE_DOMAIN, is_ssl(), true ); // httponly -- used by visitor_can_view_content() within the PHP context.
if ( true === $redirect_when_cookie_is_set ) {
$location_without_token = remove_query_arg( 'token' );
if ( wp_safe_redirect( $location_without_token, 302, 'Subscriptions' ) ) {
exit;
}
}
}
}

Expand Down
Loading