/**
  * ## OPTIONS
  *
  * [--name=<name>]
  * : Consumer name
  *
  * [--description=<description>]
  * : Consumer description
  */
 public function add($_, $args)
 {
     $consumer = WP_REST_OAuth1_Client::create($args);
     WP_CLI::line(sprintf('ID: %d', $consumer->ID));
     WP_CLI::line(sprintf('Key: %s', $consumer->key));
     WP_CLI::line(sprintf('Secret: %s', $consumer->secret));
 }
 /**
  * Generate a new access token
  *
  * @param string $oauth_consumer_key Consumer key
  * @param string $oauth_token Request token key
  * @return WP_Error|array OAuth token data on success, error otherwise
  */
 public function generate_access_token($oauth_consumer_key, $oauth_token, $oauth_verifier)
 {
     $token = $this->get_request_token($oauth_token);
     if (is_wp_error($token)) {
         return $token;
     }
     // Check verification
     if ($token['authorized'] !== true) {
         return new WP_Error('json_oauth1_unauthorized_token', __('OAuth token has not been authorized'), array('status' => 401));
     }
     if ($oauth_verifier !== $token['verifier']) {
         return new WP_Error('json_oauth1_invalid_verifier', __('OAuth verifier does not match'), array('status' => 400));
     }
     $this->should_attempt = false;
     $consumer = WP_REST_OAuth1_Client::get_by_key($oauth_consumer_key);
     $this->should_attempt = true;
     if (is_wp_error($consumer)) {
         return $consumer;
     }
     // Issue access token
     $key = apply_filters('json_oauth1_access_token_key', wp_generate_password(self::TOKEN_KEY_LENGTH, false));
     $data = array('key' => $key, 'secret' => wp_generate_password(self::TOKEN_SECRET_LENGTH, false), 'consumer' => $consumer->ID, 'user' => $token['user']);
     $data = apply_filters('json_oauth1_access_token_data', $data);
     add_option('oauth1_access_' . $key, $data, null, 'no');
     // Delete the request token
     $this->remove_request_token($oauth_token);
     // Return the new token's data
     $data = array('oauth_token' => self::urlencode_rfc3986($key), 'oauth_token_secret' => self::urlencode_rfc3986($data['secret']));
     return $data;
 }
 public static function handle_regenerate()
 {
     if (empty($_GET['id'])) {
         return;
     }
     $id = $_GET['id'];
     check_admin_referer('rest-oauth1-regenerate:' . $id);
     if (!current_user_can('edit_post', $id)) {
         wp_die('<h1>' . __('Cheatin&#8217; uh?', 'rest_oauth1') . '</h1>' . '<p>' . __('You are not allowed to edit this application.', 'rest_oauth1') . '</p>', 403);
     }
     $client = WP_REST_OAuth1_Client::get($id);
     $client->regenerate_secret();
     wp_safe_redirect(self::get_url(array('action' => 'edit', 'id' => $id, 'did_action' => 'regenerate')));
     exit;
 }
 /**
  * Generate a new access token
  *
  * @param string $oauth_consumer_key Consumer key
  * @param string $oauth_token Request token key
  * @return WP_Error|array OAuth token data on success, error otherwise
  */
 public function generate_access_token($params)
 {
     $consumer = WP_REST_OAuth1_Client::get_by_key($params['oauth_consumer_key']);
     if (is_wp_error($consumer)) {
         return $consumer;
     }
     $token = $this->get_request_token($params['oauth_token']);
     if (is_wp_error($token)) {
         return $token;
     }
     // Check the OAuth request signature against the current request
     $result = $this->check_oauth_signature($consumer, $params, $token);
     if (is_wp_error($result)) {
         return $result;
     }
     $error = $this->check_oauth_timestamp_and_nonce($consumer, $params['oauth_timestamp'], $params['oauth_nonce']);
     if (is_wp_error($error)) {
         return $error;
     }
     // Check verification
     if ($token['authorized'] !== true) {
         return new WP_Error('json_oauth1_unauthorized_token', __('OAuth token has not been authorized', 'rest_oauth1'), array('status' => 401));
     }
     if (!hash_equals((string) $params['oauth_verifier'], (string) $token['verifier'])) {
         return new WP_Error('json_oauth1_invalid_verifier', __('OAuth verifier does not match', 'rest_oauth1'), array('status' => 400));
     }
     $this->should_attempt = false;
     $consumer = WP_REST_OAuth1_Client::get_by_key($params['oauth_consumer_key']);
     $this->should_attempt = true;
     if (is_wp_error($consumer)) {
         return $consumer;
     }
     // Issue access token
     $key = apply_filters('json_oauth1_access_token_key', wp_generate_password(self::TOKEN_KEY_LENGTH, false));
     $data = array('key' => $key, 'secret' => wp_generate_password(self::TOKEN_SECRET_LENGTH, false), 'consumer' => $consumer->ID, 'user' => $token['user']);
     $data = apply_filters('json_oauth1_access_token_data', $data);
     add_option('oauth1_access_' . $key, $data, null, 'no');
     // Delete the request token
     $this->remove_request_token($params['oauth_token']);
     // Return the new token's data
     $data = array('oauth_token' => self::urlencode_rfc3986($key), 'oauth_token_secret' => self::urlencode_rfc3986($data['secret']));
     return $data;
 }