-
Notifications
You must be signed in to change notification settings - Fork 0
Revoke and Remove Tokens on Uninstall #133
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
Merged
n7studios
merged 5 commits into
revoke-remove-tokens-disconnect
from
revoke-remove-tokens-uninstall
Apr 13, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a110c61
Revoke and Remove Tokens on Uninstall
n7studios 67a0443
Merge branch 'revoke-remove-tokens-disconnect' into revoke-remove-tok…
n7studios 4fc10ce
Run UninstallCest
n7studios 7cabf01
Reinstate all tests
n7studios 4538844
Fix docblock comment
n7studios File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\EndToEnd; | ||
|
|
||
| use Tests\Support\EndToEndTester; | ||
|
|
||
| /** | ||
| * Tests Plugin uninstallation. | ||
| * | ||
| * @since 1.9.2 | ||
| */ | ||
| class UninstallCest | ||
| { | ||
| /** | ||
| * Test that the Plugin's access and refresh tokens are revoked, and all v4 and v3 | ||
| * API credentials are removed from the Plugin's settings when the Plugin is deleted. | ||
| * | ||
| * @since 1.9.2 | ||
| * | ||
| * @param EndToEndTester $I Tester. | ||
| */ | ||
| public function testPluginDeletionRevokesAndRemovesTokens(EndToEndTester $I) | ||
| { | ||
| // Activate this Plugin. | ||
| $I->activateConvertKitPlugin($I); | ||
|
|
||
| // Generate an access token and refresh token by API key and secret. | ||
| // We don't use the tokens from the environment, as revoking those | ||
| // would result in later tests failing. | ||
| $result = wp_remote_post( | ||
| 'https://api.kit.com/wordpress/accounts/oauth_access_token', | ||
| [ | ||
| 'headers' => [ | ||
| 'Content-Type' => 'application/json', | ||
| ], | ||
| 'body' => wp_json_encode( | ||
| [ | ||
| 'api_key' => $_ENV['CONVERTKIT_API_KEY'], | ||
| 'api_secret' => $_ENV['CONVERTKIT_API_SECRET'], | ||
| 'client_id' => $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], | ||
| 'tenant_name' => wp_generate_password( 10, false ), // Random tenant name to produce a token for this request only. | ||
| ] | ||
| ), | ||
| ] | ||
| ); | ||
| $tokens = json_decode(wp_remote_retrieve_body($result), true)['oauth']; | ||
|
|
||
| // Store the tokens and API keys in the Plugin's settings. | ||
| $I->setupWPFormsIntegration( | ||
| $I, | ||
| accessToken: $tokens['access_token'], | ||
| refreshToken: $tokens['refresh_token'], | ||
| apiKey: $_ENV['CONVERTKIT_API_KEY'], | ||
| apiSecret: $_ENV['CONVERTKIT_API_SECRET'] | ||
| ); | ||
|
|
||
| // Deactivate the Plugin. | ||
| $I->deactivateConvertKitPlugin($I); | ||
|
|
||
| // Delete the Plugin. | ||
| $I->deleteKitPlugin($I); | ||
|
|
||
| // Confirm the credentials have been removed from the Plugin's settings. | ||
| $I->wait(3); | ||
| $settings = $I->grabOptionFromDatabase('wpforms_providers'); | ||
| $connection = reset($settings['convertkit']); | ||
| $I->assertEmpty($connection['access_token']); | ||
| $I->assertEmpty($connection['refresh_token']); | ||
| $I->assertEmpty($connection['api_key']); | ||
| $I->assertEmpty($connection['api_secret']); | ||
|
|
||
| // Confirm attempting to use the revoked access token no longer works. | ||
| $result = wp_remote_get( | ||
| 'https://api.kit.com/v4/account', | ||
| [ | ||
| 'headers' => [ | ||
| 'Authorization' => 'Bearer ' . $tokens['access_token'], | ||
| ], | ||
| ] | ||
| ); | ||
| $data = json_decode(wp_remote_retrieve_body($result), true); | ||
| $I->assertArrayHasKey( 'errors', $data ); | ||
| $I->assertEquals( 'The access token was revoked', $data['errors'][0] ); | ||
|
|
||
| // Confirm attempting to use the revoked refresh token no longer works. | ||
| $result = wp_remote_post( | ||
| 'https://api.kit.com/v4/oauth/token', | ||
| [ | ||
| 'headers' => [ | ||
| 'Authorization' => 'Bearer ' . $tokens['access_token'], | ||
| ], | ||
| 'body' => [ | ||
| 'client_id' => $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], | ||
| 'grant_type' => 'refresh_token', | ||
| 'refresh_token' => $tokens['refresh_token'], | ||
| ], | ||
| ] | ||
| ); | ||
| $data = json_decode(wp_remote_retrieve_body($result), true); | ||
| $I->assertArrayHasKey( 'error', $data ); | ||
| $I->assertEquals( 'invalid_grant', $data['error'] ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| <?php | ||
| /** | ||
| * Uninstall routine. Runs when the Plugin is deleted | ||
| * at Plugins > Delete. | ||
| * | ||
| * @package CKWC | ||
| * @author ConvertKit | ||
| */ | ||
|
|
||
| // If uninstall.php is not called by WordPress, die. | ||
| if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) { | ||
| die; | ||
| } | ||
|
|
||
| // Only WordPress and PHP methods can be used. Plugin classes and methods | ||
| // are not reliably available due to the Plugin being deactivated and going | ||
| // through deletion now. | ||
|
|
||
| // Get providers. | ||
| $providers = get_option( 'wpforms_providers' ); | ||
|
|
||
| // Bail if no providers exist. | ||
| if ( ! $providers ) { | ||
| return; | ||
| } | ||
|
|
||
| // Bail if no Kit connections exist. | ||
| if ( ! array_key_exists( 'convertkit', $providers ) ) { | ||
| return; | ||
| } | ||
|
|
||
| // Iterate through each connection, revoking the tokens. | ||
| foreach ( $providers['convertkit'] as $account_id => $connection ) { | ||
| // Revoke Access Token. | ||
| if ( array_key_exists( 'access_token', $connection ) && ! empty( $connection['access_token'] ) ) { | ||
| wp_remote_post( | ||
| 'https://api.kit.com/v4/oauth/revoke', | ||
| array( | ||
| 'headers' => array( | ||
| 'Accept' => 'application/json', | ||
| 'Content-Type' => 'application/json', | ||
| ), | ||
| 'body' => wp_json_encode( | ||
| array( | ||
| 'client_id' => 'L0kyADsB3WP5zO5MvUpXQU64gIntQg9BBAIme17r_7A', | ||
| 'token' => $connection['access_token'], | ||
| ) | ||
| ), | ||
| 'timeout' => 5, | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| // Revoke Refresh Token. | ||
| if ( array_key_exists( 'refresh_token', $connection ) && ! empty( $connection['refresh_token'] ) ) { | ||
| wp_remote_post( | ||
| 'https://api.kit.com/v4/oauth/revoke', | ||
| array( | ||
| 'headers' => array( | ||
| 'Accept' => 'application/json', | ||
| 'Content-Type' => 'application/json', | ||
| ), | ||
| 'body' => wp_json_encode( | ||
| array( | ||
| 'client_id' => 'L0kyADsB3WP5zO5MvUpXQU64gIntQg9BBAIme17r_7A', | ||
| 'token' => $connection['refresh_token'], | ||
| ) | ||
| ), | ||
| 'timeout' => 5, | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| // Remove credentials from settings. | ||
| $providers['convertkit'][ $account_id ]['access_token'] = ''; | ||
| $providers['convertkit'][ $account_id ]['refresh_token'] = ''; | ||
| $providers['convertkit'][ $account_id ]['token_expires'] = ''; | ||
| $providers['convertkit'][ $account_id ]['api_key'] = ''; | ||
| $providers['convertkit'][ $account_id ]['api_secret'] = ''; | ||
| } | ||
|
|
||
| // Save settings. | ||
| update_option( 'wpforms_providers', $providers ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.