aboutsummaryrefslogtreecommitdiff
path: root/controller/oauthcontroller.php
blob: cb20832170bb5a02ed94e6b4444afd8de8c083b4 (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
<?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\Controller;
use \OCP\AppFramework\Controller;

/**
 * OAuth handling
 *
 * @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 OAuthController extends Controller
{
    /**
     * Handle out an access token after verifying the verification token
     * OAuth step 3 of 3
     */
    public function accessTokenAction()
    {
        $oauth = new OAuth();
        $oauth->setDeps($this->deps);

        try {
            $provider = new \OAuthProvider();
            $oauth->registerHandler($provider)
                ->registerVerificationTokenHandler($provider);
            $provider->checkOAuthRequest($this->deps->urlGen->oauthAccessToken());

            $token = $this->deps->tokens->loadAndDelete('verify', $provider->token);

            $newToken = new OAuth_Token('access');
            $newToken->tokenKey = 'a' . bin2hex($provider->generateToken(8));
            $newToken->secret   = 's' . bin2hex($provider->generateToken(8));
            $newToken->user     = $token->user;
            $this->deps->tokens->store($newToken);

            $this->deps->renderer->sendFormData(
                array(
                    'oauth_token'        => $newToken->tokenKey,
                    'oauth_token_secret' => $newToken->secret,
                )
            );
            exit(0);
        } catch (\OAuthException $e) {
            $oauth->error($e);
        }
    }

    /**
     * Log the user in and let him authorize that the app may access notes
     * OAuth step 2 of 3
     */
    public function authorizeAction()
    {
        $oauth = new OAuth();
        $oauth->setDeps($this->deps);

        if (!isset($_REQUEST['oauth_token'])) {
            $this->deps->renderer->errorOut('oauth_token missing');
        }
        if (!$oauth->validateToken($_REQUEST['oauth_token'])) {
            $this->deps->renderer->errorOut('Invalid token string');
        }

        $reqToken = $_REQUEST['oauth_token'];

        try {
            $token = $this->deps->tokens->load('temp', $reqToken);
        } catch (OAuth_Exception $e) {
            $this->deps->renderer->errorOut($e->getMessage());
        }

        $authState = $this->deps->frontend->doAuthorize(
            $this->deps->urlGen->current()
        );
        if ($authState === null) {
            //this should not happen; doAuthorize() must block
            exit(1);
        }

        try {
            $token = $this->deps->tokens->loadAndDelete('temp', $reqToken);
        } catch (OAuth_Exception $e) {
            $this->deps->renderer->errorOut($e->getMessage());
        }

        if ($authState === false) {
            //user declined

            //http://wiki.oauth.net/w/page/12238543/ProblemReporting
            $callback = $this->deps->urlGen->addParams(
                $token->callback,
                array(
                    'oauth_token'   => $token->tokenKey,
                    'oauth_problem' => 'permission_denied',
                )
            );
            header('Location: ' . $callback, true, 302);
            exit(0);
        }

        //the user is logged in and authorized
        $provider = new \OAuthProvider();

        $newToken = new OAuth_Token('verify');
        $newToken->tokenKey = $token->tokenKey;
        $newToken->secret   = $token->secret;
        $newToken->verifier = 'v' . bin2hex($provider->generateToken(8));
        $newToken->user     = $this->deps->frontend->getUser();

        $this->deps->tokens->store($newToken);

        //redirect
        //FIXME: if no callback is given, show the token to the user
        $callback = $this->deps->urlGen->addParams(
            $token->callback,
            array(
                'oauth_token'    => $newToken->tokenKey,
                'oauth_verifier' => $newToken->verifier
            )
        );

        header('Location: ' . $callback, true, 302);
        exit();
    }

    /**
     * Create and return a request token.
     * OAuth step 1 of 3
     */
    public function requestTokenAction()
    {
        $oauth = new OAuth();
        $oauth->setDeps($this->deps);

        try {
            $provider = new \OAuthProvider();
            $oauth->registerHandler($provider);
            $provider->isRequestTokenEndpoint(true);
            $provider->checkOAuthRequest($this->deps->urlGen->oauthRequestToken());

            //store token + callback URI for later
            $token = new OAuth_Token('temp');
            $token->tokenKey = 'r' . bin2hex($provider->generateToken(8));
            $token->secret   = 's' . bin2hex($provider->generateToken(8));
            $token->callback = $provider->callback;

            $this->deps->tokens->store($token);

            $this->deps->renderer->sendFormData(
                array(
                    'oauth_token'              => $token->tokenKey,
                    'oauth_token_secret'       => $token->secret,
                    'oauth_callback_confirmed' => 'TRUE'
                )
            );
        } catch (\OAuthException $e) {
            $oauth->error($e);
        }
    }
}
?>