From 17d105b4d07ce544e94b7f90ff7f50feb86b244f Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Wed, 10 Apr 2024 09:29:22 +0200 Subject: [PATCH] track twitch token expiration --- app/Console/Commands/TwitchChannelInfo.php | 7 ++++- app/Models/TwitchToken.php | 5 ++++ app/TwitchBot/TwitchBot.php | 3 ++ ...04_10_071945_store_twitch_token_expiry.php | 28 +++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 database/migrations/2024_04_10_071945_store_twitch_token_expiry.php diff --git a/app/Console/Commands/TwitchChannelInfo.php b/app/Console/Commands/TwitchChannelInfo.php index 6a3261c..7b373c8 100644 --- a/app/Console/Commands/TwitchChannelInfo.php +++ b/app/Console/Commands/TwitchChannelInfo.php @@ -37,6 +37,10 @@ class TwitchChannelInfo extends Command { $this->line('please acquire a token for localhorsttv first'); return 1; } + if ($this->token->hasExpired()) { + $this->line('access token has expired, refreshing'); + $this->token->refresh(); + } $channels = Channel::where('twitch_chat', '!=', '')->where('twitch_id', '=', '')->get(); foreach ($channels as $channel) { try { @@ -52,7 +56,8 @@ class TwitchChannelInfo extends Command { ->where(function($query) { $query->where('join', true); $query->orWhere('chat', true); - })->chunk(100, function ($channels) { + }) + ->chunk(100, function ($channels) { $this->updateChannelInfos($channels); }); return Command::SUCCESS; diff --git a/app/Models/TwitchToken.php b/app/Models/TwitchToken.php index a4c561e..3a35d8c 100644 --- a/app/Models/TwitchToken.php +++ b/app/Models/TwitchToken.php @@ -18,6 +18,10 @@ class TwitchToken extends Model .'&scope='.implode('+', array_map('rawurlencode', $this->scope)); } + public function hasExpired() { + return now()->isAfter($this->expires_at); + } + public function refresh() { $rsp = Http::post('https://id.twitch.tv/oauth2/token', [ 'client_id' => config('twitch.client_id'), @@ -32,6 +36,7 @@ class TwitchToken extends Model $this->scope = $rsp['scope']; $this->access = $rsp['access_token']; $this->refresh = $rsp['refresh_token']; + $this->expires_at = now()->add($rsp['expires_in'], 'second'); $this->save(); } diff --git a/app/TwitchBot/TwitchBot.php b/app/TwitchBot/TwitchBot.php index e5a5aa6..63f4236 100644 --- a/app/TwitchBot/TwitchBot.php +++ b/app/TwitchBot/TwitchBot.php @@ -23,6 +23,9 @@ class TwitchBot { if (!$this->token) { throw new \Exception('unable to find access token'); } + if ($this->token->hasExpired()) { + $this->token->refresh(); + } $this->connector = new Connector(); $this->connect(); diff --git a/database/migrations/2024_04_10_071945_store_twitch_token_expiry.php b/database/migrations/2024_04_10_071945_store_twitch_token_expiry.php new file mode 100644 index 0000000..a4ebcbf --- /dev/null +++ b/database/migrations/2024_04_10_071945_store_twitch_token_expiry.php @@ -0,0 +1,28 @@ +timestamp('expires_at')->nullable()->default(null); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('twitch_tokens', function (Blueprint $table) { + $table->dropColumn('expires_at'); + }); + } +}; -- 2.39.2