-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/authentication'
- Loading branch information
Showing
7 changed files
with
143 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Opengento\BetterBo\Api; | ||
|
||
use Magento\Framework\Exception\AuthenticationException; | ||
|
||
interface AuthenticatorInterface | ||
{ | ||
/** | ||
* @return string | ||
* @throws AuthenticationException | ||
*/ | ||
public function authenticate(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
/** | ||
* Authenticator | ||
* | ||
* @copyright Copyright © 2024 Blackbird Agency. All rights reserved. | ||
* @author sebastien@bird.eu | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Opengento\BetterBo\Model; | ||
|
||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Framework\Data\Form\FormKey\Validator; | ||
use Magento\Framework\Exception\AuthorizationException; | ||
use Magento\Framework\Webapi\Request; | ||
use Opengento\BetterBo\Api\AuthenticatorInterface; | ||
|
||
class Authenticator implements AuthenticatorInterface | ||
{ | ||
public function __construct( | ||
protected Validator $validator, | ||
protected Request $request, | ||
protected ScopeConfigInterface $scopeConfig | ||
) { | ||
} | ||
|
||
/** | ||
* @throws AuthorizationException | ||
*/ | ||
public function authenticate(): string | ||
{ | ||
if (!$this->validator->validate($this->request)) { | ||
throw new AuthorizationException( | ||
__( | ||
"The consumer isn't authorized to access %resources.", | ||
['resources' => ''] | ||
) | ||
); | ||
} | ||
|
||
return $this->getAppToken(); | ||
} | ||
|
||
private function getAppToken(): string | ||
{ | ||
return $this->scopeConfig->getValue('better_bo/integration/token'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
/** | ||
* AdminLoginCookie | ||
* | ||
* @copyright Copyright © 2024 Blackbird Agency. All rights reserved. | ||
* @author sebastien@bird.eu | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Opengento\BetterBo\Observer; | ||
|
||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\Event\ObserverInterface; | ||
use Magento\Integration\Model\CustomUserContext; | ||
use Magento\Integration\Model\UserToken\UserTokenParametersFactory; | ||
|
||
class AdminLoginCookie implements ObserverInterface | ||
{ | ||
public function __construct( | ||
protected ScopeConfigInterface $scopeConfig, | ||
protected \Magento\Framework\Stdlib\CookieManagerInterface $customCookieManager, | ||
protected \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $customCookieMetadataFactory, | ||
protected \Magento\Integration\Api\UserTokenIssuerInterface $tokenIssuer, | ||
protected UserTokenParametersFactory $tokenParametersFactory, | ||
) {} | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public function execute(Observer $observer) | ||
{ | ||
$event = $observer->getEvent(); | ||
$user = $event->getUser(); | ||
|
||
$context = new CustomUserContext( | ||
(int) $user->getId(), | ||
CustomUserContext::USER_TYPE_ADMIN | ||
); | ||
$params = $this->tokenParametersFactory->create(); | ||
|
||
$token = $this->tokenIssuer->create($context, $params); | ||
$this->createAdminCookie($token); | ||
} | ||
|
||
protected function createAdminCookie(string $token): void | ||
{ | ||
$ttl = $this->scopeConfig->getValue('admin/security/session_lifetime'); | ||
$customCookieMetadata = $this->customCookieMetadataFactory->createPublicCookieMetadata(); | ||
$customCookieMetadata->setDuration($ttl); | ||
$customCookieMetadata->setPath('/admin'); | ||
$customCookieMetadata->setHttpOnly(false); | ||
|
||
$this->customCookieManager->setPublicCookie( | ||
'betterbo_token', | ||
$token, | ||
$customCookieMetadata | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> | ||
<event name="backend_auth_user_login_success"> | ||
<observer name="opengento_betterbo_cookie_create" | ||
instance="Opengento\BetterBo\Observer\AdminLoginCookie"/> | ||
</event> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters