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

@@ -18,7 +18,7 @@ Dict::Add('CS CZ', 'Czech', 'Čeština', [
'itop-oauth-client:UsedForSMTP' => 'This OAuth client is used for SMTP~~',
'itop-oauth-client:TestSMTP' => 'Email send test~~',
'itop-oauth-client:MissingOAuthClient' => 'Missing Oauth client for user name %1$s~~',
'itop-oauth-client:Message:OAuthClientCreated' => 'Generate access tokens before using this OAuth client~~',
'itop-oauth-client:Message:MissingToken' => 'Generate access token before using this OAuth client~~',
'itop-oauth-client:Message:TokenCreated' => 'Access token created~~',
'itop-oauth-client:Message:TokenRecreated' => 'Access token regenerated~~',
]);

View File

@@ -18,7 +18,7 @@ Dict::Add('DA DA', 'Danish', 'Dansk', [
'itop-oauth-client:UsedForSMTP' => 'This OAuth client is used for SMTP~~',
'itop-oauth-client:TestSMTP' => 'Email send test~~',
'itop-oauth-client:MissingOAuthClient' => 'Missing Oauth client for user name %1$s~~',
'itop-oauth-client:Message:OAuthClientCreated' => 'Generate access tokens before using this OAuth client~~',
'itop-oauth-client:Message:MissingToken' => 'Generate access token before using this OAuth client~~',
'itop-oauth-client:Message:TokenCreated' => 'Access token created~~',
'itop-oauth-client:Message:TokenRecreated' => 'Access token regenerated~~',
]);

View File

@@ -101,7 +101,9 @@
if (!$bEditMode) {
$oConfig = utils::GetConfig();
$sScope = $this->Get('scope');
if (($sScope == 'SMTP' || $sScope == 'EMail') && $oConfig->Get('email_transport_smtp.username') == $this->Get('name')) {
if (empty($this->Get('token'))) {
$oPage->p('<b>'.Dict::S('itop-oauth-client:Message:MissingToken').'</b>');
} elseif (($sScope == 'SMTP' || $sScope == 'EMail') && $oConfig->Get('email_transport_smtp.username') == $this->Get('name')) {
$sLabel = Dict::S('itop-oauth-client:UsedForSMTP');
$sTestLabel = Dict::S('itop-oauth-client:TestSMTP');
$sTestURL = utils::GetAbsoluteUrlAppRoot().'setup/email.test.php';
@@ -131,6 +133,36 @@
}
]]></code>
</method>
<method id="GetAccessToken">
<static>false</static>
<access>public</access>
<code><![CDATA[
public function GetAccessToken()
{
return new \League\OAuth2\Client\Token\AccessToken([
'access_token' => $this->Get('token'),
'expires_in' => date_format(new DateTime($this->Get('token_expiration')), 'U') - time(),
'refresh_token' => $this->Get('refresh_token'),
'token_type' => 'Bearer',
]);
}
]]></code>
</method>
<method id="SetAccessToken">
<static>false</static>
<access>public</access>
<code><![CDATA[
public function SetAccessToken(\League\OAuth2\Client\Token\AccessTokenInterface $oAccessToken)
{
$this->Set('token', $oAccessToken->getToken());
$this->Set('token_expiration', date(AttributeDateTime::GetSQLFormat(), $oAccessToken->getExpires()));
if (!empty($oAccessToken->getRefreshToken())) {
$this->Set('refresh_token', $oAccessToken->getRefreshToken());
}
$this->DBUpdate();
}
]]></code>
</method>
</methods>
<presentation>
<details>

View File

@@ -10,15 +10,15 @@ Dict::Add('DE DE', 'German', 'Deutsch', [
'Menu:CreateMailbox' => 'Create a mailbox...~~',
'Menu:OAuthClient' => 'OAuth Client~~',
'Menu:OAuthClient+' => '~~',
'Menu:GenerateTokens' => 'Generate access tokens...~~',
'Menu:RegenerateTokens' => 'Regenerate access tokens...~~',
'Menu:GenerateTokens' => 'Generate access token...~~',
'Menu:RegenerateTokens' => 'Regenerate access token...~~',
'itop-oauth-client/Operation:CreateMailBox/Title' => 'Mailbox creation~~',
'itop-oauth-client:UsedForSMTP' => 'This OAuth client is used for SMTP~~',
'itop-oauth-client:TestSMTP' => 'Email send test~~',
'itop-oauth-client:MissingOAuthClient' => 'Missing Oauth client for user name %1$s~~',
'itop-oauth-client:Message:OAuthClientCreated' => 'Generate access tokens before using this OAuth client~~',
'itop-oauth-client:Message:MissingToken' => 'Generate access token before using this OAuth client~~',
'itop-oauth-client:Message:TokenCreated' => 'Access token created~~',
'itop-oauth-client:Message:TokenRecreated' => 'Access token regenerated~~',
]);

