mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-24 12:07:23 +08:00
oauth provider callback pre-generate
This commit is contained in:
@@ -13,6 +13,8 @@ use Filament\Tables;
|
|||||||
use Filament\Tables\Table;
|
use Filament\Tables\Table;
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
||||||
|
use Nexus\Database\NexusDB;
|
||||||
|
use Ramsey\Uuid;
|
||||||
|
|
||||||
class ProviderResource extends Resource
|
class ProviderResource extends Resource
|
||||||
{
|
{
|
||||||
@@ -89,9 +91,22 @@ class ProviderResource extends Resource
|
|||||||
Forms\Components\Toggle::make('enabled')
|
Forms\Components\Toggle::make('enabled')
|
||||||
->label(__('label.enabled'))
|
->label(__('label.enabled'))
|
||||||
,
|
,
|
||||||
|
Forms\Components\TextInput::make('redirect')
|
||||||
|
->default(fn ($record) => OauthProvider::getCallbackUrl($record->uuid ?? self::getNewUuid()))
|
||||||
|
->disabled()
|
||||||
|
->label(__('oauth.redirect'))
|
||||||
|
->columnSpanFull()
|
||||||
|
,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static function getNewUuid(): string
|
||||||
|
{
|
||||||
|
return NexusDB::remember("new_oauth_provider_uuid", 86400 * 365, function () {
|
||||||
|
return UUid\v4();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public static function table(Table $table): Table
|
public static function table(Table $table): Table
|
||||||
{
|
{
|
||||||
return $table
|
return $table
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ class OauthController extends Controller
|
|||||||
|
|
||||||
$query = http_build_query([
|
$query = http_build_query([
|
||||||
'client_id' => $provider->client_id,
|
'client_id' => $provider->client_id,
|
||||||
'redirect_uri' => $this->getRedirectUri($provider),
|
'redirect_uri' => $this->getCallbackUrl($provider),
|
||||||
'response_type' => 'code',
|
'response_type' => 'code',
|
||||||
'scope' => '',
|
'scope' => '',
|
||||||
'state' => $state,
|
'state' => $state,
|
||||||
@@ -46,9 +46,9 @@ class OauthController extends Controller
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getRedirectUri(OauthProvider $provider): string
|
private function getCallbackUrl(OauthProvider $provider): string
|
||||||
{
|
{
|
||||||
return sprintf("%s/oauth/callback/%s", getSchemeAndHttpHost(), $provider->uuid);
|
return OauthProvider::getCallbackUrl($provider->uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -76,7 +76,7 @@ class OauthController extends Controller
|
|||||||
'grant_type' => 'authorization_code',
|
'grant_type' => 'authorization_code',
|
||||||
'client_id' => $provider->client_id,
|
'client_id' => $provider->client_id,
|
||||||
'client_secret' => $provider->client_secret,
|
'client_secret' => $provider->client_secret,
|
||||||
'redirect_uri' => $this->getRedirectUri($provider),
|
'redirect_uri' => $this->getCallbackUrl($provider),
|
||||||
'code' => $request->code,
|
'code' => $request->code,
|
||||||
];
|
];
|
||||||
$response = Http::asForm()->post($provider->token_endpoint_url, $params);
|
$response = Http::asForm()->post($provider->token_endpoint_url, $params);
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use Laravel\Passport\Client;
|
use Laravel\Passport\Client;
|
||||||
|
use Nexus\Database\NexusDB;
|
||||||
use Ramsey\Uuid;
|
use Ramsey\Uuid;
|
||||||
|
|
||||||
class OauthProvider extends NexusModel
|
class OauthProvider extends NexusModel
|
||||||
@@ -15,13 +16,30 @@ class OauthProvider extends NexusModel
|
|||||||
|
|
||||||
public $timestamps = true;
|
public $timestamps = true;
|
||||||
|
|
||||||
|
const NEW_UUID_CACHE_KEY = 'new_oauth_provider_uuid';
|
||||||
|
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
'enabled' => 'boolean',
|
'enabled' => 'boolean',
|
||||||
];
|
];
|
||||||
protected static function booted(): void
|
protected static function booted(): void
|
||||||
{
|
{
|
||||||
static::creating(function (OauthProvider $model) {
|
static::creating(function (OauthProvider $model) {
|
||||||
$model->uuid = Uuid\v4();
|
$model->uuid = self::getNewUuid();
|
||||||
|
});
|
||||||
|
static::created(function (OauthProvider $model) {
|
||||||
|
NexusDB::cache_del(self::NEW_UUID_CACHE_KEY);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getCallbackUrl(string $uuid): string
|
||||||
|
{
|
||||||
|
return sprintf("%s/oauth/callback/%s", getSchemeAndHttpHost(), $uuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function getNewUuid(): string
|
||||||
|
{
|
||||||
|
return NexusDB::remember(self::NEW_UUID_CACHE_KEY, 86400 * 365, function () {
|
||||||
|
return UUid\v4();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user