Created
July 28, 2016 08:14
-
-
Save btribouillet/7a978cdab3a93bba864ed4a5020456d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace SE\AppBundle\Service; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\HttpFoundation\Cookie; | |
class OAuthService | |
{ | |
private $container; | |
private $request; | |
public function __construct(ContainerInterface $container, Request $request) | |
{ | |
$this->container = $container; | |
$this->request = $request; | |
} | |
private function expireSoon($oauth) | |
{ | |
if (isset($oauth->expire_at) && time() > ($oauth->expire_at - 600)) { | |
return true; | |
} | |
return false; | |
} | |
public function getAccessToken() | |
{ | |
$browser = $this->container->get('buzz'); | |
$oauth_url = $this->container->getParameter('api.oauth'); | |
$public_id = $this->container->getParameter('api.public_id'); | |
$secret = $this->container->getParameter('api.secret'); | |
// Get actual token | |
$oauth = json_decode($this->request->cookies->get('oauth')); | |
$access_token = null; | |
if ($oauth) { | |
$access_token = $oauth->access_token; | |
} | |
// If access_token will expire soon | |
if ($this->expireSoon($oauth) && property_exists($oauth, 'refresh_token')) { | |
$response = $browser->get("{$oauth_url}?client_id={$public_id}&client_secret={$secret}&grant_type=refresh_token&refresh_token={$oauth->refresh_token}"); | |
$response = json_decode($response->getContent()); | |
if (property_exists($response, 'access_token')) { | |
$oauth = $response; | |
$access_token = $response->access_token; | |
// add the expire date value to $oauth array | |
$oauth = get_object_vars($oauth); | |
$oauth['expire_at'] = time() + 3600; | |
// store access token information in a cookie | |
$cookie = new Cookie('oauth', json_encode($oauth)); | |
$response = new Response(); | |
$response->headers->setCookie($cookie); | |
$response->send(); | |
} | |
} else { | |
// If no user logged | |
if (!$this->container->get('security.authorization_checker')->isGranted('ROLE_USER')) { | |
// If access_token does not exist | |
if (!$oauth) { | |
// Create an anonymous accessToken | |
$response = $browser->get("{$oauth_url}?client_id={$public_id}&client_secret={$secret}&grant_type=client_credentials"); | |
$response = json_decode($response->getContent()); | |
if (property_exists($response, 'access_token')) { | |
$oauth = $response; | |
$access_token = $response->access_token; | |
// add the expire date value to $oauth array | |
$oauth = get_object_vars($oauth); | |
$oauth['expire_at'] = time() + 3600; | |
// store access token information in a cookie | |
$cookie = new Cookie('oauth', json_encode($oauth), time() + 3600); | |
$response = new Response(); | |
$response->headers->setCookie($cookie); | |
$response->send(); | |
} | |
} | |
} | |
// If user is logged | |
else { | |
$user = $this->container->get('security.token_storage')->getToken()->getUser(); | |
// If accessToken does NOT exist OR will expire soon OR is anonymous | |
if (!$oauth) { | |
// Create an accessToken with an identity | |
$response = $browser->get("{$oauth_url}?grant_type=http://oauth2.dev/grants/api_key&client_id={$public_id}&client_secret={$secret}&api_key={$user->getApiKey()}"); | |
$response = json_decode($response->getContent()); | |
if (property_exists($response, 'access_token')) { | |
$oauth = $response; | |
$access_token = $response->access_token; | |
// add the expire date value to $oauth array | |
$oauth = get_object_vars($oauth); | |
$oauth['expire_at'] = time() + 3600; | |
// store access token information in a cookie | |
$cookie = new Cookie('oauth', json_encode($oauth)); | |
$response = new Response(); | |
$response->headers->setCookie($cookie); | |
$response->send(); | |
} | |
} | |
} | |
} | |
return $access_token; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment