<?php
|
|
namespace App\Vola\Services\VolaFakeHTTPResponder;
|
|
use App\Vola\Classes\Utils;
|
use Cookie;
|
use GuzzleHttp\Promise\RejectedPromise;
|
use GuzzleHttp\Exception\ConnectException;
|
use Illuminate\Http\Request;
|
use Illuminate\Support\Arr;
|
use Illuminate\Support\Str;
|
|
class VolaFakeHTTPResponder
|
{
|
public array $data;
|
private array $xmlResponses;
|
|
public function __construct()
|
{
|
$this->xmlResponses = config('devtools.fake_sso_profiles');
|
}
|
|
public function getModelBasedResponses(Request $request): array
|
{
|
$picassoRequest = (str_starts_with($request->getRequestUri(), '/picasso/',));
|
$profile = Utils::getRequestedUser($request);
|
|
if (isset($profile)) {
|
|
$responseContent["data"] = 'test. funziono ma le logiche non sono state ancora implementate';
|
$responseContent["status"] = 200;
|
\RequestLogger::logProcessedRequest($request, $responseContent, (($picassoRequest) ? 'Picasso' : 'SSO Legacy') );
|
return $responseContent;
|
|
} else {
|
\RequestLogger::logRejectedRequest($request, $request->cookies);
|
return [
|
"status" => 400,
|
"data" => $this->getErrorMessage('no-cookie')
|
];
|
}
|
|
}
|
|
public function getResponses(Request $request, $selectedUser, $picasso = false): string|array
|
{
|
$uriParts = explode("?", $request->url());
|
$methodUri = Str::afterLast($uriParts[0], "/");
|
$reqParams = !empty($request->query()) ? $request->query() : [];
|
if (($methodUri == 'isLogged') || ($methodUri == 'islogged')) {
|
// controllo cookie e conseguente risposta isLogged
|
$loginResponse = ($picasso) ? $this->getLoggedPicasso($selectedUser) : $this->getLoggedLegacy($selectedUser);
|
return $loginResponse;
|
|
} else if ($methodUri != "") {
|
// controllo metodo e parametri e conseguente risposta
|
$userResponses = $this->getUserResponseGroup($selectedUser, $picasso);
|
if (isset($userResponses[$methodUri])) {
|
$responseContent = $this->getResponseArgumentsBased($userResponses, $methodUri, $reqParams);
|
return $responseContent;
|
} else {
|
return $methodUri . " " . $selectedUser . " " . $this->getErrorMessage('no-method-on-user');
|
}
|
} else {
|
return $this->getErrorMessage('no-method');
|
}
|
}
|
|
public function getLoggedPicasso($selectedUser): string|array
|
{
|
if (isset($_COOKIE["SSOSESSIONID"])) {
|
if (!isset($this->xmlResponses[$selectedUser]["picasso"])) {
|
return $this->getErrorMessage('no-picasso-user');
|
}
|
$sampleResponseString = Arr::first($this->xmlResponses[$selectedUser]["picasso"]["getWebcustomerInformation"]["parametri"]["k"] );
|
$sampleResponseObject = Utils::convertXMLStrToArray($sampleResponseString);
|
$resp = [
|
'logged' => (string) 1,
|
'errorCode' => (string) 0,
|
'username' => (string) $sampleResponseObject["Username"],
|
'next_user' => (string) 'N',
|
'token' => (string) 'xno:' . $selectedUser,
|
];
|
} else {
|
$resp = [
|
'logged' => (string) 0,
|
'errorCode' => (string) 0,
|
];
|
}
|
$xml = Utils::convertToXML($resp, $root = '<isLogged/>');
|
return $xml;
|
}
|
|
public function getLoggedLegacy($selectedUser): string|array
|
{
|
if (isset($_COOKIE["CAuthCookie"])) {
|
if (!isset($this->xmlResponses[$selectedUser]["sso"])) {
|
return $this->getErrorMessage('no-sso-user');
|
}
|
$sampleResponseString = Arr::first($this->xmlResponses[$selectedUser]["sso"]["getWebcustomerInformation"]["parametri"]["k"] );
|
$sampleResponseObject = Utils::convertXMLStrToArray($sampleResponseString);
|
$resp = [
|
'logged' => (string) 1,
|
'errorCode' => (string) 0,
|
'username' => (string) $sampleResponseObject["Username"],
|
'token' => (string) 'xno:' . $selectedUser,
|
'next_user' => (string) 'N',
|
];
|
} else {
|
$resp = [
|
'logged' => (string) 0,
|
'errorCode' => (string) 0,
|
];
|
}
|
$xml = Utils::convertToXML($resp, $root = '<isLogged/>');
|
return $xml;
|
}
|
|
public function getResponseArgumentsBased($userMethods, $methodUri, $reqParams = []): string
|
{
|
$method = $userMethods[$methodUri];
|
$params = $method["parametri"];
|
|
//assumo che se la richiesta sia arrivata fin qui abbia tutti i mandatory nel url
|
foreach ($reqParams as $key => $value) {
|
//verifico se il metodo dell' utente ha la chiave che sto cercando
|
if ($key != "k" && isset($params[$key])) {
|
if (array_key_exists($value, $params[$key])) {
|
\Log::channel('requests')->debug("[VolaFakeHTTPResponder] Inserisco xml specifico per key:$key ,params: " . print_r($params, 1) . " key: $key, value: $value, xml: " . print_r($params[$key][$value], 1));
|
return $params[$key][$value];
|
}
|
}
|
}
|
//xml di default (se richiede parametri di default conterrĂ errori)
|
$defaultResponse = Arr::first($params["k"]);
|
\Log::channel('requests')->debug("[VolaFakeHTTPResponder] Inserisco xml di default $defaultResponse per method: $methodUri");
|
return $defaultResponse;
|
}
|
|
/**
|
* @param Request $request
|
* @param mixed $selectedUser
|
* @param bool $picasso
|
* @return array|mixed
|
*/
|
private function getUserResponseGroup(mixed $selectedUser, bool $picasso): array
|
{
|
if ($picasso) {
|
$userMethods = $this->xmlResponses[$selectedUser]["picasso"] ?? [];
|
} else {
|
$userMethods = $this->xmlResponses[$selectedUser]["sso"] ?? [];
|
}
|
return $userMethods;
|
}
|
|
private function getErrorMessage(string $code): string
|
{
|
switch ($code) {
|
case 'no-cookie':
|
return 'No cookie No Party. Send your CAuthCookie or SESSIONID cookie';
|
case 'no-sso-user':
|
return 'This sso user doesnt exist. Check your CAuthCookie cookie value';
|
case 'no-picasso-user':
|
return 'This picasso user doesnt exist. Check your SSOSESSIONID coookie value';
|
case 'no-method-on-user':
|
return 'This method response is not set for this user. Check this server database.';
|
case 'no-method':
|
return 'Couldnt detect requested method. Check your request url.';
|
default:
|
return 'Couldnt detect proper response.';
|
}
|
}
|
|
}
|