Created
January 8, 2014 08:03
-
-
Save Tobur/8313383 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 | |
/** | |
* | |
* @Service("core.event_listener.kernel") | |
* | |
* @Tag("kernel.event_listener", attributes = { "event" = "kernel.request", "method" = "processApi" }) | |
*/ | |
class Kernel | |
{ | |
// Not sure about this path, because we use something different. Check please. | |
const API_CONTROLLER ='/FOS/OAuthServerBundle/Controller/TokenController.php::tokenAction'; | |
protected $em; | |
/** | |
* @InjectParams({ | |
* "em" = @Inject("doctrine.orm.entity_manager") | |
* }) | |
*/ | |
public function __construct($em) | |
{ | |
$this->em = $em; | |
} | |
public function processApi(GetResponseEvent $event) | |
{ | |
$request = $event->getRequest(); | |
$controller = $event->getRequest()->attributes->get('_controller'); | |
if ($controller !== self::API_CONTROLLER) { | |
return; | |
} | |
$clients = $this->em->getRepository('DataBundle:Client')->findAll(); | |
if (!$clients || !isset($clients[0])) { | |
throw new HttpException('API clients are empty. Please, configure them.'); | |
} | |
if (count($clients) != 1) { | |
throw new HttpException('There are more than one API client. Please, configure it correctly.'); | |
} | |
/** | |
* @var Client $client | |
*/ | |
$client = reset($clients); | |
// Setup client id and secret id | |
if ($request->getMethod() === 'POST') { | |
$request->request->set('client_id', $client->getPublicId()); | |
$request->request->set('client_secret', $client->getSecret()); | |
return; | |
} | |
$request->query->set('client_id', $client->getPublicId()); | |
$request->query->set('client_secret', $client->getSecret()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment