Skip to content

Commit d2c59df

Browse files
committed
Code cleanup
- Whitespace fixes - Move FQN to `use` statements - Use PHP source functions instead of alias - Collpase multi-if statements - Inline function returns - Remove commented out code
1 parent 4b12465 commit d2c59df

File tree

8 files changed

+123
-113
lines changed

8 files changed

+123
-113
lines changed

src/Controller/AuthorizeController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,4 @@ final public function __invoke(ServerRequestInterface $request, array $args): Re
6868
$response = $this->tokenGenerator->addIdTokenToResponse($response, $clientId, $this->getProfilePage(), $_SESSION['nonce'], $this->config->getPrivateKey());
6969
return $response;
7070
}
71-
}
71+
}

src/Controller/CorsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class CorsController extends ServerController
99
{
1010
final public function __invoke(ServerRequestInterface $request, array $args): ResponseInterface
11-
{
11+
{
1212
return $this->getResponse()->withHeader("Access-Control-Allow-Headers", "*");
1313
}
1414
}

src/Controller/OpenidController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ final public function __invoke(ServerRequestInterface $request, array $args): Re
1313
$server = new \Pdsinterop\Solid\Auth\Server($this->authServerFactory, $this->authServerConfig, $response);
1414
return $server->respondToOpenIdMetadataRequest();
1515
}
16-
}
16+
}

src/Controller/ResourceController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ final public function __invoke(Request $request, array $args) : Response
6262
return $this->WAC->addWACHeaders($request, $response, $webId);
6363
}
6464

