/** * Exchange a valid OAuth response code for a token object * * @param $response_code * * @return array|WP_Error */ private function get_token_for_code($response_code) { $options = $this->get_option(); //prepare request data for access token $data = array(); $data['client_id'] = $options[LaunchKey_WP_Options::OPTION_ROCKET_KEY]; $data['client_secret'] = $options[LaunchKey_WP_Options::OPTION_SECRET_KEY]; $data['redirect_uri'] = $this->wp_facade->admin_url(); $data['code'] = $response_code; $data['grant_type'] = "authorization_code"; //make oauth call $params = http_build_query($data); // Attempt to get an access token from the resposne code $oauth_get = $this->wp_facade->wp_remote_get("{$this->base_url}/access_token?" . $params, array('httpversion' => '1.1', 'sslverify' => $options[LaunchKey_WP_Options::OPTION_SSL_VERIFY], 'timeout' => $options[LaunchKey_WP_Options::OPTION_REQUEST_TIMEOUT], 'headers' => array('Connection' => 'close'))); if ($this->wp_facade->is_wp_error($oauth_get)) { // If the response is an error, return the error $response = $oauth_get; } else { // Otherwise, decode the response $response = json_decode($oauth_get['body'], true); } return $response; }
/** * Complete the pairing process based on the authentication attempt * Errors will displayed to the user by the pair_errors_callback * @since 1.0.0 */ public function pair_callback() { // launchkey_username in the post means it's a pairing attempt if (array_key_exists('launchkey_username', $_POST)) { $user = $this->wp_facade->wp_get_current_user(); // If there is no valid nonce, set the pair error if (!$this->wp_facade->wp_verify_nonce($_POST['launchkey_nonce'], LaunchKey_WP_User_Profile::NONCE_KEY)) { $this->pair_error = new WP_Error('launchkey_pair_error', $this->wp_facade->__('Invalid nonce. Please try again.', $this->language_domain)); } elseif (!$user) { // If there is no user, set the pair error $this->pair_error = new WP_Error('launchkey_pair_error', $this->wp_facade->__('You must me logged in to pair', $this->language_domain)); } elseif (!$_POST['launchkey_username']) { // If the launchkey_username is blank, set the pair error $this->pair_error = new WP_Error('launchkey_pair_error', $this->wp_facade->__('Username is required to pair', $this->language_domain)); } else { // Otherwise, attempt to pair the LaunchKey userusing the supplied launchkey_username $response = $this->authenticate_user($user->ID, $_POST['launchkey_username']); // If there was an error during the authentication process, set the pair error if ($this->wp_facade->is_wp_error($response)) { $this->pair_error = $response; } } } }