N°5102 - Allow to send emails (eg. notifications) using GSuite SMTP and OAuth (fix config error message)

This commit is contained in:
Eric Espie
2022-06-08 13:24:29 +02:00
parent 4c585614cd
commit 6d3118d9e9
16 changed files with 80 additions and 12 deletions

View File

@@ -466,6 +466,8 @@ Dict::Add('CS CZ', 'Czech', 'Čeština', array(
'UI:Error:MaintenanceTitle' => 'Maintenance~~',
'UI:Error:InvalidToken' => 'Error: the requested operation has already been performed (CSRF token not found)~~',
'UI:Error:SMTP:UnknownVendor' => 'OAuth SMTP provider %1$s does not exist (email_transport_smtp.oauth.provider)~~',
'UI:GroupBy:Count' => 'Množství',
'UI:GroupBy:Count+' => 'Množství prvků',
'UI:CountOfObjects' => 'Počet objektů odpovídajícíh ktritériím: %1$d',

View File

@@ -453,6 +453,8 @@ Dict::Add('DA DA', 'Danish', 'Dansk', array(
'UI:Error:MaintenanceTitle' => 'Maintenance~~',
'UI:Error:InvalidToken' => 'Error: the requested operation has already been performed (CSRF token not found)~~',
'UI:Error:SMTP:UnknownVendor' => 'OAuth SMTP provider %1$s does not exist (email_transport_smtp.oauth.provider)~~',
'UI:GroupBy:Count' => 'Antal',
'UI:GroupBy:Count+' => 'Antal af elementer',
'UI:CountOfObjects' => '%1$d objekter opfylder kriteriet.',

View File

@@ -452,6 +452,8 @@ Dict::Add('DE DE', 'German', 'Deutsch', array(
'UI:Error:MaintenanceTitle' => 'Wartung',
'UI:Error:InvalidToken' => 'Error: the requested operation has already been performed (CSRF token not found)~~',
'UI:Error:SMTP:UnknownVendor' => 'OAuth SMTP provider %1$s does not exist (email_transport_smtp.oauth.provider)~~',
'UI:GroupBy:Count' => 'Anzahl',
'UI:GroupBy:Count+' => 'Anzahl der Elemente',
'UI:CountOfObjects' => '%1$d Objekte, die das Kriterium erfüllen.',

View File

@@ -468,6 +468,8 @@ Dict::Add('EN US', 'English', 'English', array(
'UI:Error:MaintenanceTitle' => 'Maintenance',
'UI:Error:InvalidToken' => 'Error: the requested operation has already been performed (CSRF token not found)',
'UI:Error:SMTP:UnknownVendor' => 'OAuth SMTP provider %1$s does not exist (email_transport_smtp.oauth.provider)',
'UI:GroupBy:Count' => 'Count',
'UI:GroupBy:Count+' => 'Number of elements',
'UI:CountOfObjects' => '%1$d objects matching the criteria.',

View File

@@ -465,6 +465,8 @@ Dict::Add('ES CR', 'Spanish', 'Español, Castellaño', array(
'UI:Error:MaintenanceTitle' => 'Mantenimiento',
'UI:Error:InvalidToken' => 'Error: the requested operation has already been performed (CSRF token not found)~~',
'UI:Error:SMTP:UnknownVendor' => 'OAuth SMTP provider %1$s does not exist (email_transport_smtp.oauth.provider)~~',
'UI:GroupBy:Count' => 'Cuenta',
'UI:GroupBy:Count+' => 'Número de Elementos',
'UI:CountOfObjects' => '%1$d Elementos cumplen Criterio.',

View File

@@ -451,6 +451,8 @@ Dict::Add('FR FR', 'French', 'Français', array(
'UI:Error:MaintenanceTitle' => 'Maintenance',
'UI:Error:InvalidToken' => 'Erreur: l\'opération a déjà été effectuée (CSRF token not found)',
'UI:Error:SMTP:UnknownVendor' => 'Le provider SMTP Oauth 2.0 %1$s n\'existe pas',
'UI:GroupBy:Count' => 'Nombre',
'UI:GroupBy:Count+' => 'Nombre d\'éléments',
'UI:CountOfObjects' => '%1$d objets correspondants aux critères.',

View File

@@ -451,6 +451,8 @@ Dict::Add('HU HU', 'Hungarian', 'Magyar', array(
'UI:Error:MaintenanceTitle' => 'Maintenance~~',
'UI:Error:InvalidToken' => 'Error: the requested operation has already been performed (CSRF token not found)~~',
'UI:Error:SMTP:UnknownVendor' => 'OAuth SMTP provider %1$s does not exist (email_transport_smtp.oauth.provider)~~',
'UI:GroupBy:Count' => 'Számossága',
'UI:GroupBy:Count+' => '',
'UI:CountOfObjects' => '%1$d darab objektum felel meg a kritériumoknak.',

View File

@@ -464,6 +464,8 @@ Dict::Add('IT IT', 'Italian', 'Italiano', array(
'UI:Error:MaintenanceTitle' => 'Maintenance~~',
'UI:Error:InvalidToken' => 'Error: the requested operation has already been performed (CSRF token not found)~~',
'UI:Error:SMTP:UnknownVendor' => 'OAuth SMTP provider %1$s does not exist (email_transport_smtp.oauth.provider)~~',
'UI:GroupBy:Count' => 'Conteggio',
'UI:GroupBy:Count+' => '',
'UI:CountOfObjects' => '%1$d oggetti corrispondenti ai criteri.',

View File

@@ -451,6 +451,8 @@ Dict::Add('JA JP', 'Japanese', '日本語', array(
'UI:Error:MaintenanceTitle' => 'Maintenance~~',
'UI:Error:InvalidToken' => 'Error: the requested operation has already been performed (CSRF token not found)~~',
'UI:Error:SMTP:UnknownVendor' => 'OAuth SMTP provider %1$s does not exist (email_transport_smtp.oauth.provider)~~',
'UI:GroupBy:Count' => 'カウント',
'UI:GroupBy:Count+' => '要素数',
'UI:CountOfObjects' => '%1$d 個のオブジェクトが条件にマッチしました。',

View File

@@ -470,6 +470,8 @@ Dict::Add('NL NL', 'Dutch', 'Nederlands', array(
'UI:Error:MaintenanceTitle' => 'Onderhoud',
'UI:Error:InvalidToken' => 'Error: the requested operation has already been performed (CSRF token not found)~~',
'UI:Error:SMTP:UnknownVendor' => 'OAuth SMTP provider %1$s does not exist (email_transport_smtp.oauth.provider)~~',
'UI:GroupBy:Count' => 'Aantal',
'UI:GroupBy:Count+' => 'Aantal objecten',
'UI:CountOfObjects' => '%1$d objecten voldoen aan de criteria.',

View File

@@ -464,6 +464,8 @@ Dict::Add('PT BR', 'Brazilian', 'Brazilian', array(
'UI:Error:MaintenanceTitle' => 'Maintenance~~',
'UI:Error:InvalidToken' => 'Error: the requested operation has already been performed (CSRF token not found)~~',
'UI:Error:SMTP:UnknownVendor' => 'OAuth SMTP provider %1$s does not exist (email_transport_smtp.oauth.provider)~~',
'UI:GroupBy:Count' => 'Número',
'UI:GroupBy:Count+' => 'Número de elementos',
'UI:CountOfObjects' => '%1$d objetos correspondem aos critérios.',

View File

@@ -443,6 +443,8 @@ Dict::Add('RU RU', 'Russian', 'Русский', array(
'UI:Error:MaintenanceTitle' => 'Техническое обслуживание',
'UI:Error:InvalidToken' => 'Error: the requested operation has already been performed (CSRF token not found)~~',
'UI:Error:SMTP:UnknownVendor' => 'OAuth SMTP provider %1$s does not exist (email_transport_smtp.oauth.provider)~~',
'UI:GroupBy:Count' => 'Количество',
'UI:GroupBy:Count+' => 'Количество элементов',
'UI:CountOfObjects' => '%1$d объектов соответствует критериям.',

View File

@@ -451,6 +451,8 @@ Dict::Add('SK SK', 'Slovak', 'Slovenčina', array(
'UI:Error:MaintenanceTitle' => 'Maintenance~~',
'UI:Error:InvalidToken' => 'Error: the requested operation has already been performed (CSRF token not found)~~',
'UI:Error:SMTP:UnknownVendor' => 'OAuth SMTP provider %1$s does not exist (email_transport_smtp.oauth.provider)~~',
'UI:GroupBy:Count' => 'Počet',
'UI:GroupBy:Count+' => '',
'UI:CountOfObjects' => '%1$d objekt/y/ov sa nezhoduje s kritériami.',

View File

@@ -465,6 +465,8 @@ Dict::Add('TR TR', 'Turkish', 'Türkçe', array(
'UI:Error:MaintenanceTitle' => 'Maintenance~~',
'UI:Error:InvalidToken' => 'Error: the requested operation has already been performed (CSRF token not found)~~',
'UI:Error:SMTP:UnknownVendor' => 'OAuth SMTP provider %1$s does not exist (email_transport_smtp.oauth.provider)~~',
'UI:GroupBy:Count' => 'Say',
'UI:GroupBy:Count+' => 'Eleman sayısı',
'UI:CountOfObjects' => 'Kritere uyan %1$d nesne bulundu.',

View File

@@ -464,6 +464,8 @@ Dict::Add('ZH CN', 'Chinese', '简体中文', array(
'UI:Error:MaintenanceTitle' => '维护',
'UI:Error:InvalidToken' => 'Error: the requested operation has already been performed (CSRF token not found)~~',
'UI:Error:SMTP:UnknownVendor' => 'OAuth SMTP provider %1$s does not exist (email_transport_smtp.oauth.provider)~~',
'UI:GroupBy:Count' => '个数',
'UI:GroupBy:Count+' => '元素数量',
'UI:CountOfObjects' => '%1$d 个对象符合指定的条件.',

View File

@@ -2,47 +2,68 @@
namespace Combodo\iTop\Core\Authentication\Client\OAuth;
use CoreException;
use Dict;
use GuzzleHttp\Client;
use League\OAuth2\Client\Token\AccessTokenInterface;
use MetaModel;
class OAuthClientProviderFactory {
class OAuthClientProviderFactory
{
/**
* @return mixed
* @throws \CoreException
*/
public static function getProviderForSMTP()
{
$sProviderVendor = MetaModel::GetConfig()->Get('email_transport_smtp.oauth.provider'); // email_transport_smtp.oauth.provider
$sProviderClass = "\Combodo\iTop\Core\Authentication\Client\OAuth\OAuthClientProvider".$sProviderVendor;
$sProviderClass = self::getProviderClass($sProviderVendor);
$aProviderVendorParams = [
'clientId' => MetaModel::GetConfig()->Get('email_transport_smtp.oauth.client_id'), // email_transport_smtp.oauth.client_id
'clientSecret' => MetaModel::GetConfig()->Get('email_transport_smtp.oauth.client_secret'),// email_transport_smtp.oauth.client_secret
'redirectUri' => $sProviderClass::GetRedirectUri(),
'scope' => $sProviderClass::GetRequiredSMTPScope()
'scope' => $sProviderClass::GetRequiredSMTPScope(),
];
$aAccessTokenParams = [
"access_token" => MetaModel::GetConfig()->Get('email_transport_smtp.oauth.access_token'), // email_transport_smtp.oauth.access_token
"refresh_token" => MetaModel::GetConfig()->Get('email_transport_smtp.oauth.refresh_token'), // email_transport_smtp.oauth.refresh_token
'scope' => $sProviderClass::GetRequiredSMTPScope()
'scope' => $sProviderClass::GetRequiredSMTPScope(),
];
$aCollaborators = [
'httpClient' => new Client(['verify' => false]),
];
return new $sProviderClass($aProviderVendorParams, $aCollaborators, $aAccessTokenParams);
}
public static function getVendorProvider($sProviderVendor, $sClientId, $sClientSecret, $sScope, $aAdditional){
/**
* @param $sProviderVendor
* @param $sClientId
* @param $sClientSecret
* @param $sScope
* @param $aAdditional
*
* @return mixed
* @throws \CoreException
*/
public static function getVendorProvider($sProviderVendor, $sClientId, $sClientSecret, $sScope, $aAdditional)
{
$sRedirectUrl = OAuthClientProviderAbstract::GetRedirectUri();
$sProviderClass = "\Combodo\iTop\Core\Authentication\Client\OAuth\OAuthClientProvider".$sProviderVendor;
$sProviderClass = self::getProviderClass($sProviderVendor);
$aCollaborators = [
'httpClient' => new Client(['verify' => false]),
];
return new $sProviderClass(array_merge(['clientId' => $sClientId, 'clientSecret' => $sClientSecret, 'redirectUri' => $sRedirectUrl, 'scope' => $sScope], $aAdditional), $aCollaborators);
}
public static function getVendorProviderForAccessUrl($sProviderVendor, $sClientId, $sClientSecret, $sScope, $aAdditional){
public static function getVendorProviderForAccessUrl($sProviderVendor, $sClientId, $sClientSecret, $sScope, $aAdditional)
{
$oProvider = static::getVendorProvider($sProviderVendor, $sClientId, $sClientSecret, $sScope, $aAdditional);
return $oProvider->GetVendorProvider()->getAuthorizationUrl([
'scope' => [
$sScope
$sScope,
],
]);
}
@@ -58,16 +79,33 @@ class OAuthClientProviderFactory {
{
return $oProvider->GetVendorProvider()->getAccessToken('authorization_code', ['code' => $sCode, 'scope' => $oProvider->GetScope()]);
}
public static function getConfFromRedirectUrl($sProviderVendor, $sClientId, $sClientSecret, $sRedirectUrlQuery)
{
$sRedirectUrl = OAuthClientProviderAbstract::GetRedirectUri();
$sProviderClass = "\Combodo\iTop\Core\Authentication\Client\OAuth\OAuthClientProvider".$sProviderVendor;
$sProviderClass = self::getProviderClass($sProviderVendor);
$aQuery = [];
parse_str($sRedirectUrlQuery, $aQuery);
$sCode = $aQuery['code'];
$oProvider = new $sProviderClass(['clientId' => $sClientId, 'clientSecret' => $sClientSecret, 'redirectUri' => $sRedirectUrl]);
return $sProviderClass::getConfFromAccessToken($oProvider->GetVendorProvider()->getAccessToken('authorization_code', ['code' => $sCode]), $sClientId, $sClientSecret);
}
/**
* @param $sProviderVendor
*
* @return string
* @throws \CoreException
*/
public static function getProviderClass($sProviderVendor): string
{
$sProviderClass = "\Combodo\iTop\Core\Authentication\Client\OAuth\OAuthClientProvider".$sProviderVendor;
if (!class_exists($sProviderClass)) {
throw new CoreException(dict::Format('UI:Error:SMTP:UnknownVendor', $sProviderVendor));
}
return $sProviderClass;
}
}