Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

III-6220 Check issuer for JWT when working with Keycloak #298

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Http/Authentication/AuthenticateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use ICultureFeed;
use Lcobucci\JWT\Token\InvalidTokenStructure;
use League\Container\Container;
use Noodlehaus\Config;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
Expand Down Expand Up @@ -142,7 +143,10 @@ private function handleAccessToken(
if (!$token->validate($this->pemFile)) {
return (new InvalidToken('Token "' . $tokenString . '" is expired or not valid for Search API.'))->toResponse();
}
if (!$token->isAllowedOnSearchApi()) {

$config = $this->container->get(Config::class);
$jwtUrl = $config->get('keycloack.enabled') ? $config->get('jwt.domain') : null;
if (!$token->isAllowedOnSearchApi($jwtUrl)) {
return (new NotAllowedToUseSapi())->toResponse();
}

Expand Down
11 changes: 7 additions & 4 deletions src/Http/Authentication/JsonWebToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public function validate(string $publicKey, ?string $keyPassphrase = null): bool
);
}

public function isAllowedOnSearchApi(): bool
public function isAllowedOnSearchApi(?string $jwtProviderDomain): bool
{
$allowedApis = $this->token->claims()->get('https://publiq.be/publiq-apis', '');
return $this->hasSapiAccess($allowedApis) && !$this->isV2JwtProviderToken();
return $this->hasSapiAccess($allowedApis) && !$this->isV2JwtProviderToken($jwtProviderDomain);
}

private function hasSapiAccess(string $allowedApis): bool
Expand All @@ -58,9 +58,12 @@ private function hasSapiAccess(string $allowedApis): bool
return in_array('sapi', $apis, true);
}

private function isV2JwtProviderToken(): bool
private function isV2JwtProviderToken(?string $jwtProviderDomain): bool
{
// TODO: Find solution for email being present on Keycloak tokens
if ($jwtProviderDomain) {
return $this->token->claims()->get('iss') === $jwtProviderDomain;
}

return $this->token->claims()->has('nickname') || $this->token->claims()->has('email');
}
}
5 changes: 5 additions & 0 deletions tests/Http/Authentication/AuthenticateRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use ICultureFeed;
use League\Container\Container;
use League\Container\Definition\DefinitionInterface;
use Noodlehaus\Config;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -59,6 +60,10 @@ final class AuthenticateRequestTest extends TestCase
protected function setUp(): void
{
$this->container = $this->createMock(Container::class);
$this->container
->method('get')
->willReturn(new Config([]));

$this->cultureFeed = $this->createMock(ICultureFeed::class);

$this->pemFile = file_get_contents(__DIR__ . '/samples/public.pem');
Expand Down
Loading