2024-06-05 15:35:46 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Integrations\OAuth\Drivers;
|
|
|
|
|
|
|
|
|
|
use App\Integrations\OAuth\Drivers\Contracts\OAuthDriver;
|
|
|
|
|
use Google\Service\Sheets;
|
|
|
|
|
use Laravel\Socialite\Contracts\User;
|
|
|
|
|
use Laravel\Socialite\Facades\Socialite;
|
|
|
|
|
use Laravel\Socialite\Two\GoogleProvider;
|
|
|
|
|
|
|
|
|
|
class OAuthGoogleDriver implements OAuthDriver
|
|
|
|
|
{
|
2024-08-19 15:22:57 +02:00
|
|
|
private ?string $redirectUrl = null;
|
2024-08-29 13:28:02 +02:00
|
|
|
private ?array $scopes = [];
|
2024-08-19 15:22:57 +02:00
|
|
|
|
2024-06-05 15:35:46 +02:00
|
|
|
protected GoogleProvider $provider;
|
|
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->provider = Socialite::driver('google');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getRedirectUrl(): string
|
|
|
|
|
{
|
|
|
|
|
return $this->provider
|
2024-08-29 13:28:02 +02:00
|
|
|
->scopes($this->scopes ?? [])
|
2024-06-05 15:35:46 +02:00
|
|
|
->stateless()
|
2024-08-19 15:22:57 +02:00
|
|
|
->redirectUrl($this->redirectUrl ?? config('services.google.redirect'))
|
2024-06-05 15:35:46 +02:00
|
|
|
->with([
|
|
|
|
|
'access_type' => 'offline',
|
|
|
|
|
'prompt' => 'consent select_account'
|
|
|
|
|
])
|
|
|
|
|
->redirect()
|
|
|
|
|
->getTargetUrl();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getUser(): User
|
|
|
|
|
{
|
|
|
|
|
return $this->provider
|
|
|
|
|
->stateless()
|
2024-08-19 15:22:57 +02:00
|
|
|
->redirectUrl($this->redirectUrl ?? config('services.google.redirect'))
|
2024-06-05 15:35:46 +02:00
|
|
|
->user();
|
|
|
|
|
}
|
2024-08-19 15:22:57 +02:00
|
|
|
|
|
|
|
|
public function canCreateUser(): bool
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-29 13:28:02 +02:00
|
|
|
public function setRedirectUrl(string $url): OAuthDriver
|
2024-08-19 15:22:57 +02:00
|
|
|
{
|
|
|
|
|
$this->redirectUrl = $url;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-29 13:28:02 +02:00
|
|
|
public function setScopes(array $scopes): OAuthDriver
|
|
|
|
|
{
|
|
|
|
|
$this->scopes = $scopes;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function fullScopes(): OAuthDriver
|
|
|
|
|
{
|
|
|
|
|
return $this->setScopes([Sheets::DRIVE_FILE]);
|
|
|
|
|
}
|
2024-06-05 15:35:46 +02:00
|
|
|
}
|