X-Git-Url: https://git.cweiske.de/grauphel.git/blobdiff_plain/eb5c81dea5a60bc65d3ec607daf5ad81fd709928..a730b8aac9a7d644014f501e24151fa8ff4af5b7:/lib/tokenstorage.php diff --git a/lib/tokenstorage.php b/lib/tokenstorage.php index cdbce11..8b266f1 100644 --- a/lib/tokenstorage.php +++ b/lib/tokenstorage.php @@ -14,7 +14,7 @@ namespace OCA\Grauphel\Lib; /** - * Token store + * OAuth token store * * @category Tools * @package Grauphel @@ -26,6 +26,35 @@ namespace OCA\Grauphel\Lib; */ class TokenStorage { + /** + * @var \OCP\IDBConnection + */ + protected $db; + + public function __construct() + { + $this->db = \OC::$server->getDatabaseConnection(); + } + + /** + * Delete token + * + * @param string $type Token type: temp, access, verify + * @param string $tokenKey Random token string to load + * + * @return void + * + * @throws OAuthException When token does not exist + */ + public function delete($type, $tokenKey) + { + $this->db->executeQuery( + 'DELETE FROM `*PREFIX*grauphel_oauth_tokens`' + . ' WHERE `token_key` = ? AND `token_type` = ?', + array($tokenKey, $type) + ); + } + /** * Store the given token * @@ -35,7 +64,7 @@ class TokenStorage */ public function store(Token $token) { - \OC_DB::executeAudited( + $this->db->executeQuery( 'INSERT INTO `*PREFIX*grauphel_oauth_tokens`' . '(`token_user`, `token_type`, `token_key`, `token_secret`, `token_verifier`, `token_callback`, `token_client`, `token_lastuse`)' . ' VALUES(?, ?, ?, ?, ?, ?, ?, ?)', @@ -47,7 +76,7 @@ class TokenStorage (string) $token->verifier, (string) $token->callback, (string) $token->client, - (string) date('c'), + date('Y-m-d H:i:s'), ) ); } @@ -66,11 +95,7 @@ class TokenStorage { try { $token = $this->load($type, $tokenKey); - \OC_DB::executeAudited( - 'DELETE FROM `*PREFIX*grauphel_oauth_tokens`' - . ' WHERE `token_key` = ? AND `token_type` = ?', - array($tokenKey, $type) - ); + $this->delete($type, $tokenKey); return $token; } catch (OAuthException $e) { throw $e; @@ -90,11 +115,11 @@ class TokenStorage */ public function load($type, $tokenKey) { - $tokenRow = \OC_DB::executeAudited( + $tokenRow = $this->db->executeQuery( 'SELECT * FROM `*PREFIX*grauphel_oauth_tokens`' . ' WHERE `token_key` = ? AND `token_type` = ?', array($tokenKey, $type) - )->fetchRow(); + )->fetch(); if ($tokenRow === false) { throw new OAuthException( @@ -121,14 +146,14 @@ class TokenStorage */ public function loadForUser($username, $type) { - $result = \OC_DB::executeAudited( + $result = $this->db->executeQuery( 'SELECT * FROM `*PREFIX*grauphel_oauth_tokens`' . ' WHERE `token_user` = ? AND `token_type` = ?', array($username, $type) ); $tokens = array(); - while ($tokenRow = $result->fetchRow()) { + while ($tokenRow = $result->fetch()) { $tokens[] = $this->fromDb($tokenRow); } @@ -144,11 +169,11 @@ class TokenStorage */ public function updateLastUse($tokenKey) { - \OC_DB::executeAudited( + $this->db->executeQuery( 'UPDATE `*PREFIX*grauphel_oauth_tokens`' . ' SET `token_lastuse` = ? WHERE `token_key` = ?', array( - (string) date('c'), + date('Y-m-d H:i:s'), $tokenKey, ) );