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
32 * @param string $type Token type: temp, access, verify
33 * @param string $tokenKey Random token string to load
37 * @throws OAuthException When token does not exist
39 public function delete($type, $tokenKey)
41 \OC_DB::executeAudited(
42 'DELETE FROM `*PREFIX*grauphel_oauth_tokens`'
43 . ' WHERE `token_key` = ? AND `token_type` = ?',
44 array($tokenKey, $type)
49 * Store the given token
51 * @param Token $token Token object to store
55 public function store(Token $token)
57 \OC_DB::executeAudited(
58 'INSERT INTO `*PREFIX*grauphel_oauth_tokens`'
59 . '(`token_user`, `token_type`, `token_key`, `token_secret`, `token_verifier`, `token_callback`, `token_client`, `token_lastuse`)'
60 . ' VALUES(?, ?, ?, ?, ?, ?, ?, ?)',
65 (string) $token->secret,
66 (string) $token->verifier,
67 (string) $token->callback,
68 (string) $token->client,
75 * Load the token and destroy it.
77 * @param string $type Token type: temp, access, verify
78 * @param string $tokenKey Random token string to load
80 * @return OAuth_Token Stored token
82 * @throws OAuthException When token does not exist
84 public function loadAndDelete($type, $tokenKey)
87 $token = $this->load($type, $tokenKey);
88 $this->delete($type, $tokenKey);
90 } catch (OAuthException $e) {
99 * @param string $type Token type: temp, access, verify
100 * @param string $tokenKey Random token string to load
102 * @return OAuth_Token Stored token
104 * @throws OAuthException When token does not exist or it is invalid
106 public function load($type, $tokenKey)
108 $tokenRow = \OC_DB::executeAudited(
109 'SELECT * FROM `*PREFIX*grauphel_oauth_tokens`'
110 . ' WHERE `token_key` = ? AND `token_type` = ?',
111 array($tokenKey, $type)
114 if ($tokenRow === false) {
115 throw new OAuthException(
116 'Unknown token: ' . $type . ' / ' . $tokenKey,
121 $token = $this->fromDb($tokenRow);
122 if ($token->tokenKey != $tokenKey) {
123 throw new OAuthException('Invalid token', OAUTH_TOKEN_REJECTED);
130 * Load multiple tokens
132 * @param string $username User name
133 * @param string $type Token type: temp, access, verify
135 * @return array Array of Token objects
137 public function loadForUser($username, $type)
139 $result = \OC_DB::executeAudited(
140 'SELECT * FROM `*PREFIX*grauphel_oauth_tokens`'
141 . ' WHERE `token_user` = ? AND `token_type` = ?',
142 array($username, $type)
146 while ($tokenRow = $result->fetchRow()) {
147 $tokens[] = $this->fromDb($tokenRow);
154 * Update the "last use" field of a token
156 * @param string $tokenKey Random token string to load
160 public function updateLastUse($tokenKey)
162 \OC_DB::executeAudited(
163 'UPDATE `*PREFIX*grauphel_oauth_tokens`'
164 . ' SET `token_lastuse` = ? WHERE `token_key` = ?',
172 protected function fromDb($tokenRow)
174 $token = new Token();
175 $token->type = $tokenRow['token_user'];
176 $token->tokenKey = $tokenRow['token_key'];
177 $token->secret = $tokenRow['token_secret'];
178 $token->user = $tokenRow['token_user'];
179 $token->verifier = $tokenRow['token_verifier'];
180 $token->callback = $tokenRow['token_callback'];
181 $token->client = $tokenRow['token_client'];
182 $token->lastuse = \strtotime($tokenRow['token_lastuse']);