Skip to content

Commit d4ec5b4

Browse files
author
root
committed
More cors headers
1 parent 8dcffbf commit d4ec5b4

3 files changed

Lines changed: 28 additions & 13 deletions

File tree

solid/lib/Controller/ProfileController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,10 @@ private function respond($response) {
246246
$result->addHeader($header, $value);
247247
}
248248
}
249-
249+
$origin = $_SERVER['HTTP_ORIGIN'];
250+
$result->addHeader('Access-Control-Allow-Credentials', 'true');
251+
$result->addHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
252+
$result->addHeader('Access-Control-Allow-Origin', $origin);
250253
$result->setStatus($statusCode);
251254
return $result;
252255
}

solid/lib/Controller/ServerController.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,13 @@ private function createAuthServerConfig() {
105105
* @NoCSRFRequired
106106
*/
107107
public function cors($path) {
108-
return true;
108+
$origin = $_SERVER['HTTP_ORIGIN'];
109+
error_log('Allowing in OPTIONS:' . $origin);
110+
return (new DataResponse('OK'))
111+
->addHeader('Access-Control-Allow-Origin', $origin)
112+
->addHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization')
113+
->addHeader('Access-Control-Allow-Methods', 'POST')
114+
->addHeader('Access-Control-Allow-Credentials', 'true');
109115
}
110116

111117
/**
@@ -117,7 +123,7 @@ public function openid() {
117123
$response = new \Laminas\Diactoros\Response();
118124
$server = new \Pdsinterop\Solid\Auth\Server($this->authServerFactory, $this->authServerConfig, $response);
119125
$response = $server->respondToOpenIdMetadataRequest();
120-
return $this->respond($response->withHeader('Access-Control-Allow-Origin', '*'));
126+
return $this->respond($response->addHeader('Access-Control-Allow-Origin', '*'));
121127
}
122128

123129
/**
@@ -129,7 +135,7 @@ public function authorize() {
129135
if (!$this->userManager->userExists($this->userId)) {
130136
$result = new JSONResponse('Authorization required');
131137
$result->setStatus(401);
132-
return $result->withHeader('Access-Control-Allow-Origin', '*');
138+
return $result->addHeader('Access-Control-Allow-Origin', '*');
133139
}
134140

135141
$parser = new \Lcobucci\JWT\Parser();
@@ -154,7 +160,7 @@ public function authorize() {
154160
} catch(\Exception $e) {
155161
$result = new JSONResponse('Bad request, missing redirect uri');
156162
$result->setStatus(400);
157-
return $result->withHeader('Access-Control-Allow-Origin', '*');
163+
return $result->addHeader('Access-Control-Allow-Origin', '*');
158164
}
159165
}
160166
$clientId = $getVars['client_id'];
@@ -164,7 +170,7 @@ public function authorize() {
164170
$result->setStatus(302);
165171
$approvalUrl = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute("solid.page.approval", array("clientId" => $clientId, "returnUrl" => $_SERVER['REQUEST_URI'])));
166172
$result->addHeader("Location", $approvalUrl);
167-
return $result->withHeader('Access-Control-Allow-Origin', '*');
173+
return $result->addHeader('Access-Control-Allow-Origin', '*');
168174
}
169175

170176
$user = new \Pdsinterop\Solid\Auth\Entity\User();
@@ -177,7 +183,7 @@ public function authorize() {
177183
$response = $server->respondToAuthorizationRequest($request, $user, $approval);
178184
$response = $this->tokenGenerator->addIdTokenToResponse($response, $clientId, $this->getProfilePage(), $this->session->get("nonce"), $this->config->getPrivateKey());
179185

180-
return $this->respond($response)->withHeader('Access-Control-Allow-Origin', '*');
186+
return $this->respond($response)->addHeader('Access-Control-Allow-Origin', '*');
181187
}
182188

183189
private function checkApproval($clientId) {
@@ -250,7 +256,7 @@ public function token() {
250256
$codeInfo = $this->tokenGenerator->getCodeInfo($code);
251257
$response = $this->tokenGenerator->addIdTokenToResponse($response, $clientId, $codeInfo['user_id'], $_SESSION['nonce'], $this->config->getPrivateKey(), $dpopKey);
252258

253-
return $this->respond($response)->withHeader('Access-Control-Allow-Origin', '*');
259+
return $this->respond($response)->addHeader('Access-Control-Allow-Origin', '*');
254260
}
255261

256262
/**
@@ -271,7 +277,7 @@ public function logout() {
271277
$this->userService->logout();
272278
return new JSONResponse("ok");
273279
}
274-
280+
275281
/**
276282
* @PublicPage
277283
* @NoAdminRequired
@@ -285,7 +291,7 @@ public function register() {
285291
}
286292
$clientData['client_id_issued_at'] = time();
287293
$parsedOrigin = parse_url($clientData['redirect_uris'][0]);
288-
$origin = $parsedOrigin['host'];
294+
$origin = 'https://' . $parsedOrigin['host'];
289295

290296
$clientId = $this->config->saveClientRegistration($origin, $clientData);
291297
$registration = array(
@@ -294,10 +300,11 @@ public function register() {
294300
'client_id_issued_at' => $clientData['client_id_issued_at'],
295301
'redirect_uris' => $clientData['redirect_uris'],
296302
);
297-
303+
error_log('allowingin POST:' . $origin);
298304
$registration = $this->tokenGenerator->respondToRegistration($registration, $this->config->getPrivateKey());
299-
300-
return (new JSONResponse($registration))->addHeader('Access-Control-Allow-Origin', 'https://noeldemartin.github.io');
305+
return (new JSONResponse($registration))
306+
->addHeader('Access-Control-Allow-Origin', $origin)
307+
->addHeader('Access-Control-Allow-Methods', 'POST');
301308
}
302309

303310
/**

solid/lib/Controller/StorageController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,11 @@ private function respond($response) {
388388
$result->addHeader($header, $value);
389389
}
390390
}
391+
$origin = $_SERVER['HTTP_ORIGIN'];
392+
$result->addHeader('Access-Control-Allow-Credentials', 'true');
393+
$result->addHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
394+
$result->addHeader('Access-Control-Allow-Origin', $origin);
395+
391396

392397
$result->setStatus($statusCode);
393398
return $result;

0 commit comments

Comments
 (0)