Try Token
This commit is contained in:
@ -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]);
|
||||
|
Reference in New Issue
Block a user