/**
  * Verify user entitlement
  *
  * @return bool|\WP_Error Whether user has entitlement or error message
  */
 public function verify_entitlement()
 {
     $auth_token = '';
     $product_id = '';
     if (!empty($_REQUEST['authToken'])) {
         $auth_token = trim($_REQUEST['authToken']);
     }
     if (!empty($_REQUEST['productId'])) {
         $product_id = trim($_REQUEST['productId']);
     }
     if ('' === $product_id) {
         // Empty Product ID
         return Util::get_wp_error('product-required');
     }
     // Get user from auth token
     $user = User::get_user_from_auth_token($auth_token);
     if ($user && !is_wp_error($user)) {
         // Check if user has entitlement
         $has_entitlement = $user->has_entitlement($product_id);
     } else {
         // Error getting user from auth token
         $has_entitlement = $user;
     }
     return $has_entitlement;
 }
 /**
  * Renew auth token or revoke if inactive
  *
  * @return string|\WP_Error Auth token or error message
  */
 public function renew_auth_token()
 {
     $auth_token = '';
     $uuid = '';
     if (!empty($_REQUEST['authToken'])) {
         $auth_token = trim($_REQUEST['authToken']);
     }
     if (!empty($_REQUEST['uuid'])) {
         $uuid = trim($_REQUEST['uuid']);
     }
     if (!empty($uuid)) {
         // Get user from auth token
         $user = User::get_user_from_auth_token($auth_token);
         if (!is_wp_error($user)) {
             // Check if subscription is active
             $subscription_is_active = $user->is_subscription_active();
             if ($subscription_is_active) {
                 // Check if UUID is registered
                 if (!$user->is_uuid_allowed($uuid, false)) {
                     $auth_token = Util::get_wp_error('uuid-not-registered');
                 }
             } else {
                 // Subscription is not active
                 // Delete auth token from user
                 $user->delete_auth_token();
                 $auth_token = Util::get_wp_error('subscription-inactive');
             }
         } else {
             // Error getting user from auth token
             $auth_token = $user;
         }
     } else {
         // Missing UUID
         $auth_token = Util::get_wp_error('uuid-invalid');
     }
     return $auth_token;
 }
 /**
  * Get auth token from credentials and check if UUID is allowed
  *
  * @return string|\WP_Error Auth token or false if there is an error
  */
 public function get_auth_token_from_credentials()
 {
     $uuid = '';
     if (!empty($_REQUEST['uuid'])) {
         $uuid = trim($_REQUEST['uuid']);
     }
     if (!empty($uuid)) {
         $user_login = '';
         $password = '';
         if (isset($_REQUEST['emailAddress']) || isset($_REQUEST['password'])) {
             // Allow for integration when not sent via XML
             if (!empty($_REQUEST['emailAddress'])) {
                 $user_login = $_REQUEST['emailAddress'];
             }
             if (!empty($_REQUEST['password'])) {
                 $password = $_REQUEST['password'];
             }
         } else {
             // DPS sends credentials via XML request string
             $credentials_string = file_get_contents('php://input');
             $credentials_xml = simplexml_load_string($credentials_string);
             if ($credentials_xml) {
                 if (!empty($credentials_xml->emailAddress)) {
                     $user_login = $credentials_xml->emailAddress;
                 }
                 if (!empty($credentials_xml->password)) {
                     $password = $credentials_xml->password;
                 }
             }
         }
         $user_login = trim($user_login);
         $password = trim($password);
         if (!empty($user_login) && !empty($password)) {
             $user = User::get_user_by_login_email($user_login);
             if ($user && !is_wp_error($user)) {
                 if (wp_check_password($password, $user->user_pass)) {
                     if ($user->is_uuid_allowed($uuid)) {
                         // Device is allowed
                         // Get / create auth token for user
                         $auth_token = $user->get_auth_token();
                     } else {
                         // Max limit reached
                         $auth_token = Util::get_wp_error('max-devices');
                     }
                 } else {
                     // Password does not match
                     $auth_token = Util::get_wp_error('login-invalid');
                 }
             } else {
                 // User not found
                 $auth_token = Util::get_wp_error('login-invalid');
             }
         } else {
             // Missing details
             $auth_token = Util::get_wp_error('login-required');
         }
     } else {
         // Missing UUID
         $auth_token = Util::get_wp_error('uuid-invalid');
     }
     return $auth_token;
 }