Skip to content

Commit a93e745

Browse files
authored
Merge pull request #59 from pdsinterop/cleanup
Code cleanup
2 parents ce78530 + 5b39d75 commit a93e745

16 files changed

+496
-370
lines changed

README.md

+32-53
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,17 @@ _Standalone Solid Server written in PHP by PDS Interop_
1616
<!-- toc -->
1717

1818
- [Background](#background)
19-
* [Installation](#installation)
19+
- [Installation](#installation)
2020
- [Usage](#usage)
21-
* [Docker images](#docker-images)
22-
* [Local environment](#local-environment)
23-
+ [Built-in PHP HTTP server](#built-in-php-http-server)
21+
- [Docker images](#docker-images)
22+
- [Local environment](#local-environment)
23+
- [Built-in PHP HTTP server](#built-in-php-http-server)
2424
- [Security](#security)
2525
- [Running solid/webid-provider-tests](#running-solidwebid-provider-tests)
2626
- [Available Features](#available-features)
2727
- [Development](#development)
28-
* [Project structure](#project-structure)
29-
* [Coding conventions](#coding-conventions)
30-
* [Testing](#testing)
28+
- [Project structure](#project-structure)
29+
- [Testing](#testing)
3130
- [Contributing](#contributing)
3231
- [License](#license)
3332

@@ -47,10 +46,6 @@ they define:
4746
- Resource (reading and writing) API
4847
- Social Web App Protocols (Notifications, Friends Lists, Followers and Following)
4948

50-
<!--
51-
To read more about Solid, and which IETF and W3C specifications are used, visit: https://pdsinterop.org/solid-specs-overview/
52-
-->
53-
5449
## Installation
5550

5651
To install the project, clone it from GitHub and install the PHP dependencies
@@ -67,21 +62,15 @@ At this point, the application is ready to run.
6762

6863
The PHP Solid server can be run in several different ways.
6964

70-
<!-- @TODO: Add local Dockerfile -->
71-
7265
The application can be run with a Docker image of your choice or on a local
7366
environment, using Apache, NginX, or PHP's internal HTTP server. The latter is
7467
only advised in development.
7568

7669
For security reasons, the server expects to run on HTTPS (also known as HTTP+TLS).
7770

78-
To run insecure, set the environment variable `ENVIRONMENT` to `develop`. This
79-
will prohibit the application from running in production mode.
80-
81-
<!--
82-
@TODO: Add single-button deploy scripts/config for Heroku, Glitch, and other
83-
popular playgrounds/developer oriented service providers.
84-
-->
71+
To run insecure, for instance when the application is run behind a proxy or in a
72+
PHP-FPM (or similar) setup, set the environment variable `PROXY_MODE`.
73+
This will allow the application to accept HTTP requests.
8574

8675
### Docker images
8776

@@ -209,71 +198,61 @@ The underlying functionality for these features is provided by:
209198

210199
## Development
211200

201+
The easiest way to develop this project is by running the environment provided
202+
by the `docker-compose.yml` file. This can be done by running `docker-compose up`.
203+
204+
This will start the application and a pubsub server in separate docker containers.
205+
212206
### Project structure
213207

214208
This project is structured as follows:
215209

216-
<!--
217-
.
218-
├── build <- Artifacts created by CI and CLI scripts
219-
├── cli <- CLI scripts
220-
├── docs <- Documentation, hosted at https://pdsinterop.org/solid-server-php/
221-
├── src <- Source code
222-
├── tests <- Unit- and integration-tests
223-
├── vendor <- Third-party and vendor code
224-
├── web <- Web content
225-
├── composer.json <- PHP package and dependency configuration
226-
└── README.md <- You are now here
227-
-->
228210
```
229211
.
230-
├── src <- Source code
231-
├── vendor <- Third-party and vendor code
232-
├── web <- Web content
212+
├── bin/ <- CLI scripts
213+
├── config/ <- Empty directory where server configuration is generated
214+
├── docs/ <- Documentation
215+
├── src/ <- Source code
216+
├── tests/ <- Test fixtures, Integration- and unit-tests
217+
├── vendor/ <- Third-party and vendor code
218+
├── web/ <- Web content
233219
├── composer.json <- PHP package and dependency configuration
234220
└── README.md <- You are now here
235221
```
236222

237-
<!--
238-
### Coding conventions
239-
240-
You can also run [php-cs-fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer) with the configuration file that can be found in the project root directory.
241-
242-
This project comes with a configuration file and an executable for [php-cs-fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer) (`.php_cs`) that you can use to (re)format your sourcecode for compliance with this project's coding guidelines:
243-
244-
```sh
245-
$ composer php-cs-fixer fix
246-
```
247-
248223
### Testing
249224

250225
The PHPUnit version to be used is the one installed as a `dev-` dependency via composer. It can be run using `composer test` or by calling it directly:
251226

252227
```sh
253-
$ ./vendor/bin/phpunit
228+
$ ./bin/phpunit
254229
```
255-
-->
256230

257231
## Contributing
258232

259-
Questions or feedback can be given by [opening an issue on GitHub](https://github.com/pdsinterop/php-solid-server/issues).
233+
Questions or feedback can be given by [opening an issue on GitHub][issues-link].
260234

261235
All PDS Interop projects are open source and community-friendly.
262236
Any contribution is welcome!
263-
For more details read the [contribution guidelines](CONTRIBUTING.md).
237+
For more details read the [contribution guidelines][contributing-link].
264238

265239
All PDS Interop projects adhere to [the Code Manifesto](http://codemanifesto.com)
266-
as its [code-of-conduct](CODE_OF_CONDUCT.md). Contributors are expected to abide by its terms.
240+
as its [code-of-conduct][code-of-conduct]. Contributors are expected to abide by its terms.
267241

268242
There is [a list of all contributors on GitHub][contributors-page].
269243

270-
For a list of changes see the [CHANGELOG](CHANGELOG.md) or the GitHub releases page.
244+
For a list of changes see the [CHANGELOG][changelog] or [the GitHub releases page][releases-page].
271245

272246
## License
273247

274248
All code created by PDS Interop is licensed under the [MIT License][license-link].
275249

276-
[contributors-page]: https://github.com/pdsinterop/php-solid-server/contributors
250+
[changelog]: CHANGELOG.md
251+
[code-of-conduct]: CODE_OF_CONDUCT.md
252+
[contributing-link]: CONTRIBUTING.md
253+
[contributors-page]: https://github.com/pdsinterop/php-solid-server/contributors
254+
[issues-link]: https://github.com/pdsinterop/php-solid-server/issues
255+
[releases-page]: https://github.com/pdsinterop/php-solid-server/releases
277256
[keep-a-changelog-link]: https://keepachangelog.com/
278257
[keep-a-changelog-shield]: https://img.shields.io/badge/Keep%20a%20Changelog-f15d30.svg?logo=data%3Aimage%2Fsvg%2Bxml%3Bbase64%2CPHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGZpbGw9IiNmZmYiIHZpZXdCb3g9IjAgMCAxODcgMTg1Ij48cGF0aCBkPSJNNjIgN2MtMTUgMy0yOCAxMC0zNyAyMmExMjIgMTIyIDAgMDAtMTggOTEgNzQgNzQgMCAwMDE2IDM4YzYgOSAxNCAxNSAyNCAxOGE4OSA4OSAwI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MyAwIDAwLTEtMTJjLTYtMTgtMTYtMzEtMzItMzh6bS0yMyA5MWgtMWMtNyAwLTE0LTItMjEtN2EyNyAyNyAwIDAxLTEwLTEzIDU3IDU3IDAgMDEtNC0yMCA2MyA2MyAwIDAxNi0yNWM1LTEyIDEyLTE5IDI0LTIxIDktMyAxOC0yIDI3IDIgMTQgNiAyMyAxOCAyNyAzM3MtMiAzMS0xNiA0MGMtMTEgOC0yMSAxMS0zMiAxMXptMS0zNHYxNGgtOFY2OGg4djI4bDEwLTEwaDExbC0xNCAxNSAxNyAxOEg5NnoiLz48L3N2Zz4K
279258
[license-link]: ./LICENSE

docker-compose.yml

+9
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,21 @@ services:
44
build:
55
context: .
66
environment:
7+
# During development it can be useful to set the ENVIRONMENT to "development"
8+
# in order to see more details about the errors.
9+
# ENVIRONMENT: development
710
USERNAME: alice
811
PASSWORD: alice123
12+
# to run in HTTP mode, set PROXY_MODE
13+
# PROXY_MODE: true
914
PUBSUB_URL: http://pubsub:8080
1015
ports:
1116
- 80:80
1217
- 443:443
18+
volumes:
19+
- .:/app/
20+
# @TODO: The storage directory should be mounted separately
21+
# as it really _should_ live outside the code directory
1322
pubsub:
1423
build:
1524
context: https://github.com/pdsinterop/php-solid-pubsub-server.git#main

src/Controller/AuthorizeController.php

+65-28
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/HelloWorldController.php

+60-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,67 @@ class HelloWorldController extends AbstractController
99
{
1010
final public function __invoke(ServerRequestInterface $request, array $args): ResponseInterface
1111
{
12-
$response = $this->getResponse();
1312

14-
$response->getBody()->write('<h1>Hello, World!</h1>');
13+
$body = <<<'HTML'
14+
<h1 class="title">Hello, World!</h1>
15+
<p>The following main URL are available on this server:</p>
16+
<ul>
17+
<li>
18+
<a href="/.well-known/openid-configuration"><code>/.well-known/openid-configuration</code></a>
19+
<span>Openid</span>
20+
</li>
21+
<li>
22+
<a href="/authorize"><code>/authorize</code></a>
23+
<span>Authorize</span>
24+
</li>
25+
<li>
26+
<a href="/jwks"><code>/jwks</code></a>
27+
<span>Jwks</span>
28+
</li>
29+
<li>
30+
<a href="/login/"><code>/login/</code></a><sup>1, 3</sup>
31+
<span>Login</span>
32+
</li>
33+
<li>
34+
<a href="/profile/"><code>/profile/</code></a><sup>1</sup>
35+
<span>Profile</span>
36+
</li>
37+
<li>
38+
<a href="/profile/card"><code>/profile/card</code></a><sup>2</sup>
39+
<span>Card</span>
40+
</li>
41+
<li>
42+
<code>/register</code><sup>4</sup>
43+
<span>Register</span>
44+
</li>
45+
<li>
46+
<code>/sharing/{clientId}</code><sup>3</sup>
47+
<span>Approval</span>
48+
</li>
49+
<li>
50+
<a href="/storage/"><code>/storage/{path}</code></a><sup>3</sup>
51+
<span>Resource</span>
52+
</li>
53+
<li>
54+
<code>/token/</code><sup>1, 4</sup>
55+
<span>Token</span>
56+
</li>
57+
</ul>
1558
16-
return $response;
59+
<section class="is-info is-light is-size-7 notification">
60+
<h2>Footnotes</h2>
61+
<ol>
62+
<li>Also available without trailing slash <code>/</code></li>
63+
<li>A file extension can be added to force a specific format. For instance <code>/profile/card</code> can be
64+
requested as <code>/profile/card.ttl</code> to request a Turtle file, or <code>/profile/card.json</code> to
65+
request a JSON-LD file
66+
</li>
67+
<li>This URL also accepts POST requests</li>
68+
<li>This URL <em>only</em> accepts POST requests</li>
69+
</ol>
70+
</section>
71+
HTML;
72+
73+
return $this->createTemplateResponse($body);
1774
}
1875
}

0 commit comments

Comments
 (0)