|
@@ -0,0 +1,72 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace Base\OAuthClientBundle\EventListener;
|
|
|
+
|
|
|
+use Symfony\Component\HttpKernel\Event\GetResponseEvent;
|
|
|
+
|
|
|
+class RequestListener
|
|
|
+{
|
|
|
+
|
|
|
+ protected $securityTokenStorage;
|
|
|
+
|
|
|
+ protected $client_id;
|
|
|
+
|
|
|
+ protected $client_secret;
|
|
|
+
|
|
|
+ protected $access_token_url;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param SecurityTokenStorage $securityTokenStorage
|
|
|
+ * @param string $client_id
|
|
|
+ * @param string $client_secret
|
|
|
+ * @param string $access_token_url
|
|
|
+ */
|
|
|
+ public function __construct($securityTokenStorage, $client_id, $client_secret, $access_token_url)
|
|
|
+ {
|
|
|
+ $this->securityTokenStorage = $securityTokenStorage;
|
|
|
+ $this->client_id = $client_id;
|
|
|
+ $this->client_secret = $client_secret;
|
|
|
+ $this->access_token_url = $access_token_url;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param GetResponseEvent $event
|
|
|
+ * @return type
|
|
|
+ */
|
|
|
+ public function onKernelRequest(GetResponseEvent $event)
|
|
|
+ {
|
|
|
+ if (!$event->isMasterRequest()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ $token = $this->securityTokenStorage->getToken();
|
|
|
+ if ($token && method_exists($token, 'isExpired') && $token->isExpired()) {
|
|
|
+ $parameters = array(
|
|
|
+ 'refresh_token' => $token->getRefreshToken(),
|
|
|
+ 'grant_type' => 'refresh_token',
|
|
|
+ 'client_id' => $this->client_id,
|
|
|
+ 'client_secret' => $this->client_secret
|
|
|
+ );
|
|
|
+
|
|
|
+ $curl = curl_init();
|
|
|
+ curl_setopt_array($curl, array(
|
|
|
+ CURLOPT_RETURNTRANSFER => 1,
|
|
|
+ CURLOPT_URL => $this->access_token_url . '?' . http_build_query($parameters, '', '&')
|
|
|
+ ));
|
|
|
+ $response = curl_exec($curl);
|
|
|
+ curl_close($curl);
|
|
|
+
|
|
|
+ $newToken = json_decode($response, true);
|
|
|
+
|
|
|
+ $token->setAccessToken($newToken['access_token']);
|
|
|
+ $token->setRefreshToken($newToken['refresh_token']);
|
|
|
+ $token->setExpiresIn($newToken['expires_in']);
|
|
|
+
|
|
|
+ $this->securityTokenStorage->setToken($token);
|
|
|
+ }
|
|
|
+
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|