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