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 * @var \OCP\IDBConnection
34 public function __construct()
36 $this->db = \OC::$server->getDatabaseConnection();
42 * @param string $type Token type: temp, access, verify
43 * @param string $tokenKey Random token string to load
47 * @throws OAuthException When token does not exist
49 public function delete($type, $tokenKey)
51 $this->db->executeQuery(
52 'DELETE FROM `*PREFIX*grauphel_oauth_tokens`'
53 . ' WHERE `token_key` = ? AND `token_type` = ?',
54 array($tokenKey, $type)
59 * Store the given token
61 * @param Token $token Token object to store
65 public function store(Token $token)
67 $this->db->executeQuery(
68 'INSERT INTO `*PREFIX*grauphel_oauth_tokens`'
69 . '(`token_user`, `token_type`, `token_key`, `token_secret`, `token_verifier`, `token_callback`, `token_client`, `token_lastuse`)'
70 . ' VALUES(?, ?, ?, ?, ?, ?, ?, ?)',
75 (string) $token->secret,
76 (string) $token->verifier,
77 (string) $token->callback,
78 (string) $token->client,
85 * Load the token and destroy it.
87 * @param string $type Token type: temp, access, verify
88 * @param string $tokenKey Random token string to load
90 * @return OAuth_Token Stored token
92 * @throws OAuthException When token does not exist
94 public function loadAndDelete($type, $tokenKey)
97 $token = $this->load($type, $tokenKey);
98 $this->delete($type, $tokenKey);
100 } catch (OAuthException $e) {
109 * @param string $type Token type: temp, access, verify
110 * @param string $tokenKey Random token string to load
112 * @return OAuth_Token Stored token
114 * @throws OAuthException When token does not exist or it is invalid
116 public function load($type, $tokenKey)
118 $tokenRow = $this->db->executeQuery(
119 'SELECT * FROM `*PREFIX*grauphel_oauth_tokens`'
120 . ' WHERE `token_key` = ? AND `token_type` = ?',
121 array($tokenKey, $type)
124 if ($tokenRow === false) {
125 throw new OAuthException(
126 'Unknown token: ' . $type . ' / ' . $tokenKey,
131 $token = $this->fromDb($tokenRow);
132 if ($token->tokenKey != $tokenKey) {
133 throw new OAuthException('Invalid token', OAUTH_TOKEN_REJECTED);
140 * Load multiple tokens
142 * @param string $username User name
143 * @param string $type Token type: temp, access, verify
145 * @return array Array of Token objects
147 public function loadForUser($username, $type)
149 $result = $this->db->executeQuery(
150 'SELECT * FROM `*PREFIX*grauphel_oauth_tokens`'
151 . ' WHERE `token_user` = ? AND `token_type` = ?',
152 array($username, $type)
156 while ($tokenRow = $result->fetch()) {
157 $tokens[] = $this->fromDb($tokenRow);
164 * Update the "last use" field of a token
166 * @param string $tokenKey Random token string to load
170 public function updateLastUse($tokenKey)
172 $this->db->executeQuery(
173 'UPDATE `*PREFIX*grauphel_oauth_tokens`'
174 . ' SET `token_lastuse` = ? WHERE `token_key` = ?',
182 protected function fromDb($tokenRow)
184 $token = new Token();
185 $token->type = $tokenRow['token_user'];
186 $token->tokenKey = $tokenRow['token_key'];
187 $token->secret = $tokenRow['token_secret'];
188 $token->user = $tokenRow['token_user'];
189 $token->verifier = $tokenRow['token_verifier'];
190 $token->callback = $tokenRow['token_callback'];
191 $token->client = $tokenRow['token_client'];
192 $token->lastuse = \strtotime($tokenRow['token_lastuse']);