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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
<?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;
use \OCP\AppFramework\Http;
use \OCP\AppFramework\Http\RedirectResponse;
use \OCP\AppFramework\Http\TemplateResponse;
use \OCA\Grauphel\Lib\Client;
use \OCA\Grauphel\Lib\Token;
use \OCA\Grauphel\Lib\OAuth;
use \OCA\Grauphel\Lib\Dependencies;
use \OCA\Grauphel\Lib\Response\ErrorResponse;
use \OCA\Grauphel\Lib\Response\FormResponse;
use \OCA\Grauphel\Lib\OAuthException;
use \OCA\Grauphel\Lib\UrlHelper;
/**
* 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
{
protected $user;
/**
* constructor of the controller
*
* @param string $appName Name of the app
* @param IRequest $request Instance of the request
*/
public function __construct($appName, \OCP\IRequest $request, $user)
{
parent::__construct($appName, $request);
$this->user = $user;
$this->deps = Dependencies::get();
//default http header: we assume something is broken
header('HTTP/1.0 500 Internal Server Error');
}
/**
* Handle out an access token after verifying the verification token
* OAuth step 3 of 3
*
* @NoAdminRequired
* @NoCSRFRequired
* @PublicPage
*/
public function accessToken()
{
$oauth = new OAuth();
$oauth->setDeps($this->deps);
$urlGen = $this->deps->urlGen;
try {
$provider = OAuth::getProvider();
$oauth->registerHandler($provider)
->registerVerificationTokenHandler($provider);
$provider->checkOAuthRequest(
$urlGen->getAbsoluteURL(
$urlGen->linkToRoute('grauphel.oauth.accessToken')
)
);
$token = $this->deps->tokens->loadAndDelete('verify', $provider->token);
$newToken = new Token('access');
$newToken->tokenKey = 'a' . bin2hex($provider->generateToken(8));
$newToken->secret = 's' . bin2hex($provider->generateToken(8));
$newToken->user = $token->user;
$newToken->client = $token->client;
$this->deps->tokens->store($newToken);
return new FormResponse(
array(
'oauth_token' => $newToken->tokenKey,
'oauth_token_secret' => $newToken->secret,
)
);
} catch (OAuthException $e) {
return new ErrorResponse($e->getMessage());
} 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
*
* Page is not public and thus requires owncloud login
*
* @NoAdminRequired
* @NoCSRFRequired
*/
public function authorize()
{
$token = $this->verifyRequestToken();
if (!$token instanceof Token) {
return $token;
}
$clientTitle = 'unknown';
$clientAgent = '';
if (isset($_GET['client'])) {
$clientAgent = $_GET['client'];
$cl = new Client();
$clientTitle = $cl->getNiceName($clientAgent);
}
$res = new TemplateResponse('grauphel', 'oauthAuthorize');
$res->setParams(
array(
'oauth_token' => $token->tokenKey,
'client' => $clientTitle,
'formaction' => $this->deps->urlGen->linkToRoute(
'grauphel.oauth.confirm'
) . '?client=' . urlencode($clientAgent),
)
);
return $res;
}
/**
* User confirms or declines the authorization request
* OAuth step 2.5 of 3
*
* @NoAdminRequired
*/
public function confirm()
{
$token = $this->verifyRequestToken();
$oauth = new OAuth();
$oauth->setDeps($this->deps);
try {
$token = $this->deps->tokens->loadAndDelete('temp', $token->tokenKey);
} catch (OAuthException $e) {
return new ErrorResponse($e->getMessage());
}
$authState = isset($_POST['auth']) && $_POST['auth'] == 'ok';
if ($authState === false) {
//user declined
//http://wiki.oauth.net/w/page/12238543/ProblemReporting
$res = new RedirectResponse(
UrlHelper::addParams(
$token->callback,
array(
'oauth_token' => $token->tokenKey,
'oauth_problem' => 'permission_denied',
)
)
);
$res->setStatus(Http::STATUS_SEE_OTHER);
return $res;
}
$clientAgent = '';
if (isset($_GET['client'])) {
$clientAgent = $_GET['client'];
}
//the user is logged in and authorized
$provider = OAuth::getProvider();
$newToken = new Token('verify');
$newToken->tokenKey = $token->tokenKey;
$newToken->secret = $token->secret;
$newToken->verifier = 'v' . bin2hex($provider->generateToken(8));
$newToken->user = $this->user->getUID();
$newToken->client = $clientAgent;
$this->deps->tokens->store($newToken);
//redirect
//FIXME: if no callback is given, show the token to the user
$res = new RedirectResponse(
UrlHelper::addParams(
$token->callback,
array(
'oauth_token' => $newToken->tokenKey,
'oauth_verifier' => $newToken->verifier
)
)
);
$res->setStatus(Http::STATUS_SEE_OTHER);
return $res;
}
protected function verifyRequestToken()
{
if (!isset($_REQUEST['oauth_token'])) {
return new ErrorResponse('oauth_token missing');
}
$oauth = new OAuth();
$oauth->setDeps($this->deps);
if (!$oauth->validateToken($_REQUEST['oauth_token'])) {
return new ErrorResponse('Invalid token string');
}
$reqToken = $_REQUEST['oauth_token'];
try {
$token = $this->deps->tokens->load('temp', $reqToken);
} catch (OAuthException $e) {
return new ErrorResponse($e->getMessage());
}
return $token;
}
/**
* Create and return a request token.
* OAuth step 1 of 3
*
* @NoAdminRequired
* @NoCSRFRequired
* @PublicPage
*/
public function requestToken()
{
$oauth = new OAuth();
$oauth->setDeps($this->deps);
$urlGen = $this->deps->urlGen;
try {
$provider = OAuth::getProvider();
$oauth->registerHandler($provider);
$provider->isRequestTokenEndpoint(true);
$provider->checkOAuthRequest(
$urlGen->getAbsoluteURL(
$urlGen->linkToRoute('grauphel.oauth.requestToken')
)
);
//store token + callback URI for later
$token = new 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);
return new FormResponse(
array(
'oauth_token' => $token->tokenKey,
'oauth_token_secret' => $token->secret,
'oauth_callback_confirmed' => 'true'
)
);
} catch (OAuthException $e) {
return new ErrorResponse($e->getMessage());
} catch (\OAuthException $e) {
$oauth->error($e);
}
}
}
?>
|