blob: acc9238c111e8f1e12e6979e4d39b24fbe6f6bab (
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
|
<?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\RedirectResponse;
use \OCA\Grauphel\Lib\Dependencies;
use \OCA\Grauphel\Lib\OAuthException;
use \OCA\Grauphel\Lib\Response\ErrorResponse;
use \OCA\Grauphel\Lib\TokenStorage;
/**
* OAuth token management
*
* @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 TokenController extends Controller
{
/**
* 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');
}
/**
* Delete an access token
* DELETE /tokens/$username/$tokenKey
*
* @NoAdminRequired
* @NoCSRFRequired
*/
public function delete($username, $tokenKey)
{
if (false && ($this->user === null || $this->user->getUid() != $username)) {
$res = new ErrorResponse('You may only delete your own tokens.');
$res->setStatus(\OCP\AppFramework\Http::STATUS_FORBIDDEN);
return $res;
}
$deps = Dependencies::get();
try {
$token = $deps->tokens->load('access', $tokenKey);
} catch (OAuthException $e) {
$res = new ErrorResponse('Token not found.');
$res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
return $res;
}
if ($username != $token->user) {
$res = new ErrorResponse('You may only delete your own tokens.');
$res->setStatus(\OCP\AppFramework\Http::STATUS_FORBIDDEN);
return $res;
}
$deps->tokens->delete('access', $tokenKey);
$res = new \OCP\AppFramework\Http\Response();
$res->setStatus(\OCP\AppFramework\Http::STATUS_NO_CONTENT);
return $res;
}
/**
* Delete an access token via POST
* POST /tokens/$username/$tokenKey
*
* @NoAdminRequired
* @NoCSRFRequired
*/
public function deletePost($username, $tokenKey)
{
if (isset($_POST['delete']) && $_POST['delete'] == 1) {
$this->delete($username, $tokenKey);
}
$res = new RedirectResponse(
$this->deps->urlGen->getAbsoluteURL(
$this->deps->urlGen->linkToRoute('grauphel.gui.tokens')
)
);
$res->setStatus(\OCP\AppFramework\Http::STATUS_FOUND);
return $res;
}
}
?>
|