/**
  * handler for LaunchKey authentication
  * @since 1.0.0
  */
 public function launchkey_callback()
 {
     try {
         $headers = array();
         array_walk($_SERVER, function ($value, $key) use(&$headers) {
             if (preg_match('/^HTTP\\_(.+)$/', $key, $matches)) {
                 $headers[str_replace('_', '-', $matches[1])] = $value;
             }
         });
         preg_match('/^[^\\/]+\\/(.*)$/', $_SERVER['SERVER_PROTOCOL'], $matches);
         $protocol_version = $matches ? $matches[1] : null;
         $request = new Request($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], $headers, $this->wp_facade->fopen('php://input', 'rb'), $protocol_version);
         $http_response = new Response();
         // Have the SDK client handle the callback
         $response = $this->launchkey_client->serverSentEvent()->handleEvent($request, $http_response);
         if ($response instanceof \LaunchKey\SDK\Domain\AuthResponse) {
             // If this is an auth response
             // Find the user by the auth_request provided in the response
             $users = $this->wp_facade->get_users(array('meta_key' => 'launchkey_auth', 'meta_value' => $response->getAuthRequestId()));
             if (count($users) > 1) {
                 throw new \LaunchKey\SDK\Service\Exception\InvalidRequestError('Too many users found for user hash ' . $response->getUserHash());
             } elseif (count($users) < 1) {
                 throw new \LaunchKey\SDK\Service\Exception\InvalidRequestError('No user found for user hash ' . $response->getUserHash());
             }
             $user = array_pop($users);
             // Update the auth value and the user hash in the user metadata based on response data
             $this->wp_facade->update_user_meta($user->ID, "launchkey_authorized", $response->isAuthorized() ? 'true' : 'false');
             $this->wp_facade->update_user_meta($user->ID, "launchkey_user", $response->getUserHash());
             // If this is a native implementation and we have a valid User Push ID in the response,
             // replace the username with that to prevent exposure of the username
             $options = $this->get_option(LaunchKey_WP_Admin::OPTION_KEY);
             $user_push_id = $response->getUserPushId();
             if ($user_push_id && LaunchKey_WP_Implementation_Type::NATIVE === $options[LaunchKey_WP_Options::OPTION_IMPLEMENTATION_TYPE]) {
                 $this->wp_facade->update_user_meta($user->ID, "launchkey_username", $user_push_id);
             }
         } elseif ($response instanceof \LaunchKey\SDK\Domain\DeOrbitCallback) {
             // If it's a de-orbit request
             // Find the user by the provided user hash
             $users = $this->wp_facade->get_users(array('meta_key' => 'launchkey_user', 'meta_value' => $response->getUserHash()));
             if (count($users) !== 1) {
                 throw new \LaunchKey\SDK\Service\Exception\InvalidRequestError('Too many users found for user hash ' . $response->getUserHash());
             }
             $user = array_pop($users);
             // Set authorized to false in the user metadata
             $this->wp_facade->update_user_meta($user->ID, "launchkey_authorized", 'false');
             $this->launchkey_client->auth()->deOrbit($user->launchkey_auth);
         }
     } catch (\Exception $e) {
         if ($this->wp_facade->is_debug_log()) {
             $this->wp_facade->error_log('Callback Exception: ' . $e->getMessage());
         }
         if ($e instanceof \LaunchKey\SDK\Service\Exception\InvalidRequestError || $e instanceof \LaunchKey\SDK\Service\Exception\UnknownCallbackActionError) {
             $this->wp_facade->wp_die('Invalid Request', 400);
         } else {
             // Otherwise, return 500
             $this->wp_facade->wp_die('Server Error', 500);
         }
     }
 }
 /**
  * handler for LaunchKey authentication
  * @since 1.0.0
  */
 public function launchkey_callback()
 {
     // Get an SDK auth client
     $auth = $this->launchkey_client->auth();
     try {
         // We are going to modify the query parameters, so copy the global $_GET
         $query = $_GET;
         // If deorbit is present, strip slashes as they being added by WordPress to "sanitize" request data
         if (isset($query['deorbit'])) {
             $query['deorbit'] = stripslashes($query['deorbit']);
         }
         // Have the SDK client handle the callback
         $response = $auth->handleCallback($query);
         if ($response instanceof \LaunchKey\SDK\Domain\AuthResponse) {
             // If this is an auth response
             // Find the user by the auth_request provided in the response
             $users = $this->wp_facade->get_users(array('meta_key' => 'launchkey_auth', 'meta_value' => $response->getAuthRequestId()));
             if (count($users) > 1) {
                 throw new \LaunchKey\SDK\Service\Exception\InvalidRequestError('Too many users found for user hash ' . $response->getUserHash());
             } elseif (count($users) < 1) {
                 throw new \LaunchKey\SDK\Service\Exception\InvalidRequestError('No user found for user hash ' . $response->getUserHash());
             }
             $user = array_pop($users);
             // Update the auth value and the user hash in the user metadata based on response data
             $this->wp_facade->update_user_meta($user->ID, "launchkey_authorized", $response->isAuthorized() ? 'true' : 'false');
             $this->wp_facade->update_user_meta($user->ID, "launchkey_user", $response->getUserHash());
             // If this is a native implementation and we have a valid User Push ID in the response, replace the username with that to prevent exposure of the username
             $options = $this->wp_facade->get_option(LaunchKey_WP_Admin::OPTION_KEY);
             $user_push_id = $response->getUserPushId();
             if ($user_push_id && LaunchKey_WP_Implementation_Type::NATIVE === $options[LaunchKey_WP_Options::OPTION_IMPLEMENTATION_TYPE]) {
                 $this->wp_facade->update_user_meta($user->ID, "launchkey_username", $user_push_id);
             }
         } elseif ($response instanceof \LaunchKey\SDK\Domain\DeOrbitCallback) {
             // If it's a de-orbit request
             // Find the user by the provided user hash
             $users = $this->wp_facade->get_users(array('meta_key' => 'launchkey_user', 'meta_value' => $response->getUserHash()));
             if (count($users) !== 1) {
                 throw new \LaunchKey\SDK\Service\Exception\InvalidRequestError('Too many users found for user hash ' . $response->getUserHash());
             }
             $user = array_pop($users);
             // Set authorized to false in the user metadata
             $this->wp_facade->update_user_meta($user->ID, "launchkey_authorized", 'false');
             $auth->deOrbit($user->launchkey_auth);
         }
     } catch (\Exception $e) {
         if ($e instanceof \LaunchKey\SDK\Service\Exception\InvalidRequestError || $e instanceof \LaunchKey\SDK\Service\Exception\UnknownCallbackActionError) {
             $this->wp_facade->wp_die('Invalid Request', 400);
         } else {
             // Otherwise, return 500
             if ($this->wp_facade->is_debug_log()) {
                 $this->wp_facade->error_log('Callback Exception: ' . $e->getMessage());
             }
             $this->wp_facade->wp_die('Server Error', 500);
         }
     }
 }
 /**
  * @param $user_hash
  *
  * @return int
  *
  */
 private function get_user_id_by_launchkey_user_hash($user_hash)
 {
     //Match existing user to LaunchKey user
     $meta_args = array('meta_key' => 'launchkey_user', 'meta_value' => $user_hash);
     $users = $this->wp_facade->get_users($meta_args);
     $id = null;
     if (!empty($users) && is_array($users)) {
         $user = array_shift($users);
         if ($user instanceof WP_User && $user->ID) {
             $id = $user->ID;
         }
     }
     return $id;
 }