Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions
CustomerTokenService Class Reference
Inheritance diagram for CustomerTokenService:
CustomerTokenServiceInterface

Public Member Functions

 __construct (TokenModelFactory $tokenModelFactory, AccountManagementInterface $accountManagement, TokenCollectionFactory $tokenModelCollectionFactory, CredentialsValidator $validatorHelper)
 
 createCustomerAccessToken ($username, $password)
 
 revokeCustomerAccessToken ($customerId)
 

Detailed Description

Definition at line 18 of file CustomerTokenService.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( TokenModelFactory  $tokenModelFactory,
AccountManagementInterface  $accountManagement,
TokenCollectionFactory  $tokenModelCollectionFactory,
CredentialsValidator  $validatorHelper 
)

Initialize service

Parameters
TokenModelFactory$tokenModelFactory
AccountManagementInterface$accountManagement
TokenCollectionFactory$tokenModelCollectionFactory
\Magento\Integration\Model\CredentialsValidator$validatorHelper

Definition at line 59 of file CustomerTokenService.php.

64  {
65  $this->tokenModelFactory = $tokenModelFactory;
66  $this->accountManagement = $accountManagement;
67  $this->tokenModelCollectionFactory = $tokenModelCollectionFactory;
68  $this->validatorHelper = $validatorHelper;
69  }
$accountManagement

Member Function Documentation

◆ createCustomerAccessToken()

createCustomerAccessToken (   $username,
  $password 
)

{Create access token for admin given the customer credentials.

Parameters
string$username
string$password
Returns
string Token created
Exceptions
}

Implements CustomerTokenServiceInterface.

Definition at line 74 of file CustomerTokenService.php.

75  {
76  $this->validatorHelper->validate($username, $password);
77  $this->getRequestThrottler()->throttle($username, RequestThrottler::USER_TYPE_CUSTOMER);
78  try {
79  $customerDataObject = $this->accountManagement->authenticate($username, $password);
80  } catch (\Exception $e) {
81  $this->getRequestThrottler()->logAuthenticationFailure($username, RequestThrottler::USER_TYPE_CUSTOMER);
82  throw new AuthenticationException(
83  __(
84  'The account sign-in was incorrect or your account is disabled temporarily. '
85  . 'Please wait and try again later.'
86  )
87  );
88  }
89  $this->getRequestThrottler()->resetAuthenticationFailuresCount($username, RequestThrottler::USER_TYPE_CUSTOMER);
90  return $this->tokenModelFactory->create()->createCustomerToken($customerDataObject->getId())->getToken();
91  }
__()
Definition: __.php:13

◆ revokeCustomerAccessToken()

revokeCustomerAccessToken (   $customerId)

Revoke token by customer id.

The function will delete the token from the oauth_token table.

Parameters
int$customerId
Returns
bool
Exceptions

Implements CustomerTokenServiceInterface.

Definition at line 102 of file CustomerTokenService.php.

103  {
104  $tokenCollection = $this->tokenModelCollectionFactory->create()->addFilterByCustomerId($customerId);
105  if ($tokenCollection->getSize() == 0) {
106  throw new LocalizedException(__('This customer has no tokens.'));
107  }
108  try {
109  foreach ($tokenCollection as $token) {
110  $token->delete();
111  }
112  } catch (\Exception $e) {
113  throw new LocalizedException(__("The tokens couldn't be revoked."));
114  }
115  return true;
116  }
__()
Definition: __.php:13

The documentation for this class was generated from the following file: