Skip to content

Commit f7fbae8

Browse files
committed
Add more error handling to AuthorizeController.
1 parent b484ed7 commit f7fbae8

File tree

2 files changed

+68
-30
lines changed

2 files changed

+68
-30
lines changed

src/Controller/AuthorizeController.php

Lines changed: 65 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,62 +10,99 @@ class AuthorizeController extends ServerController
1010
final public function __invoke(ServerRequestInterface $request, array $args): ResponseInterface
1111
{
1212
if (!isset($_SESSION['userid'])) {
13-
$response = $this->getResponse();
14-
$response = $response->withStatus(302, "Approval required");
15-
1613
// FIXME: Generate a proper url for this;
17-
$baseUrl = $this->baseUrl;
18-
$loginUrl = $baseUrl . "/login/?returnUrl=" . urlencode($_SERVER['REQUEST_URI']);
19-
$response = $response->withHeader("Location", $loginUrl);
20-
return $response;
14+
$loginUrl = $this->baseUrl . "/login/?returnUrl=" . urlencode($_SERVER['REQUEST_URI']);
15+
16+
return $this->getResponse()
17+
->withHeader("Location", $loginUrl)
18+
->withStatus(302, "Approval required")
19+
;
2120
}
21+
22+
$queryParams = $request->getQueryParams();
23+
24+
if (! isset($queryParams['request'])) {
25+
return $this->getResponse()
26+
->withStatus(400, "Bad request, missing request")
27+
;
28+
}
29+
2230
$parser = new \Lcobucci\JWT\Parser();
2331

32+
try {
33+
$token = $parser->parse($queryParams['request']);
34+
} catch (\Exception $exception) {
35+
return $this->getResponse()
36+
->withStatus(400, $exception->getMessage())
37+
;
38+
}
39+
2440
try {
25-
$token = $parser->parse($request->getQueryParams()['request']);
2641
$_SESSION["nonce"] = $token->getClaim('nonce');
27-
} catch(\Exception $e) {
28-
$_SESSION["nonce"] = $request->getQueryParams()['nonce'];
42+
} catch(\OutOfBoundsException $e) {
43+
if (! isset($queryParams['nonce'])) {
44+
return $this->getResponse()
45+
->withStatus(400, "Bad request, missing nonce")
46+
;
47+
}
48+
49+
$_SESSION["nonce"] = $queryParams['nonce'];
2950
}
3051

31-
$getVars = $request->getQueryParams();
52+
/*/ Prepare GET parameters for OAUTH server request /*/
53+
$getVars = $queryParams;
54+
55+
$getVars['response_type'] = $this->getResponseType($queryParams);
56+
$getVars['scope'] = "openid" ;
57+
3258
if (!isset($getVars['grant_type'])) {
3359
$getVars['grant_type'] = 'implicit';
3460
}
35-
$getVars['response_type'] = $this->getResponseType();
36-
$getVars['scope'] = "openid" ;
3761

3862
if (!isset($getVars['redirect_uri'])) {
3963
try {
4064
$getVars['redirect_uri'] = $token->getClaim("redirect_uri");
4165
} catch(\Exception $e) {
42-
$response = $this->getResponse();
43-
$response->withStatus(400, "Bad request, missing redirect uri");
44-
return $response;
66+
return $this->getResponse()
67+
->withStatus(400, "Bad request, missing redirect uri")
68+
;
4569
}
4670
}
47-
$clientId = $getVars['client_id'];
48-
$approval = $this->checkApproval($clientId);
71+
72+
if (! isset($queryParams['client_id'])) {
73+
return $this->getResponse()
74+
->withStatus(400, "Bad request, missing client_id")
75+
;
76+
}
77+
78+
$clientId = $getVars['client_id'];
79+
$approval = $this->checkApproval($clientId);
4980
if (!$approval) {
50-
$response = $this->getResponse();
51-
$response = $response->withStatus(302, "Approval required");
52-
5381
// FIXME: Generate a proper url for this;
54-
$baseUrl = $this->baseUrl;
55-
$approvalUrl = $baseUrl . "/sharing/$clientId/?returnUrl=" . urlencode($_SERVER['REQUEST_URI']);
56-
$response = $response->withHeader("Location", $approvalUrl);
57-
return $response;
82+
$approvalUrl = $this->baseUrl . "/sharing/$clientId/?returnUrl=" . urlencode($_SERVER['REQUEST_URI']);
83+
84+
return $this->getResponse()
85+
->withHeader("Location", $approvalUrl)
86+
->withStatus(302, "Approval required")
87+
;
5888
}
5989

90+
// replace the request getVars with the morphed version
91+
$request = $request->withQueryParams($getVars);
92+
6093
$user = new \Pdsinterop\Solid\Auth\Entity\User();
6194
$user->setIdentifier($this->getProfilePage());
6295

63-
$request = $request->withQueryParams($getVars); // replace the request getVars with the morphed version;
6496
$response = new \Laminas\Diactoros\Response();
6597
$server = new \Pdsinterop\Solid\Auth\Server($this->authServerFactory, $this->authServerConfig, $response);
6698

6799
$response = $server->respondToAuthorizationRequest($request, $user, $approval);
68-
$response = $this->tokenGenerator->addIdTokenToResponse($response, $clientId, $this->getProfilePage(), $_SESSION['nonce'], $this->config->getPrivateKey());
69-
return $response;
100+
101+
return $this->tokenGenerator->addIdTokenToResponse($response,
102+
$clientId,
103+
$this->getProfilePage(),
104+
$_SESSION['nonce'],
105+
$this->config->getPrivateKey()
106+
);
70107
}
71108
}

src/Controller/ServerController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,10 @@ public function getProfilePage() : string
126126
return $this->baseUrl . "/profile/card#me"; // FIXME: would be better to base this on the available routes if possible.
127127
}
128128

129-
public function getResponseType() : string
129+
public function getResponseType($params) : string
130130
{
131-
$responseTypes = explode(" ", $_GET['response_type'] ?? '');
131+
$responseTypes = explode(" ", $params['response_type'] ?? '');
132+
132133
foreach ($responseTypes as $responseType) {
133134
switch ($responseType) {
134135
case "token":

0 commit comments

Comments
 (0)