Delete references to API_OAuth2_Application class

This commit is contained in:
Nicolas Le Goff
2014-03-05 20:50:19 +01:00
parent 3ecdd4306b
commit 53dda0b09e
14 changed files with 238 additions and 270 deletions

View File

@@ -13,6 +13,7 @@ namespace Alchemy\Phrasea\Command\Developer;
use Alchemy\Phrasea\Border\Manager; use Alchemy\Phrasea\Border\Manager;
use Alchemy\Phrasea\Command\Command; use Alchemy\Phrasea\Command\Command;
use Alchemy\Phrasea\Model\Entities\ApiApplication;
use Alchemy\Phrasea\Model\Entities\AuthFailure; use Alchemy\Phrasea\Model\Entities\AuthFailure;
use Alchemy\Phrasea\Model\Entities\AggregateToken; use Alchemy\Phrasea\Model\Entities\AggregateToken;
use Alchemy\Phrasea\Model\Entities\Basket; use Alchemy\Phrasea\Model\Entities\Basket;
@@ -124,8 +125,8 @@ class RegenerateSqliteDb extends Command
$fixtures['user']['test_phpunit_alt2'] = $DI['user_alt2']->getId(); $fixtures['user']['test_phpunit_alt2'] = $DI['user_alt2']->getId();
$fixtures['user']['user_guest'] = $DI['user_guest']->getId(); $fixtures['user']['user_guest'] = $DI['user_guest']->getId();
$fixtures['oauth']['user'] = $DI['app-user']->get_id(); $fixtures['oauth']['user'] = $DI['api-app-user']->getId();
$fixtures['oauth']['user_notAdmin'] = $DI['app-user_notAdmin']->get_id(); $fixtures['oauth']['user-not-admin'] = $DI['api-app-user-not-admin']->getId();
$fixtures['databox']['records'] = $DI['databox']->get_sbas_id(); $fixtures['databox']['records'] = $DI['databox']->get_sbas_id();
$fixtures['collection']['coll'] = $DI['coll']->get_base_id(); $fixtures['collection']['coll'] = $DI['coll']->get_base_id();
@@ -182,15 +183,23 @@ class RegenerateSqliteDb extends Command
private function insertOauthApps(\Pimple $DI) private function insertOauthApps(\Pimple $DI)
{ {
$DI['app-user'] = \API_OAuth2_Application::create($this->container, $DI['user'], 'test application for user'); $DI['api-app-user'] = $this->container['manipulator.api-application']->create(
$DI['app-user']->set_redirect_uri('http://callback.com/callback/'); 'test application for user',
$DI['app-user']->set_website('http://website.com/'); ApiApplication::WEB_TYPE,
$DI['app-user']->set_type(\API_OAuth2_Application::WEB_TYPE); 'an api application description',
'http://website.com/',
$DI['user'],
'http://callback.com/callback/'
);
$DI['app-user_notAdmin'] = \API_OAuth2_Application::create($this->container, $DI['user_notAdmin'], 'test application for user not admin'); $DI['api-app-user-not-admin'] = $this->container['manipulator.api-application']->create(
$DI['app-user_notAdmin']->set_redirect_uri('http://callback.com/callback/'); 'test application for user',
$DI['app-user_notAdmin']->set_website('http://website.com/'); ApiApplication::WEB_TYPE,
$DI['app-user_notAdmin']->set_type(\API_OAuth2_Application::WEB_TYPE); 'an api application description',
'http://website.com/',
$DI['user_notAdmin'],
'http://callback.com/callback/'
);
} }
private function insertAuthFailures(EntityManager $em, \Pimple $DI) private function insertAuthFailures(EntityManager $em, \Pimple $DI)

View File

