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