Newer
Older
<?php
require_once __DIR__ . '/oauth2_controller.php';
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
use Studip\OAuth2\Bridge\UserEntity;
use Studip\OAuth2\Exceptions\InvalidAuthTokenException;
use Studip\OAuth2\Models\Scope;
class Api_Oauth2_AuthorizeController extends OAuth2Controller
{
public function before_filter(&$action, &$args)
{
parent::before_filter($action, $args);
if ('index' !== $action) {
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
}
$action = $this->determineAction();
}
private function determineAction(): string
{
$method = $this->getMethod();
if (Request::submitted('auth_token')) {
$GLOBALS['auth']->login_if('nobody' === $GLOBALS['user']->id);
CSRFProtection::verifyUnsafeRequest();
switch ($method) {
case 'POST':
return 'approved';
case 'DELETE':
return 'denied';
}
}
return 'authorize';
}
public function authorize_action(): void
{
$psrRequest = $this->getPsrRequest();
$authRequest = $this->server->validateAuthorizationRequest($psrRequest);
$scopes = $authRequest->getScopes();
$client = $authRequest->getClient();
$authToken = randomString(32);
$this->freezeSessionVars($authRequest, $authToken);
// show login form if not logged in
$authPlugin = Config::get()->getValue('API_OAUTH_AUTH_PLUGIN');
if ('nobody' === $GLOBALS['user']->id && 'Standard' !== $authPlugin && !Request::option('sso')) {
$queryParams = $psrRequest->getQueryParams();
$queryParams['sso'] = strtolower($authPlugin);
$this->redirect($this->url_for('api/oauth2/authorize', $queryParams));
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
return;
} else {
$GLOBALS['auth']->login_if('nobody' === $GLOBALS['user']->id);
}
$this->client = $client;
$this->user = $GLOBALS['user'];
$this->scopes = $this->scopesFor($scopes);
$this->authToken = $authToken;
$this->state = $authRequest->getState();
PageLayout::disableHeader();
$this->render_template(
'api/oauth2/authorize.php',
$GLOBALS['template_factory']->open('layouts/base.php')
);
}
public function approved_action(): void
{
[$authRequest, $authToken] = $this->thawSessionVars();
$this->assertValidAuthToken($authToken);
$authRequest->setUser(new UserEntity($GLOBALS['user']->id));
$authRequest->setAuthorizationApproved(true);
$response = $this->server->completeAuthorizationRequest($authRequest, $this->getPsrResponse());
$this->renderPsrResponse($response);
}
public function denied_action(): void
{
[$authRequest, $authToken] = $this->thawSessionVars();
$this->assertValidAuthToken($authToken);
$authRequest->setUser(new UserEntity($GLOBALS['user']->id));
$authRequest->setAuthorizationApproved(false);
$clientUris = $authRequest->getClient()->getRedirectUri();
$uri = $authRequest->getRedirectUri();
if (!in_array($uri, $clientUris)) {
$uri = current($clientUris);
}
$uri = URLHelper::getURL($uri, [
'error' => 'access_denied',
'state' => Request::get('state'),
], true);
$this->redirect($uri);
}
private function getMethod(): string
{
$method = Request::method();
if ('POST' === $method && Request::submitted('_method')) {
$_method = strtoupper(Request::get('_method'));
if (in_array($_method, ['DELETE', 'PATCH', 'PUT'])) {
$method = $_method;
}
}
return $method;
}
/**
* Make sure the auth token matches the one in the session.
*
* @throws InvalidAuthTokenException
*/
private function assertValidAuthToken(string $authToken): void
{
if (Request::submitted('auth_token') && $authToken !== Request::get('auth_token')) {
throw InvalidAuthTokenException::different();
}
}
private function freezeSessionVars(AuthorizationRequest $authRequest, string $authToken): void
{
$_SESSION['oauth2'] = [
'authRequest' => serialize($authRequest),
'authToken' => $authToken,
];
}
private function thawSessionVars(): array
{
$authRequest = null;
$authToken = null;
if (
isset($_SESSION['oauth2']) &&
is_array($_SESSION['oauth2']) &&
isset($_SESSION['oauth2']['authRequest']) &&
isset($_SESSION['oauth2']['authToken'])
) {
$authRequest = unserialize($_SESSION['oauth2']['authRequest']);
$authToken = $_SESSION['oauth2']['authToken'];
}
return [$authRequest, $authToken];
}
private function scopesFor(array $scopeEntities): array
{
$scopes = Scope::scopes();
$scopeModels = [];
foreach ($scopeEntities as $scopeEntity) {
if (isset($scopes[$scopeEntity->getIdentifier()])) {
$scopeModels[] = $scopes[$scopeEntity->getIdentifier()];
}
}
return $scopeModels;
}
}