Skip to content

Commit

Permalink
Merge pull request #4 from achmadhadikurnia/main
Browse files Browse the repository at this point in the history
Fix Token bugs
  • Loading branch information
achmadhadikurnia authored Sep 18, 2023
2 parents 712500c + 2aaae07 commit 85ee601
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 15 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
"aliases": {
"Siasn": "Kanekescom\\Siasn\\Api\\Facades\\Siasn"
}
},
"branch-alias": {
"dev-main": "dev-main"
}
},
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/GetData.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Kanekescom\Siasn\Api\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
use Kanekescom\Siasn\Api\Facades\Siasn;

class GetData extends Command
{
Expand All @@ -27,6 +27,6 @@ class GetData extends Command
*/
public function handle()
{
$this->info(json_encode(Http::siasn()->get($this->argument('endpoint'))->object(), JSON_PRETTY_PRINT));
$this->info(json_encode(Siasn::get($this->argument('endpoint'))->object(), JSON_PRETTY_PRINT));
}
}
18 changes: 16 additions & 2 deletions src/Credentials/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Kanekescom\Siasn\Api\Credentials;

use Kanekescom\Siasn\Api\Exceptions\InvalidSsoCredentialsException;
use Kanekescom\Siasn\Api\Exceptions\InvalidWsCredentialsException;
use Kanekescom\Siasn\Api\SiasnConfig;

class Token
Expand All @@ -12,7 +14,13 @@ class Token
public static function getSsoToken()
{
return cache()->remember('sso-token', SiasnConfig::getSsoTokenAge(), function () {
return Sso::getToken()->object();
$ssoToken = Sso::getToken()->object();

if (blank(optional($ssoToken)->token_type) || blank(optional($ssoToken)->access_token)) {
throw new InvalidSsoCredentialsException('Invalid SSO user credentials.');
}

return $ssoToken;
});
}

Expand All @@ -22,7 +30,13 @@ public static function getSsoToken()
public static function getWsToken()
{
return cache()->remember('ws-token', SiasnConfig::getWsTokenAge(), function () {
return Ws::getToken()->object();
$wsToken = Ws::getToken()->object();

if (blank(optional($wsToken)->access_token)) {
throw new InvalidWsCredentialsException('Invalid WS user credentials.');
}

return $wsToken;
});
}
}
10 changes: 0 additions & 10 deletions src/Exceptions/InvalidCredentialsException.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Siasn.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class Siasn

public function __construct()
{
$wsToken = Token::getWsToken();
$ssoToken = Token::getSsoToken();
$wsToken = Token::getWsToken();

$this->request = Http::retry(3, 100)
->withOptions([
Expand Down
6 changes: 6 additions & 0 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,10 @@ public function can_forget_token()
$this->assertNull(cache('sso-token'));
$this->assertNull(cache('ws-token'));
}

/** @test */
public function can_get_data()
{
$this->artisan('siasn:get '.env('SIASN_GET_ENDPOINT_TEST'))->assertSuccessful();
}
}

0 comments on commit 85ee601

Please sign in to comment.