/** * ## OPTIONS * * [--name=<name>] * : Consumer name * * [--description=<description>] * : Consumer description */ public function add($_, $args) { $authenticator = new WP_JSON_Authentication_OAuth1(); $consumer = $authenticator->add_consumer($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)); }
/** * Render authorization page * * @return null|WP_Error Null on success, error otherwise */ public function render_page() { // Check required fields if (empty($_REQUEST['oauth_token'])) { return new WP_Error('json_oauth1_missing_param', sprintf(__('Missing parameter %s'), 'oauth_token'), array('status' => 400)); } // Set up fields $token_key = wp_unslash($_REQUEST['oauth_token']); $scope = '*'; if (!empty($_REQUEST['wp_scope'])) { $scope = wp_unslash($_REQUEST['wp_scope']); } $authenticator = new WP_JSON_Authentication_OAuth1(); $errors = array(); $this->token = $authenticator->get_request_token($token_key); if (is_wp_error($this->token)) { return $this->token; } if (!empty($_REQUEST['oauth_callback'])) { $resp = $authenticator->set_request_token_callback($this->token['key'], $_REQUEST['oauth_callback']); if (is_wp_error($resp)) { return $resp; } } if ($this->token['authorized'] === true) { return $this->handle_callback_redirect($this->token['verifier']); } // Fetch consumer $this->consumer = $consumer = get_post($this->token['consumer']); if (!empty($_POST['wp-submit'])) { check_admin_referer('json_oauth1_authorize'); switch ($_POST['wp-submit']) { case 'authorize': $verifier = $authenticator->authorize_request_token($this->token['key']); if (is_wp_error($verifier)) { return $verifier; } return $this->handle_callback_redirect($verifier); case 'cancel': exit; default: return new WP_Error('json_oauth1_invalid_action', __('Invalid authorization action'), array('status' => 400)); } } $file = locate_template('oauth1-authorize.php'); if (empty($file)) { $file = dirname(dirname(__FILE__)) . '/theme/oauth1-authorize.php'; } include $file; }
/** * Load the JSON API */ function json_oauth_server_loaded() { if (empty($GLOBALS['wp']->query_vars['json_oauth_route'])) { return; } $authenticator = new WP_JSON_Authentication_OAuth1(); $response = $authenticator->dispatch($GLOBALS['wp']->query_vars['json_oauth_route']); if (is_wp_error($response)) { $error_data = $response->get_error_data(); if (is_array($error_data) && isset($error_data['status'])) { $status = $error_data['status']; } else { $status = 500; } status_header($status); echo $response->get_error_message(); die; } header('Content-Type: application/x-www-form-urlencoded; charset=utf-8'); $response = http_build_query($response, '', '&'); echo $response; // Finish off our request die; }
function json_oauth_profile_save($user_id) { if (empty($_POST['oauth_revoke'])) { return; } $key = wp_unslash($_POST['oauth_revoke']); $authenticator = new WP_JSON_Authentication_OAuth1(); $result = $authenticator->revoke_access_token($key); if (is_wp_error($result)) { $redirect = add_query_arg('oauth_revocation_failed', true, get_edit_user_link($user_id)); } else { $redirect = add_query_arg('oauth_revoked', $key, get_edit_user_link($user_id)); } wp_redirect($redirect); exit; }