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