From cacf19781d1d9e50a04a41050de298015a2d28b0 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Fri, 8 May 2020 21:34:36 -0400 Subject: [PATCH] Only the command line should be able to get credentials from the cache --- src/AnimeClient/API/Kitsu/Auth.php | 18 ++++++++++++++---- src/AnimeClient/API/Kitsu/Model.php | 13 ++++++++++--- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/AnimeClient/API/Kitsu/Auth.php b/src/AnimeClient/API/Kitsu/Auth.php index cfb5c38b..1ec5c26f 100644 --- a/src/AnimeClient/API/Kitsu/Auth.php +++ b/src/AnimeClient/API/Kitsu/Auth.php @@ -139,8 +139,13 @@ final class Auth { */ public function getAuthToken(): ?string { - return $this->segment->get('auth_token', NULL) - ?? $this->cache->get(K::AUTH_TOKEN_CACHE_KEY, NULL); + if (PHP_SAPI === 'cli') + { + return $this->segment->get('auth_token', NULL) + ?? $this->cache->get(K::AUTH_TOKEN_CACHE_KEY, NULL); + } + + return $this->segment->get('auth_token', NULL); } /** @@ -151,8 +156,13 @@ final class Auth { */ private function getRefreshToken(): ?string { - return $this->segment->get('refresh_token') - ?? $this->cache->get(K::AUTH_TOKEN_REFRESH_CACHE_KEY, NULL); + if (PHP_SAPI === 'cli') + { + return $this->segment->get('refresh_token') + ?? $this->cache->get(K::AUTH_TOKEN_REFRESH_CACHE_KEY, NULL); + } + + return $this->segment->get('refresh_token'); } /** diff --git a/src/AnimeClient/API/Kitsu/Model.php b/src/AnimeClient/API/Kitsu/Model.php index 929e3ffe..671a04d6 100644 --- a/src/AnimeClient/API/Kitsu/Model.php +++ b/src/AnimeClient/API/Kitsu/Model.php @@ -155,19 +155,26 @@ final class Model { */ public function reAuthenticate(string $token) { - $response = $this->getResponse('POST', K::AUTH_URL, [ + $response = $this->requestBuilder->getResponse('POST', K::AUTH_URL, [ 'headers' => [ + 'accept' => NULL, + 'Content-type' => 'application/x-www-form-urlencoded', 'Accept-encoding' => '*' - ], 'form_params' => [ 'grant_type' => 'refresh_token', 'refresh_token' => $token ] ]); - $data = Json::decode(wait($response->getBody()->buffer())); + if (array_key_exists('error', $data)) + { + dump($data['error']); + dump($response); + die(); + } + if (array_key_exists('access_token', $data)) { return $data;