65-
private function generateDefaultAcl() {
65+
private function generateDefaultAcl() {
6666
$defaultProfile = <<< EOF
6767
# Root ACL resource for the user account
6868
@prefix acl: <http://www.w3.org/ns/auth/acl#>.

src/Controller/ServerController.php

Lines changed: 102 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ abstract class ServerController extends AbstractController
1717
public function __construct()
1818
{
1919
$this->config = new \Pdsinterop\Solid\ServerConfig(__DIR__.'/../../config/');
20-
21-
$this->authServerConfig = $this->createAuthServerConfig();
22-
$this->authServerFactory = (new \Pdsinterop\Solid\Auth\Factory\AuthorizationServerFactory($this->authServerConfig))->create();
20+
21+
$this->authServerConfig = $this->createAuthServerConfig();
22+
$this->authServerFactory = (new \Pdsinterop\Solid\Auth\Factory\AuthorizationServerFactory($this->authServerConfig))->create();
2323
$this->tokenGenerator = (new \Pdsinterop\Solid\Auth\TokenGenerator($this->authServerConfig));
2424
$this->baseUrl = isset($_ENV['SERVER_ROOT']) ? $_ENV['SERVER_ROOT'] : "https://localhost";
2525
}
@@ -39,98 +39,107 @@ public function getOpenIdEndpoints() {
3939
];
4040
}
4141

42-
public function getKeys() {
43-
$encryptionKey = $this->config->getEncryptionKey();
44-
$privateKey = $this->config->getPrivateKey();
45-
$key = openssl_pkey_get_private($privateKey);
46-
$publicKey = openssl_pkey_get_details($key)['key'];
47-
return [
48-
"encryptionKey" => $encryptionKey,
49-
"privateKey" => $privateKey,
50-
"publicKey" => $publicKey
51-
];
52-
}
42+
public function getKeys()
43+
{
44+
$encryptionKey = $this->config->getEncryptionKey();
45+
$privateKey = $this->config->getPrivateKey();
46+
$key = openssl_pkey_get_private($privateKey);
47+
$publicKey = openssl_pkey_get_details($key)['key'];
5348

54-
public function createAuthServerConfig() {
55-
$clientId = $_GET['client_id']; // FIXME: No request object here to get the client Id from.
56-
$client = $this->getClient($clientId);
57-
$keys = $this->getKeys();
58-
try {
59-
$config = (new \Pdsinterop\Solid\Auth\Factory\ConfigFactory(
60-
$client,
61-
$keys['encryptionKey'],
62-
$keys['privateKey'],
63-
$keys['publicKey'],
64-
$this->getOpenIdEndpoints()
65-
))->create();
66-
} catch(\Throwable $e) {
67-
// var_dump($e);
68-
}
69-
return $config;
70-
}
49+
return [
50+
"encryptionKey" => $encryptionKey,
51+
"privateKey" => $privateKey,
52+
"publicKey" => $publicKey,
53+
];
54+
}
7155

72-
public function getClient($clientId) {
73-
$clientRegistration = $this->config->getClientRegistration($clientId);
74-
75-
if ($clientId && sizeof($clientRegistration)) {
76-
return new \Pdsinterop\Solid\Auth\Config\Client(
77-
$clientId,
78-
$clientRegistration['client_secret'],
79-
$clientRegistration['redirect_uris'],
80-
$clientRegistration['client_name']
81-
);
82-
} else {
83-
return new \Pdsinterop\Solid\Auth\Config\Client('','',array(),'');
84-
}
85-
}
56+
public function createAuthServerConfig()
57+
{
58+
$clientId = $_GET['client_id']; // FIXME: No request object here to get the client Id from.
59+
$client = $this->getClient($clientId);
60+
$keys = $this->getKeys();
61+
try {
62+
$config = (new ConfigFactory(
63+
$client,
64+
$keys['encryptionKey'],
65+
$keys['privateKey'],
66+
$keys['publicKey'],
67+
$this->getOpenIdEndpoints()
68+
))->create();
69+
} catch (Throwable $e) {
70+
// var_dump($e);
71+
}
8672

87-
public function createConfig($baseUrl) {
88-
// if (isset($_GET['client_id'])) {
89-
$clientId = $_GET['client_id'];
90-
$client = $this->getClient($clientId, $baseUrl);
91-
// }
92-
try {
93-
$config = (new \Pdsinterop\Solid\Auth\Factory\ConfigFactory(
94-
$client,
95-
$this->keys['encryptionKey'],
96-
$this->keys['privateKey'],
97-
$this->keys['publicKey'],
98-
$this->openIdConfiguration
99-
))->create();
100-
} catch(\Throwable $e) {
101-
var_dump($e);
102-
}
103-
return $config;
104-
}
105-
106-
public function checkApproval($clientId) {
107-
$allowedClients = $this->config->getAllowedClients($this->userId);
108-
if ($clientId == md5("tester")) { // FIXME: Double check that this is not a security issue; It is only here to help the test suite;
109-
return \Pdsinterop\Solid\Auth\Enum\Authorization::APPROVED;
110-
}
111-
if (in_array($clientId, $allowedClients)) {
112-
return \Pdsinterop\Solid\Auth\Enum\Authorization::APPROVED;
113-
} else {
114-
return \Pdsinterop\Solid\Auth\Enum\Authorization::DENIED;
115-
}
116-
}
117-
118-
public function getProfilePage() {
119-
return $this->baseUrl . "/profile/card#me"; // FIXME: would be better to base this on the available routes if possible.
120-
}
121-
122-
public function getResponseType() {
73+
return $config;
74+
}
75+
76+
public function getClient($clientId)
77+
{
78+
$clientRegistration = $this->config->getClientRegistration($clientId);
79+
80+
if ($clientId && count($clientRegistration)) {
81+
$client = new Client(
82+
$clientId,
83+
$clientRegistration['client_secret'],
84+
$clientRegistration['redirect_uris'],
85+
$clientRegistration['client_name']
86+
);
87+
} else {
88+
$client = new Client('', '', [], '');
89+
}
90+
91+
return $client;
92+
}
93+
94+
public function createConfig()
95+
{
96+
// if (isset($_GET['client_id'])) {
97+
$clientId = $_GET['client_id'];
98+
$client = $this->getClient($clientId);
99+
100+
// }
101+
return (new ConfigFactory(
102+
$client,
103+
$this->keys['encryptionKey'],
104+
$this->keys['privateKey'],
105+
$this->keys['publicKey'],
106+
$this->openIdConfiguration
107+
))->create();
108+
}
109+
public function checkApproval($clientId)
110+
{
111+
$approval = Authorization::DENIED;
112+
113+
$allowedClients = $this->config->getAllowedClients($this->userId);
114+
115+
if (
116+
$clientId === md5("tester") // FIXME: Double check that this is not a security issue; It is only here to help the test suite;
117+
|| in_array($clientId, $allowedClients, true)
118+
) {
119+
$approval = Authorization::APPROVED;
120+
}
121+
122+
return $approval;
123+
}
124+
public function getProfilePage() : string
125+
{
126+
return $this->baseUrl . "/profile/card#me"; // FIXME: would be better to base this on the available routes if possible.
127+
}
128+
129+
public function getResponseType() : string
130+
{
123131
$responseTypes = explode(" ", $_GET['response_type'] ?? '');
124-
foreach ($responseTypes as $responseType) {
125-
switch ($responseType) {
126-
case "token":
127-
return "token";
128-
break;
129-
case "code":
130-
return "code";
131-
break;
132-
}
133-
}
134-
return "token"; // default to token response type;
135-
}
132+
foreach ($responseTypes as $responseType) {
133+
switch ($responseType) {
134+
case "token":
135+
return "token";
136+
break;
137+
case "code":
138+
return "code";
139+
break;
140+
}
141+
}
142+
143+
return "token"; // default to token response type;
144+
}
136145
}

