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

Public Member Functions

 __construct (TokenModelFactory $tokenModelFactory, UserModel $userModel, TokenCollectionFactory $tokenModelCollectionFactory, CredentialsValidator $validatorHelper)
 
 createAdminAccessToken ($username, $password)
 
 revokeAdminAccessToken ($adminId)
 

Detailed Description

Class to handle token generation for Admins

Definition at line 21 of file AdminTokenService.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( TokenModelFactory  $tokenModelFactory,
UserModel  $userModel,
TokenCollectionFactory  $tokenModelCollectionFactory,
CredentialsValidator  $validatorHelper 
)

Initialize service

Parameters
TokenModelFactory$tokenModelFactory
UserModel$userModel
TokenCollectionFactory$tokenModelCollectionFactory
\Magento\Integration\Model\CredentialsValidator$validatorHelper

Definition at line 62 of file AdminTokenService.php.

67  {
68  $this->tokenModelFactory = $tokenModelFactory;
69  $this->userModel = $userModel;
70  $this->tokenModelCollectionFactory = $tokenModelCollectionFactory;
71  $this->validatorHelper = $validatorHelper;
72  }

Member Function Documentation

◆ createAdminAccessToken()

createAdminAccessToken (   $username,
  $password 
)

{Create access token for admin given the admin credentials.

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

Implements AdminTokenServiceInterface.

Definition at line 77 of file AdminTokenService.php.

78  {
79  $this->validatorHelper->validate($username, $password);
80  $this->getRequestThrottler()->throttle($username, RequestThrottler::USER_TYPE_ADMIN);
81  $this->userModel->login($username, $password);
82  if (!$this->userModel->getId()) {
83  $this->getRequestThrottler()->logAuthenticationFailure($username, RequestThrottler::USER_TYPE_ADMIN);
84  /*
85  * This message is same as one thrown in \Magento\Backend\Model\Auth to keep the behavior consistent.
86  * Constant cannot be created in Auth Model since it uses legacy translation that doesn't support it.
87  * Need to make sure that this is refactored once exception handling is updated in Auth Model.
88  */
89  throw new AuthenticationException(
90  __(
91  'The account sign-in was incorrect or your account is disabled temporarily. '
92  . 'Please wait and try again later.'
93  )
94  );
95  }
96  $this->getRequestThrottler()->resetAuthenticationFailuresCount($username, RequestThrottler::USER_TYPE_ADMIN);
97  return $this->tokenModelFactory->create()->createAdminToken($this->userModel->getId())->getToken();
98  }
__()
Definition: __.php:13

◆ revokeAdminAccessToken()

revokeAdminAccessToken (   $adminId)

Revoke token by admin id.

The function will delete the token from the oauth_token table.

Parameters
int$adminId
Returns
bool
Exceptions

Implements AdminTokenServiceInterface.

Definition at line 109 of file AdminTokenService.php.

110  {
111  $tokenCollection = $this->tokenModelCollectionFactory->create()->addFilterByAdminId($adminId);
112  if ($tokenCollection->getSize() == 0) {
113  throw new LocalizedException(__('This user has no tokens.'));
114  }
115  try {
116  foreach ($tokenCollection as $token) {
117  $token->delete();
118  }
119  } catch (\Exception $e) {
120  throw new LocalizedException(__("The tokens couldn't be revoked."));
121  }
122  return true;
123  }
__()
Definition: __.php:13

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