@@ -69,7 +69,8 @@ class Account implements ControllerProviderInterface
->bind('account_auth_apps'); ->bind('account_auth_apps');
// Displays a an authorized app grant // Displays a an authorized app grant
$controllers->get('/security/application/{application_id}/grant/', 'account.controller:grantAccess') $controllers->get('/security/application/{application}/grant/', 'account.controller:grantAccess')
->before($app['middleware.api-application.converter'])
->assert('application_id', '\d+') ->assert('application_id', '\d+')
->bind('grant_app_access'); ->bind('grant_app_access');
@@ -191,33 +192,29 @@ class Account implements ControllerProviderInterface
/** /**
* Display authorized applications that can access user informations * Display authorized applications that can access user informations
* *
* @param Application $app A Silex application where the controller is mounted on * @param Application $app
* @param Request $request The current request * @param Request $request
* @param Integer $application_id The application id * @param ApiApplication $application
* *
* @return JsonResponse * @return JsonResponse
*/ */
public function grantAccess(Application $app, Request $request, $application_id) public function grantAccess(Application $app, Request $request, ApiApplication $application)
{ {
if (!$request->isXmlHttpRequest() || !array_key_exists($request->getMimeType('json'), array_flip($request->getAcceptableContentTypes()))) { if (!$request->isXmlHttpRequest() || !array_key_exists($request->getMimeType('json'), array_flip($request->getAcceptableContentTypes()))) {
$app->abort(400, $app->trans('Bad request format, only JSON is allowed')); $app->abort(400, $app->trans('Bad request format, only JSON is allowed'));
} }
$error = false; if (null === $account = $app['repo.api-accounts']->findByUserAndApplication($app['authentication']->getUser(), $application)) {
return $app->json(['success' => false]);
try {
$account = \API_OAuth2_Account::load_with_user(
$app
, new \API_OAuth2_Application($app, $application_id)
, $app['authentication']->getUser()
);
$account->set_revoked((bool) $request->query->get('revoke'), false);
} catch (NotFoundHttpException $e) {
$error = true;
} }
return $app->json(['success' => !$error]); if ((Boolean) $request->query->get('revoke')) {
$app['manipulator.api-account']->authorizeAccess($account);
} else {
$app['manipulator.api-account']->revokeAccess($account);
}
return $app->json(['success' => true]);
} }
/** /**
@@ -244,7 +241,7 @@ class Account implements ControllerProviderInterface
public function accountAuthorizedApps(Application $app, Request $request) public function accountAuthorizedApps(Application $app, Request $request)
{ {
return $app['twig']->render('account/authorized_apps.html.twig', [ return $app['twig']->render('account/authorized_apps.html.twig', [
"applications" => \API_OAuth2_Application::load_app_by_user($app, $app['authentication']->getUser()), "applications" => $app['repo.api-applications']->findByUser($app['authentication']->getUser()),
]); ]);
} }

View File

@@ -11,6 +11,8 @@
namespace Alchemy\Phrasea\Controller\Root; namespace Alchemy\Phrasea\Controller\Root;
use Alchemy\Phrasea\Exception\InvalidArgumentException;
use Alchemy\Phrasea\Model\Entities\ApiApplication;
use Silex\Application; use Silex\Application;
use Silex\ControllerProviderInterface; use Silex\ControllerProviderInterface;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
@@ -37,23 +39,28 @@ class Developers implements ControllerProviderInterface
$controllers->post('/application/', 'controller.account.developers:newApp') $controllers->post('/application/', 'controller.account.developers:newApp')
->bind('submit_developers_application'); ->bind('submit_developers_application');
$controllers->get('/application/{id}/', 'controller.account.developers:getApp') $controllers->get('/application/{application}/', 'controller.account.developers:getApp')
->before($app['middleware.api-application.converter'])
->assert('id', '\d+') ->assert('id', '\d+')
->bind('developers_application'); ->bind('developers_application');
$controllers->delete('/application/{id}/', 'controller.account.developers:deleteApp') $controllers->delete('/application/{application}/', 'controller.account.developers:deleteApp')
->before($app['middleware.api-application.converter'])
->assert('id', '\d+') ->assert('id', '\d+')
->bind('delete_developers_application'); ->bind('delete_developers_application');
$controllers->post('/application/{id}/authorize_grant_password/', 'controller.account.developers:authorizeGrantpassword') $controllers->post('/application/{application}/authorize_grant_password/', 'controller.account.developers:authorizeGrantPassword')
->before($app['middleware.api-application.converter'])
->assert('id', '\d+') ->assert('id', '\d+')
->bind('submit_developers_application_authorize_grant_password'); ->bind('submit_developers_application_authorize_grant_password');
$controllers->post('/application/{id}/access_token/', 'controller.account.developers:renewAccessToken') $controllers->post('/application/{application}/access_token/', 'controller.account.developers:renewAccessToken')
->before($app['middleware.api-application.converter'])
->assert('id', '\d+') ->assert('id', '\d+')
->bind('submit_developers_application_token'); ->bind('submit_developers_application_token');
$controllers->post('/application/{id}/callback/', 'controller.account.developers:renewAppCallback') $controllers->post('/application/{application}/callback/', 'controller.account.developers:renewAppCallback')
->before($app['middleware.api-application.converter'])
->assert('id', '\d+') ->assert('id', '\d+')
->bind('submit_application_callback'); ->bind('submit_application_callback');
@@ -61,123 +68,97 @@ class Developers implements ControllerProviderInterface
} }
/** /**
* Delete application * Delete application.
*
* @param Application $app
* @param Request $request
* @param ApiApplication $application
* *
* @param Application $app A Silex application where the controller is mounted on
* @param Request $request The current request
* @param integer $id The application id
* @return JsonResponse * @return JsonResponse
*/ */
public function deleteApp(Application $app, Request $request, $id) public function deleteApp(Application $app, Request $request, ApiApplication $application)
{ {
if (!$request->isXmlHttpRequest() || !array_key_exists($request->getMimeType('json'), array_flip($request->getAcceptableContentTypes()))) { if (!$request->isXmlHttpRequest() || !array_key_exists($request->getMimeType('json'), array_flip($request->getAcceptableContentTypes()))) {
$app->abort(400, 'Bad request format, only JSON is allowed'); $app->abort(400, 'Bad request format, only JSON is allowed');
} }
$error = false; $app['manipulator.api-application']->delete($application);
try { return $app->json(['success' => true]);
$clientApp = new \API_OAuth2_Application($app, $id);
$clientApp->delete();
} catch (NotFoundHttpException $e) {
$error = true;
}
return $app->json(['success' => !$error]);
} }
/** /**
* Change application callback * Change application callback.
*
* @param Application $app
* @param Request $request
* @param ApiApplication $application
* *
* @param Application $app A Silex application where the controller is mounted on
* @param Request $request The current request
* @param integer $id The application id
* @return JsonResponse * @return JsonResponse
*/ */
public function renewAppCallback(Application $app, Request $request, $id) public function renewAppCallback(Application $app, Request $request, ApiApplication $application)
{ {
if (!$request->isXmlHttpRequest() || !array_key_exists($request->getMimeType('json'), array_flip($request->getAcceptableContentTypes()))) { if (!$request->isXmlHttpRequest() || !array_key_exists($request->getMimeType('json'), array_flip($request->getAcceptableContentTypes()))) {
$app->abort(400, 'Bad request format, only JSON is allowed'); $app->abort(400, 'Bad request format, only JSON is allowed');
} }
$error = false;
try { try {
$clientApp = new \API_OAuth2_Application($app, $id); $app['manipulator.api-application']->setRedirectUri($request->request->get("callback"));
} catch (InvalidArgumentException $e) {
if (null !== $request->request->get("callback")) { return $app->json(['success' => false]);
$clientApp->set_redirect_uri($request->request->get("callback"));
} else {
$error = true;
}
} catch (NotFoundHttpException $e) {
$error = true;
} }
return $app->json(['success' => !$error]); return $app->json(['success' => true]);
} }
/** /**
* Authorize application to use a grant password type * Authorize application to use a grant password type.
*
* @param Application $app
* @param Request $request
* @param ApiApplication $application
* *
* @param Application $app A Silex application where the controller is mounted on
* @param Request $request The current request
* @param integer $id The application id
* @return JsonResponse * @return JsonResponse
*/ */
public function renewAccessToken(Application $app, Request $request, $id) public function renewAccessToken(Application $app, Request $request, ApiApplication $application)
{ {
if (!$request->isXmlHttpRequest() || !array_key_exists($request->getMimeType('json'), array_flip($request->getAcceptableContentTypes()))) { if (!$request->isXmlHttpRequest() || !array_key_exists($request->getMimeType('json'), array_flip($request->getAcceptableContentTypes()))) {
$app->abort(400, 'Bad request format, only JSON is allowed'); $app->abort(400, 'Bad request format, only JSON is allowed');
} }
$error = false; if (null === $account = $app['repo.api-accounts']->findByUserAndApplication($app['authentication']->getUser(), $application)) {
$accessToken = null; $app->abort(404, sprintf('Account not found for application %s', $application->getName()));
try {
$clientApp = new \API_OAuth2_Application($app, $id);
$account = $clientApp->get_user_account($app['authentication']->getUser());
$token = $account->get_token();
if ($token instanceof \API_OAuth2_Token) {
$token->renew();
} else {
$token = \API_OAuth2_Token::create($app['phraseanet.appbox'], $account, $app['random.medium']);
}
$accessToken = $token->get_value();
} catch (\Exception $e) {
$error = true;
} }
return $app->json(['success' => !$error, 'token' => $accessToken]); $token = $account->getOauthToken();
if ($account->hasOauthToken()) {
$app['manipulator.api-oauth-token']->renew($token);
} else {
$token = $app['manipulator.api-oauth-token']->create($account);
}
return $app->json(['success' => true, 'token' => $token->getOauthToken()]);
} }
/** /**
* Authorize application to use a grant password type * Authorize application to use a grant password type.
*
* @param Application $app
* @param Request $request
* @param ApiApplication $application
* *
* @param Application $app A Silex application where the controller is mounted on
* @param Request $request The current request
* @param integer $id The application id
* @return JsonResponse * @return JsonResponse
*/ */
public function authorizeGrantpassword(Application $app, Request $request, $id) public function authorizeGrantPassword(Application $app, Request $request, ApiApplication $application)
{ {
if (!$request->isXmlHttpRequest() || !array_key_exists($request->getMimeType('json'), array_flip($request->getAcceptableContentTypes()))) { if (!$request->isXmlHttpRequest() || !array_key_exists($request->getMimeType('json'), array_flip($request->getAcceptableContentTypes()))) {
$app->abort(400, 'Bad request format, only JSON is allowed'); $app->abort(400, 'Bad request format, only JSON is allowed');
} }
$error = false; $application->setGrantPassword((Boolean) $request->request->get('grant'));
$app['manipulator.api-application']->update($application);
try { return $app->json(['success' => true]);
$clientApp = new \API_OAuth2_Application($app, $id);
$clientApp->set_grant_password((bool) $request->request->get('grant', false));
} catch (NotFoundHttpException $e) {
$error = true;
}
return $app->json(['success' => !$error]);
} }
/** /**
@@ -189,7 +170,7 @@ class Developers implements ControllerProviderInterface
*/ */
public function newApp(Application $app, Request $request) public function newApp(Application $app, Request $request)
{ {
if ($request->request->get('type') === \API_OAuth2_Application::DESKTOP_TYPE) { if ($request->request->get('type') === ApiApplication::DESKTOP_TYPE) {
$form = new \API_OAuth2_Form_DevAppDesktop($app['request']); $form = new \API_OAuth2_Form_DevAppDesktop($app['request']);
} else { } else {
$form = new \API_OAuth2_Form_DevAppInternet($app['request']); $form = new \API_OAuth2_Form_DevAppInternet($app['request']);
@@ -198,22 +179,22 @@ class Developers implements ControllerProviderInterface
$violations = $app['validator']->validate($form); $violations = $app['validator']->validate($form);
if ($violations->count() === 0) { if ($violations->count() === 0) {
$application = \API_OAuth2_Application::create($app, $app['authentication']->getUser(), $form->getName()); $application = $app['manipulator.api-application']->create(
$application $form->getName(),
->set_description($form->getDescription()) $form->getType(),
->set_redirect_uri($form->getSchemeCallback() . $form->getCallback()) $form->getDescription(),
->set_type($form->getType()) sprintf('%s%s', $form->getSchemeWebsite(), $form->getWebsite()),
->set_website($form->getSchemeWebsite() . $form->getWebsite()); $app['authentication']->getUser(),
sprintf('%s%s', $form->getSchemeCallback(), $form->getCallback())
);
return $app->redirectPath('developers_application', ['id' => $application->get_id()]); return $app->redirectPath('developers_application', ['id' => $application->get_id()]);
} }
$var = [ return $app['twig']->render('/developers/application_form.html.twig', [
"violations" => $violations, "violations" => $violations,
"form" => $form "form" => $form
]; ]);
return $app['twig']->render('/developers/application_form.html.twig', $var);
} }
/** /**
@@ -226,7 +207,7 @@ class Developers implements ControllerProviderInterface
public function listApps(Application $app, Request $request) public function listApps(Application $app, Request $request)
{ {
return $app['twig']->render('developers/applications.html.twig', [ return $app['twig']->render('developers/applications.html.twig', [
"applications" => \API_OAuth2_Application::load_dev_app_by_user($app, $app['authentication']->getUser()) "applications" => $app['repo.api-applications']->findByCreator($app['authentication']->getUser())
]); ]);
} }
@@ -247,25 +228,26 @@ class Developers implements ControllerProviderInterface
} }
/** /**
* Get application information * Gets application information.
* *
* @param Application $app A Silex application where the controller is mounted on * @param Application $app
* @param Request $request The current request * @param Request $request
* @param integer $id The application id * @param ApiApplication $application
* @return Response *
* @return mixed
*/ */
public function getApp(Application $app, Request $request, $id) public function getApp(Application $app, Request $request, ApiApplication $application)
{ {
try { $token = null;
$client = new \API_OAuth2_Application($app, $id);
} catch (NotFoundHttpException $e) { if (null !== $account = $app['repo.api-accounts']->findByUserAndApplication($app['authentication']->getUser(), $application)) {
$app->abort(404); if ($account->hasOauthToken()) {
$token = $account->getOauthToken()->getOauthToken();
}
} }
$token = $client->get_user_account($app['authentication']->getUser())->get_token()->get_value();
return $app['twig']->render('developers/application.html.twig', [ return $app['twig']->render('developers/application.html.twig', [
"application" => $client, "application" => $application,
"user" => $app['authentication']->getUser(), "user" => $app['authentication']->getUser(),
"token" => $token "token" => $token
]); ]);

View File

@@ -9,6 +9,7 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
use Alchemy\Phrasea\Model\Entities\ApiApplication;
use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Constraints; use Symfony\Component\Validator\Constraints;
@@ -48,9 +49,7 @@ class API_OAuth2_Form_DevAppDesktop
public $urlwebsite; public $urlwebsite;
/** /**
* * @param Request $request
* @param Request $request
* @return API_OAuth2_Form_DevApp
*/ */
public function __construct(Request $request) public function __construct(Request $request)
{ {
@@ -58,8 +57,8 @@ class API_OAuth2_Form_DevAppDesktop
$this->description = $request->get('description', ''); $this->description = $request->get('description', '');
$this->scheme_website = $request->get('scheme-website', 'http://'); $this->scheme_website = $request->get('scheme-website', 'http://');
$this->website = $request->get('website', ''); $this->website = $request->get('website', '');
$this->callback = API_OAuth2_Application::NATIVE_APP_REDIRECT_URI; $this->callback = ApiApplication::NATIVE_APP_REDIRECT_URI;
$this->type = API_OAuth2_Application::DESKTOP_TYPE; $this->type = ApiApplication::DESKTOP_TYPE;
$this->urlwebsite = $this->scheme_website . $this->website; $this->urlwebsite = $this->scheme_website . $this->website;

View File

@@ -9,6 +9,7 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
use Alchemy\Phrasea\Model\Entities\ApiApplication;
use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Constraints; use Symfony\Component\Validator\Constraints;
@@ -44,9 +45,7 @@ class API_OAuth2_Form_DevAppInternet
public $urlcallback; public $urlcallback;
/** /**
* * @param Request $request
* @param Request $request
* @return API_OAuth2_Form_DevApp
*/ */
public function __construct(Request $request) public function __construct(Request $request)
{ {
@@ -56,10 +55,10 @@ class API_OAuth2_Form_DevAppInternet
$this->callback = $request->get('callback', ''); $this->callback = $request->get('callback', '');
$this->scheme_website = $request->get('scheme-website', 'http://'); $this->scheme_website = $request->get('scheme-website', 'http://');
$this->scheme_callback = $request->get('scheme-callback', 'http://'); $this->scheme_callback = $request->get('scheme-callback', 'http://');
$this->type = API_OAuth2_Application::WEB_TYPE; $this->type = ApiApplication::WEB_TYPE;
$this->urlwebsite = $this->scheme_website . $this->website; $this->urlwebsite = sprintf('%s%s', $this->scheme_website, $this->website);
$this->urlcallback = $this->scheme_callback . $this->callback; $this->urlcallback = sprintf('%s%s', $this->scheme_callback, $this->callback);
return $this; return $this;
} }

View File

@@ -10,6 +10,7 @@
*/ */
use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Model\Entities\ApiApplication;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class patch_370alpha3a extends patchAbstract class patch_370alpha3a extends patchAbstract
@@ -58,18 +59,20 @@ class patch_370alpha3a extends patchAbstract
*/ */
public function apply(base $appbox, Application $app) public function apply(base $appbox, Application $app)
{ {
try { if (null === $app['repo.api-applications']->findByClientId(\API_OAuth2_Application_Navigator::CLIENT_ID)) {
\API_OAuth2_Application::load_from_client_id($app, \API_OAuth2_Application_Navigator::CLIENT_ID); $application = $app['manipulator.api-applications']->create(
} catch (NotFoundHttpException $e) { \API_OAuth2_Application_Navigator::CLIENT_NAME,
$client = \API_OAuth2_Application::create($app, null, \API_OAuth2_Application_Navigator::CLIENT_NAME); ApiApplication::DESKTOP_TYPE,
'http://www.phraseanet.com',
null,
ApiApplication::NATIVE_APP_REDIRECT_URI
);
$client->set_activated(true); $application->setGrantPassword(true);
$client->set_grant_password(true); $application->setClientId(\API_OAuth2_Application_Navigator::CLIENT_ID);
$client->set_website("http://www.phraseanet.com"); $application->setClientSecret(\API_OAuth2_Application_Navigator::CLIENT_SECRET);
$client->set_client_id(\API_OAuth2_Application_Navigator::CLIENT_ID);
$client->set_client_secret(\API_OAuth2_Application_Navigator::CLIENT_SECRET); $app['manipulator.api-applications']->update($application);
$client->set_type(\API_OAuth2_Application::DESKTOP_TYPE);
$client->set_redirect_uri(\API_OAuth2_Application::NATIVE_APP_REDIRECT_URI);
} }
return true; return true;

View File

@@ -10,6 +10,7 @@
*/ */
use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Model\Entities\ApiApplication;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class patch_3715alpha1a extends patchAbstract class patch_3715alpha1a extends patchAbstract
@@ -59,18 +60,20 @@ class patch_3715alpha1a extends patchAbstract
*/ */
public function apply(base $appbox, Application $app) public function apply(base $appbox, Application $app)
{ {
try { if (null === $app['repo.api-applications']->findByClientId(\API_OAuth2_Application_OfficePlugin::CLIENT_ID)) {
\API_OAuth2_Application::load_from_client_id($app, \API_OAuth2_Application_OfficePlugin::CLIENT_ID); $application = $app['manipulator.api-applications']->create(
} catch (NotFoundHttpException $e) { \API_OAuth2_Application_OfficePlugin::CLIENT_NAME,
$client = \API_OAuth2_Application::create($app, null, \API_OAuth2_Application_OfficePlugin::CLIENT_NAME); ApiApplication::DESKTOP_TYPE,
'http://www.phraseanet.com',
null,
ApiApplication::NATIVE_APP_REDIRECT_URI
);
$client->set_activated(true); $application->setGrantPassword(true);
$client->set_grant_password(true); $application->setClientId(\API_OAuth2_Application_OfficePlugin::CLIENT_ID);
$client->set_website("http://www.phraseanet.com"); $application->setClientSecret(\API_OAuth2_Application_OfficePlugin::CLIENT_SECRET);
$client->set_client_id(\API_OAuth2_Application_OfficePlugin::CLIENT_ID);
$client->set_client_secret(\API_OAuth2_Application_OfficePlugin::CLIENT_SECRET); $app['manipulator.api-applications']->update($application);
$client->set_type(\API_OAuth2_Application::DESKTOP_TYPE);
$client->set_redirect_uri(\API_OAuth2_Application::NATIVE_APP_REDIRECT_URI);
} }
return true; return true;

View File

@@ -32,7 +32,7 @@
</tr> </tr>
<tr> <tr>
<td>{{ "URL de callback" | trans }}</td> <td>{{ "URL de callback" | trans }}</td>
{% if application.get_type() == constant("API_OAuth2_Application::DESKTOP_TYPE") %} {% if application.get_type() == constant("Alchemy\Phrasea\Model\Entities\ApiApplication::DESKTOP_TYPE") %}
<td> <td>
<span>{{ application.get_redirect_uri() }}</span> <span>{{ application.get_redirect_uri() }}</span>
</td> </td>

View File

@@ -7,6 +7,7 @@ use Alchemy\Phrasea\Border\File;
use Alchemy\Phrasea\Controller\Api\V1; use Alchemy\Phrasea\Controller\Api\V1;
use Alchemy\Phrasea\Core\PhraseaEvents; use Alchemy\Phrasea\Core\PhraseaEvents;
use Alchemy\Phrasea\Authentication\Context; use Alchemy\Phrasea\Authentication\Context;
use Alchemy\Phrasea\Model\Entities\ApiApplication;
use Alchemy\Phrasea\Model\Entities\Task; use Alchemy\Phrasea\Model\Entities\Task;
use Alchemy\Phrasea\Model\Entities\User; use Alchemy\Phrasea\Model\Entities\User;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
@@ -27,7 +28,7 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase
*/ */
private static $account; private static $account;
/** /**
* @var \API_OAuth2_Application * @var ApiApplication
*/ */
private static $oauthApplication; private static $oauthApplication;
/** /**
@@ -39,7 +40,7 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase
*/ */
private static $adminAccount; private static $adminAccount;
/** /**
* @var \API_OAuth2_Application * @var \ApiApplication
*/ */
private static $adminApplication; private static $adminApplication;
private static $apiInitialized = false; private static $apiInitialized = false;
@@ -167,9 +168,10 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase
$fail = null; $fail = null;
try { try {
$nativeApp = self::$DI['app']['repo.api-applications']->findByClientId(\API_OAuth2_Application_Navigator::CLIENT_ID);
$nativeApp = \API_OAuth2_Application::load_from_client_id(self::$DI['app'], \API_OAuth2_Application_Navigator::CLIENT_ID); if (null === $nativeApp) {
throw new \Exception(sprintf('%s not found', \API_OAuth2_Application_Navigator::CLIENT_ID));
}
$account = \API_OAuth2_Account::create(self::$DI['app'], self::$DI['user'], $nativeApp); $account = \API_OAuth2_Account::create(self::$DI['app'], self::$DI['user'], $nativeApp);
$token = $account->get_token()->get_value(); $token = $account->get_token()->get_value();
$this->setToken($token); $this->setToken($token);

View File

@@ -4,6 +4,7 @@ namespace Alchemy\Tests\Phrasea\Controller\Api;
use Alchemy\Phrasea\Core\PhraseaEvents; use Alchemy\Phrasea\Core\PhraseaEvents;
use Alchemy\Phrasea\Authentication\Context; use Alchemy\Phrasea\Authentication\Context;
use Alchemy\Phrasea\Model\Entities\ApiApplication;
/** /**
* Test oauthv2 flow based on ietf authv2 spec * Test oauthv2 flow based on ietf authv2 spec
@@ -13,7 +14,7 @@ class OAuth2Test extends \PhraseanetAuthenticatedWebTestCase
{ {
/** /**
* *
* @var API_OAuth2_Application * @var ApiApplication
*/ */
public static $account_id; public static $account_id;
public static $account; public static $account;
@@ -44,26 +45,9 @@ class OAuth2Test extends \PhraseanetAuthenticatedWebTestCase
parent::tearDownAfterClass(); parent::tearDownAfterClass();
} }
public static function deleteInsertedRow(\appbox $appbox, \API_OAuth2_Application $app) public static function deleteInsertedRow(\appbox $appbox, ApiApplication $application)
{ {
$conn = $appbox->get_connection(); self::$DI['app']['manipulator.api-application']->delete($application);
$sql = '
DELETE FROM api_applications
WHERE application_id = :id
';
$t = [':id' => $app->get_id()];
$stmt = $conn->prepare($sql);
$stmt->execute($t);
$stmt->closeCursor();
$sql = '
DELETE FROM api_accounts
WHERE api_account_id = :id
';
$acc = self::getAccount();
$t = [':id' => $acc->get_id()];
$stmt = $conn->prepare($sql);
$stmt->execute($t);
$stmt->closeCursor();
} }
/** /**
@@ -136,11 +120,9 @@ class OAuth2Test extends \PhraseanetAuthenticatedWebTestCase
public function testAuthorizeRedirect() public function testAuthorizeRedirect()
{ {
//session off //session off
$apps = \API_OAuth2_Application::load_authorized_app_by_user(self::$DI['app'], self::$DI['user']); $apps = self::$DI['app']['repos.api-application']->findAuthorizedAppsByUser(self::$DI['user']);
foreach ($apps as $app) { foreach ($apps as $app) {
if ($app->get_client_id() == self::$DI['oauth2-app-user']->get_client_id()) { if ($app->get_client_id() === self::$DI['oauth2-app-user']->getClientId()) {
$authorize = true;
self::$DI['client']->followRedirects(); self::$DI['client']->followRedirects();
} }
} }

View File

@@ -2,6 +2,7 @@
namespace Alchemy\Tests\Phrasea\Controller\Root; namespace Alchemy\Tests\Phrasea\Controller\Root;
use Alchemy\Phrasea\Model\Entities\ApiApplication;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class DevelopersTest extends \PhraseanetAuthenticatedWebTestCase class DevelopersTest extends \PhraseanetAuthenticatedWebTestCase
@@ -34,7 +35,7 @@ class DevelopersTest extends \PhraseanetAuthenticatedWebTestCase
public function testPostNewAppInvalidArguments() public function testPostNewAppInvalidArguments()
{ {
$crawler = self::$DI['client']->request('POST', '/developers/application/', [ $crawler = self::$DI['client']->request('POST', '/developers/application/', [
'type' => \API_OAuth2_Application::WEB_TYPE, 'type' => ApiApplication::WEB_TYPE,
'name' => '', 'name' => '',
'description' => 'okok', 'description' => 'okok',
'website' => 'my.website.com', 'website' => 'my.website.com',
@@ -55,11 +56,11 @@ class DevelopersTest extends \PhraseanetAuthenticatedWebTestCase
*/ */
public function testPostNewApp() public function testPostNewApp()
{ {
$apps = \API_OAuth2_Application::load_dev_app_by_user(self::$DI['app'], self::$DI['user']); $apps = self::$DI['app']['repos.api-applications']->findByCreator(self::$DI['user']);
$nbApp = count($apps); $nbApp = count($apps);
self::$DI['client']->request('POST', '/developers/application/', [ self::$DI['client']->request('POST', '/developers/application/', [
'type' => \API_OAuth2_Application::WEB_TYPE, 'type' => ApiApplication::WEB_TYPE,
'name' => 'hello', 'name' => 'hello',
'description' => 'okok', 'description' => 'okok',
'website' => 'my.website.com', 'website' => 'my.website.com',
@@ -68,7 +69,7 @@ class DevelopersTest extends \PhraseanetAuthenticatedWebTestCase
'scheme-callback' => 'http://' 'scheme-callback' => 'http://'
]); ]);
$apps = \API_OAuth2_Application::load_dev_app_by_user(self::$DI['app'], self::$DI['user']); $apps = self::$DI['app']['repos.api-applications']->findByCreator(self::$DI['user']);
$this->assertTrue(self::$DI['client']->getResponse()->isRedirect()); $this->assertTrue(self::$DI['client']->getResponse()->isRedirect());
$this->assertGreaterThan($nbApp, count($apps)); $this->assertGreaterThan($nbApp, count($apps));
@@ -121,16 +122,16 @@ class DevelopersTest extends \PhraseanetAuthenticatedWebTestCase
*/ */
public function testDeleteApp() public function testDeleteApp()
{ {
$oauthApp = \API_OAuth2_Application::create(self::$DI['app'], self::$DI['user'], 'test app'); $oauthApp = self::$DI['app']['manipulator.api-application']->create(
$this->XMLHTTPRequest('DELETE', '/developers/application/' . $oauthApp->get_id() . '/'); 'test app',
'',
'',
'http://phraseanet.com/'
);
$this->XMLHTTPRequest('DELETE', '/developers/application/' . $oauthApp->getId() . '/');
$this->assertTrue(self::$DI['client']->getResponse()->isOk()); $this->assertTrue(self::$DI['client']->getResponse()->isOk());
try { $this->assertNull(self::$DI['app']['repos.api-application']->find($oauthApp->getId()));
new \API_OAuth2_Application(self::$DI['app'], $oauthApp->get_id());
$this->fail('Application not deleted');
} catch (NotFoundHttpException $e) {
}
} }
/** /**
@@ -183,8 +184,8 @@ class DevelopersTest extends \PhraseanetAuthenticatedWebTestCase
$this->assertTrue(self::$DI['client']->getResponse()->isOk()); $this->assertTrue(self::$DI['client']->getResponse()->isOk());
$content = json_decode(self::$DI['client']->getResponse()->getContent()); $content = json_decode(self::$DI['client']->getResponse()->getContent());
$this->assertTrue($content->success); $this->assertTrue($content->success);
$oauthApp = new \API_OAuth2_Application(self::$DI['app'], $oauthApp->get_id()); $oauthApp = self::$DI['app']['repos.api-application']->find($oauthApp->getId());
$this->assertEquals('my.callback.com', $oauthApp->get_redirect_uri()); $this->assertEquals('my.callback.com', $oauthApp->getRedirectUri());
} }
/** /**
@@ -265,7 +266,7 @@ class DevelopersTest extends \PhraseanetAuthenticatedWebTestCase
$this->assertTrue(self::$DI['client']->getResponse()->isOk()); $this->assertTrue(self::$DI['client']->getResponse()->isOk());
$content = json_decode(self::$DI['client']->getResponse()->getContent()); $content = json_decode(self::$DI['client']->getResponse()->getContent());
$this->assertTrue($content->success); $this->assertTrue($content->success);
$oauthApp = new \API_OAuth2_Application(self::$DI['app'], $oauthApp->get_id()); $oauthApp = self::$DI['app']['repos.api-application']->find($oauthApp->getId());
$this->assertTrue($oauthApp->is_password_granted()); $this->assertTrue($oauthApp->isPasswordGranted());
} }
} }

View File

@@ -199,11 +199,11 @@ abstract class PhraseanetTestCase extends WebTestCase
}); });
self::$DI['oauth2-app-user'] = self::$DI->share(function ($DI) { self::$DI['oauth2-app-user'] = self::$DI->share(function ($DI) {
return new \API_OAuth2_Application($DI['app'], self::$fixtureIds['oauth']['user']); return new $DI['app']['repo.api-applications']->find(self::$fixtureIds['oauth']['user']);
}); });
self::$DI['oauth2-app-user_notAdmin'] = self::$DI->share(function ($DI) { self::$DI['oauth2-app-user_notAdmin'] = self::$DI->share(function ($DI) {
return new \API_OAuth2_Application($DI['app'], self::$fixtureIds['oauth']['user_notAdmin']); return new $DI['app']['repo.api-applications']->find(self::$fixtureIds['oauth']['user-not-admin']);
}); });
self::$DI['logger'] = self::$DI->share(function () { self::$DI['logger'] = self::$DI->share(function () {

View File

@@ -32,7 +32,7 @@ class api_oauthv2_AccountTest extends \PhraseanetTestCase
$this->assertInstanceOf('API_OAuth2_Token', $this->object->get_token()); $this->assertInstanceOf('API_OAuth2_Token', $this->object->get_token());
$this->assertInstanceOf('API_OAuth2_Application', $this->object->get_application()); $this->assertInstanceOf('ApiApplication', $this->object->get_application());
$this->assertEquals(self::$DI['oauth2-app-user'], $this->object->get_application()); $this->assertEquals(self::$DI['oauth2-app-user'], $this->object->get_application());
} }

View File

@@ -1,113 +1,104 @@
<?php <?php
use Alchemy\Phrasea\Model\Entities\ApiApplication;
class api_oauthv2_ApplicationTest extends \PhraseanetTestCase class api_oauthv2_ApplicationTest extends \PhraseanetTestCase
{ {
public function testLoad_from_client_id() public function testLoad_from_client_id()
{ {
$client_id = self::$DI['oauth2-app-user']->get_client_id(); $loaded = self::$DI['app']['repo.api-applications']->findByClientId(self::$DI['oauth2-app-user']->getClientId());
$loaded = API_OAuth2_Application::load_from_client_id(self::$DI['app'], $client_id); $this->assertInstanceOf('ApiApplication', $loaded);
$this->assertInstanceOf('API_OAuth2_Application', $loaded);
$this->assertEquals(self::$DI['oauth2-app-user'], $loaded); $this->assertEquals(self::$DI['oauth2-app-user'], $loaded);
} }
public function testLoad_dev_app_by_user() public function testLoad_dev_app_by_user()
{ {
$apps = API_OAuth2_Application::load_dev_app_by_user(self::$DI['app'], self::$DI['user']); $apps = self::$DI['app']['repo.api-applications']->findByCreator(self::$DI['user']);
$this->assertTrue(is_array($apps)); $this->assertTrue(is_array($apps));
$this->assertTrue(count($apps) > 0); $this->assertTrue(count($apps) > 0);
$found = false; $found = false;
foreach ($apps as $app) { foreach ($apps as $app) {
if ($app->get_id() === self::$DI['oauth2-app-user']->get_id()) if ($app->get_id() === self::$DI['oauth2-app-user']->getId()) {
$found = true; $found = true;
$this->assertInstanceOf('API_OAuth2_Application', $app); }
$this->assertInstanceOf('ApiApplication', $app);
} }
if ( ! $found) if (!$found) {
$this->fail(); $this->fail();
}
} }
public function testLoad_app_by_user() public function testLoad_app_by_user()
{ {
$apps = API_OAuth2_Application::load_app_by_user(self::$DI['app'], self::$DI['user']); $apps = self::$DI['app']['repo.api-applications']->findByUser(self::$DI['user']);
$this->assertTrue(is_array($apps)); $this->assertTrue(is_array($apps));
$this->assertTrue(count($apps) > 0); $this->assertTrue(count($apps) > 0);
$found = false; $found = false;
foreach ($apps as $app) { foreach ($apps as $app) {
if ($app->get_id() === self::$DI['oauth2-app-user']->get_id()) if ($app->get_id() === self::$DI['oauth2-app-user']->get_id()) {
$found = true; $found = true;
$this->assertInstanceOf('API_OAuth2_Application', $app); }
$this->assertInstanceOf('ApiApplication', $app);
} }
if ( ! $found) if (!$found) {
$this->fail(); $this->fail();
}
} }
public function testGettersAndSetters() public function testGettersAndSetters()
{ {
$this->assertTrue(is_int(self::$DI['oauth2-app-user']->get_id())); $this->assertTrue(is_int(self::$DI['oauth2-app-user']->getId()));
$this->assertInstanceOf('Alchemy\Phrasea\Model\Entities\User', self::$DI['oauth2-app-user']->get_creator()); $this->assertInstanceOf('Alchemy\Phrasea\Model\Entities\User', self::$DI['oauth2-app-user']->getCreator());
$this->assertEquals(self::$DI['user']->getId(), self::$DI['oauth2-app-user']->get_creator()->getId()); $this->assertEquals(self::$DI['user']->getId(), self::$DI['oauth2-app-user']->getCreator()->getId());
$this->assertTrue(in_array(self::$DI['oauth2-app-user']->getType(), [ApiApplication::DESKTOP_TYPE, ApiApplication::WEB_TYPE]));
$this->assertTrue(is_string(self::$DI['oauth2-app-user']->getNonce()));
$this->assertEquals(64, strlen(self::$DI['oauth2-app-user']->getNonce()));
self::$DI['oauth2-app-user']->set_type(ApiApplication::WEB_TYPE);
$this->assertEquals(ApiApplication::WEB_TYPE, self::$DI['oauth2-app-user']->getType());
self::$DI['oauth2-app-user']->set_type(ApiApplication::DESKTOP_TYPE);
$this->assertEquals(ApiApplication::DESKTOP_TYPE, self::$DI['oauth2-app-user']->getType());
$this->assertEquals(ApiApplication::NATIVE_APP_REDIRECT_URI, self::$DI['oauth2-app-user']->getRedirectUri());
self::$DI['oauth2-app-user']->setType(ApiApplication::WEB_TYPE);
$this->assertTrue(in_array(self::$DI['oauth2-app-user']->get_type(), [API_OAuth2_Application::DESKTOP_TYPE, API_OAuth2_Application::WEB_TYPE])); self::$DI['oauth2-app-user']->setName('prout');
$this->assertEquals('prout', self::$DI['oauth2-app-user']->getName());
$this->assertTrue(is_string(self::$DI['oauth2-app-user']->get_nonce())); self::$DI['oauth2-app-user']->setName('test application for user');
$this->assertEquals(64, strlen(self::$DI['oauth2-app-user']->get_nonce())); $this->assertEquals('test application for user', self::$DI['oauth2-app-user']->getName());
try {
self::$DI['oauth2-app-user']->set_type('prout');
$this->fail();
} catch (Exception_InvalidArgument $e) {
}
self::$DI['oauth2-app-user']->set_type(API_OAuth2_Application::WEB_TYPE);
$this->assertEquals(API_OAuth2_Application::WEB_TYPE, self::$DI['oauth2-app-user']->get_type());
self::$DI['oauth2-app-user']->set_type(API_OAuth2_Application::DESKTOP_TYPE);
$this->assertEquals(API_OAuth2_Application::DESKTOP_TYPE, self::$DI['oauth2-app-user']->get_type());
$this->assertEquals(API_OAuth2_Application::NATIVE_APP_REDIRECT_URI, self::$DI['oauth2-app-user']->get_redirect_uri());
self::$DI['oauth2-app-user']->set_type(API_OAuth2_Application::WEB_TYPE);
self::$DI['oauth2-app-user']->set_name('prout');
$this->assertEquals('prout', self::$DI['oauth2-app-user']->get_name());
self::$DI['oauth2-app-user']->set_name('test application for user');
$this->assertEquals('test application for user', self::$DI['oauth2-app-user']->get_name());
$desc = 'prouti prouto prout prout'; $desc = 'prouti prouto prout prout';
self::$DI['oauth2-app-user']->set_description($desc); self::$DI['oauth2-app-user']->setDescription($desc);
$this->assertEquals($desc, self::$DI['oauth2-app-user']->get_description()); $this->assertEquals($desc, self::$DI['oauth2-app-user']->getDescription());
self::$DI['oauth2-app-user']->set_description(''); self::$DI['oauth2-app-user']->setDescription('');
$this->assertEquals('', self::$DI['oauth2-app-user']->get_description()); $this->assertEquals('', self::$DI['oauth2-app-user']->getDescription());
$site = 'http://www.example.com/'; $site = 'http://www.example.com/';
self::$DI['oauth2-app-user']->set_website($site); self::$DI['oauth2-app-user']->setWebsite($site);
$this->assertEquals($site, self::$DI['oauth2-app-user']->get_website()); $this->assertEquals($site, self::$DI['oauth2-app-user']->getWebsite());
self::$DI['oauth2-app-user']->set_website(''); self::$DI['oauth2-app-user']->setWebsite('');
$this->assertEquals('', self::$DI['oauth2-app-user']->get_website()); $this->assertEquals('', self::$DI['oauth2-app-user']->getWebsite());
$this->assertInstanceOf('DateTime', self::$DI['oauth2-app-user']->get_created_on()); $this->assertInstanceOf('DateTime', self::$DI['oauth2-app-user']->getCreated());
$this->assertInstanceOf('DateTime', self::$DI['oauth2-app-user']->getUpdated());
$this->assertInstanceOf('DateTime', self::$DI['oauth2-app-user']->get_last_modified()); $this->assertMd5(self::$DI['oauth2-app-user']->getClientId());
$this->assertMd5(self::$DI['oauth2-app-user']->get_client_id());
$client_id = md5('prouto'); $client_id = md5('prouto');
self::$DI['oauth2-app-user']->set_client_id($client_id); self::$DI['oauth2-app-user']->seClientId($client_id);
$this->assertEquals($client_id, self::$DI['oauth2-app-user']->get_client_id()); $this->assertEquals($client_id, self::$DI['oauth2-app-user']->getClientId());
$this->assertMd5(self::$DI['oauth2-app-user']->get_client_id()); $this->assertMd5(self::$DI['oauth2-app-user']->getClientId());
$this->assertMd5(self::$DI['oauth2-app-user']->get_client_secret()); $this->assertMd5(self::$DI['oauth2-app-user']->getClientSecret());
$client_secret = md5('prouto'); $client_secret = md5('prouto');
self::$DI['oauth2-app-user']->set_client_secret($client_secret); self::$DI['oauth2-app-user']->setClientSecret($client_secret);
$this->assertEquals($client_secret, self::$DI['oauth2-app-user']->get_client_secret()); $this->assertEquals($client_secret, self::$DI['oauth2-app-user']->getClientSecret());
$this->assertMd5(self::$DI['oauth2-app-user']->get_client_secret()); $this->assertMd5(self::$DI['oauth2-app-user']->getClientSecret());
$uri = 'http://www.example.com/callback/'; $uri = 'http://www.example.com/callback/';
self::$DI['oauth2-app-user']->set_redirect_uri($uri); self::$DI['oauth2-app-user']->setRedirectUri($uri);
$this->assertEquals($uri, self::$DI['oauth2-app-user']->get_redirect_uri()); $this->assertEquals($uri, self::$DI['oauth2-app-user']->getRedirectUri());
$this->assertInstanceOf('API_OAuth2_Account', self::$DI['oauth2-app-user']->get_user_account(self::$DI['user']));
} }
private function assertmd5($md5) private function assertmd5($md5)