Skip to content
60 changes: 60 additions & 0 deletions AdobeIms/Model/FlushUserTokens.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\AdobeIms\Model;

use Magento\AdobeImsApi\Api\FlushUserTokensInterface;
use Magento\AdobeImsApi\Api\UserProfileRepositoryInterface;
use Magento\Authorization\Model\UserContextInterface;

/**
* Remove user access and refresh tokens
*/
class FlushUserTokens implements FlushUserTokensInterface
{
/**
* @var UserProfileRepositoryInterface
*/
private $userProfileRepository;

/**
* @var UserContextInterface
*/
private $userContext;

/**
* UserAuthorized constructor.
* @param UserContextInterface $userContext
* @param UserProfileRepositoryInterface $userProfileRepository
*/
public function __construct(
UserContextInterface $userContext,
UserProfileRepositoryInterface $userProfileRepository
) {
$this->userContext = $userContext;
$this->userProfileRepository = $userProfileRepository;
}

/**
* @inheritdoc
*/
public function execute(int $adminUserId = null): void
{
try {
if ($adminUserId === null) {
$adminUserId = (int) $this->userContext->getUserId();
}

$userProfile = $this->userProfileRepository->getByUserId($adminUserId);
$userProfile->setAccessToken('');
$userProfile->setRefreshToken('');
$this->userProfileRepository->save($userProfile);
} catch (\Exception $e) {
// User profile and tokens are not present in the system
}
}
}
57 changes: 57 additions & 0 deletions AdobeIms/Model/GetAccessToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\AdobeIms\Model;

use Magento\AdobeImsApi\Api\GetAccessTokenInterface;
use Magento\AdobeImsApi\Api\UserProfileRepositoryInterface;
use Magento\Authorization\Model\UserContextInterface;
use Magento\Framework\Exception\NoSuchEntityException;

/**
* Get admin user access token
*/
class GetAccessToken implements GetAccessTokenInterface
{
/**
* @var UserProfileRepositoryInterface
*/
private $userProfileRepository;

/**
* @var UserContextInterface
*/
private $userContext;

/**
* UserAuthorized constructor.
* @param UserContextInterface $userContext
* @param UserProfileRepositoryInterface $userProfileRepository
*/
public function __construct(
UserContextInterface $userContext,
UserProfileRepositoryInterface $userProfileRepository
) {
$this->userContext = $userContext;
$this->userProfileRepository = $userProfileRepository;
}

/**
* @inheritdoc
*/
public function execute(int $adminUserId = null): ?string
{
try {
if ($adminUserId === null) {
$adminUserId = (int) $this->userContext->getUserId();
}
return $this->userProfileRepository->getByUserId($adminUserId)->getAccessToken();
} catch (NoSuchEntityException $exception) {
return null;
}
}
}
15 changes: 13 additions & 2 deletions AdobeIms/Model/LogOut.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Magento\AdobeImsApi\Api\LogOutInterface;
use Magento\AdobeImsApi\Api\Data\ConfigInterface;
use Magento\Authorization\Model\UserContextInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\HTTP\Client\CurlFactory;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -75,12 +76,22 @@ public function __construct(
public function execute() : bool
{
try {
$userProfile = $this->userProfileRepository->getByUserId((int)$this->userContext->getUserId());
try {
$userProfile = $this->userProfileRepository->getByUserId((int)$this->userContext->getUserId());
} catch (NoSuchEntityException $exception) {
return true;
}

$accessToken = $userProfile->getAccessToken();

if (empty($accessToken)) {
return true;
}

$curl = $this->curlFactory->create();
$curl->addHeader('Content-Type', 'application/x-www-form-urlencoded');
$curl->addHeader('cache-control', 'no-cache');
$curl->get($this->config->getLogoutUrl($userProfile->getAccessToken()));
$curl->get($this->config->getLogoutUrl($accessToken));

if ($curl->getStatus() === self::HTTP_FOUND) {
$userProfile->setAccessToken('');
Expand Down
2 changes: 2 additions & 0 deletions AdobeIms/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@
<preference for="Magento\AdobeImsApi\Api\GetImageInterface" type="Magento\AdobeIms\Model\GetImage"/>
<preference for="Magento\AdobeImsApi\Api\UserAuthorizedInterface" type="Magento\AdobeIms\Model\UserAuthorized"/>
<preference for="Magento\AdobeImsApi\Api\LogOutInterface" type="Magento\AdobeIms\Model\LogOut"/>
<preference for="Magento\AdobeImsApi\Api\GetAccessTokenInterface" type="Magento\AdobeIms\Model\GetAccessToken"/>
<preference for="Magento\AdobeImsApi\Api\FlushUserTokensInterface" type="Magento\AdobeIms\Model\FlushUserTokens"/>
</config>
24 changes: 24 additions & 0 deletions AdobeImsApi/Api/FlushUserTokensInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\AdobeImsApi\Api;

/**
* Interface UserAuthorizedInterface
* @api
*/
interface FlushUserTokensInterface
{
/**
* Remove access and refresh tokens for the specified user or current user
*
* @param int $adminUserId
* @return bool
*/
public function execute(int $adminUserId = null): void;
}
24 changes: 24 additions & 0 deletions AdobeImsApi/Api/GetAccessTokenInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\AdobeImsApi\Api;

/**
* Interface GetAccessTokenInterface
* @api
*/
interface GetAccessTokenInterface
{
/**
* Get adobe access token for specified or current admin user
*
* @param int $adminUserId
* @return string|null
*/
public function execute(int $adminUserId = null): ?string;
}
Loading