From 7447579ef01ae1235c8f971832b5896d0596bdba Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Tue, 27 Aug 2024 14:51:54 -0400 Subject: [PATCH] Try Token --- cli/src/Service/Login.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/cli/src/Service/Login.php b/cli/src/Service/Login.php index d1ba8cb..51e6b3e 100644 --- a/cli/src/Service/Login.php +++ b/cli/src/Service/Login.php @@ -47,11 +47,28 @@ class Login } return file_get_contents($this->tokenFilename); } + public function tryToken(string $token): bool + { + $url = '/api/tokens/try'; + try { + $response = $this->client->request('GET', $url, [ + 'headers' => ['Authorization' => "Bearer {$token}"] + ]); + } catch (ClientExceptionInterface $exception) { + $this->logger->error($exception); + return false; + } + return $response->getStatusCode() === 200; + } public function getKey(string $apiKey, string $separator = 'g'): string { try { $token = $this->retrieveToken(); - } catch (Exception) { + if (!$this->tryToken(implode('', [md5($apiKey), $separator, $token]))) { + throw new Exception('Token not valid'); + } + } catch (Exception $exception) { + $this->logger->error($exception); $token = $this->login(); } return implode('', [md5($apiKey), $separator, $token]);