add OAUth2.0
This commit is contained in:
@@ -4,6 +4,8 @@ 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
|
||||
@@ -14,7 +16,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'
|
||||
'last_sync_at', 'is_active', 'signature', 'signature_enabled', 'auth_method'
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
@@ -41,6 +43,9 @@ class EmailSetting extends Model
|
||||
|
||||
public function getDecryptedPassword(): string
|
||||
{
|
||||
if ($this->isOauth()) {
|
||||
return $this->getOAuthAccessToken() ?? '';
|
||||
}
|
||||
if (empty($this->imap_password)) {
|
||||
return '';
|
||||
}
|
||||
@@ -53,6 +58,9 @@ class EmailSetting extends Model
|
||||
|
||||
public function getDecryptedSmtpPassword(): string
|
||||
{
|
||||
if ($this->isOauth()) {
|
||||
return $this->getOAuthAccessToken() ?? '';
|
||||
}
|
||||
if (empty($this->smtp_password)) {
|
||||
return $this->getDecryptedPassword();
|
||||
}
|
||||
@@ -63,6 +71,26 @@ 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 [
|
||||
@@ -80,6 +108,10 @@ class EmailSetting extends Model
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->isOauth()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
$encryption = match ($this->imap_encryption) {
|
||||
'ssl' => 'ssl',
|
||||
@@ -103,6 +135,10 @@ class EmailSetting extends Model
|
||||
public function testConnection(): array
|
||||
{
|
||||
try {
|
||||
if ($this->isOauth()) {
|
||||
return $this->testOAuthImapConnection();
|
||||
}
|
||||
|
||||
$mailbox = $this->getImapClient();
|
||||
|
||||
if (!$mailbox) {
|
||||
@@ -125,6 +161,32 @@ 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();
|
||||
|
||||
Reference in New Issue
Block a user