Skip to content

Commit 300d6cf

Browse files
author
Elliot Smith
committed
Replace tuupola/slim-jwt-auth with our fork
slim-jwt-auth currently does not support firebase/php-jwt 6. This raise critical security alerts when our containers are scanned (namely CVE-2021-46743). There is an issue on the slim-jwt-auth repo for this, which I have commented on, asking for further info: tuupola/slim-jwt-auth#217 In the meantime, I have taken a copy of the key part of that package and reworked it slightly so that it functions with php-jwt v6. I also removed a lot of the options we're not using. The existing middleware which functions with slim-jwt-auth is commented out but still present. To reinstate that package, we just need to remove this commit.
1 parent dd9cd48 commit 300d6cf

File tree

8 files changed

+449
-118
lines changed

8 files changed

+449
-118
lines changed

service-admin/composer.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@
2424
"ministryofjustice/opg-lpa-datamodels": "^13.3",
2525
"php-http/guzzle6-adapter": "^2.0.2",
2626
"slim/flash": "^0.4.0",
27-
"tuupola/slim-jwt-auth": "^3.5.2",
27+
28+
"firebase/php-jwt": "^6.0",
29+
"psr/http-message": "^1.0",
30+
"tuupola/http-factory": "^0.4.0|^1.0.2",
31+
"tuupola/callable-handler": "^0.3.0|^0.4.0|^1.0",
32+
"psr/http-server-middleware": "^1.0",
33+
2834
"laminas/laminas-authentication": "^2.8",
2935
"laminas/laminas-cache": "^3.1.2",
3036
"laminas/laminas-cache-storage-adapter-memory": "2.0",

service-admin/composer.lock

+31-101
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

service-admin/config/autoload/mezzio.global.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@
2525

2626
'api_base_uri' => getenv('OPG_LPA_ENDPOINTS_API') ?: null,
2727

28-
'admin_accounts' => (getenv('OPG_LPA_COMMON_ADMIN_ACCOUNTS') ? explode(',', getenv('OPG_LPA_COMMON_ADMIN_ACCOUNTS')) : []),
28+
'admin_accounts' => (
29+
getenv('OPG_LPA_COMMON_ADMIN_ACCOUNTS') ?
30+
explode(',', getenv('OPG_LPA_COMMON_ADMIN_ACCOUNTS')) : []
31+
),
2932

3033
'jwt' => [
3134
'secret' => getenv('OPG_LPA_ADMIN_JWT_SECRET') ?: null,
3235
'path' => '/',
3336
'header' => 'lpa-admin',
3437
'cookie' => 'lpa-admin',
35-
'ttl' => 60 * 15, // 15 minutes
38+
'ttl' => 60 * 15, // 15 minutes
3639
'algo' => 'HS256',
3740
],
3841

service-admin/config/pipeline.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?php
22

3+
/**
4+
* Setup middleware pipeline:
5+
*/
6+
37
declare(strict_types=1);
48

59
use App\Middleware;
@@ -17,10 +21,7 @@
1721
use Mezzio\Router\Middleware\RouteMiddleware;
1822
use Laminas\Stratigility\Middleware\ErrorHandler;
1923

20-
/**
21-
* Setup middleware pipeline:
22-
*/
23-
return function (Application $app, MiddlewareFactory $factory, ContainerInterface $container) : void {
24+
return function (Application $app, MiddlewareFactory $factory, ContainerInterface $container): void {
2425
// The error handler should be the first (most outer) middleware to catch
2526
// all Exceptions.
2627
$app->pipe(ErrorHandler::class);
@@ -60,7 +61,8 @@
6061

6162
// Set up the custom middleware to handle sessions and authorization
6263
$app->pipe(Middleware\Session\SessionMiddleware::class);
63-
$app->pipe(JwtAuthentication::class);
64+
//$app->pipe(JwtAuthentication::class);
65+
$app->pipe(Middleware\Session\JwtMiddleware::class);
6466
$app->pipe(Middleware\Authorization\AuthorizationMiddleware::class);
6567
$app->pipe(Middleware\Session\CsrfMiddleware::class);
6668
$app->pipe(Middleware\Flash\SlimFlashMiddleware::class);

service-admin/src/App/src/ConfigProvider.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace App;
66

77
use App\Logging\LoggingErrorListenerDelegatorFactory;
8-
use Tuupola\Middleware\JwtAuthentication;
8+
//use Tuupola\Middleware\JwtAuthentication;
99
use Laminas\Stratigility\Middleware\ErrorHandler;
1010

1111
/**
@@ -64,8 +64,10 @@ public function getDependencies(): array
6464
Handler\UserFindHandler::class => Handler\UserFindHandlerFactory::class,
6565

6666
// Middleware
67-
JwtAuthentication::class =>
68-
Middleware\Session\JwtAuthenticationFactory::class,
67+
//JwtAuthentication::class =>
68+
// Middleware\Session\JwtAuthenticationFactory::class,
69+
Middleware\Session\JwtMiddleware::class =>
70+
Middleware\Session\JwtMiddlewareFactory::class,
6971
Middleware\Authorization\AuthorizationMiddleware::class =>
7072
Middleware\Authorization\AuthorizationMiddlewareFactory::class,
7173
Middleware\Session\SessionMiddleware::class =>

service-admin/src/App/src/Middleware/Authorization/AuthorizationMiddleware.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -90,23 +90,23 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
9090
$roles = ['guest'];
9191

9292
if (is_string($token)) {
93-
// Attempt to get a user with the token value
93+
// Attempt to get a user with the token value
9494
$result = $this->authenticationService->verify($token);
9595

9696
$identity = $result->getIdentity();
9797

9898
if ($identity instanceof Identity) {
99-
// Try to get the user details
99+
// Try to get the user details
100100
$user = $this->userService->fetch($identity->getUserId() ?? '');
101101

102-
// There is something wrong with the user here so throw an exception
102+
// There is something wrong with the user here so throw an exception
103103
if (!$user instanceof User) {
104104
throw new Exception('Can not find a user for ID ' . $identity->getUserId());
105105
}
106106

107107
$roles[] = 'authenticated-user';
108108
} else {
109-
// Clear the bad token
109+
// Clear the bad token
110110
$this->clearTokenData();
111111
}
112112
}
@@ -123,7 +123,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
123123
// Check each role to see if the user has access to the route
124124
foreach ($roles as $role) {
125125
if ($this->rbac->hasRole($role) && $this->rbac->isGranted($role, $matchedRoute->getName())) {
126-
// Catch any unauthorized exceptions and trigger a sign out if required
126+
// Catch any unauthorized exceptions and trigger a sign out if required
127127
try {
128128
return $handler->handle($request->withAttribute('user', $user));
129129
} catch (ApiException $ae) {
@@ -136,7 +136,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
136136
}
137137
}
138138

139-
// If there is no user (not logged in) then redirect to the sign in screen
139+
// If there is no user (not logged in) then redirect to the sign in screen
140140
if (is_null($user)) {
141141
return new RedirectResponse($this->urlHelper->generate('sign.in'));
142142
}

0 commit comments

Comments
 (0)