Exemplo n.º 1
0
 /**
  * Check if admin has Latch enabled
  * 
  * @param string $latchId
  * @param Mage_Admin_Model_User $user
  * @return array
  */
 public function getIfAdminLatchEnabled($latchId, $user)
 {
     $appId = $this->getApplicationId();
     $appSecret = $this->getSecretKey();
     $apiUrl = $this->getApiUrl();
     if (!empty($latchId) && !empty($appId) && !empty($appSecret)) {
         require_once Mage::getBaseDir('lib') . '/Latch/latch.php';
         if ($apiUrl) {
             $api = new Latch($appId, $appSecret, $apiUrl);
         } else {
             $api = new Latch($appId, $appSecret);
         }
         $apiResponse = $api->status($latchId);
         $responseData = $apiResponse->getData();
         $responseError = $apiResponse->getError();
         if (empty($apiResponse) || empty($responseData) && empty($responseError)) {
             return array("status" => 0, "message" => $this->__("Latch is not ready. Please try to log out and log in again."));
         } else {
             if (!empty($responseError)) {
                 if ($responseError->getCode() == 201) {
                     $user->setData('latch_id', $latchId);
                     try {
                         $user->save();
                     } catch (Exception $ex) {
                         return array("status" => 0, "message" => $this->__("Something was wrong, please try to log in again later: ") . $this->__($ex->getMessage()));
                     }
                 } else {
                     return array("status" => 0, "message" => $this->__("Something was wrong, please try to log in again later."));
                 }
             }
         }
         if (!empty($responseData) && $responseData->{"operations"}->{$appId}->{"status"} === "on") {
             return array("status" => 0, "message" => "");
         } else {
             return array("status" => 1, "message" => $this->__("Invalid login or password"));
         }
     }
 }
 public static function wfLoginHook(&$returnTo, &$returnToQuery, &$type)
 {
     global $wgUser, $wgOut, $wgRequest, $wgTitle;
     $acc_id = "";
     $msg = "";
     $app_id = "";
     $secret = "";
     $type = 'error';
     $two_factor_token = "";
     $user_id = "";
     # We remove the user's name to "freeze" the session
     $wgRequest->setSessionData('wsUserName', "");
     # If app_id, secret, user_id and the account_id are already in the DB, we take them
     SpecialLatch::accDB_appsecret($app_id, $secret);
     SpecialLatch::accDB_useraccid($wgUser->getId(), $user_id, $acc_id);
     # If the user doesn't have Latch configured we redirect him to Main Page without checking anything
     if (!empty($user_id) && !empty($acc_id)) {
         # We call the Status function from the Latch SDK
         $api = new Latch($app_id, $secret);
         $statusResponse = $api->status($acc_id);
         $responseData = $statusResponse->getData();
         $responseError = $statusResponse->getError();
         if (empty($statusResponse) || empty($responseData) && empty($responseError)) {
             return false;
         } else {
             # If everything is OK and the status is on, we redirect the user to the main page and set the user's name again
             if (!empty($responseData) && $responseData->{"operations"}->{$app_id}->{"status"} === "on") {
                 if (!empty($responseData->{"operations"}->{$app_id}->{"two_factor"})) {
                     $two_factor_token = $responseData->{"operations"}->{$app_id}->{"two_factor"}->{"token"};
                     # We have another special page for the OTP page. We insert the OTP token on DB and we redirect to that page
                     if (!empty($two_factor_token)) {
                         SpecialLatch::updDB_useraccid($user_id, $acc_id, $two_factor_token);
                         $wgOut->redirect(SpecialPage::getTitleFor('LatchOTP')->getFullURL('', false, PROTO_CURRENT));
                     }
                 } else {
                     SpecialLatch::putUserInSession();
                 }
             } else {
                 if (!empty($responseData) && $responseData->{"operations"}->{$app_id}->{"status"} === "off") {
                     $wgUser->logout();
                     $specialUserlogin = new LoginForm();
                     $specialUserlogin->load();
                     $error = $specialUserlogin->mAbortLoginErrorMsg ?: 'wrongpassword';
                     $specialUserlogin->mainLoginForm($specialUserlogin->msg($error)->text());
                 } else {
                     SpecialLatch::putUserInSession();
                 }
             }
         }
     } else {
         SpecialLatch::putUserInSession();
     }
     return true;
 }