@@ -529,6 +529,59 @@ public function getResettingRequestAction()
529
529
));
530
530
}
531
531
532
+ /**
533
+ * Invalidate Current Access Token
534
+ *
535
+ * @Post("/user/invalidate")
536
+ *
537
+ * @ApiDoc(
538
+ * resource=true,
539
+ * description="Invalidate current access token. Access token to be provided in header (Authorization = Bearer <access token>)",
540
+ * )
541
+ */
542
+ public function invalidateAction ()
543
+ {
544
+ $ request = $ this ->container ->get ('request ' );
545
+
546
+ $ user = $ this ->container ->get ('security.context ' )->getToken ()->getUser ();
547
+ if (!is_object ($ user ) || !$ user instanceof UserInterface) {
548
+ $ this ->logAndThrowError (400 , 'Invalid/Missing Access Token ' , $ this ->get ('translator ' )->trans ('api.show_error_username_missing ' , array (), 'messages ' , $ request ->getLocale ()), $ request ->getLocale ());
549
+ }
550
+
551
+ // Fetch Access Token
552
+ $ token = $ this ->container ->get ('security.context ' )->getToken ()->getToken ();
553
+
554
+ // Fetch Client Id
555
+ $ em = $ this ->getDoctrine ()->getManager ();
556
+ $ connection = $ em ->getConnection ();
557
+ $ statement = $ connection ->prepare ("SELECT client_id FROM oauth2_access_tokens WHERE user_id = :id AND token = :token " );
558
+ $ statement ->bindValue ('id ' , $ user ->getId ());
559
+ $ statement ->bindValue ('token ' , $ token );
560
+ $ statement ->execute ();
561
+ $ results = $ statement ->fetchAll ();
562
+ $ clientId = $ results [0 ]['client_id ' ];
563
+
564
+ // Delete Access Token
565
+ $ accessTokenManager = $ this ->container ->get ('fos_oauth_server.access_token_manager.default ' );
566
+ $ accessToken = $ accessTokenManager ->findTokenBy (array ('token ' => $ token ));
567
+ $ accessTokenManager ->deleteToken ($ accessToken );
568
+
569
+ // Delete Refresh Token
570
+ $ em = $ this ->getDoctrine ()->getManager ();
571
+ $ connection = $ em ->getConnection ();
572
+ $ statement = $ connection ->prepare ("DELETE FROM oauth2_refresh_tokens WHERE user_id = :id AND client_id = :client " );
573
+ $ statement ->bindValue ('id ' , $ user ->getId ());
574
+ $ statement ->bindValue ('client ' , $ clientId );
575
+ $ results = $ statement ->execute ();
576
+
577
+ $ this ->logMessage (200 , 'Token ' .$ token .' Invalidated for ' .$ user ->getUsername ().' ' .$ results );
578
+
579
+ return new JsonResponse (array (
580
+ 'code ' => 201 ,
581
+ 'show_message ' => 'User logged out successfully ' ,
582
+ ));
583
+ }
584
+
532
585
/**
533
586
* Get Access Token. Will return a JsonResponse from oAuth upon success.
534
587
*
0 commit comments