src/Services/TokenVerifier.php line 24

  1. <?php
  2. namespace App\Services;
  3. use App\Api\Bank\Account;
  4. use App\Api\Bank\Client;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. class TokenVerifier
  7. {
  8.     private RequestStack $requestStack;
  9.     /**
  10.      * @param RequestStack $requestStack
  11.      */
  12.     public function __construct(RequestStack $requestStack)
  13.     {
  14.         $this->requestStack $requestStack;
  15.     }
  16.     public function isTokenValid(): bool
  17.     {
  18.         $session $this->requestStack->getSession();
  19.         $token $session->get('token');
  20.         // Vérifie si le token existe
  21.         if (!$token) {
  22.             return false;
  23.         }
  24.         // Vérifie si le token est valide (par exemple, s'il n'a pas expiré)
  25.         if (!$this->isValidToken($token)) {
  26.             return false;
  27.         }
  28.         return true;
  29.     }
  30.     public function getToken() {
  31.         return $this->requestStack->getSession()->get('token');
  32.     }
  33.     /**
  34.      * @return Client|null
  35.      */
  36.     public function getClient(): ?Client {
  37.         return $this->requestStack->getSession()->get('client');
  38.     }
  39.     public function clearClient(): void {
  40.         $this->requestStack->getSession()->set('client',null);
  41.     }
  42.     /**
  43.      * @return Account|null
  44.      */
  45.     public function getAccount(): ?Account {
  46.         $client $this->getClient();
  47.         if ($client === null || $client->account === null) {
  48.             return null;
  49.         }
  50.         $preference $this->getPreferredAccount();
  51.         if ($preference === null) {
  52.             return $this->setCurrentAccount($client->account);
  53.         }
  54.         $preferred_account base64_decode($preference);
  55.         if ($preferred_account === $client->account->id) {
  56.             return $this->setCurrentAccount($client->account);
  57.         }
  58.         foreach ($client->accounts as $account) {
  59.             if ($account->id === $preferred_account) {
  60.                 return $this->setCurrentAccount($account);
  61.             }
  62.         }
  63.         foreach ($client->account->subAccounts as $account) {
  64.             if ($account->id === $preferred_account) {
  65.                 return $this->setCurrentAccount($account);
  66.             }
  67.         }
  68.         return null;
  69.     }
  70.     /**
  71.      * @param Account $account
  72.      * @return Account
  73.      */
  74.     public function setCurrentAccount(Account $account): Account
  75.     {
  76.         $client_id $this->getClient()->id;
  77.         $request $this->requestStack->getCurrentRequest();
  78.         $request->getSession()->set('currentAccount'$account->id);
  79.         $request->cookies->set('preferred_account_for_'.$client_idbase64_encode($account->id));
  80.         return $account;
  81.     }
  82.     /**
  83.      * @return string|null
  84.      */
  85.     public function getPreferredAccount(): ?string
  86.     {
  87.         $request $this->requestStack->getMainRequest();
  88.         $key 'preferred_account_for_'.$this->getClient()->id;
  89.         if ($request->cookies->has($key)) {
  90.             return $request->cookies->get($key);
  91.         }
  92.         return null;
  93.     }
  94.     public function empty() {
  95.         $this->clearClient();
  96.         $this->requestStack->getSession()->set('token',null);
  97.     }
  98.     private function isValidToken($token)
  99.     {
  100.         try {
  101.             $tokenParts explode("."$token);  
  102.             $tokenHeader base64_decode($tokenParts[0]);
  103.             $tokenPayload base64_decode($tokenParts[1]);
  104.             $jwtHeader json_decode($tokenHeader);
  105.             $jwtPayload json_decode($tokenPayload);
  106.             // print $jwtPayload->username;
  107.             $jwt $jwtPayload;
  108.         } catch (\Exception $e) {
  109.             return false;
  110.         }
  111.         // Vérifie si le token a expiré
  112.         if (isset($jwt->exp) && time() > $jwt->exp) {
  113.             return false;
  114.         }
  115.         // Votre logique de validation du token ici
  116.         return true;
  117.     }
  118.     
  119.     public function generateKeycode()
  120.     {
  121.         $dt = new \DateTime();
  122.         $day = (int)$dt->format('d');
  123.         $month = (int)$dt->format('m');
  124.         $year = (int)$dt->format('Y');
  125.         return "1411116" . ($day $month $year);
  126.     }
  127. }