N°5102 - Allow to send emails using GSuite SMTP and OAuth - Rework

This commit is contained in:
Eric Espie
2022-06-23 12:20:53 +02:00
parent 9f60f27636
commit 54eb9d081b
29 changed files with 188 additions and 324 deletions

View File

@@ -25,19 +25,15 @@ class AjaxOauthClientController extends Controller
IssueLog::Debug("GetAuthorizationUrl for $sClass::$sId", self::LOG_CHANNEL);
$oObject = MetaModel::GetObject($sClass, $sId);
/** @var \OAuthClient $oOAuthClient */
$oOAuthClient = MetaModel::GetObject($sClass, $sId);
$aResult = ['status' => 'success', 'data' => []];
$sProvider = $oObject->Get('provider');
$sClientId = $oObject->Get('client_id');
$sClientSecret = $oObject->Get('client_secret');
$sScope = $oObject->GetScope();
$aAdditional = [];
$sAuthorizationUrl = OAuthClientProviderFactory::getVendorProviderForAccessUrl($sProvider, $sClientId, $sClientSecret, $sScope, $aAdditional);
$sAuthorizationUrl = OAuthClientProviderFactory::GetAuthorizationUrl($oOAuthClient);
$aResult['data']['authorization_url'] = $sAuthorizationUrl;
$this->DisplayJSONPage($aResult);
}
public function OperationGetDisplayAuthenticationResults()
@@ -47,14 +43,9 @@ class AjaxOauthClientController extends Controller
IssueLog::Debug("GetDisplayAuthenticationResults for $sClass::$sId", self::LOG_CHANNEL);
$oObject = MetaModel::GetObject($sClass, $sId);
$bIsCreation = empty($oObject->Get('token'));
$sProvider = $oObject->Get('provider');
$sClientId = $oObject->Get('client_id');
$sClientSecret = $oObject->Get('client_secret');
$sScope = $oObject->GetScope();
$aAdditional = [];
/** @var \OAuthClient $oOAuthClient */
$oOAuthClient = MetaModel::GetObject($sClass, $sId);
$bIsCreation = empty($oOAuthClient->Get('token'));
$sRedirectUrl = utils::ReadParam('redirect_url', '', false, 'raw');
@@ -63,12 +54,9 @@ class AjaxOauthClientController extends Controller
$aQuery = [];
parse_str($sRedirectUrlQuery, $aQuery);
$sCode = $aQuery['code'];
$oProvider = OAuthClientProviderFactory::getVendorProvider($sProvider, $sClientId, $sClientSecret, $sScope, $aAdditional);
$oAccessToken = OAuthClientProviderFactory::getAccessTokenFromCode($oProvider, $sCode);
$oAccessToken = OAuthClientProviderFactory::GetAccessTokenFromCode($oOAuthClient, $sCode);
$oObject->Set('token', $oAccessToken->getToken());
$oObject->Set('refresh_token', $oAccessToken->getRefreshToken());
$oObject->DBUpdate();
$oOAuthClient->SetAccessToken($oAccessToken);
cmdbAbstractObject::SetSessionMessage(
$sClass,

View File

@@ -4,7 +4,7 @@
* @license http://opensource.org/licenses/AGPL-3.0
*/
use Combodo\iTop\Core\Authentication\Client\OAuth\OAuthClientProviderAbstract;
use Combodo\iTop\Core\Authentication\Client\OAuth\OAuthClientProviderFactory;
class OAuthClientAzure extends OAuthClient
{
@@ -40,10 +40,10 @@ class OAuthClientAzure extends OAuthClient
MetaModel::Init_Params($aParams);
MetaModel::Init_InheritAttributes();
MetaModel::Init_AddAttribute(new AttributeEnum('scope', [
'allowed_values' => new ValueSetEnum('SMTP,IMAP'),
'allowed_values' => new ValueSetEnum('EMail'),
'display_style' => 'list',
'sql' => 'scope',
'default_value' => 'SMTP',
'default_value' => 'EMail',
'is_null_allowed' => false,
'depends_on' => [],
'always_load_in_tables' => true,
@@ -70,7 +70,7 @@ class OAuthClientAzure extends OAuthClient
public function PrefillCreationForm(&$aContextParam)
{
$this->Set('provider', 'Azure');
$this->Set('redirect_url', OAuthClientProviderAbstract::GetRedirectUri());
$this->Set('redirect_url', OAuthClientProviderFactory::GetRedirectUri());
parent::PrefillCreationForm($aContextParam);
}
@@ -90,15 +90,10 @@ class OAuthClientAzure extends OAuthClient
$this->Set('provider', 'Azure');
}
if (empty($this->Get('redirect_url'))) {
$this->Set('redirect_url', OAuthClientProviderAbstract::GetRedirectUri());
$this->Set('redirect_url', OAuthClientProviderFactory::GetRedirectUri());
}
}
public function GetDefaultMailServer()
{
return 'outlook.office365.com';
}
public function GetAttributeFlags($sAttCode, &$aReasons = array(), $sTargetState = '')
{
if ($sAttCode == 'provider' || $sAttCode == 'redirect_url') {
@@ -117,31 +112,13 @@ class OAuthClientAzure extends OAuthClient
return parent::GetInitialStateAttributeFlags($sAttCode, $aReasons);
}
public function GetDefaultMailServer()
{
return 'outlook.office365.com';
}
public function GetScope()
{
$sScope = $this->Get('scope');
if ($sScope == 'IMAP') {
return 'https://outlook.office.com/IMAP.AccessAsUser.All offline_access';
}
// default is smtp
return 'https://outlook.office.com/SMTP.Send offline_access';
}
public function AfterInsert()
{
parent::AfterInsert();
$sClass = get_class($this);
$sId = $this->GetKey();
cmdbAbstractObject::SetSessionMessage(
$sClass,
$sId,
"$sClass:$sId:OAuthClientCreated",
Dict::S('itop-oauth-client:Message:OAuthClientCreated'),
'info',
100,
true
);
return 'https://outlook.office.com/IMAP.AccessAsUser.All https://outlook.office.com/SMTP.Send offline_access';
}
}

View File

@@ -4,7 +4,7 @@
* @license http://opensource.org/licenses/AGPL-3.0
*/
use Combodo\iTop\Core\Authentication\Client\OAuth\OAuthClientProviderAbstract;
use Combodo\iTop\Core\Authentication\Client\OAuth\OAuthClientProviderFactory;
class OAuthClientGoogle extends OAuthClient
{
@@ -72,16 +72,11 @@ class OAuthClientGoogle extends OAuthClient
{
$this->Set('provider', 'Google');
$this->Set('scope', 'EMail');
$this->Set('redirect_url', OAuthClientProviderAbstract::GetRedirectUri());
$this->Set('redirect_url', OAuthClientProviderFactory::GetRedirectUri());
parent::PrefillCreationForm($aContextParam);
}
public function GetDefaultMailServer()
{
return 'imap.gmail.com';
}
public function GetAttributeFlags($sAttCode, &$aReasons = array(), $sTargetState = '')
{
if ($sAttCode == 'provider' || $sAttCode == 'scope' || $sAttCode == 'redirect_url') {
@@ -115,31 +110,21 @@ class OAuthClientGoogle extends OAuthClient
$this->Set('provider', 'Google');
}
if (empty($this->Get('redirect_url'))) {
$this->Set('redirect_url', OAuthClientProviderAbstract::GetRedirectUri());
$this->Set('redirect_url', OAuthClientProviderFactory::GetRedirectUri());
}
if (empty($this->Get('scope'))) {
$this->Set('scope', 'EMail');
}
}
public function GetDefaultMailServer()
{
return 'imap.gmail.com';
}
public function GetScope()
{
return 'https://mail.google.com/';
}
public function AfterInsert()
{
parent::AfterInsert();
$sClass = get_class($this);
$sId = $this->GetKey();
cmdbAbstractObject::SetSessionMessage(
$sClass,
$sId,
"$sClass:$sId:OAuthClientCreated",
Dict::S('itop-oauth-client:Message:OAuthClientCreated'),
'info',
100,
true
);
}
}

View File

@@ -1,30 +0,0 @@
<?php
/**
* @copyright Copyright (C) 2010-2022 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
namespace Combodo\iTop\OAuthClient\Service;
use AbstractApplicationObjectExtension;
use Exception;
use OAuthClient;
class ApplicationObjectExtension extends AbstractApplicationObjectExtension
{
public function OnDBInsert($oObject, $oChange = null)
{
if ($oObject instanceof OAuthClient) {
try {
// Ask for tokens the first time
//$response = utils::DoPostRequest($sRestUrl, $aPostedData);
} catch (Exception $e) {
}
}
parent::OnDBInsert($oObject, $oChange); // TODO: Change the autogenerated stub
}
}

View File

@@ -7,7 +7,7 @@
namespace Combodo\iTop\OAuthClient\Service;
use ApplicationContext;
use Combodo\iTop\Core\Authentication\Client\OAuth\OAuthClientProviderAbstract;
use Combodo\iTop\Core\Authentication\Client\OAuth\OAuthClientProviderFactory;
use Dict;
use iPopupMenuExtension;
use JSPopupMenuItem;
@@ -42,7 +42,7 @@ class PopupMenuExtension implements \iPopupMenuExtension
$sAjaxUri = utils::GetAbsoluteUrlModulePage(static::MODULE_CODE, 'ajax.php');
// Add a new menu item that triggers a custom JS function defined in our own javascript file: js/sample.js
$sJSFileUrl = utils::GetAbsoluteUrlModulesRoot().static::MODULE_CODE.'/assets/js/oauth_connect.js';
$sRedirectUri = OAuthClientProviderAbstract::GetRedirectUri();
$sRedirectUri = OAuthClientProviderFactory::GetRedirectUri();
$aResult[] = new JSPopupMenuItem(
$sMenu.' from '.$sObjClass,
Dict::S($sMenu),
@@ -52,7 +52,7 @@ class PopupMenuExtension implements \iPopupMenuExtension
if ($bHasToken) {
$sScope = $oObj->Get('scope');
if ($sScope == 'IMAP' || $sScope == 'EMail') {
if ($sScope == 'EMail') {
$aParams = $oAppContext->GetAsHash();
$sMenu = 'Menu:CreateMailbox';
$sObjClass = get_class($oObj);