View File

@@ -10,15 +10,15 @@ Dict::Add('EN US', 'English', 'English', [
'Menu:CreateMailbox' => 'Create a mailbox...',
'Menu:OAuthClient' => 'OAuth Client',
'Menu:OAuthClient+' => '',
'Menu:GenerateTokens' => 'Generate access tokens...',
'Menu:RegenerateTokens' => 'Regenerate access tokens...',
'Menu:GenerateTokens' => 'Generate access token...',
'Menu:RegenerateTokens' => 'Regenerate access token...',
'itop-oauth-client/Operation:CreateMailBox/Title' => 'Mailbox creation',
'itop-oauth-client:UsedForSMTP' => 'This OAuth client is used for SMTP',
'itop-oauth-client:TestSMTP' => 'Email send test',
'itop-oauth-client:MissingOAuthClient' => 'Missing Oauth client for user name %1$s',
'itop-oauth-client:Message:OAuthClientCreated' => 'Generate access tokens before using this OAuth client',
'itop-oauth-client:Message:MissingToken' => 'Generate access token before using this OAuth client',
'itop-oauth-client:Message:TokenCreated' => 'Access token created',
'itop-oauth-client:Message:TokenRecreated' => 'Access token regenerated',
]);

View File

@@ -18,7 +18,7 @@ Dict::Add('ES CR', 'Spanish', 'Español, Castellaño', [
'itop-oauth-client:UsedForSMTP' => 'This OAuth client is used for SMTP~~',
'itop-oauth-client:TestSMTP' => 'Email send test~~',
'itop-oauth-client:MissingOAuthClient' => 'Missing Oauth client for user name %1$s~~',
'itop-oauth-client:Message:OAuthClientCreated' => 'Generate access tokens before using this OAuth client~~',
'itop-oauth-client:Message:MissingToken' => 'Generate access token before using this OAuth client~~',
'itop-oauth-client:Message:TokenCreated' => 'Access token created~~',
'itop-oauth-client:Message:TokenRecreated' => 'Access token regenerated~~',
]);

View File

@@ -17,7 +17,7 @@ Dict::Add('FR FR', 'French', 'Français', [
'itop-oauth-client:UsedForSMTP' => 'Ce client Oauth est utilisé pour SMTP',
'itop-oauth-client:TestSMTP' => 'Tester l\'envoi de mail',
'itop-oauth-client:MissingOAuthClient' => 'Il n\'y a pas de client OAuth pour l\'utilisateur %1$s',
'itop-oauth-client:Message:OAuthClientCreated' => 'Générez les jetons d\'accès avant d\'utiliser cd client OAuth',
'itop-oauth-client:Message:MissingToken' => 'Générez le jeton d\'accès avant d\'utiliser ce client OAuth',
'itop-oauth-client:Message:TokenCreated' => 'Le jeton d\'accès à été créé',
'itop-oauth-client:Message:TokenRecreated' => 'Le jeton d\'accès à été renouvelé',
]);

View File

@@ -18,7 +18,7 @@ Dict::Add('HU HU', 'Hungarian', 'Magyar', [
'itop-oauth-client:UsedForSMTP' => 'This OAuth client is used for SMTP~~',
'itop-oauth-client:TestSMTP' => 'Email send test~~',
'itop-oauth-client:MissingOAuthClient' => 'Missing Oauth client for user name %1$s~~',
'itop-oauth-client:Message:OAuthClientCreated' => 'Generate access tokens before using this OAuth client~~',
'itop-oauth-client:Message:MissingToken' => 'Generate access token before using this OAuth client~~',
'itop-oauth-client:Message:TokenCreated' => 'Access token created~~',
'itop-oauth-client:Message:TokenRecreated' => 'Access token regenerated~~',
]);

View File

