Created
July 12, 2012 20:39
-
-
Save zircote/3100816 to your computer and use it in GitHub Desktop.
An example of using Zend_Db_Adapter_* as the storage connector for OAuth2 library at https://github.com/zircote/oauth2-php
This file contains hidden or 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 | |
| /** | |
| * @category Cautela | |
| * @package Auth | |
| * @subpackage OAuth2 | |
| */ | |
| namespace Auth\OAuth2; | |
| use OAuth2\Grant\GrantCodeInterface; | |
| use OAuth2\RefreshTokensInterface; | |
| use Zend_Db_Adapter_Abstract; | |
| use Zend_Db_Exception; | |
| /** | |
| * | |
| * @category OAuth2 | |
| * @package Auth | |
| * @subpackage OAuth2 | |
| */ | |
| class ZendDb implements GrantCodeInterface, RefreshTokensInterface | |
| { | |
| /** | |
| * Database table names | |
| * | |
| * @var array | |
| */ | |
| protected $_tableMap = array( | |
| 'clients' => 'clients', | |
| 'auth_code' => 'auth_code', | |
| 'access_tokens' => 'access_tokens', | |
| 'refresh_tokens' => 'refresh_tokens' | |
| ); | |
| /** | |
| * @var Zend_Db_Adapter_Abstract | |
| */ | |
| protected $_db; | |
| /** | |
| * | |
| */ | |
| public function __construct(Zend_Db_Adapter_Abstract $db) | |
| { | |
| try { | |
| $this->db = $db; | |
| } catch (Zend_Db_Exception $e) { | |
| die('Connection failed: ' . $e->getMessage()); | |
| } | |
| } | |
| /** | |
| * @param array $tableMap | |
| * @return ZendDb | |
| */ | |
| public function setTableMap(array $tableMap) | |
| { | |
| $this->_tableMap = $tableMap; | |
| return $this; | |
| } | |
| /** | |
| * @return Zend_Db_Adapter_Abstract | |
| */ | |
| public function getDb() | |
| { | |
| return $this->_db; | |
| } | |
| /** | |
| * @param Zend_Db_Adapter_Abstract $db | |
| * @return ZendDb | |
| */ | |
| public function setDb(Zend_Db_Adapter_Abstract $db) | |
| { | |
| $this->_db = $db; | |
| return $this; | |
| } | |
| /** | |
| * | |
| */ | |
| protected function handleException(\Exception $e) | |
| { | |
| throw $e; | |
| } | |
| /** | |
| * | |
| * @param string $client_id Client identifier to be stored. | |
| * @param string $client_secret Client secret to be stored. | |
| * @param string $redirect_uri Redirect URI to be stored. | |
| * @return ZendDb | |
| */ | |
| public function addClient($client_id, $client_secret, $redirect_uri) | |
| { | |
| try { | |
| $client_secret = $this->hash($client_secret, $client_id); | |
| $data = array( | |
| 'client_id' => $client_id, | |
| 'client_secret' => $client_secret, | |
| 'redirect_uri' => $redirect_uri | |
| ); | |
| $this->getDb()->insert($this->_tableMap['clients'], $data); | |
| } | |
| catch (Zend_Db_Exception $e) { | |
| $this->handleException($e); | |
| } | |
| return $this; | |
| } | |
| /** | |
| * @param string $client_id | |
| * @param null|string $client_secret | |
| * @return bool | |
| */ | |
| public function checkClientCredentials($client_id, $client_secret = null) | |
| { | |
| try { | |
| $sql = $this->getDb()->select() | |
| ->from($this->_tableMap['clients'], 'client_secret') | |
| ->where($this->getDb()->quoteInto('client_id = ?', $client_id)); | |
| $hashed_secret = $this->getDb()->fetchOne($sql); | |
| if ($client_secret === null) { | |
| return $hashed_secret !== false; | |
| } | |
| return $this->checkPassword( | |
| $client_secret, $hashed_secret, $client_id | |
| ); | |
| } | |
| catch (Zend_Db_Exception $e) { | |
| $this->handleException($e); | |
| } | |
| return false; | |
| } | |
| /** | |
| * @param $client_id | |
| * @return bool|string|void | |
| */ | |
| public function getClientDetails($client_id) | |
| { | |
| try { | |
| $sql = $this->getDb()->select() | |
| ->from($this->_tableMap['clients'], 'redirect_uri') | |
| ->where($this->getDb()->quoteInto('client_id = ?', $client_id)); | |
| $redirect_uri = $this->getDb()->fetchOne($sql); | |
| if ($redirect_uri === false) { | |
| return $redirect_uri !== false; | |
| } | |
| } | |
| catch (Zend_Db_Exception $e) { | |
| $this->handleException($e); | |
| } | |
| return null; | |
| } | |
| /** | |
| * @param string $oauth_token | |
| * @return array|null | |
| */ | |
| public function getAccessToken($oauth_token) | |
| { | |
| return $this->getToken($oauth_token, false); | |
| } | |
| /** | |
| * @param string $oauth_token | |
| * @param string $client_id | |
| * @param string $user_id | |
| * @param string $expires | |
| * @param null $scope | |
| * @return ZendDb | |
| */ | |
| public function setAccessToken($oauth_token, $client_id, $user_id, $expires, | |
| $scope = null) | |
| { | |
| return $this->setToken( | |
| $oauth_token, $client_id, $user_id, $expires, $scope, false | |
| ); | |
| } | |
| /** | |
| * @param string $refresh_token | |
| * @return array|null | |
| */ | |
| public function getRefreshToken($refresh_token) | |
| { | |
| return $this->getToken($refresh_token, true); | |
| } | |
| /** | |
| * @param string $refresh_token | |
| * @param string $client_id | |
| * @param string $user_id | |
| * @param string $expires | |
| * @param null $scope | |
| * @return ZendDb | |
| */ | |
| public function setRefreshToken($refresh_token, $client_id, $user_id, | |
| $expires, $scope = null) | |
| { | |
| return $this->setToken( | |
| $refresh_token, $client_id, $user_id, $expires, $scope, true | |
| ); | |
| } | |
| /** | |
| * @param string $refresh_token | |
| * @return ZendDb | |
| */ | |
| public function unsetRefreshToken($refresh_token) | |
| { | |
| try { | |
| $this->getDb()->delete( | |
| $this->_tableMap['refresh_tokens'], | |
| $this->getDb()->quoteInto('refresh_token = ?', $refresh_token) | |
| ); | |
| } | |
| catch (Zend_Db_Exception $e) { | |
| $this->handleException($e); | |
| } | |
| return $this; | |
| } | |
| /** | |
| * @param string $code | |
| * @return array|null | |
| */ | |
| public function getAuthCode($code) | |
| { | |
| try { | |
| $sql = $this->getDb()->select() | |
| ->from( | |
| $this->_tableMap['auth_codes'], array( | |
| 'code', 'client_id', | |
| 'user_id', 'redirect_uri', | |
| 'expires', 'scope' | |
| ) | |
| ) | |
| ->where($this->getDb()->quoteInto('code = ?', $code)); | |
| $result = $this->getDb()->fetchRow($sql); | |
| return $result !== false ? $result : null; | |
| } | |
| catch (Zend_Db_Exception $e) { | |
| $this->handleException($e); | |
| } | |
| return null; | |
| } | |
| /** | |
| * @param string $code | |
| * @param string $client_id | |
| * @param string $user_id | |
| * @param string $redirect_uri | |
| * @param string $expires | |
| * @param null $scope | |
| * @return ZendDb | |
| */ | |
| public function setAuthCode($code, $client_id, $user_id, $redirect_uri, | |
| $expires, $scope = null) | |
| { | |
| try { | |
| $data = array( | |
| 'code' => $code, 'client_code' => $client_id, | |
| 'user_id' => $user_id, 'redirect_uri' => $redirect_uri, | |
| 'expires' => $expires, 'scope' => $scope | |
| ); | |
| $this->getDb()->insert($this->_tableMap['auth_codes'], $data); | |
| } | |
| catch (Zend_Db_Exception $e) { | |
| $this->handleException($e); | |
| } | |
| return $this; | |
| } | |
| /** | |
| * @deprecated | |
| * | |
| * @param string $client_id | |
| * @param string $grant_type | |
| * @return bool | |
| */ | |
| public function checkRestrictedGrantType($client_id, $grant_type) | |
| { | |
| return true; | |
| } | |
| /** | |
| * Creates a refresh or access token | |
| * | |
| * @param string $token - Access or refresh token id | |
| * @param string $client_id | |
| * @param mixed $user_id | |
| * @param int $expires | |
| * @param string $scope | |
| * @param bool $isRefresh | |
| * @return ZendDb | |
| */ | |
| protected function setToken($token, $client_id, $user_id, $expires, $scope, | |
| $isRefresh = true) | |
| { | |
| try { | |
| $data = array( | |
| 'token' => $token, 'client_id' => $client_id, | |
| 'user_id' => $user_id, 'expires' => $expires, 'scope' => $scope | |
| ); | |
| $this->getDb()->insert( | |
| $isRefresh ? $this->_tableMap['refresh_tokens'] : | |
| $this->_tableMap['access_tokens'], $data | |
| ); | |
| } | |
| catch (Zend_Db_Exception $e) { | |
| $this->handleException($e); | |
| } | |
| return $this; | |
| } | |
| /** | |
| * Retrieves an access or refresh token. | |
| * | |
| * @param string $token | |
| * @param bool $refresh | |
| */ | |
| protected function getToken($token, $isRefresh = true) | |
| { | |
| try { | |
| $tableName = $isRefresh ? $this->_tableMap['refresh_tokens'] : | |
| $this->_tableMap['access_tokens']; | |
| $tokenName = $isRefresh ? 'refresh_token' : 'oauth_token'; | |
| $sql = $this->getDb()->select() | |
| ->from( | |
| $tableName, | |
| array($tokenName, 'client_id', 'expires', 'scope', 'user_id') | |
| )->where($this->getDb()->quoteInto('token = ?', $token)); | |
| $result = $this->getDb()->fetchRow($sql); | |
| return $result !== false ? $result : null; | |
| } | |
| catch (Zend_Db_Exception $e) { | |
| $this->handleException($e); | |
| } | |
| return null; | |
| } | |
| /** | |
| * | |
| * @param string $secret | |
| * @return string | |
| */ | |
| protected function hash($client_secret, $client_id) | |
| { | |
| return hash('blowfish', $client_id . $client_secret); | |
| } | |
| /** | |
| * | |
| * @param string $client_id | |
| * @param string $client_secret | |
| * @param string $actualPassword | |
| */ | |
| protected function checkPassword($try, $client_secret, $client_id) | |
| { | |
| return $try == $this->hash($client_secret, $client_id); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/zircote/oauth2-php