🔒 Persist Trakt authentication tokens to config JSON file

This commit is contained in:
Dan Jones 2025-11-17 00:03:08 -06:00
commit dd7196572c
4 changed files with 17 additions and 3 deletions

View file

@ -6,13 +6,14 @@ namespace App\Commands;
use App\Services\Trakt; use App\Services\Trakt;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Contracts\Config\Repository;
class TraktDebug extends Command class TraktDebug extends Command
{ {
protected $signature = 'trakt:debug {endpoint : Trakt API endpoint to call} {method=GET : HTTP method to use} {--d|data=* : data}'; protected $signature = 'trakt:debug {endpoint : Trakt API endpoint to call} {method=GET : HTTP method to use} {--d|data=* : data}';
protected $description = 'Make arbitrary requests to Trakt API.' . PHP_EOL . '-d should be formatted with key=value or key:=value for non-strings'; protected $description = 'Make arbitrary requests to Trakt API.' . PHP_EOL . '-d should be formatted with key=value or key:=value for non-strings';
public function handle(Trakt $trakt): int public function handle(Trakt $trakt, Repository $config): int
{ {
$body = $this->getBody(); $body = $this->getBody();
$method = $this->argument('method'); $method = $this->argument('method');
@ -28,6 +29,8 @@ class TraktDebug extends Command
$resp = $resp->send($method, $url); $resp = $resp->send($method, $url);
$this->line('Response (' . $resp->status() . '):' . PHP_EOL . json_encode($resp->json(), JSON_PRETTY_PRINT)); $this->line('Response (' . $resp->status() . '):' . PHP_EOL . json_encode($resp->json(), JSON_PRETTY_PRINT));
$this->line('Config: ' . json_encode($config->get('trakt')));
return static::SUCCESS; return static::SUCCESS;
} }

View file

@ -6,12 +6,14 @@ namespace App\Commands;
use App\Services\Trakt; use App\Services\Trakt;
use Illuminate\Contracts\Config\Repository; use Illuminate\Contracts\Config\Repository;
use Illuminate\Filesystem\Filesystem;
class TraktLogin extends Command class TraktLogin extends Command
{ {
protected $signature = 'trakt:login'; protected $signature = 'trakt:login';
protected $description = 'Login and get new token.'; protected $description = 'Login and get new token.';
public function handle(Trakt $trakt, Repository $config): int
public function handle(Trakt $trakt, Repository $config, Filesystem $files): int
{ {
$data = [ $data = [
'client_id' => $config->get('trakt.app_id'), 'client_id' => $config->get('trakt.app_id'),
@ -31,6 +33,7 @@ class TraktLogin extends Command
} }
$this->line("TRAKT_DEVICE_RESP='" . json_encode($body) . "'"); $this->line("TRAKT_DEVICE_RESP='" . json_encode($body) . "'");
$config->set('trakt.login.device', $body);
$data = [ $data = [
'client_id' => $config->get('trakt.app_id'), 'client_id' => $config->get('trakt.app_id'),
@ -48,6 +51,8 @@ class TraktLogin extends Command
$body = $resp->json(); $body = $resp->json();
$this->line("TRAKT_OAUTH_RESP='" . json_encode($body) . "'"); $this->line("TRAKT_OAUTH_RESP='" . json_encode($body) . "'");
$this->line('TRAKT_OAUTH_TOKEN=' . $body['access_token']); $this->line('TRAKT_OAUTH_TOKEN=' . $body['access_token']);
$config->set('trakt.login.oauth', $body);
$files->put($config->get('trakt.path'), json_encode($config->get('trakt'), JSON_PRETTY_PRINT));
return static::SUCCESS; return static::SUCCESS;

View file

@ -6,12 +6,14 @@ namespace App\Commands;
use App\Services\Trakt; use App\Services\Trakt;
use Illuminate\Contracts\Config\Repository; use Illuminate\Contracts\Config\Repository;
use Illuminate\Filesystem\Filesystem;
class TraktRefreshLogin extends Command class TraktRefreshLogin extends Command
{ {
protected $signature = 'trakt:refresh'; protected $signature = 'trakt:refresh';
protected $description = 'Refresh access token.'; protected $description = 'Refresh access token.';
public function handle(Trakt $trakt, Repository $config): int
public function handle(Trakt $trakt, Repository $config, Filesystem $files): int
{ {
$refresh_token = $config->get('trakt.login.oauth.refresh_token'); $refresh_token = $config->get('trakt.login.oauth.refresh_token');
if (empty($refresh_token)) { if (empty($refresh_token)) {
@ -40,6 +42,9 @@ class TraktRefreshLogin extends Command
$this->line("TRAKT_OAUTH_RESP='" . json_encode($body) . "'"); $this->line("TRAKT_OAUTH_RESP='" . json_encode($body) . "'");
$this->line('TRAKT_OAUTH_TOKEN=' . $body['access_token']); $this->line('TRAKT_OAUTH_TOKEN=' . $body['access_token']);
$config->set('trakt.login.oauth', $body);
$files->put($config->get('trakt.path'), json_encode($config->get('trakt'), JSON_PRETTY_PRINT));
return static::SUCCESS; return static::SUCCESS;
} }
} }

View file

@ -11,6 +11,7 @@ if (file_exists($path)) {
$data = json_decode(file_get_contents($path), true); $data = json_decode(file_get_contents($path), true);
} }
$data['path'] = $path;
$data = Arr::add($data, 'app_id', env('TRAKT_APP_ID')); $data = Arr::add($data, 'app_id', env('TRAKT_APP_ID'));
$data = Arr::add($data, 'app_secret', env('TRAKT_APP_SECRET')); $data = Arr::add($data, 'app_secret', env('TRAKT_APP_SECRET'));
$data = Arr::add($data, 'redirect_uri', env('TRAKT_REDIRECT_URI', 'urn:ietf:wg:oauth:2.0:oob')); $data = Arr::add($data, 'redirect_uri', env('TRAKT_REDIRECT_URI', 'urn:ietf:wg:oauth:2.0:oob'));