@@ -18,7 +18,7 @@ Dict::Add('IT IT', 'Italian', 'Italiano', [
'itop-oauth-client:UsedForSMTP' => 'This OAuth client is used for SMTP~~',
'itop-oauth-client:TestSMTP' => 'Email send test~~',
'itop-oauth-client:MissingOAuthClient' => 'Missing Oauth client for user name %1$s~~',
'itop-oauth-client:Message:OAuthClientCreated' => 'Generate access tokens before using this OAuth client~~',
'itop-oauth-client:Message:MissingToken' => 'Generate access token before using this OAuth client~~',
'itop-oauth-client:Message:TokenCreated' => 'Access token created~~',
'itop-oauth-client:Message:TokenRecreated' => 'Access token regenerated~~',
]);

View File

@@ -18,7 +18,7 @@ Dict::Add('JA JP', 'Japanese', '日本語', [
'itop-oauth-client:UsedForSMTP' => 'This OAuth client is used for SMTP~~',
'itop-oauth-client:TestSMTP' => 'Email send test~~',
'itop-oauth-client:MissingOAuthClient' => 'Missing Oauth client for user name %1$s~~',
'itop-oauth-client:Message:OAuthClientCreated' => 'Generate access tokens before using this OAuth client~~',
'itop-oauth-client:Message:MissingToken' => 'Generate access token before using this OAuth client~~',
'itop-oauth-client:Message:TokenCreated' => 'Access token created~~',
'itop-oauth-client:Message:TokenRecreated' => 'Access token regenerated~~',
]);

View File

@@ -28,7 +28,6 @@ SetupWebPage::AddModule(
'src/Model/OAuthClientGoogle.php',
'src/Model/OAuthClientAzure.php',
'src/Service/PopupMenuExtension.php',
'src/Service/ApplicationObjectExtension.php',
),
'webservice' => array(

View File

@@ -18,7 +18,7 @@ Dict::Add('NL NL', 'Dutch', 'Nederlands', [
'itop-oauth-client:UsedForSMTP' => 'This OAuth client is used for SMTP~~',
'itop-oauth-client:TestSMTP' => 'Email send test~~',
'itop-oauth-client:MissingOAuthClient' => 'Missing Oauth client for user name %1$s~~',
'itop-oauth-client:Message:OAuthClientCreated' => 'Generate access tokens before using this OAuth client~~',
'itop-oauth-client:Message:MissingToken' => 'Generate access token before using this OAuth client~~',
'itop-oauth-client:Message:TokenCreated' => 'Access token created~~',
'itop-oauth-client:Message:TokenRecreated' => 'Access token regenerated~~',
]);

View File

@@ -18,7 +18,7 @@ Dict::Add('PT BR', 'Brazilian', 'Brazilian', [
'itop-oauth-client:UsedForSMTP' => 'This OAuth client is used for SMTP~~',
'itop-oauth-client:TestSMTP' => 'Email send test~~',
'itop-oauth-client:MissingOAuthClient' => 'Missing Oauth client for user name %1$s~~',
'itop-oauth-client:Message:OAuthClientCreated' => 'Generate access tokens before using this OAuth client~~',
'itop-oauth-client:Message:MissingToken' => 'Generate access token before using this OAuth client~~',
'itop-oauth-client:Message:TokenCreated' => 'Access token created~~',
'itop-oauth-client:Message:TokenRecreated' => 'Access token regenerated~~',
]);

View File

@@ -18,7 +18,7 @@ Dict::Add('RU RU', 'Russian', 'Русский', [
'itop-oauth-client:UsedForSMTP' => 'This OAuth client is used for SMTP~~',
'itop-oauth-client:TestSMTP' => 'Email send test~~',
'itop-oauth-client:MissingOAuthClient' => 'Missing Oauth client for user name %1$s~~',
'itop-oauth-client:Message:OAuthClientCreated' => 'Generate access tokens before using this OAuth client~~',
'itop-oauth-client:Message:MissingToken' => 'Generate access token before using this OAuth client~~',
'itop-oauth-client:Message:TokenCreated' => 'Access token created~~',
'itop-oauth-client:Message:TokenRecreated' => 'Access token regenerated~~',
]);

View File

@@ -18,7 +18,7 @@ Dict::Add('SK SK', 'Slovak', 'Slovenčina', [
'itop-oauth-client:UsedForSMTP' => 'This OAuth client is used for SMTP~~',
'itop-oauth-client:TestSMTP' => 'Email send test~~',
'itop-oauth-client:MissingOAuthClient' => 'Missing Oauth client for user name %1$s~~',
'itop-oauth-client:Message:OAuthClientCreated' => 'Generate access tokens before using this OAuth client~~',
'itop-oauth-client:Message:MissingToken' => 'Generate access token before using this OAuth client~~',
'itop-oauth-client:Message:TokenCreated' => 'Access token created~~',
'itop-oauth-client:Message:TokenRecreated' => 'Access token regenerated~~',
]);

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);

