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

/**
 * Storage base class that implements note updating
 *
 * @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 OAuth
{
    /**
     * Token data store
     *
     * @var Token_Storage
     */
    protected $tokens;

    public function setDeps(Dependencies $deps)
    {
        $this->tokens = $deps->tokens;
    }

    /**
     * Register callbacks for the oauth dance.
     */
    public function registerHandler(\OAuthProvider $provider)
    {
        $provider->consumerHandler(array($this, 'lookupConsumer'));
        $provider->timestampNonceHandler(array($this, 'timestampNonceChecker'));
        return $this;
    }

    public function registerVerificationTokenHandler(\OAuthProvider $provider)
    {
        $provider->tokenHandler(array($this, 'verifyTokenHandler'));
        return $this;
    }

    public function registerAccessTokenHandler(\OAuthProvider $provider)
    {
        $provider->tokenHandler(array($this, 'accessTokenHandler'));
        return $this;
    }

    public function validateToken($tokenKey)
    {
        return (bool) preg_match('#^[a-z0-9]+$#', $tokenKey);
    }

    public function lookupConsumer(\OAuthProvider $provider)
    {
        //tomboy assumes secret==key=="anyone"
        $provider->consumer_secret = $provider->consumer_key;//'anyone';
        $provider->addRequiredParameter('oauth_callback');

        return OAUTH_OK;
    }

    public function timestampNonceChecker(\OAuthProvider $provider)
    {
        //var_dump($provider->nonce, $provider->timestamp);
        //OAUTH_BAD_NONCE
        //OAUTH_BAD_TIMESTAMP
        return OAUTH_OK;
    }

    public function verifyTokenHandler(\OAuthProvider $provider)
    {
        $token = $this->tokens->load('verify', $provider->token);
        if ($provider->verifier == '') {
            return OAUTH_VERIFIER_INVALID;
        }
        if ($provider->verifier != $token->verifier) {
            return OAUTH_VERIFIER_INVALID;
        }

        $provider->token_secret = $token->secret;
        return OAUTH_OK;
    }

    public function accessTokenHandler(\OAuthProvider $provider)
    {
        $token = $this->tokens->load('access', $provider->token);
        $provider->token_secret = $token->secret;
        return OAUTH_OK;
    }

    public function verifyOAuthUser($username, $url)
    {
        try {
            $provider = OAuth::getProvider();
            $this->registerHandler($provider);
            $this->registerAccessTokenHandler($provider);
            //do not use "user" in signature
            $provider->setParam('user', null);

            $provider->checkOAuthRequest($url);

            $token = $this->tokens->load('access', $provider->token);
            if ($token->user != $username) {
                errorOut('Invalid user');
            }
        } catch (\OAuthException $e) {
            $this->error($e);
        }
    }

    public function error(\OAuthException $e)
    {
        header('HTTP/1.0 400 Bad Request');
        //header('Content-type: application/x-www-form-urlencoded');
        echo \OAuthProvider::reportProblem($e);
        //var_dump($e);
        exit(1);
    }

    /**
     * Get a new oauth provider instance.
     * Used to work around the fastcgi bug in oauthprovider.
     * 
     * @return \OAuthProvider
     */
    public static function getProvider()
    {
        //$_SERVER['REDIRECT_HTTP_AUTHORIZATION'] = $_SERVER['HTTP_AUTHORIZATION'];
        //unset($_SERVER['HTTP_AUTHORIZATION']);

        $params = array();
        if (!isset($_SERVER['HTTP_AUTHORIZATION'])
            && isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])
        ) {
            //FastCgi puts the headers in REDIRECT_HTTP_AUTHORIZATION,
            // but the oauth extension does not read that.
            // we have to parse the parameters manually
            $regex = "/(oauth_[a-z_-]*)=(?:\"([^\"]*)\"|([^,]*))/";
            preg_match_all(
                $regex, $_SERVER['REDIRECT_HTTP_AUTHORIZATION'], $matches
            );

            foreach ($matches[1] as $key => $paramName) {
                $params[$paramName] = urldecode($matches[2][$key]);
            }
        }

        return new \OAuthProvider($params);
    }
}
?>