From 07094dc79f741d94fbab6289be48aa47557c3cb9 Mon Sep 17 00:00:00 2001 From: Achmad Hadi Kurnia Date: Sun, 7 Jul 2024 22:50:25 +0700 Subject: [PATCH] refactor(improvement): relocate throw exception --- src/Commands/GenerateTokenCommand.php | 2 +- src/Contracts/Tokenize.php | 4 +--- src/Credentials/Apim.php | 14 +++++++++++--- src/Credentials/Sso.php | 14 +++++++++++--- src/Credentials/Token.php | 18 ++---------------- src/Exceptions/InvalidTokenException.php | 10 ++++++++++ tests/GenerateTokenTest.php | 20 ++++++++++++++++---- 7 files changed, 52 insertions(+), 30 deletions(-) create mode 100644 src/Exceptions/InvalidTokenException.php diff --git a/src/Commands/GenerateTokenCommand.php b/src/Commands/GenerateTokenCommand.php index c098122..12c9c41 100644 --- a/src/Commands/GenerateTokenCommand.php +++ b/src/Commands/GenerateTokenCommand.php @@ -23,8 +23,8 @@ public function handle(): int } $this->info(json_encode([ - 'sso' => $ssoToken, 'apim' => $apimToken, + 'sso' => $ssoToken, ], JSON_PRETTY_PRINT)); return self::SUCCESS; diff --git a/src/Contracts/Tokenize.php b/src/Contracts/Tokenize.php index 4645895..d5413ae 100644 --- a/src/Contracts/Tokenize.php +++ b/src/Contracts/Tokenize.php @@ -2,9 +2,7 @@ namespace Kanekescom\Siasn\Api\Contracts; -use Illuminate\Http\Client\Response; - interface Tokenize { - public static function getToken(): Response; + public static function getToken(): object; } diff --git a/src/Credentials/Apim.php b/src/Credentials/Apim.php index 7f8b81f..fcfca4e 100644 --- a/src/Credentials/Apim.php +++ b/src/Credentials/Apim.php @@ -3,10 +3,10 @@ namespace Kanekescom\Siasn\Api\Credentials; use Illuminate\Http\Client\ConnectionException; -use Illuminate\Http\Client\Response; use Illuminate\Support\Facades\Http; use Kanekescom\Siasn\Api\Contracts\Tokenize; use Kanekescom\Siasn\Api\Exceptions\InvalidApimCredentialsException; +use Kanekescom\Siasn\Api\Exceptions\InvalidTokenException; use Kanekescom\Siasn\Api\Helpers\Config; class Apim implements Tokenize @@ -14,7 +14,7 @@ class Apim implements Tokenize /** * @throws InvalidApimCredentialsException|ConnectionException */ - public static function getToken(): Response + public static function getToken(): object { $credential = Config::getApimCredential(); @@ -26,7 +26,7 @@ public static function getToken(): Response throw new InvalidApimCredentialsException('password must be set'); } - return Http::timeout(config('siasn-api.request_timeout')) + $response = Http::timeout(config('siasn-api.request_timeout')) ->retry(config('siasn-api.max_request_attempts'), config('siasn-api.max_request_wait_attempts')) ->withOptions([ 'debug' => Config::getDebug(), @@ -37,5 +37,13 @@ public static function getToken(): Response )->post($credential->url, [ 'grant_type' => $credential->grant_type, ]); + + $token = $response->object(); + + if (blank(optional($token)->access_token)) { + throw new InvalidTokenException('Not receiving tokens correctly.'); + } + + return $token; } } diff --git a/src/Credentials/Sso.php b/src/Credentials/Sso.php index b5500e5..ef55d43 100644 --- a/src/Credentials/Sso.php +++ b/src/Credentials/Sso.php @@ -3,10 +3,10 @@ namespace Kanekescom\Siasn\Api\Credentials; use Illuminate\Http\Client\ConnectionException; -use Illuminate\Http\Client\Response; use Illuminate\Support\Facades\Http; use Kanekescom\Siasn\Api\Contracts\Tokenize; use Kanekescom\Siasn\Api\Exceptions\InvalidSsoCredentialsException; +use Kanekescom\Siasn\Api\Exceptions\InvalidTokenException; use Kanekescom\Siasn\Api\Helpers\Config; class Sso implements Tokenize @@ -14,7 +14,7 @@ class Sso implements Tokenize /** * @throws InvalidSsoCredentialsException|ConnectionException */ - public static function getToken(): Response + public static function getToken(): object { $credential = Config::getSsoCredential(); @@ -30,7 +30,7 @@ public static function getToken(): Response throw new InvalidSsoCredentialsException('password must be set'); } - return Http::timeout(config('siasn-api.request_timeout')) + $response = Http::timeout(config('siasn-api.request_timeout')) ->asForm() ->retry(config('siasn-api.max_request_attempts'), config('siasn-api.max_request_wait_attempts')) ->withOptions([ @@ -43,5 +43,13 @@ public static function getToken(): Response 'username' => $credential->username, 'password' => $credential->password, ]); + + $token = $response->object(); + + if (blank(optional($token)->token_type) || blank(optional($token)->access_token)) { + throw new InvalidTokenException('Not receiving tokens correctly.'); + } + + return $token; } } diff --git a/src/Credentials/Token.php b/src/Credentials/Token.php index bf3e038..1c2cdc0 100644 --- a/src/Credentials/Token.php +++ b/src/Credentials/Token.php @@ -2,8 +2,6 @@ namespace Kanekescom\Siasn\Api\Credentials; -use Kanekescom\Siasn\Api\Exceptions\InvalidApimCredentialsException; -use Kanekescom\Siasn\Api\Exceptions\InvalidSsoCredentialsException; use Kanekescom\Siasn\Api\Helpers\Config; class Token @@ -11,26 +9,14 @@ class Token public static function getApimToken(): object { return cache()->remember('apim-token', Config::getApimTokenAge(), function () { - $apimToken = Apim::getToken()->object(); - - if (blank(optional($apimToken)->access_token)) { - throw new InvalidApimCredentialsException('Invalid Apim user credentials.'); - } - - return $apimToken; + return Apim::getToken(); }); } public static function getSsoToken(): object { return cache()->remember('sso-token', Config::getSsoTokenAge(), function () { - $ssoToken = Sso::getToken()->object(); - - if (blank(optional($ssoToken)->token_type) || blank(optional($ssoToken)->access_token)) { - throw new InvalidSsoCredentialsException('Invalid SSO user credentials.'); - } - - return $ssoToken; + return Sso::getToken(); }); } diff --git a/src/Exceptions/InvalidTokenException.php b/src/Exceptions/InvalidTokenException.php new file mode 100644 index 0000000..ad4c73e --- /dev/null +++ b/src/Exceptions/InvalidTokenException.php @@ -0,0 +1,10 @@ +object(); + $apimToken = Apim::getToken(); expect($apimToken)->toHaveProperty('access_token'); }); it('can generate sso token', function () { - $ssoToken = Sso::getToken()->object(); + $ssoToken = Sso::getToken(); expect($ssoToken)->toHaveProperty('access_token'); }); @@ -22,12 +22,24 @@ expect($apimToken)->toHaveProperty('access_token'); }); +it('can clear cache and generate apim token cache first', function () { + $apimToken = Token::getNewApimToken(); + + expect($apimToken)->toHaveProperty('access_token'); +}); + it('can generate sso token cache first', function () { $ssoToken = Token::getSsoToken(); expect($ssoToken)->toHaveProperty('access_token'); }); +it('can clear cache and generate sso token cache first', function () { + $ssoToken = Token::getNewSsoToken(); + + expect($ssoToken)->toHaveProperty('access_token'); +}); + it('can generate apim token same on cache', function () { $apimToken = Token::getApimToken(); $cachedApimToken = cache('apim-token'); @@ -43,14 +55,14 @@ }); it('can generate apim token not same on cache', function () { - $apimTokenObject = Apim::getToken()->object(); + $apimTokenObject = Apim::getToken(); $apimToken = Token::getApimToken(); expect($apimTokenObject)->not()->toBe($apimToken); }); it('can generate sso token not same on cache', function () { - $ssoTokenObject = Sso::getToken()->object(); + $ssoTokenObject = Sso::getToken(); $ssoToken = Token::getSsoToken(); expect($ssoTokenObject)->not()->toBe($ssoToken);