9a173f5c03c26248512848b98e38e2d4515cf97a
[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 OAuth_Token $token Token object to store
33      *
34      * @return void
35      */
36     public function store(OAuth_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('Unknown token: ' . $type . ' / ' . $tokenKey);
99         }
100
101         $token = $this->fromDb($tokenRow);
102         if ($token->tokenKey != $tokenKey) {
103             throw new OAuthException('Invalid token');
104         }
105
106         return $token;
107     }
108
109     protected function fromDb($tokenRow)
110     {
111         $token = new Token();
112         $token->type     = $tokenRow['token_user'];
113         $token->tokenKey = $tokenRow['token_key'];
114         $token->secret   = $tokenRow['token_secret'];
115         $token->user     = $tokenRow['token_user'];
116         $token->verifier = $tokenRow['token_verifier'];
117         $token->callback = $tokenRow['token_callback'];
118         return $token;
119     }
120 }
121 ?>