Skip to content

Commit a404863

Browse files
committed
Change code in ServerController to more closely match code in Standalone PHP Solid Server.
1 parent 5889e26 commit a404863

File tree

2 files changed

+45
-38
lines changed

2 files changed

+45
-38
lines changed

solid/lib/Controller/ServerController.php

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ public function authorize() {
162162
}
163163
$clientId = $_GET['client_id'];
164164

165+
$getVars = $_GET;
165166
if (isset($_GET['request'])) {
166167
$jwtConfig = Configuration::forSymmetricSigner(new Sha256(), InMemory::plainText($this->config->getPrivateKey()));
167168
try {
@@ -170,51 +171,51 @@ public function authorize() {
170171
} catch(\Exception $e) {
171172
$this->session->set("nonce", $_GET['nonce']);
172173
}
173-
}
174174

175-
$getVars = $_GET;
176-
if (!isset($getVars['grant_type'])) {
177-
$getVars['grant_type'] = 'implicit';
178-
}
179-
$getVars['response_type'] = $this->getResponseType();
180-
$getVars['scope'] = "openid" ;
181-
182-
if (!isset($getVars['redirect_uri'])) {
183-
if (!isset($token)) {
184-
$result = new JSONResponse('Bad request, does not contain valid token');
185-
$result->setStatus(400);
186-
return $result;
187-
// return $result->addHeader('Access-Control-Allow-Origin', '*');
175+
if (!isset($getVars['grant_type'])) {
176+
$getVars['grant_type'] = 'implicit';
188177
}
189-
try {
190-
$getVars['redirect_uri'] = $token->claims()->get("redirect_uri");
191-
} catch(\Exception $e) {
192-
$result = new JSONResponse('Bad request, missing redirect uri');
193-
$result->setStatus(400);
194-
return $result;
195-
// return $result->addHeader('Access-Control-Allow-Origin', '*');
178+
$getVars['response_type'] = $this->getResponseType();
179+
$getVars['scope'] = "openid";
180+
181+
if (!isset($getVars['redirect_uri'])) {
182+
if (!isset($token)) {
183+
return new JSONResponse('Bad request, does not contain valid token', 400);
184+
}
185+
186+
try {
187+
$getVars['redirect_uri'] = $token->claims()->get("redirect_uri");
188+
} catch(\Exception $e) {
189+
return new JSONResponse('Bad request, missing redirect uri', 400);
190+
}
196191
}
197192
}
198193

199-
if (preg_match("/^http(s)?:/", $getVars['client_id'])) {
194+
$request = \Laminas\Diactoros\ServerRequestFactory::fromGlobals($_SERVER, $getVars, $_POST, $_COOKIE, $_FILES);
195+
$response = new \Laminas\Diactoros\Response();
196+
$authServer = new \Pdsinterop\Solid\Auth\Server($this->authServerFactory, $this->authServerConfig, $response);
197+
198+
// @FIXME: Check OIDC Spec for rules regarding Client updates
199+
if (preg_match("/^http(s)?:/", $clientId)) {
200200
$parsedOrigin = parse_url($getVars['redirect_uri']);
201201
$origin = $parsedOrigin['scheme'] . '://' . $parsedOrigin['host'];
202202
if (isset($parsedOrigin['port'])) {
203203
$origin .= ":" . $parsedOrigin['port'];
204204
}
205205
$clientData = array(
206206
"client_id_issued_at" => time(),
207-
"client_name" => $getVars['client_id'],
207+
"client_name" => $clientId,
208208
"origin" => $origin,
209209
"redirect_uris" => array(
210210
$getVars['redirect_uri']
211211
)
212212
);
213-
$clientId = $this->config->saveClientRegistration($origin, $clientData)['client_id'];
214-
$clientId = $this->config->saveClientRegistration($getVars['client_id'], $clientData)['client_id'];
213+
214+
$this->config->saveClientRegistration($origin, $clientData);
215+
$clientId = $this->config->saveClientRegistration($clientId, $clientData)['client_id'];
216+
215217
$returnUrl = $getVars['redirect_uri'];
216218
} else {
217-
$clientId = $getVars['client_id'];
218219
$returnUrl = $_SERVER['REQUEST_URI'];
219220
}
220221

@@ -231,7 +232,8 @@ public function authorize() {
231232
$result->setStatus(302);
232233
$approvalUrl = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute("solid.page.approval", array("clientId" => $clientId, "returnUrl" => $returnUrl)));
233234
$result->addHeader("Location", $approvalUrl);
234-
return $result; // ->addHeader('Access-Control-Allow-Origin', '*');
235+
236+
return $result;
235237
}
236238

237239
if (isset($getVars['redirect_uri'])) {
@@ -266,23 +268,21 @@ public function authorize() {
266268
return $result;
267269
}
268270

271+
$webId = $this->getProfilePage();
269272
$user = new \Pdsinterop\Solid\Auth\Entity\User();
270-
$user->setIdentifier($this->getProfilePage());
273+
$user->setIdentifier($webId);
271274

272-
$request = \Laminas\Diactoros\ServerRequestFactory::fromGlobals($_SERVER, $getVars, $_POST, $_COOKIE, $_FILES);
273-
$response = new \Laminas\Diactoros\Response();
274-
$server = new \Pdsinterop\Solid\Auth\Server($this->authServerFactory, $this->authServerConfig, $response);
275+
$response = $authServer->respondToAuthorizationRequest($request, $user, $approval);
275276

276-
$response = $server->respondToAuthorizationRequest($request, $user, $approval);
277277
$response = $this->tokenGenerator->addIdTokenToResponse(
278278
$response,
279279
$clientId,
280-
$this->getProfilePage(),
280+
$webId,
281281
$this->session->get("nonce"),
282282
$this->config->getPrivateKey()
283283
);
284284

285-
return $this->respond($response); // ->addHeader('Access-Control-Allow-Origin', '*');
285+
return $this->respond($response);
286286
}
287287

288288
private function checkApproval($clientId) {

solid/tests/Unit/Controller/ServerControllerTest.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class ServerControllerTest extends TestCase
3939
////////////////////////////////// FIXTURES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
4040

4141
private const MOCK_CLIENT_ID = 'mock-client-id';
42+
private const MOCK_RESPONSE_TYPE = 'mock-response-type';
43+
private const MOCK_URI = 'mock uri';
4244
private const MOCK_USER_ID = 'mock user id';
4345

4446
public static string $clientData = '';
@@ -141,7 +143,12 @@ public function testAuthorizeWithoutClientId()
141143
*/
142144
public function testAuthorizeWithoutValidToken()
143145
{
144-
$_GET['response_type'] = 'mock-response-type';
146+
$_GET['client_id'] = self::MOCK_CLIENT_ID;
147+
$_GET['nonce'] = 'mock-nonce';
148+
$_GET['request'] = 'mock request';
149+
$_GET['response_type'] = self::MOCK_RESPONSE_TYPE;
150+
151+
$_SERVER['REQUEST_URI'] = self::MOCK_URI;
145152

146153
$parameters = $this->createMockConstructorParameters();
147154

@@ -166,9 +173,9 @@ public function testAuthorizeWithoutApprovedClient()
166173
$_GET['nonce'] = 'mock-nonce';
167174
// JWT with empty payload, HS256 encoded, created with `private.key` from fixtures
168175
$_GET['request'] = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.8VKCTiBegJPuPIZlp0wbV0Sbdn5BS6TE5DCx6oYNc5o';
169-
$_GET['response_type'] = 'mock-response-type';
176+
$_GET['response_type'] = self::MOCK_RESPONSE_TYPE;
170177

171-
$_SERVER['REQUEST_URI'] = 'mock uri';
178+
$_SERVER['REQUEST_URI'] = self::MOCK_URI;
172179

173180
$parameters = $this->createMockConstructorParameters();
174181

@@ -243,7 +250,7 @@ public function testAuthorize()
243250
$_GET['nonce'] = 'mock-nonce';
244251
// JWT with empty payload, HS256 encoded, created with `private.key` from fixtures
245252
$_GET['request'] = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.8VKCTiBegJPuPIZlp0wbV0Sbdn5BS6TE5DCx6oYNc5o';
246-
$_GET['response_type'] = 'mock-response-type';
253+
$_GET['response_type'] = self::MOCK_RESPONSE_TYPE;
247254
$_GET['redirect_uri'] = 'https://mock.client/redirect';
248255

249256
$_SERVER['REQUEST_URI'] = 'https://mock.server';

0 commit comments

Comments
 (0)