Skip to content

Commit 1304ea5

Browse files
authored
Merge pull request #14 from pdsinterop/solid-community-login
achieve login with solid.community
2 parents bf4e171 + 5df5872 commit 1304ea5

17 files changed

Lines changed: 569 additions & 210 deletions

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"league/oauth2-server": "^8.0",
3030
"league/route": "^4.5",
3131
"pdsinterop/flysystem-rdf": "dev-dev",
32-
"pdsinterop/solid-auth": "dev-feature/implicit-grant",
32+
"pdsinterop/solid-auth": "dev-master",
3333
"php-http/httplug": "^2.1",
3434
"phptal/phptal": "^1.4"
3535
},

config/placeholder.txt

Whitespace-only changes.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Pdsinterop\Solid\Controller;
4+
5+
use Psr\Http\Message\ResponseInterface;
6+
use Psr\Http\Message\ServerRequestInterface;
7+
8+
class ApprovalController extends ServerController
9+
{
10+
public function __invoke(ServerRequestInterface $request, array $args) : ResponseInterface
11+
{
12+
$clientId = $args['clientId'];
13+
$returnUrl = $_GET['returnUrl'];
14+
15+
return $this->createTemplateResponse('approval.html', [
16+
'clientId' => $clientId,
17+
'returnUrl' => $returnUrl,
18+
]);
19+
}
20+
}

src/Controller/AuthorizeController.php

Lines changed: 58 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,65 @@
55
use Psr\Http\Message\ResponseInterface;
66
use Psr\Http\Message\ServerRequestInterface;
77

8-
class AuthorizeController extends AbstractController
8+
class AuthorizeController extends ServerController
99
{
1010
final public function __invoke(ServerRequestInterface $request, array $args): ResponseInterface
1111
{
12-
$httpHost = $request->getServerParams()['HTTP_HOST'];
13-
14-
// // Create a request
15-
// if (!$this->userManager->userExists($this->userId)) {
16-
// $result = new JSONResponse('Authorization required');
17-
// $result->setStatus(401);
18-
// return $result;
19-
// }
20-
21-
$parser = new \Lcobucci\JWT\Parser();
22-
$token = $parser->parse($_GET['request']);
23-
$_SESSION['token'] = $token;
12+
if (!isset($_SESSION['userid'])) {
13+
$response = $this->getResponse();
14+
$response = $response->withStatus(302, "Approval required");
2415

25-
$user = new \Pdsinterop\Solid\Auth\Entity\User();
26-
$user->setIdentifier('https://server/profile/card#me');
27-
28-
$getVars = $_GET;
29-
if (!isset($getVars['grant_type'])) {
30-
$getVars['grant_type'] = 'implicit';
31-
}
32-
$getVars['response_type'] = 'token';
33-
$getVars['scope'] = "openid";
34-
35-
if (!isset($getVars['redirect_uri'])) {
36-
$getVars['redirect_uri'] = 'https://solid.community/.well-known/solid/login';
37-
}
38-
$request = \Laminas\Diactoros\ServerRequestFactory::fromGlobals($_SERVER, $getVars, $_POST, $_COOKIE, $_FILES);
39-
$response = new \Laminas\Diactoros\Response();
40-
$server = new \Pdsinterop\Solid\Auth\Server($this->authServerFactory, $this->authServerConfig, $response);
41-
42-
// if (!$this->checkApproval()) {
43-
// $result = new JSONResponse('Approval required');
44-
// $result->setStatus(302);
45-
// $result->addHeader("Location", $this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute("solid.server.sharing")));
46-
// return $result;
47-
// }
48-
49-
// FIXME: check if the user has approved - if not, show approval screen;
50-
$approval = \Pdsinterop\Solid\Auth\Enum\Authorization::APPROVED;
51-
// $approval = false;
52-
return $server->respondToAuthorizationRequest($request, $user, $approval);
53-
}
54-
}
16+
// FIXME: Generate a proper url for this;
17+
$loginUrl = "https://localhost/login/?returnUrl=" . urlencode($_SERVER['REQUEST_URI']);
18+
$response = $response->withHeader("Location", $loginUrl);
19+
return $response;
20+
}
21+
$parser = new \Lcobucci\JWT\Parser();
22+
23+
try {
24+
$token = $parser->parse($request->getQueryParams()['request']);
25+
$_SESSION["nonce"] = $token->getClaim('nonce');
26+
} catch(\Exception $e) {
27+
$_SESSION["nonce"] = $request->getQueryParams()['nonce'];
28+
}
29+
30+
$getVars = $request->getQueryParams();
31+
if (!isset($getVars['grant_type'])) {
32+
$getVars['grant_type'] = 'implicit';
33+
}
34+
$getVars['response_type'] = $this->getResponseType();
35+
$getVars['scope'] = "openid" ;
36+
37+
if (!isset($getVars['redirect_uri'])) {
38+
try {
39+
$getVars['redirect_uri'] = $token->getClaim("redirect_uri");
40+
} catch(\Exception $e) {
41+
$response = $this->getResponse();
42+
$response->withStatus(400, "Bad request, missing redirect uri");
43+
return $response;
44+
}
45+
}
46+
$clientId = $getVars['client_id'];
47+
$approval = $this->checkApproval($clientId);
48+
if (!$approval) {
49+
$response = $this->getResponse();
50+
$response = $response->withStatus(302, "Approval required");
51+
52+
// FIXME: Generate a proper url for this;
53+
$approvalUrl = "https://localhost/sharing/$clientId/?returnUrl=" . urlencode($_SERVER['REQUEST_URI']);
54+
$response = $response->withHeader("Location", $approvalUrl);
55+
return $response;
56+
}
57+
58+
$user = new \Pdsinterop\Solid\Auth\Entity\User();
59+
$user->setIdentifier($this->getProfilePage());
60+
61+
$request = $request->withQueryParams($getVars); // replace the request getVars with the morphed version;
62+
$response = new \Laminas\Diactoros\Response();
63+
$server = new \Pdsinterop\Solid\Auth\Server($this->authServerFactory, $this->authServerConfig, $response);
64+
65+
$response = $server->respondToAuthorizationRequest($request, $user, $approval);
66+
$response = $this->tokenGenerator->addIdTokenToResponse($response, $clientId, $this->getProfilePage(), $_SESSION['nonce'], $this->config->getPrivateKey());
67+
return $response;
68+
}
69+
}

