aboutsummaryrefslogtreecommitdiff
path: root/lib/tokenstorage.php
blob: 8b266f1d5352df9cc6b495deeb6934512eb2c864 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/**
 * Part of grauphel
 *
 * PHP version 5
 *
 * @category  Tools
 * @package   Grauphel
 * @author    Christian Weiske <cweiske@cweiske.de>
 * @copyright 2014 Christian Weiske
 * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
 * @link      http://cweiske.de/grauphel.htm
 */
namespace OCA\Grauphel\Lib;

/**
 * OAuth token store
 *
 * @category  Tools
 * @package   Grauphel
 * @author    Christian Weiske <cweiske@cweiske.de>
 * @copyright 2014 Christian Weiske
 * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
 * @version   Release: @package_version@
 * @link      http://cweiske.de/grauphel.htm
 */
class TokenStorage
{
    /**
     * @var \OCP\IDBConnection
     */
    protected $db;

    public function __construct()
    {
        $this->db = \OC::$server->getDatabaseConnection();
    }

    /**
     * Delete token
     *
     * @param string $type     Token type: temp, access, verify
     * @param string $tokenKey Random token string to load
     *
     * @return void
     *
     * @throws OAuthException When token does not exist
     */
    public function delete($type, $tokenKey)
    {
        $this->db->executeQuery(
            'DELETE FROM `*PREFIX*grauphel_oauth_tokens`'
            . ' WHERE `token_key` = ? AND `token_type` = ?',
            array($tokenKey, $type)
        );
    }

    /**
     * Store the given token
     *
     * @param Token $token Token object to store
     *
     * @return void
     */
    public function store(Token $token)
    {
        $this->db->executeQuery(
            'INSERT INTO `*PREFIX*grauphel_oauth_tokens`'
            . '(`token_user`, `token_type`, `token_key`, `token_secret`, `token_verifier`, `token_callback`, `token_client`, `token_lastuse`)'
            . ' VALUES(?, ?, ?, ?, ?, ?, ?, ?)',
            array(
                $token->user,
                $token->type,
                $token->tokenKey,
                (string) $token->secret,
                (string) $token->verifier,
                (string) $token->callback,
                (string) $token->client,
                date('Y-m-d H:i:s'),
            )
        );
    }

    /**
     * Load the token and destroy it.
     *
     * @param string $type     Token type: temp, access, verify
     * @param string $tokenKey Random token string to load
     *
     * @return OAuth_Token Stored token
     *
     * @throws OAuthException When token does not exist
     */
    public function loadAndDelete($type, $tokenKey)
    {
        try {
            $token = $this->load($type, $tokenKey);
            $this->delete($type, $tokenKey);
            return $token;
        } catch (OAuthException $e) {
            throw $e;
        }
    }


    /**
     * Load the token.
     *
     * @param string $type     Token type: temp, access, verify
     * @param string $tokenKey Random token string to load
     *
     * @return OAuth_Token Stored token
     *
     * @throws OAuthException When token does not exist or it is invalid
     */
    public function load($type, $tokenKey)
    {
        $tokenRow = $this->db->executeQuery(
            'SELECT * FROM `*PREFIX*grauphel_oauth_tokens`'
            . ' WHERE `token_key` = ? AND `token_type` = ?',
            array($tokenKey, $type)
        )->fetch();

        if ($tokenRow === false) {
            throw new OAuthException(
                'Unknown token: ' . $type . ' / ' . $tokenKey,
                OAUTH_TOKEN_REJECTED
            );
        }

        $token = $this->fromDb($tokenRow);
        if ($token->tokenKey != $tokenKey) {
            throw new OAuthException('Invalid token', OAUTH_TOKEN_REJECTED);
        }

        return $token;
    }

    /**
     * Load multiple tokens
     *
     * @param string $username User name
     * @param string $type     Token type: temp, access, verify
     *
     * @return array Array of Token objects
     */
    public function loadForUser($username, $type)
    {
        $result = $this->db->executeQuery(
            'SELECT * FROM `*PREFIX*grauphel_oauth_tokens`'
            . ' WHERE `token_user` = ? AND `token_type` = ?',
            array($username, $type)
        );

        $tokens = array();
        while ($tokenRow = $result->fetch()) {
            $tokens[] = $this->fromDb($tokenRow);
        }

        return $tokens;
    }

    /**
     * Update the "last use" field of a token
     *
     * @param string $tokenKey Random token string to load
     *
     * @return void
     */
    public function updateLastUse($tokenKey)
    {
        $this->db->executeQuery(
            'UPDATE `*PREFIX*grauphel_oauth_tokens`'
            . ' SET `token_lastuse` = ? WHERE `token_key` = ?',
            array(
                date('Y-m-d H:i:s'),
                $tokenKey,
            )
        );
    }

    protected function fromDb($tokenRow)
    {
        $token = new Token();
        $token->type     = $tokenRow['token_user'];
        $token->tokenKey = $tokenRow['token_key'];
        $token->secret   = $tokenRow['token_secret'];
        $token->user     = $tokenRow['token_user'];
        $token->verifier = $tokenRow['token_verifier'];
        $token->callback = $tokenRow['token_callback'];
        $token->client   = $tokenRow['token_client'];
        $token->lastuse  = \strtotime($tokenRow['token_lastuse']);
        return $token;
    }
}
?>