X-Git-Url: https://git.cweiske.de/grauphel.git/blobdiff_plain/f7ecfa21088175bc236b1136c1a4b2aa2488f37c..6c8ad60e9888fa5625dad2460ca073f93ac1ae0d:/lib/tokenstorage.php diff --git a/lib/tokenstorage.php b/lib/tokenstorage.php index f8f0806..92736cc 100644 --- a/lib/tokenstorage.php +++ b/lib/tokenstorage.php @@ -26,26 +26,47 @@ namespace OCA\Grauphel\Lib; */ class TokenStorage { + /** + * 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) + { + \OC_DB::executeAudited( + 'DELETE FROM `*PREFIX*grauphel_oauth_tokens`' + . ' WHERE `token_key` = ? AND `token_type` = ?', + array($tokenKey, $type) + ); + } + /** * Store the given token * - * @param OAuth_Token $token Token object to store + * @param Token $token Token object to store * * @return void */ - public function store(OAuth_Token $token) + public function store(Token $token) { \OC_DB::executeAudited( 'INSERT INTO `*PREFIX*grauphel_oauth_tokens`' - . '(`token_user`, `token_type`, `token_key`, `token_secret`, `token_verifier`, `token_callback`)' - . ' VALUES(?, ?, ?, ?, ?, ?)', + . '(`token_user`, `token_type`, `token_key`, `token_secret`, `token_verifier`, `token_callback`, `token_client`, `token_lastuse`)' + . ' VALUES(?, ?, ?, ?, ?, ?, ?, ?)', array( $token->user, $token->type, $token->tokenKey, (string) $token->secret, (string) $token->verifier, - (string) $token->callback + (string) $token->callback, + (string) $token->client, + (string) date('c'), ) ); } @@ -64,11 +85,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; @@ -95,12 +112,15 @@ class TokenStorage )->fetchRow(); if ($tokenRow === false) { - throw new OAuthException('Unknown token: ' . $type . ' / ' . $tokenKey); + throw new OAuthException( + 'Unknown token: ' . $type . ' / ' . $tokenKey, + OAUTH_TOKEN_REJECTED + ); } $token = $this->fromDb($tokenRow); if ($token->tokenKey != $tokenKey) { - throw new OAuthException('Invalid token'); + throw new OAuthException('Invalid token', OAUTH_TOKEN_REJECTED); } return $token; @@ -130,6 +150,25 @@ class TokenStorage return $tokens; } + /** + * Update the "last use" field of a token + * + * @param string $tokenKey Random token string to load + * + * @return void + */ + public function updateLastUse($tokenKey) + { + \OC_DB::executeAudited( + 'UPDATE `*PREFIX*grauphel_oauth_tokens`' + . ' SET `token_lastuse` = ? WHERE `token_key` = ?', + array( + (string) date('c'), + $tokenKey, + ) + ); + } + protected function fromDb($tokenRow) { $token = new Token(); @@ -139,6 +178,8 @@ class TokenStorage $token->user = $tokenRow['token_user']; $token->verifier = $tokenRow['token_verifier']; $token->callback = $tokenRow['token_callback']; + $token->client = $tokenRow['token_client']; + $token->lastuse = \strtotime($tokenRow['token_lastuse']); return $token; } }