Initial commit
This commit is contained in:
61
application/Espo/Tools/OAuth/Api/DeleteConnection.php
Normal file
61
application/Espo/Tools/OAuth/Api/DeleteConnection.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\OAuth\Api;
|
||||
|
||||
use Espo\Core\Api\Action;
|
||||
use Espo\Core\Api\Request;
|
||||
use Espo\Core\Api\Response;
|
||||
use Espo\Core\Api\ResponseComposer;
|
||||
use Espo\Core\Exceptions\BadRequest;
|
||||
use Espo\Core\Record\EntityProvider;
|
||||
use Espo\Entities\OAuthAccount;
|
||||
use Espo\Tools\OAuth\ConnectionService;
|
||||
|
||||
/**
|
||||
* @noinspection PhpUnused
|
||||
*/
|
||||
class DeleteConnection implements Action
|
||||
{
|
||||
public function __construct(
|
||||
private ConnectionService $service,
|
||||
private EntityProvider $entityProvider,
|
||||
) {}
|
||||
|
||||
public function process(Request $request): Response
|
||||
{
|
||||
$id = $request->getRouteParam('id') ?? throw new BadRequest();
|
||||
|
||||
$account = $this->entityProvider->getByClass(OAuthAccount::class, $id);
|
||||
|
||||
$this->service->disconnect($account);
|
||||
|
||||
return ResponseComposer::json(true);
|
||||
}
|
||||
}
|
||||
66
application/Espo/Tools/OAuth/Api/PostConnection.php
Normal file
66
application/Espo/Tools/OAuth/Api/PostConnection.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\OAuth\Api;
|
||||
|
||||
use Espo\Core\Api\Action;
|
||||
use Espo\Core\Api\Request;
|
||||
use Espo\Core\Api\Response;
|
||||
use Espo\Core\Api\ResponseComposer;
|
||||
use Espo\Core\Exceptions\BadRequest;
|
||||
use Espo\Core\Record\EntityProvider;
|
||||
use Espo\Entities\OAuthAccount;
|
||||
use Espo\Tools\OAuth\ConnectionService;
|
||||
|
||||
/**
|
||||
* @noinspection PhpUnused
|
||||
*/
|
||||
class PostConnection implements Action
|
||||
{
|
||||
public function __construct(
|
||||
private ConnectionService $service,
|
||||
private EntityProvider $entityProvider,
|
||||
) {}
|
||||
|
||||
public function process(Request $request): Response
|
||||
{
|
||||
$id = $request->getRouteParam('id') ?? throw new BadRequest();
|
||||
$code = $request->getParsedBody()->code ?? null;
|
||||
|
||||
if (!is_string($code)) {
|
||||
throw new BadRequest("No code.");
|
||||
}
|
||||
|
||||
$account = $this->entityProvider->getByClass(OAuthAccount::class, $id);
|
||||
|
||||
$this->service->connect($account, $code);
|
||||
|
||||
return ResponseComposer::json(true);
|
||||
}
|
||||
}
|
||||
44
application/Espo/Tools/OAuth/ConfigDataProvider.php
Normal file
44
application/Espo/Tools/OAuth/ConfigDataProvider.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\OAuth;
|
||||
|
||||
use Espo\Core\Utils\Config\ApplicationConfig;
|
||||
|
||||
class ConfigDataProvider
|
||||
{
|
||||
public function __construct(
|
||||
private ApplicationConfig $config,
|
||||
) {}
|
||||
|
||||
public function getRedirectUri(): string
|
||||
{
|
||||
return $this->config->getSiteUrl() . '/oauth/callback';
|
||||
}
|
||||
}
|
||||
82
application/Espo/Tools/OAuth/ConnectionService.php
Normal file
82
application/Espo/Tools/OAuth/ConnectionService.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\OAuth;
|
||||
|
||||
use Espo\Core\Exceptions\Error;
|
||||
use Espo\Core\Exceptions\Forbidden;
|
||||
use Espo\Entities\OAuthAccount;
|
||||
use Espo\ORM\EntityManager;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
|
||||
|
||||
class ConnectionService
|
||||
{
|
||||
public function __construct(
|
||||
private EntityManager $entityManager,
|
||||
private GenericProviderFactory $genericProviderFactory,
|
||||
private TokenSetter $tokenSetter,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws Forbidden
|
||||
* @throws Error
|
||||
*/
|
||||
public function connect(OAuthAccount $account, string $code): void
|
||||
{
|
||||
$provider = $account->getProvider();
|
||||
|
||||
if (!$provider->isActive()) {
|
||||
throw new Forbidden("Provider is not active.");
|
||||
}
|
||||
|
||||
$genericProvider = $this->genericProviderFactory->create($provider);
|
||||
|
||||
try {
|
||||
$tokens = $genericProvider->getAccessToken('authorization_code', ['code' => $code]);
|
||||
} catch (GuzzleException $e) {
|
||||
throw new Error("Token request error.", 500, $e);
|
||||
} catch (IdentityProviderException $e) {
|
||||
throw new Error("Token request response error.", 500, $e);
|
||||
}
|
||||
|
||||
$this->tokenSetter->set($account, $tokens);
|
||||
|
||||
$this->entityManager->saveEntity($account);
|
||||
}
|
||||
|
||||
public function disconnect(OAuthAccount $account): void
|
||||
{
|
||||
$account->setAccessToken(null);
|
||||
$account->setRefreshToken(null);
|
||||
$account->setExpiresAt(null);
|
||||
|
||||
$this->entityManager->saveEntity($account);
|
||||
}
|
||||
}
|
||||
33
application/Espo/Tools/OAuth/Exceptions/AccountNotFound.php
Normal file
33
application/Espo/Tools/OAuth/Exceptions/AccountNotFound.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\OAuth\Exceptions;
|
||||
|
||||
class AccountNotFound extends OAuthException
|
||||
{}
|
||||
33
application/Espo/Tools/OAuth/Exceptions/NoToken.php
Normal file
33
application/Espo/Tools/OAuth/Exceptions/NoToken.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\OAuth\Exceptions;
|
||||
|
||||
class NoToken extends OAuthException
|
||||
{}
|
||||
35
application/Espo/Tools/OAuth/Exceptions/OAuthException.php
Normal file
35
application/Espo/Tools/OAuth/Exceptions/OAuthException.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\OAuth\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class OAuthException extends Exception
|
||||
{}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\OAuth\Exceptions;
|
||||
|
||||
class ProviderNotAvailable extends OAuthException
|
||||
{}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\OAuth\Exceptions;
|
||||
|
||||
class TokenObtainingFailure extends OAuthException
|
||||
{}
|
||||
60
application/Espo/Tools/OAuth/GenericProviderFactory.php
Normal file
60
application/Espo/Tools/OAuth/GenericProviderFactory.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\OAuth;
|
||||
|
||||
use Espo\Core\Utils\Crypt;
|
||||
use Espo\Entities\OAuthProvider;
|
||||
use League\OAuth2\Client\Provider\GenericProvider;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class GenericProviderFactory
|
||||
{
|
||||
public function __construct(
|
||||
private ConfigDataProvider $configDataProvider,
|
||||
private Crypt $crypt,
|
||||
) {}
|
||||
|
||||
public function create(OAuthProvider $provider): GenericProvider
|
||||
{
|
||||
$secret = $this->crypt->decrypt($provider->getClientSecret());
|
||||
|
||||
return new GenericProvider([
|
||||
'clientId' => $provider->getClientId(),
|
||||
'clientSecret' => $secret,
|
||||
'redirectUri' => $this->configDataProvider->getRedirectUri(),
|
||||
'urlAccessToken' => $provider->getTokenEndpoint(),
|
||||
|
||||
'urlAuthorize' => 'dummy',
|
||||
'urlResourceOwnerDetails' => 'dummy',
|
||||
]);
|
||||
}
|
||||
}
|
||||
62
application/Espo/Tools/OAuth/TokenSetter.php
Normal file
62
application/Espo/Tools/OAuth/TokenSetter.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\OAuth;
|
||||
|
||||
use Espo\Core\Field\DateTime;
|
||||
use Espo\Core\Utils\Crypt;
|
||||
use Espo\Entities\OAuthAccount;
|
||||
use League\OAuth2\Client\Token\AccessTokenInterface;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class TokenSetter
|
||||
{
|
||||
public function __construct(
|
||||
private Crypt $crypt,
|
||||
) {}
|
||||
|
||||
public function set(OAuthAccount $account, AccessTokenInterface $tokens): void
|
||||
{
|
||||
$accessToken = $this->crypt->encrypt($tokens->getToken());
|
||||
|
||||
$refreshToken = $tokens->getRefreshToken() ?
|
||||
$this->crypt->encrypt($tokens->getRefreshToken()) :
|
||||
null;
|
||||
|
||||
$expires = $tokens->getExpires() !== null ?
|
||||
DateTime::fromTimestamp($tokens->getExpires()) :
|
||||
null;
|
||||
|
||||
$account->setAccessToken($accessToken);
|
||||
$account->setRefreshToken($refreshToken);
|
||||
$account->setExpiresAt($expires);
|
||||
}
|
||||
}
|
||||
62
application/Espo/Tools/OAuth/Tokens.php
Normal file
62
application/Espo/Tools/OAuth/Tokens.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\OAuth;
|
||||
|
||||
use Espo\Core\Field\DateTime;
|
||||
use SensitiveParameter;
|
||||
|
||||
/**
|
||||
* Immutable.
|
||||
*/
|
||||
class Tokens
|
||||
{
|
||||
public function __construct(
|
||||
#[SensitiveParameter]
|
||||
private string $accessToken,
|
||||
#[SensitiveParameter]
|
||||
private ?string $refreshToken,
|
||||
private ?DateTime $expiresAt,
|
||||
) {}
|
||||
|
||||
public function getAccessToken(): string
|
||||
{
|
||||
return $this->accessToken;
|
||||
}
|
||||
|
||||
public function getRefreshToken(): ?string
|
||||
{
|
||||
return $this->refreshToken;
|
||||
}
|
||||
|
||||
public function getExpiresAt(): ?DateTime
|
||||
{
|
||||
return $this->expiresAt;
|
||||
}
|
||||
}
|
||||
177
application/Espo/Tools/OAuth/TokensProvider.php
Normal file
177
application/Espo/Tools/OAuth/TokensProvider.php
Normal file
@@ -0,0 +1,177 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\OAuth;
|
||||
|
||||
use Espo\Core\Field\DateTime;
|
||||
use Espo\Core\Utils\Crypt;
|
||||
use Espo\Entities\OAuthAccount;
|
||||
use Espo\ORM\EntityManager;
|
||||
use Espo\ORM\Name\Attribute;
|
||||
use Espo\ORM\Query\SelectBuilder;
|
||||
use Espo\Tools\OAuth\Exceptions\NoToken;
|
||||
use Espo\Tools\OAuth\Exceptions\ProviderNotAvailable;
|
||||
use Espo\Tools\OAuth\Exceptions\AccountNotFound;
|
||||
use Espo\Tools\OAuth\Exceptions\TokenObtainingFailure;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
|
||||
use LogicException;
|
||||
|
||||
class TokensProvider
|
||||
{
|
||||
private const EXPIRATION_LEAD_TIME = 60;
|
||||
|
||||
public function __construct(
|
||||
private EntityManager $entityManager,
|
||||
private GenericProviderFactory $genericProviderFactory,
|
||||
private TokenSetter $tokenSetter,
|
||||
private Crypt $crypt,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws AccountNotFound
|
||||
* @throws ProviderNotAvailable
|
||||
* @throws NoToken
|
||||
* @throws TokenObtainingFailure
|
||||
*/
|
||||
public function get(string $id): Tokens
|
||||
{
|
||||
$account = $this->fetch($id);
|
||||
|
||||
if (
|
||||
$account->getRefreshToken() &&
|
||||
$account->getExpiresAt() &&
|
||||
$account->getExpiresAt()->isLessThan(
|
||||
DateTime::createNow()->addSeconds(self::EXPIRATION_LEAD_TIME)
|
||||
)
|
||||
) {
|
||||
$this->refresh($account);
|
||||
}
|
||||
|
||||
if (!$account->getAccessToken()) {
|
||||
throw new NoToken();
|
||||
}
|
||||
|
||||
$accessToken = $this->crypt->decrypt($account->getAccessToken());
|
||||
|
||||
$refreshToken = $account->getRefreshToken() ?
|
||||
$this->crypt->decrypt($account->getRefreshToken()) :
|
||||
null;
|
||||
|
||||
return new Tokens(
|
||||
accessToken: $accessToken,
|
||||
refreshToken: $refreshToken,
|
||||
expiresAt: $account->getExpiresAt(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ProviderNotAvailable
|
||||
* @throws AccountNotFound
|
||||
* @throws NoToken
|
||||
*/
|
||||
private function fetch(string $id): OAuthAccount
|
||||
{
|
||||
// Ensuring the token is not being refreshed.
|
||||
$this->entityManager->getTransactionManager()->start();
|
||||
|
||||
$account = $this->entityManager
|
||||
->getRDBRepositoryByClass(OAuthAccount::class)
|
||||
->clone(
|
||||
SelectBuilder::create()
|
||||
->from(OAuthAccount::ENTITY_TYPE)
|
||||
->forShare()
|
||||
->build()
|
||||
)
|
||||
->where([Attribute::ID => $id])
|
||||
->findOne();
|
||||
|
||||
$this->entityManager->getTransactionManager()->commit();
|
||||
|
||||
if (!$account) {
|
||||
throw new AccountNotFound();
|
||||
}
|
||||
|
||||
if (!$account->getProvider()->isActive()) {
|
||||
throw new ProviderNotAvailable();
|
||||
}
|
||||
|
||||
if (!$account->getAccessToken()) {
|
||||
throw new NoToken();
|
||||
}
|
||||
|
||||
return $account;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws TokenObtainingFailure
|
||||
* @noinspection PhpDocRedundantThrowsInspection
|
||||
*/
|
||||
private function refresh(OAuthAccount $account): void
|
||||
{
|
||||
$this->entityManager
|
||||
->getTransactionManager()
|
||||
->run(function () use ($account) {
|
||||
$this->refreshInTransaction($account);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws TokenObtainingFailure
|
||||
*/
|
||||
private function refreshInTransaction(OAuthAccount $account): void
|
||||
{
|
||||
$refreshToken = $account->getRefreshToken();
|
||||
|
||||
if (!$refreshToken) {
|
||||
throw new LogicException();
|
||||
}
|
||||
|
||||
$refreshToken = $this->crypt->decrypt($refreshToken);
|
||||
|
||||
$this->entityManager
|
||||
->getRDBRepositoryByClass(OAuthAccount::class)
|
||||
->forUpdate()
|
||||
->sth()
|
||||
->where([Attribute::ID => $account->getId()])
|
||||
->find();
|
||||
|
||||
$genericProvider = $this->genericProviderFactory->create($account->getProvider());
|
||||
|
||||
try {
|
||||
$tokens = $genericProvider->getAccessToken('refresh_token', ['refresh_token' => $refreshToken]);
|
||||
} catch (GuzzleException|IdentityProviderException $e) {
|
||||
throw new TokenObtainingFailure($e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
|
||||
$this->tokenSetter->set($account, $tokens);
|
||||
|
||||
$this->entityManager->saveEntity($account);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user