From 4ef8030a93164d1e02947a86988e5b3760bd51b2 Mon Sep 17 00:00:00 2001 From: Alex Bilbie Date: Sun, 28 Apr 2013 23:58:01 +0100 Subject: [PATCH] First commit of PDO storage class --- src/OAuth2/Storage/PDO/Session.php | 151 +++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 src/OAuth2/Storage/PDO/Session.php diff --git a/src/OAuth2/Storage/PDO/Session.php b/src/OAuth2/Storage/PDO/Session.php new file mode 100644 index 00000000..0d6c42ef --- /dev/null +++ b/src/OAuth2/Storage/PDO/Session.php @@ -0,0 +1,151 @@ +prepare('INSERT INTO oauth_sessions (client_id, owner_type, owner_id) VALUE (:clientId, :ownerType, :ownerId)'); + $stmt->bindValue(':clientId', $params['client_id']); + $stmt->bindValue(':ownerType', $params['owner_type']); + $stmt->bindValue(':ownerId', $params['owner_id']); + $stmt->execute(); + + $sessionId = $db->lastInsertId(); + + if (isset($params['redirect_uri'])) { + $stmt = $db->prepare('INSERT INTO oauth_session_redirects (session_id, redirect_uri) VALUE (:sessionId, :redirectUri)'); + $stmt->bindValue(':sessionId', $sessionId); + $stmt->bindValue(':redirectUri', $params['redirect_uri']); + $stmt->execute(); + } + + if (isset($params['auth_code'])) { + $stmt = $db->prepare('INSERT INTO oauth_session_authcodes (session_id, auth_code, auth_code_expires, scope_ids) VALUE (:sessionId, :authCode, :authCodeExpires, :scopeIds)'); + $stmt->bindValue(':sessionId', $sessionId); + $stmt->bindValue(':authCode', $params['auth_code']); + $stmt->bindValue(':authCodeExpires', time() + 600); + $stmt->bindValue(':scopeIds', isset($params['scope_ids']) ? $params['scope_ids'] : null); + $stmt->execute(); + } + + if (isset($params['access_token'])) { + $stmt = $db->prepare('INSERT INTO oauth_session_access_tokens (session_id, access_token, access_token_expires) VALUE (:sessionId, :accessToken, :accessTokenExpire)'); + $stmt->bindValue(':sessionId', $sessionId); + $stmt->bindValue(':accessToken', $params['access_token']); + $stmt->bindValue(':accessTokenExpire', $params['access_token_expire']); + $stmt->execute(); + + $accessTokenId = $db->lastInsertId(); + + if (isset($params['refresh_token']) && $params['refresh_token'] !== null) { + $stmt = $db->prepare('INSERT INTO oauth_session_refresh_tokens (session_access_token_id, refresh_token) VALUE (:accessTokenId, :refreshToken)'); + $stmt->bindValue(':accessTokenId', $accessTokenId); + $stmt->bindValue(':refreshToken', $params['refresh_token']); + $stmt->execute(); + } + } + + return $sessionId; + } + + public function updateSession($sessionId, $params = array()) + { + $db = \ezcDbInstance::get(); + + if (isset($params['access_token'])) { + $stmt = $db->prepare('INSERT INTO oauth_session_access_tokens (session_id, access_token, access_token_expires) VALUE (:sessionId, :accessToken, :accessTokenExpire)'); + $stmt->bindValue(':sessionId', $sessionId); + $stmt->bindValue(':accessToken', $params['access_token']); + $stmt->bindValue(':accessTokenExpire', $params['access_token_expire']); + $stmt->execute(); + + $accessTokenId = $db->lastInsertId(); + + if (isset($params['refresh_token']) && $params['refresh_token'] !== null) { + $stmt = $db->prepare('INSERT INTO oauth_session_refresh_tokens (session_access_token_id, refresh_token) VALUE (:accessTokenId, :refreshToken)'); + $stmt->bindValue(':accessTokenId', $accessTokenId); + $stmt->bindValue(':refreshToken', $params['refresh_token']); + $stmt->execute(); + } + + return $accessTokenId; + } + } + + public function deleteSession($clientId, $type, $typeId) + { + $db = \ezcDbInstance::get(); + + $stmt = $db->prepare('DELETE FROM oauth_sessions WHERE client_id = :clientId AND owner_type = :type AND owner_id = :typeId'); + $stmt->bindValue(':clientId', $clientId); + $stmt->bindValue(':type', $type); + $stmt->bindValue(':typeId', $typeId); + $stmt->execute(); + } + + public function validateAuthCode($clientId, $redirectUri, $authCode) + { + $db = \ezcDbInstance::get(); + + $stmt = $db->prepare('SELECT oauth_sessions.id, oauth_session_authcodes.scope_ids FROM oauth_sessions JOIN oauth_session_authcodes ON oauth_session_authcodes.`session_id` = oauth_sessions.id JOIN oauth_session_redirects ON oauth_session_redirects.`session_id` = oauth_sessions.id WHERE oauth_sessions.client_id = :clientId AND oauth_session_authcodes.`auth_code` = :authCode AND `oauth_session_authcodes`.`auth_code_expires` >= :time AND `oauth_session_redirects`.`redirect_uri` = :redirectUri'); + $stmt->bindValue(':clientId', $clientId); + $stmt->bindValue(':redirectUri', $redirectUri); + $stmt->bindValue(':authCode', $authCode); + $stmt->bindValue(':time', time()); + $stmt->execute(); + + $result = $stmt->fetchObject(); + + return ($result === false) ? false : (array) $result; + } + + public function deleteAuthCode($sessionId) + { + $db = \ezcDbInstance::get(); + + $stmt = $db->prepare('DELETE FROM oauth_session_authcodes WHERE session_id = :sessionId'); + $stmt->bindValue(':sessionId', $sessionId); + $stmt->execute(); + } + + public function validateAccessToken($accessToken) + { + throw new \Exception('Not implemented '.debug_backtrace()[0]['function']); + } + + public function getAccessToken($sessionId) + { + throw new \Exception('Not implemented '.debug_backtrace()[0]['function']); + } + + public function validateRefreshToken($refreshToken, $clientId) + { + throw new \Exception('Not implemented '.debug_backtrace()[0]['function']); + } + + public function updateRefreshToken($sessionId, $newAccessToken, $newRefreshToken, $accessTokenExpires) + { + throw new \Exception('Not implemented '.debug_backtrace()[0]['function']); + } + + public function associateScope($accessTokenId, $scopeId) + { + $db = \ezcDbInstance::get(); + + $stmt = $db->prepare('INSERT INTO `oauth_session_token_scopes` (`session_access_token_id`, `scope_id`) VALUE (:accessTokenId, :scopeId)'); + $stmt->bindValue(':accessTokenId', $accessTokenId); + $stmt->bindValue(':scopeId', $scopeId); + $stmt->execute(); + } + + public function getScopes($sessionId) + { + throw new \Exception('Not implemented '.debug_backtrace()[0]['function']); + } +} \ No newline at end of file