Skip to content

Commit

Permalink
refactor: extract token interaction logic into trait
Browse files Browse the repository at this point in the history
  • Loading branch information
recursivetree committed Aug 12, 2024
1 parent 7071540 commit 92f8408
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 54 deletions.
52 changes: 52 additions & 0 deletions src/InteractsWithToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Seat\Eveapi;

use Seat\Eveapi\Models\RefreshToken;
use Seat\Services\Contracts\EsiClient;

trait InteractsWithToken
{
abstract protected function getClient(): EsiClient;
abstract protected function getToken(): ?RefreshToken;


/**
* @return void
*/
protected function configureTokenForEsiClient(): void
{
$token = $this->getToken();
if($token !== null) {
$this->getClient()->setAuthentication($token);
}
}

/**
* Update the access_token last used in the job,
* along with the expiry time.
*
* @return void
*/
public function updateRefreshToken(): void
{
$client = $this->getClient();
$token = $this->getToken();

// If it is an unauthenticated call, there is nothing to update
if (is_null($token))
return;

if (! $client->isAuthenticated())
return;

$last_auth = $client->getAuthentication();

// update the token
if (! empty($last_auth->getRefreshToken()))
$token->refresh_token = $last_auth->getRefreshToken();
$token->token = $last_auth->getAccessToken() ?? '-';
$token->expires_on = $last_auth->getExpiresOn();
$token->save();
}
}
45 changes: 10 additions & 35 deletions src/Jobs/EsiBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Seat\Eveapi\Exception\PermanentInvalidTokenException;
use Seat\Eveapi\Exception\TemporaryEsiOutageException;
use Seat\Eveapi\Exception\UnavailableEveServersException;
use Seat\Eveapi\InteractsWithToken;
use Seat\Eveapi\Jobs\Middleware\CheckEsiRateLimit;
use Seat\Eveapi\Jobs\Middleware\CheckEsiRouteStatus;
use Seat\Eveapi\Jobs\Middleware\CheckServerStatus;
Expand All @@ -46,6 +47,8 @@
*/
abstract class EsiBase extends AbstractJob
{
use InteractsWithToken;

/**
* ANTI_RACE_DELAY prevents rapid job recycling with low queue depths.
*/
Expand Down Expand Up @@ -212,6 +215,11 @@ public function getToken(): ?RefreshToken
return $this->token;
}

protected function getClient(): EsiClient
{
return $this->esi;
}

/**
* @return string
*/
Expand Down Expand Up @@ -299,17 +307,13 @@ public function retrieve(array $path_values = []): EsiResponse
if (! is_null($this->page))
$this->esi->page($this->page);

$this->configureTokenForEsiClient();

// Generally, we want to bubble up exceptions all the way to the
// callee. However, in the case of this worker class, we need to
// try and be vigilant with tokens that may have expired. So for
// those cases we wrap in a try/catch.
try {
if ($this->token) {
$this->token = $this->token->fresh();

$this->esi->setAuthentication($this->token);
}

$result = $this->esi->invoke($this->method, $this->endpoint, $path_values);

// Update the refresh token we have stored in the database.
Expand Down Expand Up @@ -398,35 +402,6 @@ public function warning(EsiResponse $response): void
}
}

/**
* Update the access_token last used in the job,
* along with the expiry time.
*/
public function updateRefreshToken(): void
{

tap($this->token, function ($token) {

// If no API call was made, the client would have never
// been instantiated and auth information never updated.
if (is_null($token))
return;

if (! $this->esi->isAuthenticated())
return;

$last_auth = $this->esi->getAuthentication();

if (! empty($last_auth->getRefreshToken()))
$token->refresh_token = $last_auth->getRefreshToken();

$token->token = $last_auth->getAccessToken() ?? '-';
$token->expires_on = $last_auth->getExpiresOn();

$token->save();
});
}

/**
* Check if there are any pages left in a response
* based on the number of pages available and the
Expand Down
41 changes: 22 additions & 19 deletions src/Jobs/Token/RefreshAccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@
namespace Seat\Eveapi\Jobs\Token;

use Illuminate\Contracts\Container\BindingResolutionException;
use Seat\Eseye\Exceptions\InvalidAuthenticationException;
use Seat\Eseye\Exceptions\InvalidContainerDataException;
use Seat\Eseye\Exceptions\RequestFailedException;
use Seat\Eveapi\InteractsWithToken;
use Seat\Eveapi\Jobs\AbstractJob;
use Seat\Eveapi\Models\RefreshToken;
use Seat\Services\Contracts\EsiClient;

class RefreshAccessToken extends AbstractJob
{
use InteractsWithToken;

/**
* @var array
*/
Expand All @@ -41,7 +46,7 @@ class RefreshAccessToken extends AbstractJob
protected $token;

/**
* @var \Seat\Services\Contracts\EsiClient
* @var EsiClient
*/
protected EsiClient $esi;

Expand All @@ -58,29 +63,27 @@ public function __construct(RefreshToken $token)

/**
* @return void
* @throws InvalidContainerDataException
*/
public function handle()
public function handle(): void
{
// normally the retrieve function passes the token down the esi stack, but we don't use retrieve
$this->esi->setAuthentication($this->token);

try {
// get or renew access token
$this->esi->getValidAccessToken();
} catch (RequestFailedException $e) {
// pass this token to the esi client
$this->configureTokenForEsiClient();

}
// ensure we have a valid access token
$this->esi->getValidAccessToken();

// save the new access token. the following logic is extracted from EsiBase
$this->token = $this->token->fresh(); // since the model might have been in the queue for a while, amke sure to get the latest info
$last_auth = $this->esi->getAuthentication(); // extract the access token info from eseye

if (! empty($last_auth->getRefreshToken()))
$this->token->refresh_token = $last_auth->getRefreshToken();
// make sure the new token is stored
$this->updateRefreshToken();
}

$this->token->token = $last_auth->getAccessToken() ?? '-';
$this->token->expires_on = $last_auth->getExpiresOn();
function getClient(): EsiClient
{
return $this->esi;
}

$this->token->save();
function getToken(): ?RefreshToken
{
return $this->token;
}
}

0 comments on commit 92f8408

Please sign in to comment.