/** * @since 1.0.0 */ public function verify_configuration_callback() { if (isset($_REQUEST['nonce']) && $this->wp_facade->wp_verify_nonce($_REQUEST['nonce'], static::VERIFIER_NONCE_KEY)) { $user = $this->wp_facade->wp_get_current_user(); $response = array('nonce' => $this->wp_facade->wp_create_nonce(static::VERIFIER_NONCE_KEY)); if (stripos($_SERVER['REQUEST_METHOD'], 'POST') !== false && isset($_POST['verify_action']) && 'pair' === $_POST['verify_action']) { try { $white_label_user = $this->launchkey_client->whiteLabel()->createUser($user->user_login); $response['qrcode_url'] = $white_label_user->getQrCodeUrl(); $response['manual_code'] = $white_label_user->getCode(); } catch (Exception $e) { $response['error'] = $e->getCode(); } } elseif (stripos($_SERVER['REQUEST_METHOD'], 'POST') !== false) { $response['completed'] = false; try { $username = empty($_POST['username']) ? $user->user_login : $_POST['username']; $auth_request = $this->launchkey_client->auth()->authorize($username); $this->wp_facade->update_user_meta($user->ID, 'launchkey_username', $username); $this->wp_facade->update_user_meta($user->ID, 'launchkey_auth', $auth_request->getAuthRequestId()); $this->wp_facade->update_user_meta($user->ID, 'launchkey_authorized', null); } catch (Exception $e) { $response['error'] = $e->getCode(); } } else { $db = $this->wp_facade->get_wpdb(); $value = $db->get_var($db->prepare("SELECT meta_value FROM {$db->usermeta} WHERE user_id = %s AND meta_key = 'launchkey_authorized' LIMIT 1", $user->ID)); $response['completed'] = !empty($value); } $this->wp_facade->wp_send_json($response); } }
/** * Compile the data that will be used by the front end to generate a QR Code for WordPress auto-config. * @since 1.4.0 */ public function wizard_easy_setup_qr_code() { if (isset($_POST['nonce'])) { if ($this->wp_facade->wp_verify_nonce($_POST['nonce'], static::WIZARD_NONCE_KEY) && $this->wp_facade->current_user_can('manage_options')) { $lk_nonce = $this->launchkey_client->auth()->nonce(); $this->update_option(static::EASY_SETUP_OPTION, array('nonce' => $lk_nonce, 'username' => $this->wp_facade->wp_get_current_user()->user_login)); $payload = json_encode(array('nonce' => $lk_nonce->getNonce(), 'payload' => array('callback_url' => $this->admin->get_callback_url(), 'rocket_name' => $this->wp_facade->get_bloginfo('name')))); $qr_data = base64_encode($payload); $response['nonce'] = $this->wp_facade->wp_create_nonce(static::WIZARD_NONCE_KEY); $response['qr_code'] = $qr_data; } else { $response['errors'] = $this->wp_facade->__("An error occurred submitting the page. Please refresh the page and submit again."); } $this->wp_facade->wp_send_json($response); } }
/** * @param $user_id * @param $launchkey_username * * @return null|WP_Error */ private function authenticate_user($user_id, $launchkey_username) { // reset user authentication $this->reset_auth($user_id); // Get the auth client from the SDK $auth = $this->launchkey_client->auth(); try { // Authenticate and get the request ID $auth_request = $auth->authenticate($launchkey_username)->getAuthRequestId(); // Set the auth request ID in the user metadata to be available to the server side event $this->wp_facade->update_user_meta($user_id, 'launchkey_auth', $auth_request); // Loop until a response has been recorded by the SSE callback do { // Sleep before checking for the response to not kill the server sleep(1); // See if the user has authorized $auth = $this->get_user_authorized($user_id); } while (null === $auth); // If the response is null, continue the loop if ($auth) { // If the user accepted, return true $response = true; } else { // Otherwise, return an error $response = new WP_Error('launchkey_authentication_denied', $this->wp_facade->__('Authentication denied!', $this->language_domain)); } } catch (Exception $e) { // Process exceptions appropriately $response = new WP_Error(); if ($e instanceof \LaunchKey\SDK\Service\Exception\NoPairedDevicesError) { $response->add('launchkey_authentication_denied', $this->wp_facade->__('No Paired Devices!', $this->language_domain)); } elseif ($e instanceof \LaunchKey\SDK\Service\Exception\NoSuchUserError) { $response->add('launchkey_authentication_denied', $this->wp_facade->__('Authentication denied!', $this->language_domain)); } elseif ($e instanceof \LaunchKey\SDK\Service\Exception\RateLimitExceededError) { $response->add('launchkey_authentication_denied', $this->wp_facade->__('Authentication denied!', $this->language_domain)); } elseif ($e instanceof \LaunchKey\SDK\Service\Exception\ExpiredAuthRequestError) { $response->add('launchkey_authentication_timeout', $this->wp_facade->__('Authentication denied!', $this->language_domain)); } else { if ($this->wp_facade->is_debug_log()) { $this->wp_facade->error_log('Error authenticating user with Launchkey: ' . $e->getMessage()); } $response->add('launchkey_authentication_error', $this->wp_facade->__('Authentication error! Please try again later', $this->language_domain)); } } return $response; }
/** * Logout the user and perform a de-orbit if there is a known LaunchKey auth_request * * @since 1.0.0 */ public function logout() { // If there is a current user if ($user = $this->wp_facade->wp_get_current_user()) { // And that user has logged in with LaunchKey if (!empty($user->launchkey_auth)) { try { // De-orbit the auth $this->launchkey_client->auth()->deOrbit($user->launchkey_auth); } catch (Exception $e) { if ($this->wp_facade->is_debug_log()) { $this->wp_facade->error_log('LaunchKey Error on native client log out: ' . $e->getMessage()); } } } // Remove the aith data for the user $this->reset_auth($user->ID); } }
public function testWpFactoryReturnsClientWithValidAuth() { $this->assertInstanceOf('LaunchKey\\SDK\\Service\\AuthService', $this->wpClient->auth()); }