src/Controller/CorsController.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Pdsinterop\Solid\Controller;
4+
5+
use Psr\Http\Message\ResponseInterface;
6+
use Psr\Http\Message\ServerRequestInterface;
7+
8+
class CorsController extends ServerController
9+
{
10+
final public function __invoke(ServerRequestInterface $request, array $args): ResponseInterface
11+
{
12+
return $this->getResponse()->withHeader("Access-Control-Allow-Headers", "*");
13+
}
14+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Pdsinterop\Solid\Controller;
4+
5+
use Psr\Http\Message\ResponseInterface;
6+
use Psr\Http\Message\ServerRequestInterface;
7+
8+
class HandleApprovalController extends ServerController
9+
{
10+
public function __invoke(ServerRequestInterface $request, array $args) : ResponseInterface
11+
{
12+
$clientId = $args['clientId'];
13+
$returnUrl = $request->getParsedBody()['returnUrl'];
14+
$approval = $request->getParsedBody()['approval'];
15+
16+
if ($approval == "allow") {
17+
$this->config->addAllowedClient($this->userId, $clientId);
18+
} else {
19+
$this->config->removeAllowedClient($this->userId, $clientId);
20+
}
21+
22+
$response = $this->getResponse();
23+
$response = $response->withHeader("Location", $returnUrl);
24+
$response = $response->withStatus("302", "ok");
25+
return $response;
26+
}
27+
}

src/Controller/JwksController.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Pdsinterop\Solid\Controller;
4+
5+
use Psr\Http\Message\ResponseInterface;
6+
use Psr\Http\Message\ServerRequestInterface;
7+
8+
class JwksController extends ServerController
9+
{
10+
final public function __invoke(ServerRequestInterface $request, array $args): ResponseInterface
11+
{
12+
$response = $this->getResponse();
13+
$server = new \Pdsinterop\Solid\Auth\Server($this->authServerFactory, $this->authServerConfig, $response);
14+
return $server->respondToJwksMetadataRequest();
15+
}
16+
}

src/Controller/LoginController.php

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,37 @@ final public function __invoke(ServerRequestInterface $request, array $args): Re
1111
{
1212
$postBody = $request->getParsedBody();
1313
$response = $this->getResponse();
14+
1415
// var_dump($_SESSION);
1516
if (isset($_SESSION['userid'])) {
1617
$user = $_SESSION['userid'];
18+
if ($request->getQueryParams()['returnUrl']) {
19+
$response = $response->withStatus(302, "Redirecting");
20+
$response = $response->withHeader("Location", $request->getQueryParams()['returnUrl']);
21+
return $response;
22+
}
1723
$response->getBody()->write("<h1>Already logged in as $user</h1>");
18-
} else if ($postBody['user'] == $_ENV['USER'] && $postBody['password'] == $_ENV['PASSWORD']) {
19-
$user = $postBody['user'];
20-
$response->getBody()->write("<h1>Welcome $user</h1>\n");
24+
} else if (
25+
($postBody['username'] == $_ENV['USER'] && $postBody['password'] == $_ENV['PASSWORD']) ||
26+
($postBody['username'] == $_SERVER['USER'] && $postBody['password'] == $_SERVER['PASSWORD'])
27+
) {
28+
$user = $postBody['username'];
2129
$_SESSION['userid'] = $user;
22-
echo("session started\n");
23-
var_dump($_SESSION);
30+
if ($request->getQueryParams()['returnUrl']) {
31+
$response = $response->withStatus(302, "Redirecting");
32+
$response = $response->withHeader("Location", $request->getQueryParams()['returnUrl']);
33+
return $response;
34+
}
35+
$response->getBody()->write("<h1>Welcome $user</h1>\n");
36+
// echo("session started\n");
37+
//var_dump($_SESSION);
2438
} else {
2539
// var_dump($postBody);
26-
echo("cookie:\n");
27-
var_dump($_COOKIE);
28-
echo("session:\n");
29-
var_dump($_SESSION);
30-
$response->getBody()->write("<h1>No (try posting user=alice&password=alice123)</h1>\n");
40+
//echo("cookie:\n");
41+
//var_dump($_COOKIE);
42+
//echo("session:\n");
43+
//var_dump($_SESSION);
44+
$response->getBody()->write("<h1>No (try posting username=alice&password=alice123)</h1>\n");
3145
}
3246
return $response;
3347
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Pdsinterop\Solid\Controller;
4+
5+
use Psr\Http\Message\ResponseInterface;
6+
use Psr\Http\Message\ServerRequestInterface;
7+
8+
class LoginPageController extends ServerController
9+
{
10+
public function __invoke(ServerRequestInterface $request, array $args) : ResponseInterface
11+
{
12+
return $this->createTemplateResponse('login.html');
13+
}
14+
}

0 commit comments

Comments
 (0)