Release 0.7.0
[grauphel.git] / lib / tokenstorage.php
1 <?php
2 /**
3  * Part of grauphel
4  *
5  * PHP version 5
6  *
7  * @category  Tools
8  * @package   Grauphel
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
13  */
14 namespace OCA\Grauphel\Lib;
15
16 /**
17  * OAuth token store
18  *
19  * @category  Tools
20  * @package   Grauphel
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
26  */
27 class TokenStorage
28 {
29     /**
30      * @var \OCP\IDBConnection
31      */
32     protected $db;
33
34     public function __construct()
35     {
36         $this->db = \OC::$server->getDatabaseConnection();
37     }
38
39     /**
40      * Delete token
41      *
42      * @param string $type     Token type: temp, access, verify
43      * @param string $tokenKey Random token string to load
44      *
45      * @return void
46      *
47      * @throws OAuthException When token does not exist
48      */
49     public function delete($type, $tokenKey)
50     {
51         $this->db->executeQuery(
52             'DELETE FROM `*PREFIX*grauphel_oauth_tokens`'
53             . ' WHERE `token_key` = ? AND `token_type` = ?',
54             array($tokenKey, $type)
55         );
56     }
57
58     /**
59      * Store the given token
60      *
61      * @param Token $token Token object to store
62      *
63      * @return void
64      */
65     public function store(Token $token)
66     {
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(?, ?, ?, ?, ?, ?, ?, ?)',
71             array(
72                 $token->user,
73                 $token->type,
74                 $token->tokenKey,
75                 (string) $token->secret,
76                 (string) $token->verifier,
77                 (string) $token->callback,
78                 (string) $token->client,
79                 date('Y-m-d H:i:s'),
80             )
81         );
82     }
83
84     /**
85      * Load the token and destroy it.
86      *
87      * @param string $type     Token type: temp, access, verify
88      * @param string $tokenKey Random token string to load
89      *
90      * @return OAuth_Token Stored token
91      *
92      * @throws OAuthException When token does not exist
93      */
94     public function loadAndDelete($type, $tokenKey)
95     {
96         try {
97             $token = $this->load($type, $tokenKey);
98             $this->delete($type, $tokenKey);
99             return $token;
100         } catch (OAuthException $e) {
101             throw $e;
102         }
103     }
104
105
106     /**
107      * Load the token.
108      *
109      * @param string $type     Token type: temp, access, verify
110      * @param string $tokenKey Random token string to load
111      *
112      * @return OAuth_Token Stored token
113      *
114      * @throws OAuthException When token does not exist or it is invalid
115      */
116     public function load($type, $tokenKey)
117     {
118         $tokenRow = $this->db->executeQuery(
119             'SELECT * FROM `*PREFIX*grauphel_oauth_tokens`'
120             . ' WHERE `token_key` = ? AND `token_type` = ?',
121             array($tokenKey, $type)
122         )->fetch();
123
124         if ($tokenRow === false) {
125             throw new OAuthException(
126                 'Unknown token: ' . $type . ' / ' . $tokenKey,
127                 OAUTH_TOKEN_REJECTED
128             );
129         }
130
131         $token = $this->fromDb($tokenRow);
132         if ($token->tokenKey != $tokenKey) {
133             throw new OAuthException('Invalid token', OAUTH_TOKEN_REJECTED);
134         }
135
136         return $token;
137     }
138
139     /**
140      * Load multiple tokens
141      *
142      * @param string $username User name
143      * @param string $type     Token type: temp, access, verify
144      *
145      * @return array Array of Token objects
146      */
147     public function loadForUser($username, $type)
148     {
149         $result = $this->db->executeQuery(
150             'SELECT * FROM `*PREFIX*grauphel_oauth_tokens`'
151             . ' WHERE `token_user` = ? AND `token_type` = ?',
152             array($username, $type)
153         );
154
155         $tokens = array();
156         while ($tokenRow = $result->fetch()) {
157             $tokens[] = $this->fromDb($tokenRow);
158         }
159
160         return $tokens;
161     }
162
163     /**
164      * Update the "last use" field of a token
165      *
166      * @param string $tokenKey Random token string to load
167      *
168      * @return void
169      */
170     public function updateLastUse($tokenKey)
171     {
172         $this->db->executeQuery(
173             'UPDATE `*PREFIX*grauphel_oauth_tokens`'
174             . ' SET `token_lastuse` = ? WHERE `token_key` = ?',
175             array(
176                 date('Y-m-d H:i:s'),
177                 $tokenKey,
178             )
179         );
180     }
181
182     protected function fromDb($tokenRow)
183     {
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']);
193         return $token;
194     }
195 }
196 ?>