Update Oauth2 application

This commit is contained in:
Romain Neutron
2012-09-21 15:52:53 +02:00
parent 4b20df1ea7
commit f40764a2a3
6 changed files with 134 additions and 123 deletions

View File

@@ -9,6 +9,8 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
use Alchemy\Phrasea\Application;
/** /**
* *
* @package OAuth2 Connector * @package OAuth2 Connector
@@ -23,9 +25,9 @@ class API_OAuth2_Account
{ {
/** /**
* *
* @var appbox * @var Application
*/ */
protected $appbox; protected $app;
/** /**
* *
@@ -82,22 +84,22 @@ class API_OAuth2_Account
* @param int $account_id * @param int $account_id
* @return API_OAuth2_Account * @return API_OAuth2_Account
*/ */
public function __construct(appbox &$appbox, $account_id) public function __construct(Application $app, $account_id)
{ {
$this->appbox = $appbox; $this->app = $app;
$this->id = (int) $account_id; $this->id = (int) $account_id;
$sql = 'SELECT api_account_id, usr_id, api_version, revoked $sql = 'SELECT api_account_id, usr_id, api_version, revoked
, application_id, created , application_id, created
FROM api_accounts FROM api_accounts
WHERE api_account_id = :api_account_id'; WHERE api_account_id = :api_account_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':api_account_id' => $this->id)); $stmt->execute(array(':api_account_id' => $this->id));
$row = $stmt->fetch(PDO::FETCH_ASSOC); $row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor(); $stmt->closeCursor();
$this->application_id = (int) $row['application_id']; $this->application_id = (int) $row['application_id'];
$this->user = User_Adapter::getInstance($row['usr_id'], $this->appbox); $this->user = User_Adapter::getInstance($row['usr_id'], $app);
$this->api_version = $row['api_version']; $this->api_version = $row['api_version'];
$this->revoked = ! ! $row['revoked']; $this->revoked = ! ! $row['revoked'];
@@ -159,7 +161,7 @@ class API_OAuth2_Account
, 'account_id' => $this->id , 'account_id' => $this->id
); );
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute($params); $stmt->execute($params);
$stmt->closeCursor(); $stmt->closeCursor();
@@ -183,9 +185,9 @@ class API_OAuth2_Account
{ {
if ( ! $this->token) { if ( ! $this->token) {
try { try {
$this->token = new API_OAuth2_Token($this->appbox, $this); $this->token = new API_OAuth2_Token($this->app['phraseanet.appbox'], $this);
} catch (Exception_NotFound $e) { } catch (Exception_NotFound $e) {
$this->token = API_OAuth2_Token::create($this->appbox, $this); $this->token = API_OAuth2_Token::create($this->app['phraseanet.appbox'], $this);
} }
} }
@@ -199,7 +201,7 @@ class API_OAuth2_Account
public function get_application() public function get_application()
{ {
if ( ! $this->application) if ( ! $this->application)
$this->application = new API_OAuth2_Application($this->appbox, $this->application_id); $this->application = new API_OAuth2_Application($this->app, $this->application_id);
return $this->application; return $this->application;
} }
@@ -212,16 +214,16 @@ class API_OAuth2_Account
{ {
$this->get_token()->delete(); $this->get_token()->delete();
foreach (API_OAuth2_AuthCode::load_codes_by_account($this->appbox, $this) as $code) { foreach (API_OAuth2_AuthCode::load_codes_by_account($this->app, $this) as $code) {
$code->delete(); $code->delete();
} }
foreach (API_OAuth2_RefreshToken::load_by_account($this->appbox, $this) as $token) { foreach (API_OAuth2_RefreshToken::load_by_account($this->app, $this) as $token) {
$token->delete(); $token->delete();
} }
$sql = 'DELETE FROM api_accounts WHERE api_account_id = :account_id'; $sql = 'DELETE FROM api_accounts WHERE api_account_id = :account_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array('account_id' => $this->id)); $stmt->execute(array('account_id' => $this->id));
$stmt->closeCursor(); $stmt->closeCursor();
@@ -235,7 +237,7 @@ class API_OAuth2_Account
* @param API_OAuth2_Application $application * @param API_OAuth2_Application $application
* @return API_OAuth2_Account * @return API_OAuth2_Account
*/ */
public static function create(appbox &$appbox, User_Adapter $user, API_OAuth2_Application $application) public static function create(Application $app, User_Adapter $user, API_OAuth2_Application $application)
{ {
$sql = 'INSERT INTO api_accounts $sql = 'INSERT INTO api_accounts
(api_account_id, usr_id, revoked, api_version, application_id, created) (api_account_id, usr_id, revoked, api_version, application_id, created)
@@ -250,13 +252,13 @@ class API_OAuth2_Account
, ':created' => $datetime->format("Y-m-d H:i:s") , ':created' => $datetime->format("Y-m-d H:i:s")
); );
$stmt = $appbox->get_connection()->prepare($sql); $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute($params); $stmt->execute($params);
$stmt->closeCursor(); $stmt->closeCursor();
$account_id = $appbox->get_connection()->lastInsertId(); $account_id = $app['phraseanet.appbox']->get_connection()->lastInsertId();
return new self($appbox, $account_id); return new self($app, $account_id);
} }
/** /**
@@ -266,7 +268,7 @@ class API_OAuth2_Account
* @param User_Adapter $user * @param User_Adapter $user
* @return API_OAuth2_Account * @return API_OAuth2_Account
*/ */
public static function load_with_user(appbox &$appbox, API_OAuth2_Application $application, User_Adapter $user) public static function load_with_user(Application $app, API_OAuth2_Application $application, User_Adapter $user)
{ {
$sql = 'SELECT api_account_id FROM api_accounts $sql = 'SELECT api_account_id FROM api_accounts
WHERE usr_id = :usr_id AND application_id = :application_id'; WHERE usr_id = :usr_id AND application_id = :application_id';
@@ -276,7 +278,7 @@ class API_OAuth2_Account
":application_id" => $application->get_id() ":application_id" => $application->get_id()
); );
$stmt = $appbox->get_connection()->prepare($sql); $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute($params); $stmt->execute($params);
$row = $stmt->fetch(PDO::FETCH_ASSOC); $row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor(); $stmt->closeCursor();
@@ -285,6 +287,6 @@ class API_OAuth2_Account
throw new Exception_NotFound(); throw new Exception_NotFound();
} }
return new self($appbox, $row['api_account_id']); return new self($app, $row['api_account_id']);
} }
} }

