/** * If the user is logged generates the access token and sets into response creating a cookie. * * @param \Kreta\Bundle\UserBundle\Event\AuthorizationEvent $event The authorization event */ public function onAuthorizationEvent(AuthorizationEvent $event) { $client = $this->clientManager->findClientBy(['secret' => $this->clientSecret]); $session = $event->getRequest()->getSession(); $request = new Request(); $request->query->add(['grant_type' => 'password', 'client_secret' => $this->clientSecret, 'client_id' => sprintf('%s_%s', $client->getId(), $client->getRandomId()), 'username' => $session->get('_email'), 'password' => $session->get('_password')]); $response = $this->oauthServer->grantAccessToken($request); $token = json_decode($response->getContent(), true); $event->getRequest()->getSession()->remove('_email'); $event->getRequest()->getSession()->remove('_password'); $event->getRequest()->getSession()->replace(['access_token' => $token['access_token'], 'refresh_token' => $token['refresh_token']]); }
function it_listens_authorization_event(ClientManagerInterface $clientManager, ClientInterface $client, AuthorizationEvent $event, Request $request, SessionInterface $session, OAuth2 $oauthServer, Response $response) { $clientManager->findClientBy(['secret' => 'client-secret'])->shouldBeCalled()->willReturn($client); $event->getRequest()->shouldBeCalled()->willReturn($request); $request->getSession()->shouldBeCalled()->willReturn($session); $client->getId()->shouldBeCalled()->willReturn('the-public-id'); $client->getRandomId()->shouldBeCalled()->willReturn('random-id'); $session->get('_email')->shouldBeCalled()->willReturn('*****@*****.**'); $session->get('_password')->shouldBeCalled()->willReturn('123456'); $oauthServer->grantAccessToken(Argument::type('Symfony\\Component\\HttpFoundation\\Request'))->shouldBeCalled()->willReturn($response); $response->getContent()->shouldBeCalled()->willReturn('the response content'); $session->remove('_email')->shouldBeCalled()->willReturn('*****@*****.**'); $session->remove('_password')->shouldBeCalled()->willReturn('123456'); $session->replace(['access_token' => null, 'refresh_token' => null])->shouldBeCalled(); $this->onAuthorizationEvent($event); }