-
-
Notifications
You must be signed in to change notification settings - Fork 371
Add auto rotation of the license key provided KEYGEN_API_KEY. #4442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+731
−255
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
68 changes: 68 additions & 0 deletions
68
app/Actions/Diagnostics/Pipes/Checks/KeygenApiTokenCheck.php
This file contains hidden or 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,68 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * SPDX-License-Identifier: MIT | ||
| * Copyright (c) 2017-2018 Tobias Reich | ||
| * Copyright (c) 2018-2026 LycheeOrg. | ||
| */ | ||
|
|
||
| namespace App\Actions\Diagnostics\Pipes\Checks; | ||
|
|
||
| use App\Contracts\DiagnosticPipe; | ||
| use App\DTO\DiagnosticData; | ||
| use App\Models\User; | ||
| use Illuminate\Support\Facades\Auth; | ||
| use LycheeVerify\TokenExtension; | ||
|
|
||
| /** | ||
| * Check the expiration status of the Keygen API token. | ||
| * Only displayed if the user is an admin and a token is configured. | ||
| */ | ||
| class KeygenApiTokenCheck implements DiagnosticPipe | ||
| { | ||
| public function __construct( | ||
| private TokenExtension $token_extension, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public function handle(array &$data, \Closure $next): array | ||
| { | ||
| /** @var User|null */ | ||
| $user = Auth::user(); | ||
|
|
||
| if ($user?->may_administrate !== true) { | ||
| return $next($data); | ||
| } | ||
|
|
||
| /** @var string $api_key */ | ||
| $api_key = config('verify.keygen_api_key', ''); | ||
| if ($api_key === '') { | ||
| return $next($data); | ||
| } | ||
|
|
||
| // @codeCoverageIgnoreStart | ||
| $result = $this->token_extension->extend(); | ||
|
|
||
| if (!$result->success) { | ||
| $data[] = DiagnosticData::error( | ||
| 'Keygen API token error: ' . ($result->message ?? 'unknown error') . ' Go to keygen.lycheeorg.dev to retrieve a new one.', | ||
| self::class, | ||
| ); | ||
|
|
||
| return $next($data); | ||
| } | ||
|
|
||
| if ($result->expires_at !== null && $result->expires_at->isBefore(now()->addWeek())) { | ||
| $data[] = DiagnosticData::warn( | ||
| 'Your Keygen API token expires on ' . $result->expires_at->toDateString() . '. Consider renewing it at keygen.lycheeorg.dev.', | ||
| self::class, | ||
| ); | ||
| } | ||
| // @codeCoverageIgnoreEnd | ||
|
|
||
| return $next($data); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,46 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * SPDX-License-Identifier: MIT | ||
| * Copyright (c) 2017-2018 Tobias Reich | ||
| * Copyright (c) 2018-2026 LycheeOrg. | ||
| */ | ||
|
|
||
| namespace App\Jobs; | ||
|
|
||
| use Illuminate\Foundation\Bus\Dispatchable; | ||
| use Illuminate\Support\Facades\Cache; | ||
| use Illuminate\Support\Facades\Schema; | ||
| use LycheeVerify\Contract\Status; | ||
| use LycheeVerify\Rotation; | ||
| use LycheeVerify\Verify; | ||
|
|
||
| class RotateLicenseKeyJob | ||
| { | ||
| use Dispatchable; | ||
|
|
||
| public function handle(Verify $verify, Rotation $rotation): void | ||
| { | ||
| if (!Schema::hasTable('configs')) { | ||
| return; | ||
| } | ||
|
|
||
| if ($verify->get_status() !== Status::FREE_EDITION) { | ||
| return; | ||
| } | ||
|
|
||
| /** @var string $api_key */ | ||
| $api_key = config('verify.keygen_api_key', ''); | ||
| if ($api_key === '') { | ||
| return; | ||
| } | ||
|
|
||
| // Make sure we try on login | ||
| Cache::forget(Rotation::CACHE_KEY); | ||
| $result = $rotation->rotate(); | ||
|
|
||
| if ($result->success) { | ||
| $verify->reset_status(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,27 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * SPDX-License-Identifier: MIT | ||
| * Copyright (c) 2017-2018 Tobias Reich | ||
| * Copyright (c) 2018-2026 LycheeOrg. | ||
| */ | ||
|
|
||
| namespace App\Listeners; | ||
|
|
||
| use App\Jobs\RotateLicenseKeyJob; | ||
| use App\Models\User; | ||
| use Illuminate\Auth\Events\Login; | ||
|
|
||
| class RotateLicenseKeyOnLogin | ||
| { | ||
| public function handle(Login $event): void | ||
| { | ||
| $user = $event->user; | ||
|
|
||
| if (!$user instanceof User || $user->may_administrate !== true) { | ||
| return; | ||
| } | ||
|
|
||
| RotateLicenseKeyJob::dispatch(); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.