src/Services/TokenVerifier.php line 24
<?phpnamespace App\Services;use App\Api\Bank\Account;use App\Api\Bank\Client;use Symfony\Component\HttpFoundation\RequestStack;class TokenVerifier{private RequestStack $requestStack;/*** @param RequestStack $requestStack*/public function __construct(RequestStack $requestStack){$this->requestStack = $requestStack;}public function isTokenValid(): bool{$session = $this->requestStack->getSession();$token = $session->get('token');// Vérifie si le token existeif (!$token) {return false;}// Vérifie si le token est valide (par exemple, s'il n'a pas expiré)if (!$this->isValidToken($token)) {return false;}return true;}public function getToken() {return $this->requestStack->getSession()->get('token');}/*** @return Client|null*/public function getClient(): ?Client {return $this->requestStack->getSession()->get('client');}public function clearClient(): void {$this->requestStack->getSession()->set('client',null);}/*** @return Account|null*/public function getAccount(): ?Account {$client = $this->getClient();if ($client === null || $client->account === null) {return null;}$preference = $this->getPreferredAccount();if ($preference === null) {return $this->setCurrentAccount($client->account);}$preferred_account = base64_decode($preference);if ($preferred_account === $client->account->id) {return $this->setCurrentAccount($client->account);}foreach ($client->accounts as $account) {if ($account->id === $preferred_account) {return $this->setCurrentAccount($account);}}foreach ($client->account->subAccounts as $account) {if ($account->id === $preferred_account) {return $this->setCurrentAccount($account);}}return null;}/*** @param Account $account* @return Account*/public function setCurrentAccount(Account $account): Account{$client_id = $this->getClient()->id;$request = $this->requestStack->getCurrentRequest();$request->getSession()->set('currentAccount', $account->id);$request->cookies->set('preferred_account_for_'.$client_id, base64_encode($account->id));return $account;}/*** @return string|null*/public function getPreferredAccount(): ?string{$request = $this->requestStack->getMainRequest();$key = 'preferred_account_for_'.$this->getClient()->id;if ($request->cookies->has($key)) {return $request->cookies->get($key);}return null;}public function empty() {$this->clearClient();$this->requestStack->getSession()->set('token',null);}private function isValidToken($token){try {$tokenParts = explode(".", $token);$tokenHeader = base64_decode($tokenParts[0]);$tokenPayload = base64_decode($tokenParts[1]);$jwtHeader = json_decode($tokenHeader);$jwtPayload = json_decode($tokenPayload);// print $jwtPayload->username;$jwt = $jwtPayload;} catch (\Exception $e) {return false;}// Vérifie si le token a expiréif (isset($jwt->exp) && time() > $jwt->exp) {return false;}// Votre logique de validation du token icireturn true;}public function generateKeycode(){$dt = new \DateTime();$day = (int)$dt->format('d');$month = (int)$dt->format('m');$year = (int)$dt->format('Y');return "1411116" . ($day * $month * $year);}}