View File

@@ -9,6 +9,8 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
use Alchemy\Phrasea\Application;
/** /**
* *
* *
@@ -35,9 +37,9 @@ class API_OAuth2_Adapter extends OAuth2
/** /**
* *
* @var appbox * @var Application
*/ */
protected $appbox; protected $app;
/** /**
* request parameter * request parameter
@@ -83,14 +85,14 @@ class API_OAuth2_Adapter extends OAuth2
/** /**
* *
* @param appbox $appbox * @param Application $app
* @return API_OAuth2_Adapter * @return API_OAuth2_Adapter
*/ */
public function __construct(appbox $appbox) public function __construct(Application $app)
{ {
parent::__construct(); parent::__construct();
$this->params = array(); $this->params = array();
$this->appbox = $appbox; $this->app = $app;
return $this; return $this;
} }
@@ -179,7 +181,7 @@ class API_OAuth2_Adapter extends OAuth2
protected function checkClientCredentials($client_id, $client_secret = NULL) protected function checkClientCredentials($client_id, $client_secret = NULL)
{ {
try { try {
$application = API_OAuth2_Application::load_from_client_id($this->appbox, $client_id); $application = API_OAuth2_Application::load_from_client_id($this->app, $client_id);
if ($client_secret === NULL) { if ($client_secret === NULL) {
return true; return true;
@@ -202,7 +204,7 @@ class API_OAuth2_Adapter extends OAuth2
*/ */
protected function getRedirectUri($client_id) protected function getRedirectUri($client_id)
{ {
$application = API_OAuth2_Application::load_from_client_id($this->appbox, $client_id); $application = API_OAuth2_Application::load_from_client_id($this->app, $client_id);
return $application->get_redirect_uri(); return $application->get_redirect_uri();
} }
@@ -219,7 +221,7 @@ class API_OAuth2_Adapter extends OAuth2
$result = null; $result = null;
try { try {
$token = API_OAuth2_Token::load_by_oauth_token($this->appbox, $oauth_token); $token = API_OAuth2_Token::load_by_oauth_token($this->app, $oauth_token);
$result = array( $result = array(
'scope' => $token->get_scope() 'scope' => $token->get_scope()
@@ -251,8 +253,8 @@ class API_OAuth2_Adapter extends OAuth2
*/ */
protected function setAccessToken($oauth_token, $account_id, $expires, $scope = NULL) protected function setAccessToken($oauth_token, $account_id, $expires, $scope = NULL)
{ {
$account = new API_OAuth2_Account($this->appbox, $account_id); $account = new API_OAuth2_Account($this->app, $account_id);
$token = API_OAuth2_Token::create($this->appbox, $account, $scope); $token = API_OAuth2_Token::create($this->app, $account, $scope);
$token->set_value($oauth_token)->set_expires($expires); $token->set_value($oauth_token)->set_expires($expires);
return $this; return $this;
@@ -292,7 +294,7 @@ class API_OAuth2_Adapter extends OAuth2
protected function getAuthCode($code) protected function getAuthCode($code)
{ {
try { try {
$code = new API_OAuth2_AuthCode($this->appbox, $code); $code = new API_OAuth2_AuthCode($this->app, $code);
return array( return array(
'redirect_uri' => $code->get_redirect_uri() 'redirect_uri' => $code->get_redirect_uri()
@@ -320,8 +322,8 @@ class API_OAuth2_Adapter extends OAuth2
*/ */
protected function setAuthCode($code, $account_id, $redirect_uri, $expires, $scope = NULL) protected function setAuthCode($code, $account_id, $redirect_uri, $expires, $scope = NULL)
{ {
$account = new API_OAuth2_Account($this->appbox, $account_id); $account = new API_OAuth2_Account($this->app, $account_id);
$code = API_OAuth2_AuthCode::create($this->appbox, $account, $code, $expires); $code = API_OAuth2_AuthCode::create($this->app, $account, $code, $expires);
$code->set_redirect_uri($redirect_uri)->set_scope($scope); $code->set_redirect_uri($redirect_uri)->set_scope($scope);
return $this; return $this;
@@ -332,8 +334,8 @@ class API_OAuth2_Adapter extends OAuth2
*/ */
protected function setRefreshToken($refresh_token, $account_id, $expires, $scope = NULL) protected function setRefreshToken($refresh_token, $account_id, $expires, $scope = NULL)
{ {
$account = new API_OAuth2_Account($this->appbox, $account_id); $account = new API_OAuth2_Account($this->app, $account_id);
API_OAuth2_RefreshToken::create($this->appbox, $account, $expires, $refresh_token, $scope); API_OAuth2_RefreshToken::create($this->app, $account, $expires, $refresh_token, $scope);
return $this; return $this;
} }
@@ -344,7 +346,7 @@ class API_OAuth2_Adapter extends OAuth2
protected function getRefreshToken($refresh_token) protected function getRefreshToken($refresh_token)
{ {
try { try {
$token = new API_OAuth2_RefreshToken($this->appbox, $refresh_token); $token = new API_OAuth2_RefreshToken($this->app, $refresh_token);
return array( return array(
'token' => $token->get_value() 'token' => $token->get_value()
@@ -363,7 +365,7 @@ class API_OAuth2_Adapter extends OAuth2
*/ */
protected function unsetRefreshToken($refresh_token) protected function unsetRefreshToken($refresh_token)
{ {
$token = new API_OAuth2_RefreshToken($this->appbox, $refresh_token); $token = new API_OAuth2_RefreshToken($this->app, $refresh_token);
$token->delete(); $token->delete();
return $this; return $this;
@@ -511,8 +513,8 @@ class API_OAuth2_Adapter extends OAuth2
throw new logicalException("Client property must be set before update an account"); throw new logicalException("Client property must be set before update an account");
try { try {
$user = User_Adapter::getInstance($usr_id, $this->appbox); $user = User_Adapter::getInstance($usr_id, $this->app);
$account = API_OAuth2_Account::load_with_user($this->appbox, $this->client, $user); $account = API_OAuth2_Account::load_with_user($this->app, $this->client, $user);
} catch (Exception $e) { } catch (Exception $e) {
$account = $this->createAccount($usr_id); $account = $this->createAccount($usr_id);
} }
@@ -527,9 +529,9 @@ class API_OAuth2_Adapter extends OAuth2
*/ */
private function createAccount($usr_id) private function createAccount($usr_id)
{ {
$user = User_Adapter::getInstance($usr_id, $this->appbox); $user = User_Adapter::getInstance($usr_id, $this->app);
return API_OAuth2_Account::create($this->appbox, $user, $this->client); return API_OAuth2_Account::create($this->app, $user, $this->client);
} }
/** /**
@@ -576,7 +578,7 @@ class API_OAuth2_Adapter extends OAuth2
public function remember_this_ses_id($ses_id) public function remember_this_ses_id($ses_id)
{ {
try { try {
$token = API_OAuth2_Token::load_by_oauth_token($this->appbox, $this->token); $token = API_OAuth2_Token::load_by_oauth_token($this->app, $this->token);
$token->set_session_id($ses_id); $token->set_session_id($ses_id);
return true; return true;
@@ -702,7 +704,7 @@ class API_OAuth2_Adapter extends OAuth2
$this->errorJsonResponse(OAUTH2_HTTP_BAD_REQUEST, OAUTH2_ERROR_EXPIRED_TOKEN); $this->errorJsonResponse(OAUTH2_HTTP_BAD_REQUEST, OAUTH2_ERROR_EXPIRED_TOKEN);
break; break;
case OAUTH2_GRANT_TYPE_USER_CREDENTIALS: case OAUTH2_GRANT_TYPE_USER_CREDENTIALS:
$application = API_OAuth2_Application::load_from_client_id($this->appbox, $client[0]); $application = API_OAuth2_Application::load_from_client_id($this->app, $client[0]);
if ( ! $application->is_password_granted()) { if ( ! $application->is_password_granted()) {
$this->errorJsonResponse(OAUTH2_HTTP_BAD_REQUEST, OAUTH2_ERROR_UNSUPPORTED_GRANT_TYPE, 'Password grant type is not enable for your client'); $this->errorJsonResponse(OAUTH2_HTTP_BAD_REQUEST, OAUTH2_ERROR_UNSUPPORTED_GRANT_TYPE, 'Password grant type is not enable for your client');
@@ -792,15 +794,13 @@ class API_OAuth2_Adapter extends OAuth2
protected function checkUserCredentials($client_id, $username, $password) protected function checkUserCredentials($client_id, $username, $password)
{ {
try { try {
$appbox = appbox::get_instance(\bootstrap::getCore()); $application = API_OAuth2_Application::load_from_client_id($this->app, $client_id);
$application = API_OAuth2_Application::load_from_client_id($appbox, $client_id); $auth = new \Session_Authentication_Native($this->app, $username, $password);
$auth = new \Session_Authentication_Native($appbox, $username, $password);
$auth->challenge_password(); $auth->challenge_password();
$account = API_OAuth2_Account::load_with_user($appbox, $application, $auth->get_user()); $account = API_OAuth2_Account::load_with_user($this->app, $application, $auth->get_user());
return array( return array(
'redirect_uri' => $application->get_redirect_uri() 'redirect_uri' => $application->get_redirect_uri()

View File

@@ -9,6 +9,8 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
use Alchemy\Phrasea\Application;
/** /**
* *
* @package OAuth2 Connector * @package OAuth2 Connector
@@ -36,9 +38,9 @@ class API_OAuth2_Application
/** /**
* *
* @var appbox * @var Application
*/ */
protected $appbox; protected $app;
/** /**
* *
@@ -126,13 +128,13 @@ class API_OAuth2_Application
/** /**
* *
* @param appbox $appbox * @param Application $app
* @param int $application_id * @param int $application_id
* @return API_OAuth2_Application * @return API_OAuth2_Application
*/ */
public function __construct(appbox &$appbox, $application_id) public function __construct(Application $app, $application_id)
{ {
$this->appbox = $appbox; $this->app = $app;
$this->id = (int) $application_id; $this->id = (int) $application_id;
$sql = ' $sql = '
@@ -143,7 +145,7 @@ class API_OAuth2_Application
FROM api_applications FROM api_applications
WHERE application_id = :application_id'; WHERE application_id = :application_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':application_id' => $this->id)); $stmt->execute(array(':application_id' => $this->id));
if (0 === $stmt->rowCount()) { if (0 === $stmt->rowCount()) {
@@ -152,7 +154,7 @@ class API_OAuth2_Application
$row = $stmt->fetch(PDO::FETCH_ASSOC); $row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor(); $stmt->closeCursor();
$this->creator = ! $row['creator'] ? null : User_Adapter::getInstance($row['creator'], $this->appbox); $this->creator = ! $row['creator'] ? null : User_Adapter::getInstance($row['creator'], $this->app);
$this->type = $row['type']; $this->type = $row['type'];
$this->name = $row['name']; $this->name = $row['name'];
$this->description = $row['description']; $this->description = $row['description'];
@@ -228,7 +230,7 @@ class API_OAuth2_Application
, ':application_id' => $this->id , ':application_id' => $this->id
); );
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute($params); $stmt->execute($params);
$stmt->closeCursor(); $stmt->closeCursor();
@@ -261,7 +263,7 @@ class API_OAuth2_Application
, ':application_id' => $this->id , ':application_id' => $this->id
); );
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute($params); $stmt->execute($params);
$stmt->closeCursor(); $stmt->closeCursor();
@@ -295,7 +297,7 @@ class API_OAuth2_Application
, ':application_id' => $this->id , ':application_id' => $this->id
); );
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute($params); $stmt->execute($params);
$stmt->closeCursor(); $stmt->closeCursor();
@@ -329,7 +331,7 @@ class API_OAuth2_Application
, ':application_id' => $this->id , ':application_id' => $this->id
); );
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute($params); $stmt->execute($params);
$stmt->closeCursor(); $stmt->closeCursor();
@@ -363,7 +365,7 @@ class API_OAuth2_Application
, ':application_id' => $this->id , ':application_id' => $this->id
); );
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute($params); $stmt->execute($params);
$stmt->closeCursor(); $stmt->closeCursor();
@@ -397,7 +399,7 @@ class API_OAuth2_Application
, ':application_id' => $this->id , ':application_id' => $this->id
); );
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute($params); $stmt->execute($params);
$stmt->closeCursor(); $stmt->closeCursor();
@@ -449,7 +451,7 @@ class API_OAuth2_Application
, ':application_id' => $this->id , ':application_id' => $this->id
); );
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute($params); $stmt->execute($params);
$stmt->closeCursor(); $stmt->closeCursor();
@@ -483,7 +485,7 @@ class API_OAuth2_Application
, ':application_id' => $this->id , ':application_id' => $this->id
); );
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute($params); $stmt->execute($params);
$stmt->closeCursor(); $stmt->closeCursor();
@@ -516,7 +518,7 @@ class API_OAuth2_Application
, ':application_id' => $this->id , ':application_id' => $this->id
); );
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute($params); $stmt->execute($params);
$stmt->closeCursor(); $stmt->closeCursor();
@@ -538,7 +540,7 @@ class API_OAuth2_Application
, ':id' => $this->id , ':id' => $this->id
); );
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute($params); $stmt->execute($params);
$row = $stmt->fetch(PDO::FETCH_ASSOC); $row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor(); $stmt->closeCursor();
@@ -546,7 +548,7 @@ class API_OAuth2_Application
if ( ! $row) if ( ! $row)
throw new Exception_NotFound(); throw new Exception_NotFound();
return new API_OAuth2_Account($this->appbox, $row['api_account_id']); return new API_OAuth2_Account($this->app, $row['api_account_id']);
} }
/** /**
@@ -562,7 +564,7 @@ class API_OAuth2_Application
$sql = 'DELETE FROM api_applications $sql = 'DELETE FROM api_applications
WHERE application_id = :application_id'; WHERE application_id = :application_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':application_id' => $this->get_id())); $stmt->execute(array(':application_id' => $this->get_id()));
$stmt->closeCursor(); $stmt->closeCursor();
@@ -578,7 +580,7 @@ class API_OAuth2_Application
$sql = 'SELECT api_account_id FROM api_accounts $sql = 'SELECT api_account_id FROM api_accounts
WHERE application_id = :application_id'; WHERE application_id = :application_id';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':application_id' => $this->get_id())); $stmt->execute(array(':application_id' => $this->get_id()));
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC); $rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->closeCursor(); $stmt->closeCursor();
@@ -586,7 +588,7 @@ class API_OAuth2_Application
$accounts = array(); $accounts = array();
foreach ($rs as $row) { foreach ($rs as $row) {
$accounts[] = new API_OAuth2_Account($this->appbox, $row['api_account_id']); $accounts[] = new API_OAuth2_Account($this->app, $row['api_account_id']);
} }
return $accounts; return $accounts;
@@ -594,12 +596,12 @@ class API_OAuth2_Application
/** /**
* *
* @param appbox $appbox * @param Application $app
* @param User_Adapter $user * @param User_Adapter $user
* @param type $name * @param type $name
* @return API_OAuth2_Application * @return API_OAuth2_Application
*/ */
public static function create(appbox &$appbox, User_Adapter $user = null, $name) public static function create(Application $app, User_Adapter $user = null, $name)
{ {
$sql = ' $sql = '
INSERT INTO api_applications ( INSERT INTO api_applications (
@@ -625,16 +627,16 @@ class API_OAuth2_Application
':grant_password' => 0 ':grant_password' => 0
); );
$stmt = $appbox->get_connection()->prepare($sql); $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute($params); $stmt->execute($params);
$stmt->closeCursor(); $stmt->closeCursor();
$application_id = $appbox->get_connection()->lastInsertId(); $application_id = $app['phraseanet.appbox']->get_connection()->lastInsertId();
$application = new self($appbox, $application_id); $application = new self($app, $application_id);
if ($user) { if ($user) {
API_OAuth2_Account::create($appbox, $user, $application); API_OAuth2_Account::create($app, $user, $application);
} }
return $application; return $application;
@@ -642,16 +644,16 @@ class API_OAuth2_Application
/** /**
* *
* @param appbox $appbox * @param Application $app
* @param type $client_id * @param type $client_id
* @return API_OAuth2_Application * @return API_OAuth2_Application
*/ */
public static function load_from_client_id(appbox &$appbox, $client_id) public static function load_from_client_id(Application $app, $client_id)
{ {
$sql = 'SELECT application_id FROM api_applications $sql = 'SELECT application_id FROM api_applications
WHERE client_id = :client_id'; WHERE client_id = :client_id';
$stmt = $appbox->get_connection()->prepare($sql); $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':client_id' => $client_id)); $stmt->execute(array(':client_id' => $client_id));
$row = $stmt->fetch(PDO::FETCH_ASSOC); $row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor(); $stmt->closeCursor();
@@ -659,7 +661,7 @@ class API_OAuth2_Application
if ( ! $row) if ( ! $row)
throw new Exception_NotFound(); throw new Exception_NotFound();
return new self($appbox, $row['application_id']); return new self($app, $row['application_id']);
} }
/** /**
@@ -668,20 +670,20 @@ class API_OAuth2_Application
* @param User_Adapter $user * @param User_Adapter $user
* @return array * @return array
*/ */
public static function load_dev_app_by_user(appbox &$appbox, User_Adapter $user) public static function load_dev_app_by_user(Application $app, User_Adapter $user)
{ {
$sql = 'SELECT a.application_id $sql = 'SELECT a.application_id
FROM api_applications a, api_accounts b FROM api_applications a, api_accounts b
WHERE a.creator = :usr_id AND a.application_id = b.application_id'; WHERE a.creator = :usr_id AND a.application_id = b.application_id';
$stmt = $appbox->get_connection()->prepare($sql); $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':usr_id' => $user->get_id())); $stmt->execute(array(':usr_id' => $user->get_id()));
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC); $rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->closeCursor(); $stmt->closeCursor();
$apps = array(); $apps = array();
foreach ($rs as $row) { foreach ($rs as $row) {
$apps[] = new API_OAuth2_Application($appbox, $row['application_id']); $apps[] = new API_OAuth2_Application($app, $row['application_id']);
} }
return $apps; return $apps;
@@ -693,26 +695,26 @@ class API_OAuth2_Application
* @param user_adapter $user * @param user_adapter $user
* @return API_OAuth2_Application * @return API_OAuth2_Application
*/ */
public static function load_app_by_user(appbox $appbox, user_adapter $user) public static function load_app_by_user(Application $app, user_adapter $user)
{ {
$sql = 'SELECT a.application_id $sql = 'SELECT a.application_id
FROM api_accounts a, api_applications c FROM api_accounts a, api_applications c
WHERE usr_id = :usr_id AND c.application_id = a.application_id'; WHERE usr_id = :usr_id AND c.application_id = a.application_id';
$stmt = $appbox->get_connection()->prepare($sql); $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':usr_id' => $user->get_id())); $stmt->execute(array(':usr_id' => $user->get_id()));
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC); $rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->closeCursor(); $stmt->closeCursor();
$apps = array(); $apps = array();
foreach ($rs as $row) { foreach ($rs as $row) {
$apps[] = new API_OAuth2_Application($appbox, $row['application_id']); $apps[] = new API_OAuth2_Application($app, $row['application_id']);
} }
return $apps; return $apps;
} }
public static function load_authorized_app_by_user(appbox $appbox, user_adapter $user) public static function load_authorized_app_by_user(Application $app, user_adapter $user)
{ {
$sql = ' $sql = '
SELECT a.application_id SELECT a.application_id
@@ -720,14 +722,14 @@ class API_OAuth2_Application
WHERE usr_id = :usr_id AND c.application_id = a.application_id WHERE usr_id = :usr_id AND c.application_id = a.application_id
AND revoked = 0'; AND revoked = 0';
$stmt = $appbox->get_connection()->prepare($sql); $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':usr_id' => $user->get_id())); $stmt->execute(array(':usr_id' => $user->get_id()));
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC); $rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->closeCursor(); $stmt->closeCursor();
$apps = array(); $apps = array();
foreach ($rs as $row) { foreach ($rs as $row) {
$apps[] = new API_OAuth2_Application($appbox, $row['application_id']); $apps[] = new API_OAuth2_Application($app, $row['application_id']);
} }
return $apps; return $apps;

View File

@@ -9,6 +9,8 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
use Alchemy\Phrasea\Application;
/** /**
* *
* @package OAuth2 Connector * @package OAuth2 Connector
@@ -21,7 +23,7 @@
*/ */
class API_OAuth2_AuthCode class API_OAuth2_AuthCode
{ {
protected $appbox; protected $app;
protected $code; protected $code;
protected $account; protected $account;
protected $account_id; protected $account_id;
@@ -29,14 +31,14 @@ class API_OAuth2_AuthCode
protected $expires; protected $expires;
protected $scope; protected $scope;
public function __construct(appbox &$appbox, $code) public function __construct(Application $app, $code)
{ {
$this->appbox = $appbox; $this->app = $app;
$this->code = $code; $this->code = $code;
$sql = 'SELECT code, api_account_id, redirect_uri, UNIX_TIMESTAMP(expires) AS expires, scope $sql = 'SELECT code, api_account_id, redirect_uri, UNIX_TIMESTAMP(expires) AS expires, scope
FROM api_oauth_codes WHERE code = :code'; FROM api_oauth_codes WHERE code = :code';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':code' => $this->code)); $stmt->execute(array(':code' => $this->code));
$row = $stmt->fetch(PDO::FETCH_ASSOC); $row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor(); $stmt->closeCursor();
@@ -64,7 +66,7 @@ class API_OAuth2_AuthCode
public function get_account() public function get_account()
{ {
if ( ! $this->account) if ( ! $this->account)
$this->account = new API_OAuth2_Account($this->appbox, $this->account_id); $this->account = new API_OAuth2_Account($this->app['phraseanet.appbox'], $this->account_id);
return $this->account; return $this->account;
} }
@@ -81,7 +83,7 @@ class API_OAuth2_AuthCode
$params = array(':redirect_uri' => $redirect_uri, ':code' => $this->code); $params = array(':redirect_uri' => $redirect_uri, ':code' => $this->code);
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute($params); $stmt->execute($params);
$stmt->closeCursor(); $stmt->closeCursor();
@@ -111,7 +113,7 @@ class API_OAuth2_AuthCode
$params = array(':scope' => $scope, ':code' => $this->code); $params = array(':scope' => $scope, ':code' => $this->code);
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute($params); $stmt->execute($params);
$stmt->closeCursor(); $stmt->closeCursor();
@@ -124,7 +126,7 @@ class API_OAuth2_AuthCode
{ {
$sql = 'DELETE FROM api_oauth_codes WHERE code = :code'; $sql = 'DELETE FROM api_oauth_codes WHERE code = :code';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':code' => $this->code)); $stmt->execute(array(':code' => $this->code));
$stmt->closeCursor(); $stmt->closeCursor();
@@ -133,16 +135,16 @@ class API_OAuth2_AuthCode
/** /**
* *
* @param appbox $appbox * @param Application $app
* @param API_OAuth2_Account $account * @param API_OAuth2_Account $account
* @return array * @return array
*/ */
public static function load_codes_by_account(appbox &$appbox, API_OAuth2_Account $account) public static function load_codes_by_account(Application $app, API_OAuth2_Account $account)
{ {
$sql = 'SELECT code FROM api_oauth_codes $sql = 'SELECT code FROM api_oauth_codes
WHERE api_account_id = :account_id'; WHERE api_account_id = :account_id';
$stmt = $appbox->get_connection()->prepare($sql); $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql);
$params = array(":account_id" => $account->get_id()); $params = array(":account_id" => $account->get_id());
$stmt->execute($params); $stmt->execute($params);
@@ -152,7 +154,7 @@ class API_OAuth2_AuthCode
$codes = array(); $codes = array();
foreach ($rs as $row) { foreach ($rs as $row) {
$codes[] = new API_OAuth2_AuthCode($appbox, $row['code']); $codes[] = new API_OAuth2_AuthCode($app, $row['code']);
} }
return $codes; return $codes;
@@ -160,19 +162,19 @@ class API_OAuth2_AuthCode
/** /**
* *
* @param appbox $appbox * @param Application $app
* @param API_OAuth2_Account $account * @param API_OAuth2_Account $account
* @param type $code * @param type $code
* @param int $expires * @param int $expires
* @return API_OAuth2_AuthCode * @return API_OAuth2_AuthCode
*/ */
public static function create(appbox &$appbox, API_OAuth2_Account $account, $code, $expires) public static function create(Application $app, API_OAuth2_Account $account, $code, $expires)
{ {
$sql = 'INSERT INTO api_oauth_codes (code, api_account_id, expires) $sql = 'INSERT INTO api_oauth_codes (code, api_account_id, expires)
VALUES (:code, :account_id, FROM_UNIXTIME(:expires))'; VALUES (:code, :account_id, FROM_UNIXTIME(:expires))';
$stmt = $appbox->get_connection()->prepare($sql); $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql);
$params = array( $params = array(
":code" => $code, ":code" => $code,
@@ -182,6 +184,6 @@ class API_OAuth2_AuthCode
$stmt->execute($params); $stmt->execute($params);
$stmt->closeCursor(); $stmt->closeCursor();
return new self($appbox, $code); return new self($app, $code);
} }
} }

View File

@@ -9,6 +9,8 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
use Alchemy\Phrasea\Application;
/** /**
* *
* @package OAuth2 Connector * @package OAuth2 Connector
@@ -21,22 +23,22 @@
*/ */
class API_OAuth2_RefreshToken class API_OAuth2_RefreshToken
{ {
protected $appbox; protected $app;
protected $token; protected $token;
protected $account_id; protected $account_id;
protected $account; protected $account;
protected $expires; protected $expires;
protected $scope; protected $scope;
public function __construct(appbox &$appbox, $token) public function __construct(Application $app, $token)
{ {
$this->appbox = $appbox; $this->app = $app;
$this->token = $token; $this->token = $token;
$sql = 'SELECT api_account_id, UNIX_TIMESTAMP(expires) AS expires, scope $sql = 'SELECT api_account_id, UNIX_TIMESTAMP(expires) AS expires, scope
FROM api_oauth_refresh_tokens WHERE refresh_token = :token'; FROM api_oauth_refresh_tokens WHERE refresh_token = :token';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':token' => $this->token)); $stmt->execute(array(':token' => $this->token));
$row = $stmt->fetch(PDO::FETCH_ASSOC); $row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor(); $stmt->closeCursor();
@@ -59,8 +61,9 @@ class API_OAuth2_RefreshToken
*/ */
public function get_account() public function get_account()
{ {
if ( ! $this->account) if ( ! $this->account) {
$this->account = new API_OAuth2_Account($this->appbox, $this->account_id); $this->account = new API_OAuth2_Account($this->app, $this->account_id);
}
return $this->account; return $this->account;
} }
@@ -84,7 +87,7 @@ class API_OAuth2_RefreshToken
$sql = 'DELETE FROM api_oauth_refresh_tokens $sql = 'DELETE FROM api_oauth_refresh_tokens
WHERE refresh_token = :refresh_token'; WHERE refresh_token = :refresh_token';
$stmt = $this->appbox->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(":refresh_token" => $this->token)); $stmt->execute(array(":refresh_token" => $this->token));
$stmt->closeCursor(); $stmt->closeCursor();
@@ -93,16 +96,16 @@ class API_OAuth2_RefreshToken
/** /**
* *
* @param appbox $appbox * @param Application $app
* @param API_OAuth2_Account $account * @param API_OAuth2_Account $account
* @return array * @return array
*/ */
public static function load_by_account(appbox &$appbox, API_OAuth2_Account $account) public static function load_by_account(Application $app, API_OAuth2_Account $account)
{ {
$sql = 'SELECT refresh_token FROM api_oauth_refresh_tokens $sql = 'SELECT refresh_token FROM api_oauth_refresh_tokens
WHERE api_account_id = :account_id'; WHERE api_account_id = :account_id';
$stmt = $appbox->get_connection()->prepare($sql); $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':account_id' => $account->get_id())); $stmt->execute(array(':account_id' => $account->get_id()));
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC); $rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->closeCursor(); $stmt->closeCursor();
@@ -110,7 +113,7 @@ class API_OAuth2_RefreshToken
$tokens = array(); $tokens = array();
foreach ($rs as $row) { foreach ($rs as $row) {
$tokens[] = new API_OAuth2_RefreshToken($appbox, $row['refresh_token']); $tokens[] = new API_OAuth2_RefreshToken($app, $row['refresh_token']);
} }
return $tokens; return $tokens;
@@ -118,20 +121,20 @@ class API_OAuth2_RefreshToken
/** /**
* *
* @param appbox $appbox * @param Application $app
* @param API_OAuth2_Account $account * @param API_OAuth2_Account $account
* @param int $expires * @param int $expires
* @param type $refresh_token * @param type $refresh_token
* @param type $scope * @param type $scope
* @return API_OAuth2_RefreshToken * @return API_OAuth2_RefreshToken
*/ */
public static function create(appbox &$appbox, API_OAuth2_Account $account, $expires, $refresh_token, $scope) public static function create(Application $app, API_OAuth2_Account $account, $expires, $refresh_token, $scope)
{ {
$sql = 'INSERT INTO api_oauth_refresh_tokens $sql = 'INSERT INTO api_oauth_refresh_tokens
(refresh_token, api_account_id, expires, scope) (refresh_token, api_account_id, expires, scope)
VALUES (:refresh_token, :account_id, :expires, :scope)'; VALUES (:refresh_token, :account_id, :expires, :scope)';
$stmt = $appbox->get_connection()->prepare($sql); $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql);
$params = array( $params = array(
":refresh_token" => $refresh_token, ":refresh_token" => $refresh_token,
":account_id" => $account->get_id(), ":account_id" => $account->get_id(),
@@ -141,6 +144,6 @@ class API_OAuth2_RefreshToken
$stmt->execute($params); $stmt->execute($params);
$stmt->closeCursor(); $stmt->closeCursor();
return new self($appbox, $refresh_token); return new self($app, $refresh_token);
} }
} }

View File

@@ -9,6 +9,8 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
use Alchemy\Phrasea\Application;
/** /**
* *
* @package OAuth2 Connector * @package OAuth2 Connector
@@ -270,14 +272,14 @@ class API_OAuth2_Token
* @param type $oauth_token * @param type $oauth_token
* @return API_OAuth2_Token * @return API_OAuth2_Token
*/ */
public static function load_by_oauth_token(appbox &$appbox, $oauth_token) public static function load_by_oauth_token(Application $app, $oauth_token)
{ {
$sql = 'SELECT a.api_account_id $sql = 'SELECT a.api_account_id
FROM api_oauth_tokens a, api_accounts b FROM api_oauth_tokens a, api_accounts b
WHERE a.oauth_token = :oauth_token WHERE a.oauth_token = :oauth_token
AND a.api_account_id = b.api_account_id'; AND a.api_account_id = b.api_account_id';
$stmt = $appbox->get_connection()->prepare($sql); $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql);
$params = array(":oauth_token" => $oauth_token); $params = array(":oauth_token" => $oauth_token);
$stmt->execute($params); $stmt->execute($params);
$row = $stmt->fetch(PDO::FETCH_ASSOC); $row = $stmt->fetch(PDO::FETCH_ASSOC);
@@ -286,9 +288,9 @@ class API_OAuth2_Token
if ( ! $row) if ( ! $row)
throw new Exception_NotFound(); throw new Exception_NotFound();
$account = new API_OAuth2_Account($appbox, $row['api_account_id']); $account = new API_OAuth2_Account($app, $row['api_account_id']);
return new self($appbox, $account); return new self($app['phraseanet.appbox'], $account);
} }
/** /**