b9689abcdc39fa8fae18f2d16b010c743c53fa49
[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`)'
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             )
50         );
51     }
52
53     /**
54      * Load the token and destroy it.
55      *
56      * @param string $type     Token type: temp, access, verify
57      * @param string $tokenKey Random token string to load
58      *
59      * @return OAuth_Token Stored token
60      *
61      * @throws OAuthException When token does not exist
62      */
63     public function loadAndDelete($type, $tokenKey)
64     {
65         try {
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)
71             );
72             return $token;
73         } catch (OAuthException $e) {
74             throw $e;
75         }
76     }
77
78
79     /**
80      * Load the token.
81      *
82      * @param string $type     Token type: temp, access, verify
83      * @param string $tokenKey Random token string to load
84      *
85      * @return OAuth_Token Stored token
86      *
87      * @throws OAuthException When token does not exist or it is invalid
88      */
89     public function load($type, $tokenKey)
90     {
91         $tokenRow = \OC_DB::executeAudited(
92             'SELECT * FROM `*PREFIX*grauphel_oauth_tokens`'
93             . ' WHERE `token_key` = ? AND `token_type` = ?',
94             array($tokenKey, $type)
95         )->fetchRow();
96
97         if ($tokenRow === false) {
98             throw new OAuthException(
99                 'Unknown token: ' . $type . ' / ' . $tokenKey,
100                 OAUTH_TOKEN_REJECTED
101             );
102         }
103
104         $token = $this->fromDb($tokenRow);
105         if ($token->tokenKey != $tokenKey) {
106             throw new OAuthException('Invalid token', OAUTH_TOKEN_REJECTED);
107         }
108
109         return $token;
110     }
111
112     /**
113      * Load multiple tokens
114      *
115      * @param string $username User name
116      * @param string $type     Token type: temp, access, verify
117      *
118      * @return array Array of Token objects
119      */
120     public function loadForUser($username, $type)
121     {
122         $result = \OC_DB::executeAudited(
123             'SELECT * FROM `*PREFIX*grauphel_oauth_tokens`'
124             . ' WHERE `token_user` = ? AND `token_type` = ?',
125             array($username, $type)
126         );
127
128         $tokens = array();
129         while ($tokenRow = $result->fetchRow()) {
130             $tokens[] = $this->fromDb($tokenRow);
131         }
132
133         return $tokens;
134     }
135
136     protected function fromDb($tokenRow)
137     {
138         $token = new Token();
139         $token->type     = $tokenRow['token_user'];
140         $token->tokenKey = $tokenRow['token_key'];
141         $token->secret   = $tokenRow['token_secret'];
142         $token->user     = $tokenRow['token_user'];
143         $token->verifier = $tokenRow['token_verifier'];
144         $token->callback = $tokenRow['token_callback'];
145         return $token;
146     }
147 }
148 ?>