View File

@@ -18,7 +18,7 @@ Dict::Add('TR TR', 'Turkish', 'Türkçe', [
'itop-oauth-client:UsedForSMTP' => 'This OAuth client is used for SMTP~~',
'itop-oauth-client:TestSMTP' => 'Email send test~~',
'itop-oauth-client:MissingOAuthClient' => 'Missing Oauth client for user name %1$s~~',
'itop-oauth-client:Message:OAuthClientCreated' => 'Generate access tokens before using this OAuth client~~',
'itop-oauth-client:Message:MissingToken' => 'Generate access token before using this OAuth client~~',
'itop-oauth-client:Message:TokenCreated' => 'Access token created~~',
'itop-oauth-client:Message:TokenRecreated' => 'Access token regenerated~~',
]);

View File

@@ -8,7 +8,6 @@ $baseDir = dirname($vendorDir);
return array(
'Combodo\\iTop\\OAuthClient\\Controller\\AjaxOauthClientController' => $baseDir . '/src/Controller/AjaxOauthClientController.php',
'Combodo\\iTop\\OAuthClient\\Controller\\OAuthClientController' => $baseDir . '/src/Controller/OAuthClientController.php',
'Combodo\\iTop\\OAuthClient\\Service\\ApplicationObjectExtension' => $baseDir . '/src/Service/ApplicationObjectExtension.php',
'Combodo\\iTop\\OAuthClient\\Service\\PopupMenuExtension' => $baseDir . '/src/Service/PopupMenuExtension.php',
'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php',
);

View File

@@ -23,7 +23,6 @@ class ComposerStaticInitd52424b43ff18219f2ec935428aff074
public static $classMap = array (
'Combodo\\iTop\\OAuthClient\\Controller\\AjaxOauthClientController' => __DIR__ . '/../..' . '/src/Controller/AjaxOauthClientController.php',
'Combodo\\iTop\\OAuthClient\\Controller\\OAuthClientController' => __DIR__ . '/../..' . '/src/Controller/OAuthClientController.php',
'Combodo\\iTop\\OAuthClient\\Service\\ApplicationObjectExtension' => __DIR__ . '/../..' . '/src/Service/ApplicationObjectExtension.php',
'Combodo\\iTop\\OAuthClient\\Service\\PopupMenuExtension' => __DIR__ . '/../..' . '/src/Service/PopupMenuExtension.php',
'Composer\\InstalledVersions' => __DIR__ . '/..' . '/composer/InstalledVersions.php',
);

View File

@@ -18,7 +18,7 @@ Dict::Add('ZH CN', 'Chinese', '简体中文', [
'itop-oauth-client:UsedForSMTP' => 'This OAuth client is used for SMTP~~',
'itop-oauth-client:TestSMTP' => 'Email send test~~',
'itop-oauth-client:MissingOAuthClient' => 'Missing Oauth client for user name %1$s~~',
'itop-oauth-client:Message:OAuthClientCreated' => 'Generate access tokens before using this OAuth client~~',
'itop-oauth-client:Message:MissingToken' => 'Generate access token before using this OAuth client~~',
'itop-oauth-client:Message:TokenCreated' => 'Access token created~~',
'itop-oauth-client:Message:TokenRecreated' => 'Access token regenerated~~',
]);

View File

@@ -4,27 +4,30 @@ namespace Combodo\iTop\Core\Authentication\Client\OAuth;
use League\OAuth2\Client\Provider\GenericProvider;
use League\OAuth2\Client\Token\AccessToken;
use utils;
use OAuthClient;
abstract class OAuthClientProviderAbstract implements IOAuthClientProvider
{
/** @var string */
static protected $sVendorName = '';
/** @var array */
static protected $sVendorColors = ['', '', '', ''];
/** @var string */
static protected $sVendorIcon = '';
static protected $sRedirectUri = '';
static protected $sRequiredSMTPScope = '';
static protected $sRequiredIMAPScope = '';
static protected $sRequiredPOPScope = '';
// /** @var string */
// static protected $sVendorName = '';
// /** @var array */
// static protected $sVendorColors = ['', '', '', ''];
// /** @var string */
// static protected $sVendorIcon = '';
// static protected $sRequiredSMTPScope = '';
// static protected $sRequiredIMAPScope = '';
// static protected $sRequiredPOPScope = '';
/** @var \League\OAuth2\Client\Provider\GenericProvider */
protected $oVendorProvider;
/** @var \League\OAuth2\Client\Token\AccessToken */
protected $oAccessToken;
protected $sScope;
/** @var OAuthClient */
protected $oOauthClient;
public function __construct($oOauthClient)
{
$this->oOauthClient = $oOauthClient;
}
/**
* @return \League\OAuth2\Client\Provider\GenericProvider
@@ -47,7 +50,7 @@ abstract class OAuthClientProviderAbstract implements IOAuthClientProvider
*/
public function GetAccessToken(): AccessToken
{
return $this->oAccessToken;
return $this->oOauthClient->GetAccessToken();
}
/**
@@ -55,60 +58,7 @@ abstract class OAuthClientProviderAbstract implements IOAuthClientProvider
*/
public function SetAccessToken(AccessToken $oAccessToken)
{
$this->oAccessToken = $oAccessToken;
}
/**
* @return string
*/
public static function GetVendorName(): string
{
return static::$sVendorName;
}
/**
* @return void
* @throws \Exception
*/
public static function InitizalizeRedirectUri()
{
static::$sRedirectUri = utils::GetAbsoluteUrlAppRoot().'pages/oauth.landing.php';
}
/**
* @return string
*/
public static function GetRedirectUri(): string
{
if (static::$sRedirectUri === '') {
static::InitizalizeRedirectUri();
}
return static::$sRedirectUri;
}
/**
* @return string
*/
public static function GetRequiredSMTPScope(): string
{
return static::$sRequiredSMTPScope;
}
/**
* @return string
*/
public static function GetRequiredIMAPScope(): string
{
return static::$sRequiredIMAPScope;
}
/**
* @return string
*/
public static function GetRequiredPOPScope(): string
{
return static::$sRequiredPOPScope;
$this->oOauthClient->SetAccessToken($oAccessToken);
}
/**
@@ -116,15 +66,7 @@ abstract class OAuthClientProviderAbstract implements IOAuthClientProvider
*/
public function GetScope()
{
return $this->sScope;
}
/**
* @param mixed $sScope
*/
public function SetScope($sScope)
{
$this->sScope = $sScope;
return $this->oOauthClient->GetScope();
}
}

View File

@@ -2,44 +2,34 @@
namespace Combodo\iTop\Core\Authentication\Client\OAuth;
use League\OAuth2\Client\Token\AccessToken;
use TheNetworg\OAuth2\Client\Provider\Azure;
class OAuthClientProviderAzure extends OAuthClientProviderAbstract
{
/** @var string */
static protected $sVendorName = 'Azure';
/** @var array */
static protected $sVendorColors = ['#0766b7', '#0d396b', '#2893df', '#3ccbf4'];
/** @var string */
static protected $sVendorIcon = '../images/icons/icons8-azure.svg';
static protected $sRequiredSMTPScope = 'https://outlook.office.com/SMTP.Send offline_access';
static protected $sRequiredIMAPScope = 'https://outlook.office.com/IMAP.AccessAsUser.All offline_access';
static protected $sRequiredPOPScope = 'https://outlook.office.com/POP.AccessAsUser.All offline access';
// /** @var string */
// static protected $sVendorName = 'Azure';
// /** @var array */
// static protected $sVendorColors = ['#0766b7', '#0d396b', '#2893df', '#3ccbf4'];
// /** @var string */
// static protected $sVendorIcon = '../images/icons/icons8-azure.svg';
// static protected $sRequiredSMTPScope = 'https://outlook.office.com/SMTP.Send offline_access';
// static protected $sRequiredIMAPScope = 'https://outlook.office.com/IMAP.AccessAsUser.All offline_access';
// static protected $sRequiredPOPScope = 'https://outlook.office.com/POP.AccessAsUser.All offline access';
/** @var \League\OAuth2\Client\Token\AccessToken */
protected $oAccessToken;
public function __construct($aVendorProvider, array $collaborators = [], array $aAccessTokenParams = [])
public function __construct($oOAuthClient, array $collaborators = [])
{
$this->oVendorProvider = new Azure(array_merge([
parent::__construct($oOAuthClient);
$aOptions = [
'prompt' => 'consent',
'scope' => 'offline_access',
'defaultEndPointVersion' => Azure::ENDPOINT_VERSION_2_0,
],
$aVendorProvider), $collaborators);
'clientId' => $oOAuthClient->Get('client_id'),
'clientSecret' => $oOAuthClient->Get('client_secret'),
'redirectUri' => $oOAuthClient->Get('redirect_url'),
];
if (!empty($aAccessTokenParams)) {
$this->oAccessToken = new AccessToken([
"access_token" => $aAccessTokenParams["access_token"],
"expires_in" => -1,
"refresh_token" => $aAccessTokenParams["refresh_token"],
"token_type" => "Bearer",
]);
}
if (isset($aVendorProvider['scope'])) {
$this->SetScope($aVendorProvider['scope']);
}
$this->oVendorProvider = new Azure($aOptions, $collaborators);
}
}

