-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: kill inactive access tokens (#32)
* feat: kill inactive access tokens * Apply fixes from StyleCI * fix: typo * chore: update README --------- Co-authored-by: StyleCI Bot <[email protected]>
- Loading branch information
1 parent
a695c9d
commit b43ff41
Showing
6 changed files
with
139 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of ianm/twofactor. | ||
* | ||
* Copyright (c) 2023 IanM. | ||
* | ||
* For the full copyright and license information, please view the LICENSE.md | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace IanM\TwoFactor\Console; | ||
|
||
use Flarum\Settings\SettingsRepositoryInterface; | ||
use Illuminate\Console\Scheduling\Event; | ||
|
||
class InactiveTokensSchedule | ||
{ | ||
public function __construct( | ||
protected SettingsRepositoryInterface $settings | ||
) { | ||
} | ||
|
||
public function __invoke(Event $event) | ||
{ | ||
if (! (bool) $this->settings->get('ianm-twofactor.kill_inactive_tokens')) { | ||
return; | ||
} | ||
|
||
$event->twiceDaily(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of ianm/twofactor. | ||
* | ||
* Copyright (c) 2023 IanM. | ||
* | ||
* For the full copyright and license information, please view the LICENSE.md | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace IanM\TwoFactor\Console; | ||
|
||
use Carbon\Carbon; | ||
use Flarum\Http\AccessToken; | ||
use Flarum\Settings\SettingsRepositoryInterface; | ||
use Illuminate\Console\Command; | ||
|
||
class KillInactiveTokensCommand extends Command | ||
{ | ||
protected $signature = 'twofactor:kill-inactive-tokens'; | ||
protected $description = 'Kill all inactive tokens'; | ||
|
||
public function __construct( | ||
protected SettingsRepositoryInterface $settings | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
public function handle(): void | ||
{ | ||
$age = (int) $this->settings->get('ianm-twofactor.kill_inactive_tokens_age_days'); | ||
$maxAge = Carbon::now()->subDays($age); | ||
|
||
$query = AccessToken::query() | ||
->where('last_activity_at', '<', $maxAge); | ||
|
||
if (! (bool) $this->settings->get('ianm-twofactor.also_kill_developer_tokens')) { | ||
$this->info('Not deleting any developer tokens.'); | ||
$query->where('type', '!=', 'developer'); | ||
} | ||
|
||
$count = $query->count(); | ||
|
||
if ($count === 0) { | ||
$this->info("No tokens found which have not been used in $age+ days."); | ||
|
||
return; | ||
} | ||
|
||
$this->info("Found $count tokens which have not been used in $age+ days. Deleting..."); | ||
|
||
$this->output->progressStart($count); | ||
|
||
$query->delete(); | ||
|
||
$this->output->progressFinish(); | ||
} | ||
} |