Add oauth2 adapter as a service

This commit is contained in:
Nicolas Le Goff
2014-03-12 15:04:49 +01:00
parent 40ff8968e9
commit fa9189f78a
5 changed files with 38 additions and 12 deletions

View File

@@ -82,6 +82,7 @@ use Alchemy\Phrasea\Core\Middleware\ApiApplicationMiddlewareProvider;
use Alchemy\Phrasea\Core\Middleware\BasketMiddlewareProvider;
use Alchemy\Phrasea\Core\Middleware\TokenMiddlewareProvider;
use Alchemy\Phrasea\Core\Provider\ACLServiceProvider;
use Alchemy\Phrasea\Core\Provider\APIServiceProvider;
use Alchemy\Phrasea\Core\Provider\AuthenticationManagerServiceProvider;
use Alchemy\Phrasea\Core\Provider\BrowserServiceProvider;
use Alchemy\Phrasea\Core\Provider\BorderManagerServiceProvider;
@@ -218,6 +219,7 @@ class Application extends SilexApplication
$this->register(new ApiApplicationMiddlewareProvider());
$this->register(new ACLServiceProvider());
$this->register(new APIServiceProvider());
$this->register(new AuthenticationManagerServiceProvider());
$this->register(new BorderManagerServiceProvider());
$this->register(new BrowserServiceProvider());

View File

@@ -31,10 +31,6 @@ class Oauth2 implements ControllerProviderInterface
$controllers = $app['controllers_factory'];
$app['oauth'] = $app->share(function ($app) {
return new \API_OAuth2_Adapter($app);
});
/**
* AUTHORIZE ENDPOINT
*
@@ -43,7 +39,7 @@ class Oauth2 implements ControllerProviderInterface
*/
$authorize_func = function () use ($app) {
$request = $app['request'];
$oauth2Adapter = $app['oauth'];
$oauth2Adapter = $app['oauth2-server'];
$context = new Context(Context::CONTEXT_OAUTH2_NATIVE);
$app['dispatcher']->dispatch(PhraseaEvents::PRE_AUTHENTICATE, new PreAuthenticate($request, $context));
@@ -156,7 +152,7 @@ class Oauth2 implements ControllerProviderInterface
throw new HttpException(400, 'This route requires the use of the https scheme', null, ['content-type' => 'application/json']);
}
$app['oauth']->grantAccessToken($request);
$app['oauth2-server']->grantAccessToken($request);
ob_flush();
flush();

View File

@@ -1971,10 +1971,9 @@ class V1 implements ControllerProviderInterface
$app['dispatcher']->dispatch(PhraseaEvents::PRE_AUTHENTICATE, new PreAuthenticate($request, $context));
$app['dispatcher']->dispatch(PhraseaEvents::API_OAUTH2_START, new ApiOAuth2StartEvent());
$oauth2_adapter = new \API_OAuth2_Adapter($app);
$oauth2_adapter->verifyAccessToken();
$app['oauth2-server']->verifyAccessToken();
if (null === $token = $app['repo.api-oauth-tokens']->find($oauth2_adapter->getToken())) {
if (null === $token = $app['repo.api-oauth-tokens']->find($app['oauth2-server']->getToken())) {
throw new NotFoundHttpException('Provided token is not valid.');
}
$app['session']->set('token', $token);
@@ -1993,7 +1992,7 @@ class V1 implements ControllerProviderInterface
}
$app['authentication']->openAccount($oAuth2Account->getUser());
$oauth2_adapter->rememberSession($app['session']);
$app['oauth2-server']->rememberSession($app['session']);
$app['dispatcher']->dispatch(PhraseaEvents::API_OAUTH2_END, new ApiOAuth2EndEvent());
}

View File

@@ -0,0 +1,29 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2014 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Core\Provider;
use Silex\Application;
use Silex\ServiceProviderInterface;
class APIServiceProvider implements ServiceProviderInterface
{
public function register(Application $app)
{
$app['oauth2-server'] = $app->share(function ($app) {
return new \API_OAuth2_Adapter($app);
});
}
public function boot(Application $app)
{
}
}

View File

@@ -80,9 +80,9 @@ class API_OAuth2_Adapter extends OAuth2
* @param Application $app
* @return API_OAuth2_Adapter
*/
public function __construct(Application $app)
public function __construct(Application $app, array $conf = [])
{
parent::__construct();
parent::__construct($conf);
$this->app = $app;
$this->params = [];
}