View File

@@ -3,12 +3,15 @@
namespace Combodo\iTop\Core\Authentication\Client\OAuth;
use CoreException;
use DBObject;
use DBObjectSet;
use DBSearch;
use Dict;
use GuzzleHttp\Client;
use League\OAuth2\Client\Token\AccessTokenInterface;
use MetaModel;
use OAuthClient;
use utils;
class OAuthClientProviderFactory
{
@@ -16,28 +19,11 @@ class OAuthClientProviderFactory
* @return mixed
* @throws \CoreException
*/
public static function getProviderForSMTP()
public static function GetProviderForSMTP()
{
$oOAuthClient = self::GetOAuthClientForSMTP();
$sProviderVendor = $oOAuthClient->Get('provider');
$sProviderClass = self::getProviderClass($sProviderVendor);
$aProviderVendorParams = [
'clientId' => $oOAuthClient->Get('client_id'),
'clientSecret' => $oOAuthClient->Get('client_secret'),
'redirectUri' => $sProviderClass::GetRedirectUri(),
'scope' => $sProviderClass::GetRequiredSMTPScope(),
];
$aAccessTokenParams = [
"access_token" => $oOAuthClient->Get('token'),
"refresh_token" => $oOAuthClient->Get('refresh_token'),
'scope' => $sProviderClass::GetRequiredSMTPScope(),
];
$aCollaborators = [
'httpClient' => new Client(['verify' => false]),
];
return new $sProviderClass($aProviderVendorParams, $aCollaborators, $aAccessTokenParams);
return self::GetClientProvider($oOAuthClient);
}
/**
@@ -58,7 +44,7 @@ class OAuthClientProviderFactory
}
while ($oOAuthClient = $oSet->Fetch()) {
$sScope = $oOAuthClient->Get('scope');
if ($sScope == 'SMTP' || $sScope == 'EMail') {
if ($sScope == 'EMail') {
return $oOAuthClient;
}
}
@@ -66,75 +52,77 @@ class OAuthClientProviderFactory
}
/**
* @param $sProviderVendor
* @param $sClientId
* @param $sClientSecret
* @param $sScope
* @param $aAdditional
* @param \OAuthClient $oOAuthClient
*
* @return mixed
* @throws \ArchivedObjectException
* @throws \CoreException
*/
public static function getVendorProvider($sProviderVendor, $sClientId, $sClientSecret, $sScope, $aAdditional)
public static function GetAuthorizationUrl(OAuthClient $oOAuthClient)
{
$sRedirectUrl = OAuthClientProviderAbstract::GetRedirectUri();
$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)
{
$oProvider = static::getVendorProvider($sProviderVendor, $sClientId, $sClientSecret, $sScope, $aAdditional);
$oProvider = self::GetClientProvider($oOAuthClient);
return $oProvider->GetVendorProvider()->getAuthorizationUrl([
'scope' => [
$sScope,
$oProvider->GetScope(),
],
]);
}
/**
* @param \Combodo\iTop\Core\Authentication\Client\OAuth\OAuthClientProviderAbstract $oProvider
* @param \OAuthClient $oOAuthClient
* @param $sCode
*
* @return AccessTokenInterface
* @throws \League\OAuth2\Client\Provider\Exception\IdentityProviderException
* @throws \ArchivedObjectException
* @throws \CoreException
*/
public static function getAccessTokenFromCode($oProvider, $sCode)
public static function GetAccessTokenFromCode(OAuthClient $oOAuthClient, $sCode)
{
$oProvider = self::GetClientProvider($oOAuthClient);
return $oProvider->GetVendorProvider()->getAccessToken('authorization_code', ['code' => $sCode, 'scope' => $oProvider->GetScope()]);
}
public static function getConfFromRedirectUrl($sProviderVendor, $sClientId, $sClientSecret, $sRedirectUrlQuery)
{
$sRedirectUrl = OAuthClientProviderAbstract::GetRedirectUri();
$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
protected 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));
throw new CoreException(Dict::Format('UI:Error:SMTP:UnknownVendor', $sProviderVendor));
}
return $sProviderClass;
}
/**
* @return string
* @throws \Exception
*/
public static function GetRedirectUri(): string
{
return utils::GetAbsoluteUrlAppRoot().'pages/oauth.landing.php';
}
/**
* @param \DBObject $oOAuthClient
*
* @return mixed
* @throws \ArchivedObjectException
* @throws \CoreException
*/
public static function GetClientProvider(DBObject $oOAuthClient)
{
$sProviderVendor = $oOAuthClient->Get('provider');
$sProviderClass = self::GetProviderClass($sProviderVendor);
$aCollaborators = [
'httpClient' => new Client(['verify' => false]),
];
return new $sProviderClass($oOAuthClient, $aCollaborators);
}
}

View File

@@ -3,38 +3,33 @@
namespace Combodo\iTop\Core\Authentication\Client\OAuth;
use League\OAuth2\Client\Provider\Google;
use League\OAuth2\Client\Token\AccessToken;
class OAuthClientProviderGoogle extends OAuthClientProviderAbstract
{
/** @var string */
static protected $sVendorName = 'Google';
/** @var array */
static protected $sVendorColors = ['#DB4437', '#F4B400', '#0F9D58', '#4285F4'];
/** @var string */
static protected $sVendorIcon = '../images/icons/icons8-google.svg';
// /** @var string */
// static protected $sVendorName = 'Google';
// /** @var array */
// static protected $sVendorColors = ['#DB4437', '#F4B400', '#0F9D58', '#4285F4'];
// /** @var string */
// static protected $sVendorIcon = '../images/icons/icons8-google.svg';
//
// static protected $sRequiredSMTPScope = 'https://mail.google.com/';
// static protected $sRequiredIMAPScope = 'https://mail.google.com/';
// static protected $sRequiredPOPScope = 'https://mail.google.com/';
/** @var \League\OAuth2\Client\Token\AccessToken */
protected $oAccessToken;
static protected $sRequiredSMTPScope = 'https://mail.google.com/';
static protected $sRequiredIMAPScope = 'https://mail.google.com/';
static protected $sRequiredPOPScope = 'https://mail.google.com/';
public function __construct($aVendorProvider, array $collaborators = [], array $aAccessTokenParams = [])
public function __construct($oOAuthClient, array $collaborators = [])
{
$this->oVendorProvider = new Google(array_merge(['prompt' => 'consent', 'accessType' => 'offline'], $aVendorProvider), $collaborators);
parent::__construct($oOAuthClient);
$aOptions = [
'prompt' => 'consent',
'accessType' => 'offline',
'clientId' => $oOAuthClient->Get('client_id'),
'clientSecret' => $oOAuthClient->Get('client_secret'),
'redirectUri' => $oOAuthClient->Get('redirect_url'),
'scope' => $oOAuthClient->GetScope(),
if (!empty($aAccessTokenParams)) {
$this->oAccessToken = new AccessToken([
"access_token" => $aAccessTokenParams["access_token"],
"expires_in" => -1,
"refresh_token" => $aAccessTokenParams["refresh_token"],
"token_type" => "Bearer",
]);
}
];
$this->oVendorProvider = new Google($aOptions, $collaborators);
if (isset($aVendorProvider['scope'])) {
$this->SetScope($aVendorProvider['scope']);
}
}
}

View File

@@ -211,7 +211,7 @@ class EMailLaminas extends Email
$oOptions = new SmtpOptions($aOptions);
$oTransport->setOptions($oOptions);
\Laminas\Mail\Protocol\Smtp\Auth\Oauth::setProvider(OAuthClientProviderFactory::getProviderForSMTP());
\Laminas\Mail\Protocol\Smtp\Auth\Oauth::setProvider(OAuthClientProviderFactory::GetProviderForSMTP());
break;
case 'Null':