Token regen
This commit is contained in:
parent
f63220ff2a
commit
b06642b1bf
3 changed files with 100 additions and 1 deletions
|
|
@ -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;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
42
app/Mail/Api/ApiTokenRegenerated.php
Normal file
42
app/Mail/Api/ApiTokenRegenerated.php
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
namespace App\Mail\Api;
|
||||
|
||||
use App\Models\Api\ApiToken;
|
||||
use Illuminate\Mail\Mailable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class ApiTokenRegenerated extends Mailable
|
||||
{
|
||||
use SerializesModels;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var \App\Models\Api\ApiToken
|
||||
*/
|
||||
private $apiToken = null;
|
||||
|
||||
/**
|
||||
* Create a new message instance.
|
||||
*
|
||||
* @param \App\Models\Api\ApiToken $apiToken
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(ApiToken $apiToken)
|
||||
{
|
||||
$this->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'));
|
||||
}
|
||||
}
|
||||
10
resources/views/emails/apiTokens/regen.blade.php
Normal file
10
resources/views/emails/apiTokens/regen.blade.php
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
@component('mail::message')
|
||||
# !! {{ __('A new Api Token has been regenerated, please replace in appropriate project') }} !!
|
||||
|
||||
{{ now() }}<br>
|
||||
{{ __('Token name \':name\'', ['name' => $apiToken->name]) }}
|
||||
|
||||
|
||||
{{ __('Thanks') }},<br>
|
||||
{{ config('app.name') }}
|
||||
@endcomponent
|
||||
Loading…
Add table
Reference in a new issue