From 5a9d88e9bb8793d5a5011a5f5cd3b4b0561f5bb2 Mon Sep 17 00:00:00 2001 From: Wilco Louwerse Date: Fri, 26 Jan 2024 14:12:47 +0100 Subject: [PATCH 1/3] Check for an APIKEY_USER when using a api-key auth --- api/src/Security/ApiKeyAuthenticator.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/api/src/Security/ApiKeyAuthenticator.php b/api/src/Security/ApiKeyAuthenticator.php index c7af2224d..74690f81e 100644 --- a/api/src/Security/ApiKeyAuthenticator.php +++ b/api/src/Security/ApiKeyAuthenticator.php @@ -153,6 +153,13 @@ public function authenticate(Request $request): PassportInterface try { $user = $application->getOrganization()->getUsers()[0]; + + $users = array_filter($application->getOrganization()->getUsers(), function (User $user) { + return $user->getName() === 'APIKEY_USER'; + }); + if (empty($users[0]) === false) { + $user = $users[0]; + } } catch (\Exception $exception) { throw new AuthenticationException('An invalid User is configured for this ApiKey'); } From 815bc4d80f89729b3a6c9954745e8a9f82864393 Mon Sep 17 00:00:00 2001 From: Wilco Louwerse Date: Fri, 26 Jan 2024 14:52:45 +0100 Subject: [PATCH 2/3] Create default APIKEY_USER during initialize command --- api/src/Command/InitializationCommand.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/api/src/Command/InitializationCommand.php b/api/src/Command/InitializationCommand.php index 6d4ac71ba..f3245572c 100644 --- a/api/src/Command/InitializationCommand.php +++ b/api/src/Command/InitializationCommand.php @@ -249,7 +249,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int // Handling users $io->section('Looking for an user'); if (!$user = $this->entityManager->getRepository('App:User')->findOneBy([])) { - $io->info('No User found, creating a new one'); + $io->info('No User found, creating a default and APIKEY one'); + $user = new User(); $user->setName('Default User'); $user->setReference('https://docs.commongateway.nl/user/default.user.json'); @@ -261,6 +262,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int $user->setOrganization($organization); $this->entityManager->persist($user); + + $apikeyUser = new User(); + $apikeyUser->setName('APIKEY_USER'); + $apikeyUser->setReference('https://docs.commongateway.nl/user/default.apikey.user.json'); + $apikeyUser->setDescription('Created during auto configuration'); + $apikeyUser->setEmail('apikey@test.com'); + $apikeyUser->setPassword($this->hasher->hashPassword($apikeyUser, '!ChangeMe!')); + $apikeyUser->addSecurityGroup($securityGroupAdmin); + $apikeyUser->addApplication($application); + $apikeyUser->setOrganization($organization); + + $this->entityManager->persist($apikeyUser); } else { $io->info('User found, continuing....'); } From 52fcc6659bc0815a6ebef844c85cfe83112523a6 Mon Sep 17 00:00:00 2001 From: Wilco Louwerse Date: Fri, 26 Jan 2024 15:26:03 +0100 Subject: [PATCH 3/3] User collection->filter instead of array_filter --- api/src/Security/ApiKeyAuthenticator.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/api/src/Security/ApiKeyAuthenticator.php b/api/src/Security/ApiKeyAuthenticator.php index 74690f81e..4bf905852 100644 --- a/api/src/Security/ApiKeyAuthenticator.php +++ b/api/src/Security/ApiKeyAuthenticator.php @@ -152,20 +152,22 @@ public function authenticate(Request $request): PassportInterface } try { - $user = $application->getOrganization()->getUsers()[0]; + $user = $application->getOrganization()->getUsers()->first(); - $users = array_filter($application->getOrganization()->getUsers(), function (User $user) { + $userCollection = $application->getOrganization()->getUsers(); + $users = $userCollection->filter(function (User $user) { return $user->getName() === 'APIKEY_USER'; }); - if (empty($users[0]) === false) { - $user = $users[0]; + + if (count($users) > 0) { + $user = $users->first(); } } catch (\Exception $exception) { - throw new AuthenticationException('An invalid User is configured for this ApiKey'); + throw new AuthenticationException('An invalid User (or no user) is configured for this ApiKey'); } if ($user instanceof User === false) { - throw new AuthenticationException('An invalid User is configured for this ApiKey'); + throw new AuthenticationException('An invalid User (or no user) is configured for this ApiKey'); } // Set apiKey Application id in session