davide.cucurnia@vola.it
2024-07-02 b55a38378482bfe6a43315f5b665f608a7651880
commit | author | age
9f6455 1 <?php
DC 2
3 namespace App\Vola\Services\VolaFakeHTTPResponder;
4
5 use App\Vola\Classes\Utils;
6 use Cookie;
7 use GuzzleHttp\Promise\RejectedPromise;
8 use GuzzleHttp\Exception\ConnectException;
9 use Illuminate\Http\Request;
10 use Illuminate\Support\Arr;
11 use Illuminate\Support\Str;
12
13 class VolaFakeHTTPResponder
14 {
15     public array $data;
bdead4 16     private array $xmlResponses;
9f6455 17
DC 18     public function __construct()
19     {
bdead4 20         $this->xmlResponses = config('devtools.fake_sso_profiles');
9f6455 21     }
DC 22
fcb093 23     public function getModelBasedResponses(Request $request): array
59b068 24     {
fcb093 25         $picassoRequest = (str_starts_with($request->getRequestUri(), '/picasso/',));
D 26         $profile = Utils::getRequestedUser($request);
27
28         if (isset($profile)) {
29
30             $responseContent["data"] = 'test. funziono ma le logiche non sono state ancora implementate';
31             $responseContent["status"] = 200;
32             \RequestLogger::logProcessedRequest($request, $responseContent, (($picassoRequest) ? 'Picasso' : 'SSO Legacy') );
33             return $responseContent;
34
35         } else {
36             \RequestLogger::logRejectedRequest($request, $request->cookies);
37             return [
38                 "status" => 400,
39                 "data" => $this->getErrorMessage('no-cookie')
40             ];
41         }
42
59b068 43     }
D 44
bdead4 45     public function getResponses(Request $request, $selectedUser, $picasso = false): string|array
9f6455 46     {
DC 47             $uriParts = explode("?", $request->url());
48             $methodUri = Str::afterLast($uriParts[0], "/");
49             $reqParams = !empty($request->query()) ? $request->query() : [];
50             if (($methodUri == 'isLogged') || ($methodUri == 'islogged')) {
51                 // controllo cookie e conseguente risposta isLogged
bdead4 52                 $loginResponse = ($picasso) ? $this->getLoggedPicasso($selectedUser) : $this->getLoggedLegacy($selectedUser);
9f6455 53                 return $loginResponse;
DC 54
55             } else if ($methodUri != "") {
56                 // controllo metodo e parametri e conseguente risposta
bdead4 57                 $userResponses = $this->getUserResponseGroup($selectedUser, $picasso);
9f6455 58                 if (isset($userResponses[$methodUri])) {
DC 59                     $responseContent = $this->getResponseArgumentsBased($userResponses, $methodUri, $reqParams);
60                     return $responseContent;
398fc7 61                 } else {
d5d253 62                     return $methodUri . " " . $selectedUser . " " . $this->getErrorMessage('no-method-on-user');
9f6455 63                 }
DC 64             } else {
bdead4 65                 return $this->getErrorMessage('no-method');
9f6455 66             }
DC 67     }
68
bdead4 69     public function getLoggedPicasso($selectedUser): string|array
9f6455 70     {
DC 71         if (isset($_COOKIE["SSOSESSIONID"])) {
bdead4 72             if (!isset($this->xmlResponses[$selectedUser]["picasso"])) {
D 73                 return $this->getErrorMessage('no-picasso-user');
398fc7 74             }
bdead4 75             $sampleResponseString = Arr::first($this->xmlResponses[$selectedUser]["picasso"]["getWebcustomerInformation"]["parametri"]["k"] );
9f6455 76             $sampleResponseObject = Utils::convertXMLStrToArray($sampleResponseString);
DC 77             $resp = [
78                 'logged' => (string) 1,
79                 'errorCode' => (string) 0,
80                 'username' => (string) $sampleResponseObject["Username"],
81                 'next_user' => (string) 'N',
d5d253 82                 'token' => (string) 'xno:' . $selectedUser,
9f6455 83             ];
DC 84         } else {
85             $resp = [
86                 'logged' => (string) 0,
87                 'errorCode' => (string) 0,
88             ];
89         }
90         $xml = Utils::convertToXML($resp, $root = '<isLogged/>');
91         return $xml;
92     }
93
bdead4 94     public function getLoggedLegacy($selectedUser): string|array
9f6455 95     {
DC 96         if (isset($_COOKIE["CAuthCookie"])) {
bdead4 97             if (!isset($this->xmlResponses[$selectedUser]["sso"])) {
D 98                 return $this->getErrorMessage('no-sso-user');
398fc7 99             }
bdead4 100             $sampleResponseString = Arr::first($this->xmlResponses[$selectedUser]["sso"]["getWebcustomerInformation"]["parametri"]["k"] );
9f6455 101             $sampleResponseObject = Utils::convertXMLStrToArray($sampleResponseString);
DC 102             $resp = [
103                 'logged' => (string) 1,
104                 'errorCode' => (string) 0,
105                 'username' => (string) $sampleResponseObject["Username"],
71688b 106                 'token' => (string) 'xno:' . $selectedUser,
9f6455 107                 'next_user' => (string) 'N',
DC 108             ];
109         } else {
110             $resp = [
111                 'logged' => (string) 0,
112                 'errorCode' => (string) 0,
113             ];
114         }
115         $xml = Utils::convertToXML($resp, $root = '<isLogged/>');
116         return $xml;
117     }
118
59b068 119     public function getResponseArgumentsBased($userMethods, $methodUri, $reqParams = []): string
9f6455 120     {
DC 121         $method = $userMethods[$methodUri];
122         $params = $method["parametri"];
123
124         //assumo che se la richiesta sia arrivata fin qui abbia tutti i mandatory nel url
125         foreach ($reqParams as $key => $value) {
126             //verifico se il metodo dell' utente ha la chiave che sto cercando
a4fbcb 127             if ($key != "k" && isset($params[$key])) {
9f6455 128                 if (array_key_exists($value, $params[$key])) {
398fc7 129                     \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));
9f6455 130                     return $params[$key][$value];
DC 131                 }
132             }
133         }
134         //xml di default (se richiede parametri di default conterrĂ  errori)
a4fbcb 135         $defaultResponse = Arr::first($params["k"]);
398fc7 136         \Log::channel('requests')->debug("[VolaFakeHTTPResponder] Inserisco xml di default $defaultResponse per method: $methodUri");
9f6455 137         return $defaultResponse;
DC 138     }
139
140     /**
141      * @param Request $request
bdead4 142      * @param mixed $selectedUser
398fc7 143      * @param bool $picasso
9f6455 144      * @return array|mixed
DC 145      */
bdead4 146     private function getUserResponseGroup(mixed $selectedUser, bool $picasso): array
9f6455 147     {
398fc7 148         if ($picasso) {
bdead4 149             $userMethods = $this->xmlResponses[$selectedUser]["picasso"] ?? [];
9f6455 150         } else {
bdead4 151             $userMethods = $this->xmlResponses[$selectedUser]["sso"] ?? [];
9f6455 152         }
DC 153         return $userMethods;
154     }
155
bdead4 156     private function getErrorMessage(string $code): string
D 157     {
158         switch ($code) {
fcb093 159             case 'no-cookie':
D 160                 return 'No cookie No Party. Send your CAuthCookie or SESSIONID cookie';
bdead4 161             case 'no-sso-user':
fcb093 162                 return 'This sso user doesnt exist. Check your CAuthCookie cookie value';
bdead4 163             case 'no-picasso-user':
fcb093 164                 return 'This picasso user doesnt exist. Check your SSOSESSIONID coookie value';
bdead4 165             case 'no-method-on-user':
fcb093 166                 return 'This method response is not set for this user. Check this server database.';
bdead4 167             case 'no-method':
fcb093 168                 return 'Couldnt detect requested method. Check your request url.';
bdead4 169             default:
fcb093 170                 return 'Couldnt detect proper response.';
bdead4 171         }
D 172     }
173
9f6455 174 }