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

Added a new method to deal with the build up of old tokens. #141

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions lib/class-wp-rest-oauth1.php
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,9 @@ public function generate_access_token( $params ) {
'user' => $token['user'],
);
$data = apply_filters( 'json_oauth1_access_token_data', $data );

$this->revoke_old_tokens( $data['user'], $data['consumer'] );

add_option( 'oauth1_access_' . $key, $data, null, 'no' );

// Delete the request token
Expand Down Expand Up @@ -798,4 +801,21 @@ public function check_oauth_timestamp_and_nonce( $consumer, $timestamp, $nonce )
protected static function urlencode_rfc3986( $value ) {
return str_replace( array( '+', '%7E' ), array( ' ', '~' ), rawurlencode( $value ) );
}

protected function revoke_old_tokens( $user, $consumer ) {
global $wpdb;

// Get all the stored access tokens and then filter out any which don't belong to the current user/consumer
$results = $wpdb->get_col( "SELECT option_value FROM {$wpdb->options} WHERE option_name LIKE 'oauth1_access_%'", 0 );
$results = array_map( 'unserialize', $results );
$revocations = array_filter( $results, function ( $row ) use ( $user, $consumer ) {
return ( $row['user'] === $user && $row['consumer'] === $consumer );
} );

// Loop through the old tokens - there might be a lot of them
foreach ($revocations as $value) {
$this->revoke_access_token( $value['key'] );
}
}

}