Skip to content

Commit cbcf04c

Browse files
Invalidate Access Token method added.
1 parent cee23db commit cbcf04c

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/ApiBundle/Controller/AuthController.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,40 @@ public function getResettingRequestAction()
532532
));
533533
}
534534

535+
/**
536+
* Invalidate Current Access Token
537+
*
538+
* @Post("/user/invalidate")
539+
*
540+
* @ApiDoc(
541+
* resource=true,
542+
* description="Invalidate current access token. Access token to be provided in header (Authorization = Bearer <access token>)",
543+
* )
544+
*/
545+
public function invalidateAction()
546+
{
547+
$request = $this->container->get('request');
548+
549+
$user = $this->container->get('security.context')->getToken()->getUser();
550+
if (!is_object($user) || !$user instanceof UserInterface) {
551+
$this->logAndThrowError(400, 'Invalid/Missing Access Token', $this->get('translator')->trans('api.show_error_username_missing', array(), 'messages', $request->getLocale()), $request->getLocale());
552+
}
553+
554+
$token = $this->container->get('security.context')->getToken()->getToken();
555+
556+
$accessTokenManager = $this->container->get('fos_oauth_server.access_token_manager.default');
557+
$accessToken = $accessTokenManager->findTokenBy(array('token' => $token));
558+
$accessToken->setExpiresAt($accessToken->getExpiresAt() - (86400 * 30));
559+
$accessTokenManager->updateToken($accessToken);
560+
561+
$this->logMessage(200, 'Token '.$token.' Invalidated for '.$user->getUsername());
562+
563+
return new JsonResponse(array(
564+
'code' => 201,
565+
'show_message' => 'User logged out successfully',
566+
));
567+
}
568+
535569
/**
536570
* Get the truncated email displayed when requesting the resetting.
537571
*

0 commit comments

Comments
 (0)