Skip to content

Commit

Permalink
Merge branch 'feature/authentication'
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienbird committed Nov 10, 2024
2 parents c6d5472 + 2464df7 commit 8fabbb8
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 1 deletion.
14 changes: 14 additions & 0 deletions Api/AuthenticatorInterface.php
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;
}
50 changes: 50 additions & 0 deletions Model/Authenticator.php
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');
}
}
61 changes: 61 additions & 0 deletions Observer/AdminLoginCookie.php
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
);
}
}
3 changes: 3 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<general>
<attributes_fields_type>text,select</attributes_fields_type>
</general>
<integration>
<token>12345</token>
</integration>
</better_bo>
</default>
</config>
2 changes: 1 addition & 1 deletion etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@

<preference for="Opengento\BetterBo\Api\ProductManagementInterface" type="Opengento\BetterBo\Model\ProductManagement" />
<preference for="Opengento\BetterBo\Api\GetProductAttributesInterface" type="Opengento\BetterBo\Model\GetProductAttributes" />

<preference for="Opengento\BetterBo\Api\AuthenticatorInterface" type="Opengento\BetterBo\Model\Authenticator" />
</config>
8 changes: 8 additions & 0 deletions etc/events.xml
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>
6 changes: 6 additions & 0 deletions etc/webapi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
-->
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route url="/V1/betterbo/authenticate" method="GET">
<service class="Opengento\BetterBo\Api\AuthenticatorInterface" method="authenticate"/>
<resources>
<resource ref="anonymous" />
</resources>
</route>

<!-- Get product attribute config and value -->
<route url="/V1/betterbo/catalog/product/attributes" method="POST">
Expand Down

0 comments on commit 8fabbb8

Please sign in to comment.