Skip to content

Commit 719148d

Browse files
RachanaRachana
authored andcommitted
BUG#AC-9337:Revoking or invalidating previous access tokens upon generating new access token
1 parent 3c27a5a commit 719148d

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

app/code/Magento/Integration/Api/TokenManager.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,15 @@ public function revokeFor(UserContextInterface $userContext): void
9494
{
9595
$this->tokenRevoker->revokeFor($userContext);
9696
}
97+
98+
/**
99+
* Revoke previously issued tokens for given user.
100+
*
101+
* @param UserContextInterface $userContext
102+
* @return void
103+
*/
104+
public function revokeForOld(UserContextInterface $userContext): void
105+
{
106+
$this->tokenRevoker->revokeForOld($userContext);
107+
}
97108
}

app/code/Magento/Integration/Model/CustomerTokenService.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ public function createCustomerAccessToken($username, $password)
7777
CustomUserContext::USER_TYPE_CUSTOMER
7878
);
7979
$params = $this->tokenManager->createUserTokenParameters();
80-
$this->revokeCustomerAccessToken($customerDataObject->getId());
80+
$this->revokeCustomerAccessTokenOld($customerDataObject->getId());
81+
8182
return $this->tokenManager->create($context, $params);
8283
}
8384

@@ -114,4 +115,23 @@ private function getRequestThrottler()
114115
}
115116
return $this->requestThrottler;
116117
}
118+
119+
/**
120+
* Revoke old token by customer id.
121+
*
122+
* @param int $customerId
123+
* @return bool
124+
* @throws \Magento\Framework\Exception\LocalizedException
125+
*/
126+
public function revokeCustomerAccessTokenOld($customerId)
127+
{
128+
try {
129+
$this->tokenManager->revokeForOld(
130+
new CustomUserContext((int)$customerId, CustomUserContext::USER_TYPE_CUSTOMER)
131+
);
132+
} catch (UserTokenException $exception) {
133+
throw new LocalizedException(__('Failed to revoke customer\'s access tokens'), $exception);
134+
}
135+
return true;
136+
}
117137
}

app/code/Magento/JwtUserToken/Model/Revoker.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ public function __construct(RevokedRepositoryInterface $revokedRepo)
3333
* @inheritDoc
3434
*/
3535
public function revokeFor(UserContextInterface $userContext): void
36+
{
37+
//Invalidating all tokens issued before current datetime.
38+
$this->revokedRepo->saveRevoked(
39+
new Revoked((int) $userContext->getUserType(), (int) $userContext->getUserId(), time())
40+
);
41+
}
42+
43+
/**
44+
* @inheritDoc
45+
*/
46+
public function revokeForOld(UserContextInterface $userContext): void
3647
{
3748
//Invalidating all tokens issued before current datetime.
3849
$this->revokedRepo->saveRevoked(

0 commit comments

Comments
 (0)