Skip to content

Commit

Permalink
fix: fixed mostly stuff on external token callback (#740)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyrch authored Aug 31, 2024
1 parent a27a463 commit 4013c91
Show file tree
Hide file tree
Showing 25 changed files with 512 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
namespace App\Actions\Models\List\ExternalProfile\ExternalEntry;

use App\Models\List\External\ExternalToken;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Crypt;

/**
* Class BaseExternalEntryTokenAction
Expand Down Expand Up @@ -43,7 +41,7 @@ public function getId(): ?int
*/
public function getToken(): string
{
return $this->token->access_token;
return Crypt::decrypt($this->token->access_token);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@
use App\Models\List\External\ExternalEntry;
use App\Models\Wiki\ExternalResource;
use Exception;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

Expand Down Expand Up @@ -79,13 +76,12 @@ public function getId(): ?int
return $this->id;
}

// TODO: This should be tested.
try {
$decoded = JWT::decode($this->getToken(), new Key(Config::get('services.anilist.client_secret'), 'HS256'));
[, $payload] = explode('.', $this->getToken());

$decodedArray = json_decode(json_encode($decoded), true);
$decodedArray = json_decode(base64_decode($payload), true);

$this->id = Arr::get($decodedArray, 'id');
$this->id = intval(Arr::get($decodedArray, 'sub'));

return $this->id;
} catch (Exception $e) {
Expand Down Expand Up @@ -140,7 +136,6 @@ protected function makeRequest(): static
->json();

return $this;

} catch (RequestException $e) {
Log::error($e->getMessage());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

use App\Actions\Models\List\ExternalProfile\ExternalToken\BaseExternalTokenAction;
use App\Models\List\External\ExternalToken;
use Illuminate\Http\Client\RequestException;
use Error;
use Exception;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

Expand All @@ -22,6 +24,8 @@ class AnilistExternalTokenAction extends BaseExternalTokenAction
*
* @param string $code
* @return ExternalToken|null
*
* @throws Exception
*/
public function store(string $code): ?ExternalToken
{
Expand All @@ -40,18 +44,17 @@ public function store(string $code): ?ExternalToken

$token = Arr::get($response, 'access_token');

if ($token !== null) {
return ExternalToken::query()->create([
ExternalToken::ATTRIBUTE_ACCESS_TOKEN => $token,
]);
if ($token === null) {
return null;
}

return null;

} catch (RequestException $e) {
return ExternalToken::query()->create([
ExternalToken::ATTRIBUTE_ACCESS_TOKEN => Crypt::encrypt($token),
]);
} catch (Exception $e) {
Log::error($e->getMessage());

throw $e;
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Exception;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

/**
Expand All @@ -32,21 +31,19 @@ class StoreExternalProfileTokenAction extends BaseStoreExternalProfileAction
*
* @param ExternalToken $token
* @param array $parameters
* @return ExternalProfile
* @return ExternalProfile|null
*
* @throws Exception
*/
public function findOrCreate(ExternalToken $token, array $parameters): ExternalProfile
public function findOrCreate(ExternalToken $token, array $parameters): ?ExternalProfile
{
try {
DB::beginTransaction();

$site = ExternalProfileSite::fromLocalizedName(Arr::get($parameters, 'site'));

$action = $this->getActionClass($site, $token);

if ($action === null) {
throw new Error("Undefined action for site {$site->localize()}"); // TODO: check if it is working
return null;
}

$userId = $action->getId();
Expand Down Expand Up @@ -77,16 +74,12 @@ public function findOrCreate(ExternalToken $token, array $parameters): ExternalP

ExternalEntry::insert($externalEntries);

DB::commit();

return $profile;

} catch (Exception $e) {
Log::error($e->getMessage());

DB::rollBack();

throw $e;
return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ class StoreExternalProfileUsernameAction extends BaseStoreExternalProfileAction
*
* @param Builder $builder
* @param array $profileParameters
* @return ExternalProfile
* @return ExternalProfile|null
*
* @throws Exception
*/
public function findOrCreate(Builder $builder, array $profileParameters): ExternalProfile
public function findOrCreate(Builder $builder, array $profileParameters): ?ExternalProfile
{
try {
$profileSite = ExternalProfileSite::fromLocalizedName(Arr::get($profileParameters, 'site'));
Expand All @@ -52,7 +52,7 @@ public function findOrCreate(Builder $builder, array $profileParameters): Extern
$action = $this->getActionClass($profileSite, $profileParameters);

if ($action === null) {
throw new Error("Undefined action for site {$profileSite->localize()}"); // TODO: check if it is working
return null;
}

$entries = $action->getEntries();
Expand Down Expand Up @@ -94,7 +94,7 @@ public function findOrCreate(Builder $builder, array $profileParameters): Extern

DB::rollBack();

throw $e;
return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use App\Actions\Models\List\ExternalProfile\ExternalToken\Site\AnilistExternalTokenAction;
use App\Enums\Models\List\ExternalProfileSite;
use App\Models\List\External\ExternalToken;
use Error;
use Exception;
use Illuminate\Support\Arr;

Expand All @@ -33,7 +32,7 @@ public function store(array $query): ?ExternalToken
$action = $this->getActionClass($profileSite);

if ($action === null) {
throw new Error("Undefined callback URL for site {$site}");
return null;
}

$externalToken = $action->store(Arr::get($query, 'code'));
Expand Down
59 changes: 59 additions & 0 deletions app/Actions/Models/List/ExternalTokenCallbackAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace App\Actions\Models\List;

use App\Actions\Models\List\ExternalProfile\StoreExternalProfileTokenAction;
use App\Actions\Models\List\ExternalProfile\StoreExternalTokenAction;
use App\Models\List\ExternalProfile;
use Error;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

/**
* Class ExternalTokenCallbackAction.
*/
class ExternalTokenCallbackAction
{
/**
* We should store the token, the profile and its entries.
*
* @param array $parameters
* @return JsonResponse|ExternalProfile
*
* @throws Exception
*/
public function store(array $parameters): JsonResponse|ExternalProfile
{
try {
DB::beginTransaction();

$action = new StoreExternalTokenAction();

$externalToken = $action->store($parameters);

if ($externalToken === null) {
throw new Error('Invalid Code', 400);
}

$profileAction = new StoreExternalProfileTokenAction();

$profile = $profileAction->findOrCreate($externalToken, $parameters);

DB::commit();

return $profile;
} catch (Exception $e) {
Log::error($e->getMessage());

DB::rollBack();

return new JsonResponse([
'error' => $e->getMessage(),
], $e->getCode());
}
}
}
1 change: 0 additions & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use App\Console\Commands\Storage\Admin\DumpPruneCommand;
use App\Console\Commands\Storage\Admin\WikiDumpCommand;
use App\Models\BaseModel;
use App\Models\List\ExternalProfile;
use BezhanSalleh\FilamentExceptions\Models\Exception;
use Illuminate\Auth\Console\ClearResetsCommand;
use Illuminate\Cache\Console\PruneStaleTagsCommand;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@
*/
class ExternalTokenCreated extends ListCreatedEvent
{
/**
* The profile the token belongs to.
*
* @var ExternalProfile
*/
protected ExternalProfile $profile;

/**
* Create a new event instance.
*
Expand All @@ -30,7 +23,6 @@ class ExternalTokenCreated extends ListCreatedEvent
public function __construct(ExternalToken $token)
{
parent::__construct($token);
$this->profile = $token->externalprofile;
}

/**
Expand Down Expand Up @@ -62,6 +54,6 @@ public function getModel(): ExternalToken
*/
protected function getDiscordMessageDescription(): string
{
return "Token '**{$this->getModel()->getName()}**' has been created for External Profile '**{$this->profile->getName()}**'.";
return "Token '**{$this->getModel()->getName()}**' has been created.";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@
*/
class ExternalTokenDeleted extends ListDeletedEvent
{
/**
* The profile the token belongs to.
*
* @var ExternalProfile
*/
protected ExternalProfile $profile;

/**
* Create a new event instance.
*
Expand All @@ -30,7 +23,6 @@ class ExternalTokenDeleted extends ListDeletedEvent
public function __construct(ExternalToken $token)
{
parent::__construct($token);
$this->profile = $token->externalprofile;
}

/**
Expand Down Expand Up @@ -62,6 +54,6 @@ public function getModel(): ExternalToken
*/
protected function getDiscordMessageDescription(): string
{
return "Token '**{$this->getModel()->getName()}**' has been deleted for External Profile '**{$this->profile->getName()}**'.";
return "Token '**{$this->getModel()->getName()}**' has been deleted.";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@
*/
class ExternalTokenRestored extends ListRestoredEvent
{
/**
* The profile the token belongs to.
*
* @var ExternalProfile
*/
protected ExternalProfile $profile;

/**
* Create a new event instance.
*
Expand All @@ -30,7 +23,6 @@ class ExternalTokenRestored extends ListRestoredEvent
public function __construct(ExternalToken $token)
{
parent::__construct($token);
$this->profile = $token->externalprofile;
}

/**
Expand Down Expand Up @@ -62,6 +54,6 @@ public function getModel(): ExternalToken
*/
protected function getDiscordMessageDescription(): string
{
return "Token '**{$this->getModel()->getName()}**' has been restored for External Profile '**{$this->profile->getName()}**'.";
return "Token '**{$this->getModel()->getName()}**' has been restored.";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@
*/
class ExternalTokenUpdated extends ListUpdatedEvent
{
/**
* The profile the token belongs to.
*
* @var ExternalProfile
*/
protected ExternalProfile $profile;

/**
* Create a new event instance.
*
Expand All @@ -30,7 +23,6 @@ class ExternalTokenUpdated extends ListUpdatedEvent
public function __construct(ExternalToken $token)
{
parent::__construct($token);
$this->profile = $token->externalprofile;
$this->initializeEmbedFields($token);
}

Expand Down Expand Up @@ -63,6 +55,6 @@ public function getModel(): ExternalToken
*/
protected function getDiscordMessageDescription(): string
{
return "Token '**{$this->getModel()->getName()}**' has been updated for External Profile '**{$this->profile->getName()}**'.";
return "Token '**{$this->getModel()->getName()}**' has been updated.";
}
}
Loading

0 comments on commit 4013c91

Please sign in to comment.