Skip to content

Commit

Permalink
Merge pull request #1613 from ConductionNL/feature/PC108-34/api-key-user
Browse files Browse the repository at this point in the history
Check for an APIKEY_USER when using a api-key auth
  • Loading branch information
WilcoLouwerse authored Jan 26, 2024
2 parents 27d58c1 + 52fcc66 commit af782d3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
15 changes: 14 additions & 1 deletion api/src/Command/InitializationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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('[email protected]');
$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....');
}
Expand Down
15 changes: 12 additions & 3 deletions api/src/Security/ApiKeyAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,22 @@ public function authenticate(Request $request): PassportInterface
}

try {
$user = $application->getOrganization()->getUsers()[0];
$user = $application->getOrganization()->getUsers()->first();

$userCollection = $application->getOrganization()->getUsers();
$users = $userCollection->filter(function (User $user) {
return $user->getName() === 'APIKEY_USER';
});

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
Expand Down

0 comments on commit af782d3

Please sign in to comment.