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
+1 -63
View File
@@ -4,8 +4,6 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use App\Enums\GoogleService;
use App\Services\GoogleOAuthService;
use Illuminate\Support\Facades\Crypt;
class EmailSetting extends Model
@@ -16,7 +14,7 @@ class EmailSetting extends Model
'imap_host', 'imap_port', 'imap_encryption', 'imap_username', 'imap_password',
'smtp_host', 'smtp_port', 'smtp_encryption', 'smtp_username', 'smtp_password',
'email_address', 'email_name', 'reply_to', 'sync_interval_minutes',
'last_sync_at', 'is_active', 'signature', 'signature_enabled', 'auth_method'
'last_sync_at', 'is_active', 'signature', 'signature_enabled'
];
protected $casts = [
@@ -43,9 +41,6 @@ class EmailSetting extends Model
public function getDecryptedPassword(): string
{
if ($this->isOauth()) {
return $this->getOAuthAccessToken() ?? '';
}
if (empty($this->imap_password)) {
return '';
}
@@ -58,9 +53,6 @@ class EmailSetting extends Model
public function getDecryptedSmtpPassword(): string
{
if ($this->isOauth()) {
return $this->getOAuthAccessToken() ?? '';
}
if (empty($this->smtp_password)) {
return $this->getDecryptedPassword();
}
@@ -71,26 +63,6 @@ class EmailSetting extends Model
}
}
public function isOauth(): bool
{
return $this->auth_method === 'oauth';
}
public function getOAuthAccessToken(): ?string
{
if (!$this->isOauth()) {
return null;
}
try {
$oauth = app(GoogleOAuthService::class);
return $oauth->getAccessToken(GoogleService::Gmail);
} catch (\Exception $e) {
\Illuminate\Support\Facades\Log::error('OAuth token refresh failed: ' . $e->getMessage());
return null;
}
}
public function getSmtpConfig(): array
{
return [
@@ -108,10 +80,6 @@ class EmailSetting extends Model
return null;
}
if ($this->isOauth()) {
return null;
}
try {
$encryption = match ($this->imap_encryption) {
'ssl' => 'ssl',
@@ -135,10 +103,6 @@ class EmailSetting extends Model
public function testConnection(): array
{
try {
if ($this->isOauth()) {
return $this->testOAuthImapConnection();
}
$mailbox = $this->getImapClient();
if (!$mailbox) {
@@ -161,32 +125,6 @@ class EmailSetting extends Model
}
}
private function testOAuthImapConnection(): array
{
try {
$token = $this->getOAuthAccessToken();
if (!$token) {
return ['success' => false, 'message' => 'Impossibile ottenere il token OAuth. Verifica la connessione Google.'];
}
$client = new \Webklex\PHPIMAP\Client([
'host' => $this->imap_host,
'port' => $this->imap_port ?? 993,
'encryption' => $this->imap_encryption ?? 'ssl',
'validate_cert' => false,
'username' => $this->imap_username,
'password' => $token,
'authentication' => 'oauth',
]);
$client->connect();
$client->disconnect();
return ['success' => true, 'message' => 'Connessione IMAP OAuth riuscita! Server: ' . $this->imap_host];
} catch (\Exception $e) {
return ['success' => false, 'message' => 'Errore connessione IMAP OAuth: ' . $e->getMessage()];
}
}
public static function getActive()
{
return self::where('is_active', true)->first();