namespace MiniOrange\IDPSaml\Controller\Actions;
use Magento\Framework\App\Action\Context;
use MiniOrange\IDPSaml\Helper\IDPUtility;
use MiniOrange\IDPSaml\Helper\IDPConstants;
use MiniOrange\IDPSaml\Helper\Data;
use Magento\Framework\App\ResponseFactory;
use Magento\Framework\UrlInterface;
use Magento\Framework\App\Response\Http;
use Magento\Framework\Controller\ResultFactory;
use Magento\Customer\Model\Session;
class Authorize extends BaseAction
{
protected $REQUEST;
protected $POST;
private $passwordGrant;
protected $resultFactory;
private $customerSession;
private $response;
protected $responseFactory;
protected $url;
private $_configLoader;
private $backendSession;
public function __construct(Context $FF, IDPUtility $Ng, ResultFactory $Fb, Session $d9, ResponseFactory $Pd, UrlInterface $cz, \Magento\Framework\ObjectManager\ConfigLoaderInterface $O7, \Magento\Backend\Model\Auth\Session $nY, Http $WG)
{
$this->resultFactory = $Fb;
$this->customerSession = $d9;
$this->responseFactory = $Pd;
$this->url = $cz;
$this->response = $WG;
$this->_configLoader = $O7;
$this->backendSession = $nY;
parent::__construct($FF, $Ng);
}
public function execute()
{
if ($this->idpUtility->check_plan(2)) {
goto iJ;
}
$this->idpUtility->checkIfValidLicense();
goto jh;
iJ:
$this->idpUtility->checkTrialExpiry();
jh:
$AG = null;
if ($this->idpUtility->check_plan(2)) {
goto Lg;
}
$AV = $this->idpUtility->getStoreConfig(IDPConstants::REG_STATUS);
$TU = isset($AV) ? true : false;
$gJ = $TU && $AV != '' && $AV == "MO_VERIFIED" ? true : false;
if ($gJ) {
goto Tx;
}
return $this->getResponse()->setBody("Please activate the plugin from Magento dashboard in order to perform the SSO.");
Tx:
Lg:
$lw = $this->getRequest()->getParams();
$this->idpUtility->log_debug("params in Authorize.php: ", $lw);
$Kb = $this->idpUtility->getAllRedirectUri("miniorange_sp_table");
$pj = $lw["redirect_uri"];
foreach ($Kb as $av) {
if (!in_array($pj, $av)) {
goto rc;
}
$AG = $this->idpUtility->fetchspallDetailsusingRedirectUri("miniorange_sp_table", $pj);
rc:
Vi:
}
Gk:
if (isset($AG)) {
goto pj;
}
return $this->getResponse()->setBody("Redirect URL Mismatch!");
pj:
$AG = $AG[0];
$MN = $AG[IDPConstants::CLIENT_ID];
$H4 = $AG[IDPConstants::REDIRECT_URL];
$SR = $AG[IDPConstants::SCOPE];
if (!(!isset($lw["scope"]) || empty($lw["scope"]) || !(strpos($SR, $lw["scope"]) !== false))) {
goto Zw;
}
return $this->getResponse()->setBody("Invalid scopes provided in the request!");
Zw:
if (isset($lw["state"])) {
goto Qa;
}
$Gq = "openid";
goto ND;
Qa:
$Gq = $lw["state"];
$l6 = $lw[IDPConstants::CLIENT_ID];
if (!isset($lw["redirect_uri"]) || !isset($lw["response_type"])) {
goto Ne;
}
$nZ = $lw["redirect_uri"];
$Kr = $lw["response_type"];
$Mk = $this->idpUtility->validateClient($MN, $l6, $H4, $nZ, $Kr);
goto Eg;
Ne:
$Mk = "Inavlid Request parameters";
Eg:
if (!$Mk) {
goto GY;
}
return $this->getResponse()->setBody($Mk);
GY:
ND:
$this->idpUtility->log_debug("Authorize: params: ", print_r($lw, true));
$Tn = isset($lw["nonce"]) ? $lw["nonce"] : '';
if (!$AG[IDPConstants::IS_BACKEND_SSO_ENABLED]) {
goto XD;
}
if ($AG[IDPConstants::IS_BACKEND_SSO_ENABLED]) {
goto BH;
}
goto wz;
XD:
if ($this->customerSession->isLoggedIn()) {
goto A9;
}
$cz = $this->url->getUrl("customer/account/login");
$WG = $this->responseFactory->create();
return $this->customerSession->authenticate();
A9:
$Ij = $this->customerSession->getCustomerId();
$Tv = $this->idpUtility->generateRandom(16);
$this->idpUtility->setStoreConfig(IDPConstants::OAUTH_CODE, $Tv);
$tR = $AG[IDPConstants::REDIRECT_URL];
if (!empty($tR)) {
goto L6;
}
return $this->getResponse()->setBody("Redirect URL not configured.");
goto bs;
L6:
if (strpos($tR, "?") !== false) {
goto UL;
}
$cz = $tR . "?code=" . $Tv . "&state=" . $Gq;
goto wj;
UL:
$cz = $tR . "&code=" . $Tv . "&state=" . $Gq;
wj:
bs:
$Ba = time();
$this->idpUtility->addAuthCodeData($Ij, $Tv, $Ba, $Tn);
$this->idpUtility->log_debug("sending authorization response");
return $this->sendOAuthHTTPRedirectRequest($cz);
goto wz;
BH:
$this->idpUtility->log_debug("Backend user flow");
$Ij = $this->idpUtility->getCookie("admin_id");
$this->idpUtility->log_debug("customerId: ", $Ij);
if (isset($Ij)) {
goto Rf;
}
$this->idpUtility->log_debug("customerId not set");
$cz = $this->idpUtility->getAdminUrl("admin/dashboard/index") . "?admin=1&state=" . $Gq . "&client_id=" . $lw[IDPConstants::CLIENT_ID] . "&scope=" . $lw["scope"] . "&nonce=" . $Tn;
return $this->getResponse()->setRedirect($cz)->sendResponse();
Rf:
$Tv = $this->idpUtility->generateRandom(16);
$this->idpUtility->setStoreConfig(IDPConstants::OAUTH_CODE, $Tv);
$tR = $AG[IDPConstants::REDIRECT_URL];
$this->idpUtility->log_debug("Authorize.php: client selected: ", $AG[IDPConstants::CLIENT_NAME]);
if (!empty($tR)) {
goto EJ;
}
return $this->getResponse()->setBody("Redirect URL not configured.");
goto pP;
EJ:
if (strpos($tR, "?") !== false) {
goto l1;
}
$cz = $tR . "?code=" . $Tv . "&state=" . $Gq;
goto Ri;
l1:
$cz = $tR . "&code=" . $Tv . "&state=" . $Gq;
Ri:
pP:
$Ba = time();
$this->idpUtility->log_debug("nonce in adminloginsucceeded: " . $Tn);
$this->idpUtility->addAuthCodeData($Ij, $Tv, $Ba, $Tn);
$this->idpUtility->log_debug("sending authorization response {$Ij}, {$Tv}, {$Ba}");
return $this->sendOAuthHTTPRedirectRequest($cz);
wz:
}
}
© 2023 Quttera Ltd. All rights reserved.