aboutsummaryrefslogtreecommitdiff
path: root/lib/tokenstorage.php
blob: c40ab3395f841918bf8b4518628256c0e4a4f1ea (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
<?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;

/**
 * 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
{
    /**
     * Store the given token
     *
     * @param OAuth_Token $token Token object to store
     *
     * @return void
     */
    public function store(OAuth_Token $token)
    {
        \OC_DB::executeAudited(
            'INSERT INTO `*PREFIX*grauphel_oauth_tokens`'
            . '(`token_user`, `token_type`, `token_key`, `token_secret`, `token_verifier`, `token_callback`)'
            . ' VALUES(?, ?, ?, ?, ?, ?)',
            array(
                $token->user,
                $token->type,
                $token->tokenKey,
                (string) $token->secret,
                (string) $token->verifier,
                (string) $token->callback
            )
        );
    }

    /**
     * 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 OAuth_Exception When token does not exist
     */
    public function loadAndDelete($type, $tokenKey)
    {
        try {
            $token = $this->load($type, $tokenKey);
            \OC_DB::executeAudited(
                'DELETE FROM `*PREFIX*grauphel_oauth_tokens`'
                . ' WHERE `token_key` = ? AND `token_type` = ?',
                array($tokenKey, $type)
            );
            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 OAuth_Exception When token does not exist or 
     */
    public function load($type, $tokenKey)
    {
        $tokenRow = \OC_DB::executeAudited(
            'SELECT * FROM `*PREFIX*grauphel_oauth_tokens`'
            . ' WHERE `token_key` = ? AND `token_type` = ?',
            array($tokenKey, $type)
        )->fetchRow();

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

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

        return $token;
    }

    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'];
        return $token;
    }
}
?>