src/Controller/SecurityCognitoController.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\Security\Core\Security;
  10. class SecurityCognitoController extends AbstractController
  11. {
  12.     /**
  13.      * Link to this controller to start the "connect" process
  14.      */
  15.     #[Route("/login-aws"name:"connect_cognito_start")]
  16.     public function connectAction(ClientRegistry $clientRegistry)
  17.     {
  18.         // will redirect to AWS Cognito!
  19.         return $clientRegistry
  20.             ->getClient('cognito'// key used in config/packages/knpu_oauth2_client.yaml
  21.             ->redirect();
  22.     }
  23.     /**
  24.      * After going to Cognito, you're redirected back here
  25.      * because this is the "callback URL" you configured
  26.      * in AWS Cognito APP settings
  27.      */
  28.     #[Route("/security/cognito/check"name:"connect_cognito_check")]
  29.     public function connectCheckAction(Request $requestClientRegistry $clientRegistry)
  30.     {
  31.         // ** if you want to *authenticate* the user, then
  32.         // leave this method blank and create a Guard authenticator
  33.         // $client = $clientRegistry->getClient('cognito');
  34.         // dd('dd',$client);
  35.         // try {
  36.         //     // the exact class depends on which provider you're using
  37.         //     $user = $client->fetchUser();
  38.         //     // do something with all this new power!
  39.         //     // e.g. $name = $user->getFirstName();
  40.         //     var_dump($user); die;
  41.         //     // ...
  42.         // } catch (IdentityProviderException $e) {
  43.         //     // something went wrong!
  44.         //     // probably you should return the reason to the user
  45.         //     var_dump($e->getMessage()); die;
  46.         // }
  47.     }
  48.     #[Route("/logout-aws"name"app_logout")]
  49.     public function logout(Request $requestClientRegistry $clientRegistryRequestStack $requestStackSecurity $security)
  50.     {
  51.         $session $requestStack->getSession();
  52.         $access_token $session->get('token');
  53.         $values $this->getParameter('cognito');
  54.         $client $clientRegistry->getClient('cognito');
  55.         $provider $client->getOAuth2Provider();
  56.        $state $session->get('knpu.oauth2_client_state');
  57.         $client_id = ($values['client_id']) ? $values['client_id'] : '';
  58.         $region = ($values['region']) ? $values['region'] : '';
  59.         $cognitoDomain = ($values['cognito_domain']) ? $values['cognito_domain'] : '';
  60.         $domain $requestStack->getCurrentRequest()->getSchemeAndHttpHost();;
  61.         $security->getToken()->setAuthenticated(false);
  62.         $targetUrl 'https://' $cognitoDomain '.auth.' $region '.amazoncognito.com/logout?response_type=code&client_id=' $client_id '&redirect_uri=' urlencode($domain) . '/security/cognito/check&state=' $state .'&scope=openid+profile+email';
  63.         return new RedirectResponse($targetUrl);
  64.     }
  65. }