1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace fkooman\RemoteStorage\Http; |
4
|
|
|
|
5
|
|
|
use fkooman\RemoteStorage\ApiModule; |
6
|
|
|
use fkooman\RemoteStorage\Http\Exception\HttpException; |
7
|
|
|
use fkooman\RemoteStorage\OAuth\BearerAuthentication; |
8
|
|
|
use fkooman\RemoteStorage\OAuth\OAuthModule; |
9
|
|
|
use fkooman\RemoteStorage\OAuth\TokenStorage; |
10
|
|
|
use fkooman\RemoteStorage\RandomInterface; |
11
|
|
|
use fkooman\RemoteStorage\RemoteStorage; |
12
|
|
|
use fkooman\RemoteStorage\TplInterface; |
13
|
|
|
use fkooman\RemoteStorage\UiModule; |
14
|
|
|
use fkooman\RemoteStorage\WebfingerModule; |
15
|
|
|
|
16
|
|
|
class Controller |
17
|
|
|
{ |
18
|
|
|
private $formAuth; |
19
|
|
|
private $bearerAuth; |
20
|
|
|
private $oauthModule; |
21
|
|
|
private $apiModule; |
22
|
|
|
private $uiModule; |
23
|
|
|
private $webfingerModule; |
24
|
|
|
|
25
|
|
|
public function __construct(TplInterface $tpl, SessionInterface $session, TokenStorage $tokenStorage, RandomInterface $random, RemoteStorage $remoteStorage, array $userPass) |
26
|
|
|
{ |
27
|
|
|
$serverMode = 'development'; |
28
|
|
|
$this->formAuth = new FormAuthentication($session, $tpl, $userPass); |
29
|
|
|
$this->bearerAuth = new BearerAuthentication($tokenStorage); |
30
|
|
|
$this->oauthModule = new OAuthModule($tpl, $random, $tokenStorage); |
31
|
|
|
$this->apiModule = new ApiModule($remoteStorage, $serverMode); |
32
|
|
|
$this->uiModule = new UiModule($remoteStorage, $tpl, $tokenStorage); |
33
|
|
|
$this->webfingerModule = new WebfingerModule($serverMode); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function run(Request $request) |
37
|
|
|
{ |
38
|
|
|
switch ($request->getRequestMethod()) { |
39
|
|
|
case 'GET': |
40
|
|
|
return $this->handleGet($request); |
41
|
|
|
case 'POST': |
42
|
|
|
return $this->handlePost($request); |
43
|
|
|
case 'PUT': |
44
|
|
|
$tokenInfo = $this->bearerAuth->requireAuth($request); |
45
|
|
|
|
46
|
|
|
return $this->apiModule->put($request, $tokenInfo); |
47
|
|
|
case 'DELETE': |
48
|
|
|
$tokenInfo = $this->bearerAuth->requireAuth($request); |
49
|
|
|
|
50
|
|
|
return $this->apiModule->delete($request, $tokenInfo); |
51
|
|
|
case 'OPTIONS': |
52
|
|
|
return $this->apiModule->options($request); |
53
|
|
|
case 'HEAD': |
54
|
|
|
$tokenInfo = $this->bearerAuth->requireAuth($request); |
55
|
|
|
|
56
|
|
|
return $this->apiModule->head($request, $tokenInfo); |
57
|
|
|
default: |
58
|
|
|
throw new HttpException('', 405); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function handleGet(Request $request) |
63
|
|
|
{ |
64
|
|
|
switch ($request->getPathInfo()) { |
65
|
|
|
case '/.well-known/webfinger': |
|
|
|
|
66
|
|
|
|
67
|
|
|
return $this->webfingerModule->getWebfinger($request); |
68
|
|
|
case '/_oauth/authorize': |
69
|
|
|
$userId = $this->formAuth->requireAuth($request); |
70
|
|
|
if ($userId instanceof Response) { |
71
|
|
|
return $userId; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->oauthModule->getAuthorize($request, $userId); |
75
|
|
|
case '/': |
76
|
|
|
$userId = $this->formAuth->optionalAuth($request); |
77
|
|
|
|
78
|
|
|
return $this->uiModule->getRootPage($request, $userId); |
79
|
|
|
case '/account': |
80
|
|
|
$userId = $this->formAuth->requireAuth($request); |
81
|
|
|
if ($userId instanceof Response) { |
82
|
|
|
return $userId; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->uiModule->getAccountPage($request, $userId); |
86
|
|
|
default: |
87
|
|
|
$tokenInfo = $this->bearerAuth->optionalAuth($request); |
88
|
|
|
|
89
|
|
|
return $this->apiModule->get($request, $tokenInfo); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function handlePost(Request $request) |
94
|
|
|
{ |
95
|
|
|
switch ($request->getPathInfo()) { |
96
|
|
|
case '/account': |
97
|
|
|
$userId = $this->formAuth->requireAuth($request); |
98
|
|
|
|
99
|
|
|
return $this->uiModule->postAccountPage($request, $userId); |
100
|
|
|
|
101
|
|
|
case '/_oauth/authorize': |
102
|
|
|
$userId = $this->formAuth->requireAuth($request); |
103
|
|
|
|
104
|
|
|
return $this->oauthModule->postAuthorize($request, $userId); |
105
|
|
|
case '/_auth/form/verify': |
|
|
|
|
106
|
|
|
|
107
|
|
|
return $this->formAuth->verifyAuth($request); |
108
|
|
|
default: |
109
|
|
|
throw new HttpException('', 404); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
//$request = new Request($_SERVER, $_GET, $_POST, file_get_contents('php://input')); |
|
|
|
|
115
|
|
|
|
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.