src/Controller/TokenController.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
class TokenController extends ServerController
1212
{
1313
final public function __invoke(ServerRequestInterface $request, array $args): ResponseInterface
14-
{
14+
{
1515
$code = $request->getParsedBody()['code'];
1616
$clientId = $request->getParsedBody()['client_id'];
1717
$DPop = new DPop();
@@ -21,16 +21,25 @@ final public function __invoke(ServerRequestInterface $request, array $args): Re
2121
} catch(\Exception $e) {
2222
return $this->getResponse()->withStatus(409, "Invalid token");
2323
}
24-
25-
$response = new \Laminas\Diactoros\Response();
26-
$server = new \Pdsinterop\Solid\Auth\Server($this->authServerFactory, $this->authServerConfig, $response);
24+
25+
$server = new \Pdsinterop\Solid\Auth\Server(
26+
$this->authServerFactory,
27+
$this->authServerConfig,
28+
new \Laminas\Diactoros\Response()
29+
);
30+
2731
$response = $server->respondToAccessTokenRequest($request);
2832

2933
// FIXME: not sure if decoding this here is the way to go.
3034
// FIXME: because this is a public page, the nonce from the session is not available here.
3135
$codeInfo = $this->tokenGenerator->getCodeInfo($code);
32-
$response = $this->tokenGenerator->addIdTokenToResponse($response, $clientId, $codeInfo['user_id'], $_SESSION['nonce'], $this->config->getPrivateKey(), $dpopKey);
3336

34-
return $response;
37+
return $this->tokenGenerator->addIdTokenToResponse($response,
38+
$clientId,
39+
$codeInfo['user_id'],
40+
$_SESSION['nonce'],
41+
$this->config->getPrivateKey(),
42+
$dpopKey
43+
);
3544
}
3645
}

src/ServerConfig.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public function addAllowedClient($userId, $clientId) {
116116
$this->userConfig['allowedClients'] = array_unique($this->userConfig['allowedClients']);
117117
$this->saveUserConfig();
118118
}
119+
119120
public function removeAllowedClient($userId, $clientId) {
120121
$this->userConfig['allowedClients'] = array_diff($this->userConfig['allowedClients'], array($clientId));
121122
$this->saveUserConfig();

web/index.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,6 @@
5555
$container->add(ServerRequestInterface::class, Request::class);
5656
$container->add(ResponseInterface::class, Response::class);
5757

58-
/*
59-
$adapter = new \League\Flysystem\Adapter\Local(__DIR__ . '/../tests/fixtures');
60-
$filesystem = new \League\Flysystem\Filesystem($adapter);
61-
$graph = new \EasyRdf_Graph();
62-
$plugin = new \Pdsinterop\Rdf\Flysystem\Plugin\ReadRdf($graph);
63-
$filesystem->addPlugin($plugin);
64-
*/
65-
6658
$container->share(FilesystemInterface::class, function () use ($request) {
6759
// @FIXME: Filesystem root and the $adapter should be configurable.
6860
// Implement this with `$filesystem = \MJRider\FlysystemFactory\create(getenv('STORAGE_ENDPOINT'));`
@@ -129,7 +121,7 @@
129121
OpenidController::class,
130122
ProfileController::class,
131123
RegisterController::class,
132-
StorageController::class,
124+
StorageController::class,
133125
TokenController::class,
134126
];
135127

@@ -200,7 +192,6 @@
200192

201193
array_walk($methods, static function ($method) use (&$group) {
202194
$group->map($method, '/', AddSlashToPathController::class);
203-
// $group->map($method, '//', StorageController::class);
204195
$group->map($method, '{path:.*}', ResourceController::class);
205196
});
206197
});

0 commit comments

Comments
 (0)