9 * @author Christian Weiske <cweiske@cweiske.de>
10 * @copyright 2014 Christian Weiske
11 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
12 * @link http://cweiske.de/grauphel.htm
14 namespace OCA\Grauphel\Lib;
21 * @author Christian Weiske <cweiske@cweiske.de>
22 * @copyright 2014 Christian Weiske
23 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
24 * @version Release: @package_version@
25 * @link http://cweiske.de/grauphel.htm
30 * Store the given token
32 * @param Token $token Token object to store
36 public function store(Token $token)
38 \OC_DB::executeAudited(
39 'INSERT INTO `*PREFIX*grauphel_oauth_tokens`'
40 . '(`token_user`, `token_type`, `token_key`, `token_secret`, `token_verifier`, `token_callback`, `token_client`, `token_lastuse`)'
41 . ' VALUES(?, ?, ?, ?, ?, ?, ?, ?)',
46 (string) $token->secret,
47 (string) $token->verifier,
48 (string) $token->callback,
49 (string) $token->client,
56 * Load the token and destroy it.
58 * @param string $type Token type: temp, access, verify
59 * @param string $tokenKey Random token string to load
61 * @return OAuth_Token Stored token
63 * @throws OAuthException When token does not exist
65 public function loadAndDelete($type, $tokenKey)
68 $token = $this->load($type, $tokenKey);
69 \OC_DB::executeAudited(
70 'DELETE FROM `*PREFIX*grauphel_oauth_tokens`'
71 . ' WHERE `token_key` = ? AND `token_type` = ?',
72 array($tokenKey, $type)
75 } catch (OAuthException $e) {
84 * @param string $type Token type: temp, access, verify
85 * @param string $tokenKey Random token string to load
87 * @return OAuth_Token Stored token
89 * @throws OAuthException When token does not exist or it is invalid
91 public function load($type, $tokenKey)
93 $tokenRow = \OC_DB::executeAudited(
94 'SELECT * FROM `*PREFIX*grauphel_oauth_tokens`'
95 . ' WHERE `token_key` = ? AND `token_type` = ?',
96 array($tokenKey, $type)
99 if ($tokenRow === false) {
100 throw new OAuthException(
101 'Unknown token: ' . $type . ' / ' . $tokenKey,
106 $token = $this->fromDb($tokenRow);
107 if ($token->tokenKey != $tokenKey) {
108 throw new OAuthException('Invalid token', OAUTH_TOKEN_REJECTED);
115 * Load multiple tokens
117 * @param string $username User name
118 * @param string $type Token type: temp, access, verify
120 * @return array Array of Token objects
122 public function loadForUser($username, $type)
124 $result = \OC_DB::executeAudited(
125 'SELECT * FROM `*PREFIX*grauphel_oauth_tokens`'
126 . ' WHERE `token_user` = ? AND `token_type` = ?',
127 array($username, $type)
131 while ($tokenRow = $result->fetchRow()) {
132 $tokens[] = $this->fromDb($tokenRow);
139 * Update the "last use" field of a token
141 * @param string $tokenKey Random token string to load
145 public function updateLastUse($tokenKey)
147 \OC_DB::executeAudited(
148 'UPDATE `*PREFIX*grauphel_oauth_tokens`'
149 . ' SET `token_lastuse` = ? WHERE `token_key` = ?',
157 protected function fromDb($tokenRow)
159 $token = new Token();
160 $token->type = $tokenRow['token_user'];
161 $token->tokenKey = $tokenRow['token_key'];
162 $token->secret = $tokenRow['token_secret'];
163 $token->user = $tokenRow['token_user'];
164 $token->verifier = $tokenRow['token_verifier'];
165 $token->callback = $tokenRow['token_callback'];
166 $token->client = $tokenRow['token_client'];
167 $token->lastuse = \strtotime($tokenRow['token_lastuse']);