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