Revert OAuth2.0

This commit is contained in:
2026-06-16 21:35:10 +02:00
parent 69f4d6a602
commit 6001c2e3b8
33 changed files with 56 additions and 1127 deletions
+3
View File
@@ -5,8 +5,11 @@ declare(strict_types=1);
namespace App\Services;
use App\Models\AppSetting;
use App\Models\Notifica;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use RuntimeException;
use ZipArchive;
@@ -4,9 +4,7 @@ declare(strict_types=1);
namespace App\Services;
use App\Enums\GoogleService;
use App\Models\CalendarioConnessione;
use App\Services\GoogleOAuthService;
use App\Models\Evento;
use Carbon\Carbon;
use Google\Client as GoogleClient;
@@ -329,16 +327,6 @@ class GoogleCalendarSyncService
private function buildClient(CalendarioConnessione $connessione): GoogleClient
{
try {
$oauth = app(GoogleOAuthService::class);
$connection = $oauth->getConnection(GoogleService::Calendar);
if ($connection) {
return $oauth->buildClient(GoogleService::Calendar);
}
} catch (\Exception $e) {
\Illuminate\Support\Facades\Log::warning('Central Calendar OAuth failed, falling back: ' . $e->getMessage());
}
$config = $connessione->getDecryptedConfig();
$client = new GoogleClient();
-150
View File
@@ -1,150 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Services;
use App\Enums\GoogleService;
use App\Models\GoogleOAuthConnection;
use Google\Client;
use Illuminate\Support\Facades\Log;
class GoogleOAuthService
{
private const TOKEN_URI = 'https://oauth2.googleapis.com/token';
private static function getRedirectUri(): string
{
$subfolder = config('app.url');
return rtrim($subfolder, '/') . '/auth/google/callback';
}
public function getAuthorizationUrl(GoogleService $service): string
{
$client = $this->createClient($service);
$client->setRedirectUri(self::getRedirectUri());
$client->setState($service->value);
$client->setAccessType('offline');
$client->setPrompt('consent');
$client->setIncludeGrantedScopes(true);
return $client->createAuthUrl();
}
public function handleCallback(string $code, GoogleService $service): GoogleOAuthConnection
{
$client = $this->createClient($service);
$client->setRedirectUri(self::getRedirectUri());
$token = $client->fetchAccessTokenWithAuthCode($code);
if (isset($token['error'])) {
throw new \RuntimeException('Google OAuth error: ' . ($token['error_description'] ?? $token['error']));
}
if (!isset($token['refresh_token'])) {
throw new \RuntimeException('No refresh token returned. Ensure access_type=offline and prompt=consent are set.');
}
$clientId = $client->getClientId();
$clientSecret = $client->getClientSecret();
return GoogleOAuthConnection::updateOrCreate(
['service' => $service->value],
[
'client_id' => $clientId,
'client_secret' => $clientSecret,
'refresh_token' => $token['refresh_token'],
]
);
}
public function revoke(GoogleService $service): void
{
$connection = $this->getConnection($service);
if (!$connection) {
return;
}
$client = $this->createClient($service);
$client->revokeToken();
$connection->delete();
}
public function getAccessToken(GoogleService $service): ?string
{
$connection = $this->getConnection($service);
if (!$connection) {
return null;
}
try {
$client = $this->createClient($service);
$client->setAccessToken([
'access_token' => '',
'refresh_token' => $connection->refresh_token,
'client_id' => $connection->client_id,
'client_secret' => $connection->client_secret,
]);
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
$newToken = $client->getAccessToken();
if (isset($newToken['refresh_token'])) {
$connection->refresh_token = $newToken['refresh_token'];
$connection->save();
}
}
$accessToken = $client->getAccessToken();
return $accessToken['access_token'] ?? null;
} catch (\Exception $e) {
Log::error('Google OAuth token refresh failed for ' . $service->value . ': ' . $e->getMessage());
return null;
}
}
public function isAuthorized(GoogleService $service): bool
{
return $this->getConnection($service) !== null;
}
public function getConnection(GoogleService $service): ?GoogleOAuthConnection
{
return GoogleOAuthConnection::where('service', $service->value)->first();
}
public function buildClient(GoogleService $service): Client
{
$client = $this->createClient($service);
$connection = $this->getConnection($service);
if ($connection) {
$client->setAccessToken([
'access_token' => '',
'refresh_token' => $connection->refresh_token,
'client_id' => $connection->client_id,
'client_secret' => $connection->client_secret,
]);
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
}
}
return $client;
}
private function createClient(GoogleService $service): Client
{
$client = new Client();
$client->setApplicationName(config('app.name'));
$client->setScopes([$service->scope()]);
$client->setClientId(config('services.google.client_id'));
$client->setClientSecret(config('services.google.client_secret'));
$client->setRedirectUri(self::getRedirectUri());
return $client;
}
}
-62
View File
@@ -4,9 +4,7 @@ declare(strict_types=1);
namespace App\Services;
use App\Enums\GoogleService;
use App\Models\StorageRepository;
use App\Services\GoogleOAuthService;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Log;
use League\Flysystem\Filesystem;
@@ -71,20 +69,6 @@ class StorageRepositoryService
private function checkGoogleDriveApiStatus(array $config): ?string
{
try {
try {
$oauth = app(GoogleOAuthService::class);
$connection = $oauth->getConnection(GoogleService::Drive);
if ($connection) {
$client = $oauth->buildClient(GoogleService::Drive);
$service = new \Google_Service_Drive($client);
$rootId = $config['root_folder_id'] ?? 'root';
$service->files->get($rootId, ['fields' => 'id']);
return null;
}
} catch (\Exception $e) {
// fall through to config-based auth
}
$client = new \Google_Client();
$client->setClientId($config['client_id']);
$client->setClientSecret($config['client_secret']);
@@ -260,18 +244,6 @@ class StorageRepositoryService
{
$config = $repo->getDecryptedConfig();
try {
$oauth = app(GoogleOAuthService::class);
$connection = $oauth->getConnection(GoogleService::Drive);
if ($connection) {
$client = $oauth->buildClient(GoogleService::Drive);
$service = new \Google_Service_Drive($client);
return $this->readGoogleDriveFileWithService($service, $config, $normalizedPath, $basename);
}
} catch (\Exception $e) {
\Illuminate\Support\Facades\Log::warning('Central Google Drive OAuth failed in readGoogleDriveFile, falling back: ' . $e->getMessage());
}
$client = new \Google_Client();
$client->setClientId($config['client_id']);
$client->setClientSecret($config['client_secret']);
@@ -311,40 +283,6 @@ class StorageRepositoryService
];
}
private function readGoogleDriveFileWithService(\Google_Service_Drive $service, array $config, string $normalizedPath, string $basename): array
{
$rootFolderId = $config['root_folder_id'] ?? 'root';
$fileId = $this->findGoogleDriveFileId($service, $rootFolderId, $normalizedPath);
$file = $service->files->get($fileId, ['fields' => 'id,name,mimeType,size']);
$mimeType = $file->getMimeType();
if ($mimeType && str_starts_with($mimeType, 'application/vnd.google-apps.')) {
return $this->exportGoogleDriveDoc($service, $fileId, $mimeType, $basename);
}
$response = $service->files->get($fileId, ['alt' => 'media']);
$body = $response->getBody();
$stream = $body instanceof \Psr\Http\Message\StreamInterface ? $body->detach() : null;
if (!$stream) {
$stream = fopen('php://temp', 'r+');
fwrite($stream, (string) $body);
rewind($stream);
}
$fileSize = $file->getSize();
return [
'stream' => $stream,
'mimeType' => $mimeType,
'basename' => $basename,
'fileSize' => $fileSize !== null ? (int) $fileSize : null,
];
}
private function exportGoogleDriveDoc(\Google_Service_Drive $service, string $fileId, string $googleMimeType, string $originalBasename): array
{
$exportMap = [