Add travis-ci.org configuration
[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  * 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      * Delete token
31      *
32      * @param string $type     Token type: temp, access, verify
33      * @param string $tokenKey Random token string to load
34      *
35      * @return void
36      *
37      * @throws OAuthException When token does not exist
38      */
39     public function delete($type, $tokenKey)
40     {
41         \OC_DB::executeAudited(
42             'DELETE FROM `*PREFIX*grauphel_oauth_tokens`'
43             . ' WHERE `token_key` = ? AND `token_type` = ?',
44             array($tokenKey, $type)
45         );
46     }
47
48     /**
49      * Store the given token
50      *
51      * @param Token $token Token object to store
52      *
53      * @return void
54      */
55     public function store(Token $token)
56     {
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(?, ?, ?, ?, ?, ?, ?, ?)',
61             array(
62                 $token->user,
63                 $token->type,
64                 $token->tokenKey,
65                 (string) $token->secret,
66                 (string) $token->verifier,
67                 (string) $token->callback,
68                 (string) $token->client,
69                 (string) date('c'),
70             )
71         );
72     }
73
74     /**
75      * Load the token and destroy it.
76      *
77      * @param string $type     Token type: temp, access, verify
78      * @param string $tokenKey Random token string to load
79      *
80      * @return OAuth_Token Stored token
81      *
82      * @throws OAuthException When token does not exist
83      */
84     public function loadAndDelete($type, $tokenKey)
85     {
86         try {
87             $token = $this->load($type, $tokenKey);
88             $this->delete($type, $tokenKey);
89             return $token;
90         } catch (OAuthException $e) {
91             throw $e;
92         }
93     }
94
95
96     /**
97      * Load the token.
98      *
99      * @param string $type     Token type: temp, access, verify
100      * @param string $tokenKey Random token string to load
101      *
102      * @return OAuth_Token Stored token
103      *
104      * @throws OAuthException When token does not exist or it is invalid
105      */
106     public function load($type, $tokenKey)
107     {
108         $tokenRow = \OC_DB::executeAudited(
109             'SELECT * FROM `*PREFIX*grauphel_oauth_tokens`'
110             . ' WHERE `token_key` = ? AND `token_type` = ?',
111             array($tokenKey, $type)
112         )->fetchRow();
113
114         if ($tokenRow === false) {
115             throw new OAuthException(
116                 'Unknown token: ' . $type . ' / ' . $tokenKey,
117                 OAUTH_TOKEN_REJECTED
118             );
119         }
120
121         $token = $this->fromDb($tokenRow);
122         if ($token->tokenKey != $tokenKey) {
123             throw new OAuthException('Invalid token', OAUTH_TOKEN_REJECTED);
124         }
125
126         return $token;
127     }
128
129     /**
130      * Load multiple tokens
131      *
132      * @param string $username User name
133      * @param string $type     Token type: temp, access, verify
134      *
135      * @return array Array of Token objects
136      */
137     public function loadForUser($username, $type)
138     {
139         $result = \OC_DB::executeAudited(
140             'SELECT * FROM `*PREFIX*grauphel_oauth_tokens`'
141             . ' WHERE `token_user` = ? AND `token_type` = ?',
142             array($username, $type)
143         );
144
145         $tokens = array();
146         while ($tokenRow = $result->fetchRow()) {
147             $tokens[] = $this->fromDb($tokenRow);
148         }
149
150         return $tokens;
151     }
152
153     /**
154      * Update the "last use" field of a token
155      *
156      * @param string $tokenKey Random token string to load
157      *
158      * @return void
159      */
160     public function updateLastUse($tokenKey)
161     {
162         \OC_DB::executeAudited(
163             'UPDATE `*PREFIX*grauphel_oauth_tokens`'
164             . ' SET `token_lastuse` = ? WHERE `token_key` = ?',
165             array(
166                 (string) date('c'),
167                 $tokenKey,
168             )
169         );
170     }
171
172     protected function fromDb($tokenRow)
173     {
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']);
183         return $token;
184     }
185 }
186 ?>