Implement token refresh

This commit is contained in:
Deon George 2022-08-13 11:48:16 +10:00
parent 8fd79ce23e
commit c1a64e2094
3 changed files with 94 additions and 0 deletions

View File

@ -0,0 +1,48 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use App\Models\{ProviderOauth,Site,User};
use App\Jobs\ProviderTokenRefresh as Job;
class ProviderTokenRefresh extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'provider:token:refresh'
.' {siteid : Site ID}'
.' {provider : Supplier Name}'
.' {user : User Email}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Refresh users access/refresh token';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$site = Site::findOrFail($this->argument('siteid'));
Config::set('site',$site);
$so = ProviderOauth::where('name',$this->argument('provider'))->singleOrFail();
$uo = User::where('email',$this->argument('user'))->singleOrFail();
if (($x=$so->tokens->where('user_id',$uo->id))->count() !== 1)
abort(500,sprintf('Unknown Tokens for [%s]',$uo->email));
Job::dispatchSync($x->pop());
}
}

View File

@ -0,0 +1,39 @@
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\ProviderToken;
class ProviderTokenRefresh implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private ProviderToken $to;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(ProviderToken $to)
{
$this->to = $to;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$this->to->refreshToken();
}
}

View File

@ -14,4 +14,11 @@ class ProviderToken extends Model
'access_token_expires_at',
'refresh_token_expires_at',
];
/* RELATIONS */
public function provider()
{
return $this->belongsTo(ProviderOauth::class,'provider_oauth_id');
}
}