From b06642b1bf7628ce92bcca089d771d43550ef4b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Mon, 1 Aug 2022 10:59:56 +0200 Subject: [PATCH] Token regen --- app/Console/Commands/Api/TokenCrud.php | 49 ++++++++++++++++++- app/Mail/Api/ApiTokenRegenerated.php | 42 ++++++++++++++++ .../views/emails/apiTokens/regen.blade.php | 10 ++++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 app/Mail/Api/ApiTokenRegenerated.php create mode 100644 resources/views/emails/apiTokens/regen.blade.php diff --git a/app/Console/Commands/Api/TokenCrud.php b/app/Console/Commands/Api/TokenCrud.php index c999d3b..cf5b86e 100644 --- a/app/Console/Commands/Api/TokenCrud.php +++ b/app/Console/Commands/Api/TokenCrud.php @@ -3,6 +3,7 @@ namespace App\Console\Commands\Api; use App\Mail\Api\ApiTokenCreated; +use App\Mail\Api\ApiTokenRegenerated; use App\Mail\Api\ApiTokenRemoved; use App\Models\Api\ApiToken; use Illuminate\Console\Command; @@ -17,7 +18,7 @@ class TokenCrud extends Command * @var string */ protected $signature = 'api:token - {action? : Can be list, add, remove, default is list} + {action? : Can be list, add, remove, regen, default is list} {api_token? : Only if action is remove, pass the ID to remove}'; /** @@ -49,6 +50,8 @@ class TokenCrud extends Command return $this->createApiToken(); case 'remove': return $this->removeApiToken(); + case 'regen': + return $this->regenApiToken(); default: $this->error(\sprintf( 'action \'%s\' is not availaible, use --help to see usage.', @@ -134,4 +137,48 @@ class TokenCrud extends Command return 0; }); } + + /** + * Regenerate Api token + * + * @return integer + */ + private function regenApiToken(): int + { + return DB::transaction(function () { + if (!\is_numeric($apiTokenId = $this->argument('api_token'))) { + $this->error('remove command needs an id, use `list` to see all.'); + return -1; + } + /** @var \App\Models\Api\ApiToken|null */ + if (!($apiToken = ApiToken::where('id', $apiTokenId)->first())) { + $this->error("api_token '{$apiTokenId}' was not found"); + return -1; + } + $publicToken = $apiToken->generatePublicToken(); + $privateToken = $apiToken->generatePrivateToken(); + if ( + !$this->confirm( + "Are you sur you want to regen apiToken '{$apiToken->id}' '{$apiToken->name}' ?" + ) + ) { + $this->info('Nothing was done.'); + return 0; + } + + $apiToken->saveOrFail(); + + Mail::send(new ApiTokenRegenerated($apiToken)); + + $this->alert(" + Your new api Token for '{$apiToken->id}' named '{$apiToken->name}' + public: {$publicToken} + private: {$privateToken} + + Please take note as it wont be available to display anymore ! + "); + + return 0; + }); + } } diff --git a/app/Mail/Api/ApiTokenRegenerated.php b/app/Mail/Api/ApiTokenRegenerated.php new file mode 100644 index 0000000..cc552af --- /dev/null +++ b/app/Mail/Api/ApiTokenRegenerated.php @@ -0,0 +1,42 @@ +apiToken = $apiToken; + } + + /** + * Build the message. + * + * @return $this + */ + public function build() + { + return $this->markdown('emails.apiTokens.regen', [ + 'apiToken' => $this->apiToken + ])->subject(trans('!!! :appname a new Api Token has been regenerated !!!', ['appname' => config('app.name')])) + ->to(config('mail.contactsList.serviceweb')); + } +} diff --git a/resources/views/emails/apiTokens/regen.blade.php b/resources/views/emails/apiTokens/regen.blade.php new file mode 100644 index 0000000..b12600c --- /dev/null +++ b/resources/views/emails/apiTokens/regen.blade.php @@ -0,0 +1,10 @@ +@component('mail::message') +# !! {{ __('A new Api Token has been regenerated, please replace in appropriate project') }} !! + +{{ now() }}
+{{ __('Token name \':name\'', ['name' => $apiToken->name]) }} + + +{{ __('Thanks') }},
+{{ config('app.name') }} +@endcomponent