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 OAuth_Token $token Token object to store
36 public function store(OAuth_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`)'
41 . ' VALUES(?, ?, ?, ?, ?, ?)',
46 (string) $token->secret,
47 (string) $token->verifier,
48 (string) $token->callback
54 * Load the token and destroy it.
56 * @param string $type Token type: temp, access, verify
57 * @param string $tokenKey Random token string to load
59 * @return OAuth_Token Stored token
61 * @throws OAuthException When token does not exist
63 public function loadAndDelete($type, $tokenKey)
66 $token = $this->load($type, $tokenKey);
67 \OC_DB::executeAudited(
68 'DELETE FROM `*PREFIX*grauphel_oauth_tokens`'
69 . ' WHERE `token_key` = ? AND `token_type` = ?',
70 array($tokenKey, $type)
73 } catch (OAuthException $e) {
82 * @param string $type Token type: temp, access, verify
83 * @param string $tokenKey Random token string to load
85 * @return OAuth_Token Stored token
87 * @throws OAuthException When token does not exist or it is invalid
89 public function load($type, $tokenKey)
91 $tokenRow = \OC_DB::executeAudited(
92 'SELECT * FROM `*PREFIX*grauphel_oauth_tokens`'
93 . ' WHERE `token_key` = ? AND `token_type` = ?',
94 array($tokenKey, $type)
97 if ($tokenRow === false) {
98 throw new OAuthException('Unknown token: ' . $type . ' / ' . $tokenKey);
101 $token = $this->fromDb($tokenRow);
102 if ($token->tokenKey != $tokenKey) {
103 throw new OAuthException('Invalid token');
110 * Load multiple tokens
112 * @param string $username User name
113 * @param string $type Token type: temp, access, verify
115 * @return array Array of Token objects
117 public function loadForUser($username, $type)
119 $result = \OC_DB::executeAudited(
120 'SELECT * FROM `*PREFIX*grauphel_oauth_tokens`'
121 . ' WHERE `token_user` = ? AND `token_type` = ?',
122 array($username, $type)
126 while ($tokenRow = $result->fetchRow()) {
127 $tokens[] = $this->fromDb($tokenRow);
133 protected function fromDb($tokenRow)
135 $token = new Token();
136 $token->type = $tokenRow['token_user'];
137 $token->tokenKey = $tokenRow['token_key'];
138 $token->secret = $tokenRow['token_secret'];
139 $token->user = $tokenRow['token_user'];
140 $token->verifier = $tokenRow['token_verifier'];
141 $token->callback = $tokenRow['token_callback'];