@@ -532,6 +532,59 @@ public function getResettingRequestAction()
532
532
));
533
533
}
534
534
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
+ // Fetch Access Token
555
+ $ token = $ this ->container ->get ('security.context ' )->getToken ()->getToken ();
556
+
557
+ // Fetch Client Id
558
+ $ em = $ this ->getDoctrine ()->getManager ();
559
+ $ connection = $ em ->getConnection ();
560
+ $ statement = $ connection ->prepare ("SELECT client_id FROM oauth2_access_tokens WHERE user_id = :id AND token = :token " );
561
+ $ statement ->bindValue ('id ' , $ user ->getId ());
562
+ $ statement ->bindValue ('token ' , $ token );
563
+ $ statement ->execute ();
564
+ $ results = $ statement ->fetchAll ();
565
+ $ clientId = $ results [0 ]['client_id ' ];
566
+
567
+ // Delete Access Token
568
+ $ accessTokenManager = $ this ->container ->get ('fos_oauth_server.access_token_manager.default ' );
569
+ $ accessToken = $ accessTokenManager ->findTokenBy (array ('token ' => $ token ));
570
+ $ accessTokenManager ->deleteToken ($ accessToken );
571
+
572
+ // Delete Refresh Token
573
+ $ em = $ this ->getDoctrine ()->getManager ();
574
+ $ connection = $ em ->getConnection ();
575
+ $ statement = $ connection ->prepare ("DELETE FROM oauth2_refresh_tokens WHERE user_id = :id AND client_id = :client " );
576
+ $ statement ->bindValue ('id ' , $ user ->getId ());
577
+ $ statement ->bindValue ('client ' , $ clientId );
578
+ $ results = $ statement ->execute ();
579
+
580
+ $ this ->logMessage (200 , 'Token ' .$ token .' Invalidated for ' .$ user ->getUsername ().' ' .$ results );
581
+
582
+ return new JsonResponse (array (
583
+ 'code ' => 201 ,
584
+ 'show_message ' => 'User logged out successfully ' ,
585
+ ));
586
+ }
587
+
535
588
/**
536
589
* Get the truncated email displayed when requesting the resetting.
537
590
*
0 commit comments