|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * ScandiPWA - Progressive Web App for Magento |
| 4 | + * |
| 5 | + * Copyright © Scandiweb, Inc. All rights reserved. |
| 6 | + * See LICENSE for license details. |
| 7 | + * |
| 8 | + * @license OSL-3.0 (Open Software License ("OSL") v. 3.0) |
| 9 | + * @package scandipwa/module-customer-graph-ql |
| 10 | + * @link https://github.com/scandipwa/module-customer-graph-ql |
| 11 | + */ |
| 12 | + |
| 13 | +declare(strict_types=1); |
| 14 | + |
| 15 | +namespace ScandiPWA\CustomerGraphQl\Model; |
| 16 | + |
| 17 | +use Magento\Customer\Api\AccountManagementInterface; |
| 18 | +use Magento\Framework\Exception\EmailNotConfirmedException; |
| 19 | +use Magento\Integration\Model\CredentialsValidator; |
| 20 | +use Magento\Integration\Model\Oauth\TokenFactory as TokenModelFactory; |
| 21 | +use Magento\Integration\Model\ResourceModel\Oauth\Token\CollectionFactory as TokenCollectionFactory; |
| 22 | +use Magento\Integration\Model\Oauth\Token\RequestThrottler; |
| 23 | +use Magento\Framework\Exception\AuthenticationException; |
| 24 | +use Magento\Framework\Event\ManagerInterface; |
| 25 | +use Magento\Framework\App\ObjectManager; |
| 26 | +use Magento\Integration\Model\CustomerTokenService as SourceCustomerTokenService; |
| 27 | + |
| 28 | +/** |
| 29 | + * Class CustomerTokenService |
| 30 | + * @package ScandiPWA\CustomerGraphQl\Model |
| 31 | + */ |
| 32 | +class CustomerTokenService extends SourceCustomerTokenService |
| 33 | +{ |
| 34 | + /** |
| 35 | + * Token Model |
| 36 | + * |
| 37 | + * @var TokenModelFactory |
| 38 | + */ |
| 39 | + protected $tokenModelFactory; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var ManagerInterface |
| 43 | + */ |
| 44 | + protected $eventManager; |
| 45 | + |
| 46 | + /** |
| 47 | + * Customer Account Service |
| 48 | + * |
| 49 | + * @var AccountManagementInterface |
| 50 | + */ |
| 51 | + protected $accountManagement; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var CredentialsValidator |
| 55 | + */ |
| 56 | + protected $validatorHelper; |
| 57 | + |
| 58 | + /** |
| 59 | + * Token Collection Factory |
| 60 | + * |
| 61 | + * @var TokenCollectionFactory |
| 62 | + */ |
| 63 | + protected $tokenModelCollectionFactory; |
| 64 | + |
| 65 | + /** |
| 66 | + * @var RequestThrottler |
| 67 | + */ |
| 68 | + protected $requestThrottler; |
| 69 | + |
| 70 | + /** |
| 71 | + * Initialize service |
| 72 | + * |
| 73 | + * @param TokenModelFactory $tokenModelFactory |
| 74 | + * @param AccountManagementInterface $accountManagement |
| 75 | + * @param TokenCollectionFactory $tokenModelCollectionFactory |
| 76 | + * @param CredentialsValidator $validatorHelper |
| 77 | + * @param ManagerInterface $eventManager |
| 78 | + */ |
| 79 | + public function __construct( |
| 80 | + TokenModelFactory $tokenModelFactory, |
| 81 | + AccountManagementInterface $accountManagement, |
| 82 | + TokenCollectionFactory $tokenModelCollectionFactory, |
| 83 | + CredentialsValidator $validatorHelper, |
| 84 | + ManagerInterface $eventManager = null |
| 85 | + ) { |
| 86 | + parent::__construct( |
| 87 | + $tokenModelFactory, |
| 88 | + $accountManagement, |
| 89 | + $tokenModelCollectionFactory, |
| 90 | + $validatorHelper, |
| 91 | + $eventManager ?: ObjectManager::getInstance() |
| 92 | + ->get(ManagerInterface::class) |
| 93 | + ); |
| 94 | + |
| 95 | + $this->tokenModelFactory = $tokenModelFactory; |
| 96 | + $this->accountManagement = $accountManagement; |
| 97 | + $this->validatorHelper = $validatorHelper; |
| 98 | + $this->eventManager = $eventManager ?: ObjectManager::getInstance() |
| 99 | + ->get(ManagerInterface::class); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Create customer access token |
| 104 | + * Adding separate message for EmailNotConfirmedException |
| 105 | + * @param $username |
| 106 | + * @param $password |
| 107 | + * @return string |
| 108 | + */ |
| 109 | + public function createCustomerAccessToken($username, $password): string |
| 110 | + { |
| 111 | + $this->validatorHelper->validate($username, $password); |
| 112 | + $this->getRequestThrottler()->throttle($username, RequestThrottler::USER_TYPE_CUSTOMER); |
| 113 | + |
| 114 | + try { |
| 115 | + $customerDataObject = $this->accountManagement->authenticate($username, $password); |
| 116 | + } catch (EmailNotConfirmedException $e) { |
| 117 | + throw new EmailNotConfirmedException( |
| 118 | + __('You must confirm your account before signing in. Please check your email.') |
| 119 | + ); |
| 120 | + } catch (\Exception $e) { |
| 121 | + $this->getRequestThrottler()->logAuthenticationFailure($username, RequestThrottler::USER_TYPE_CUSTOMER); |
| 122 | + throw new AuthenticationException( |
| 123 | + __( |
| 124 | + 'The account sign-in was incorrect or your account is disabled temporarily. ' |
| 125 | + . 'Please wait and try again later.' |
| 126 | + ) |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + $this->eventManager->dispatch('customer_login', ['customer' => $customerDataObject]); |
| 131 | + $this->getRequestThrottler()->resetAuthenticationFailuresCount($username, RequestThrottler::USER_TYPE_CUSTOMER); |
| 132 | + |
| 133 | + return $this->tokenModelFactory->create()->createCustomerToken($customerDataObject->getId())->getToken(); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Get request throttler instance |
| 138 | + * |
| 139 | + * @return RequestThrottler |
| 140 | + * @deprecated 100.0.4 |
| 141 | + */ |
| 142 | + protected function getRequestThrottler(): RequestThrottler |
| 143 | + { |
| 144 | + if (!$this->requestThrottler instanceof RequestThrottler) { |
| 145 | + return ObjectManager::getInstance()->get(RequestThrottler::class); |
| 146 | + } |
| 147 | + |
| 148 | + return $this->requestThrottler; |
| 149 | + } |
| 150 | +} |
0 commit comments