/**
  * Callback handler to process white label pairing via AJAX
  *
  * @since 1.0.0
  */
 public function white_label_pair_callback()
 {
     if (isset($_POST['nonce'])) {
         // If there is no nonce, ignore the request
         if ($this->wp_facade->wp_verify_nonce($_POST['nonce'], LaunchKey_WP_User_Profile::NONCE_KEY)) {
             // If there is a valid nonce
             if ($user = $this->wp_facade->wp_get_current_user()) {
                 // and a current logged in user
                 try {
                     // Create a LaunchKey White Label user with the WordPress username as the unique identifer
                     $pair_response = $this->launchkey_client->whiteLabel()->createUser($user->user_login);
                     // Set the WordPress username as the LaunchKey username for subsequent login attempts
                     $this->wp_facade->update_user_meta($user->ID, 'launchkey_username', $user->user_login);
                     // Set up the response with the QR Code URL and manual pairing codes
                     $response = array('qrcode' => $pair_response->getQrCodeUrl(), 'code' => $pair_response->getCode());
                 } catch (\LaunchKey\SDK\Service\Exception\CommunicationError $e) {
                     // Communication error response
                     $response = array('error' => 'There was a communication error encountered during the pairing process.  Please try again later');
                 } catch (\LaunchKey\SDK\Service\Exception\InvalidCredentialsError $e) {
                     // Invalid credentials response
                     $response = array('error' => 'There was an error encountered during the pairing process caused by a misconfiguration.  Please contact the administrator.');
                 } catch (\Exception $e) {
                     // General error response
                     $response = array('error' => 'There was an error encountered during the pairing process.  Please contact the administrator.');
                 }
                 // Add a new nonce to the response to allow another request
                 $response['nonce'] = $this->wp_facade->wp_create_nonce(LaunchKey_WP_User_Profile::NONCE_KEY);
                 // Set the headers for the AJAX response
                 $this->wp_facade->wp_send_json($response);
             }
         }
     }
 }
 /**
  * @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) && $this->wp_facade->current_user_can('manage_options')) {
         $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);
     }
 }
Ejemplo n.º 3
0
 public function testWpFactoryReturnsClientWithValidWhiteLabelService()
 {
     $this->assertInstanceOf('LaunchKey\\SDK\\Service\\WhiteLabelService', $this->wpClient->whiteLabel());
 }