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

Check for an APIKEY_USER when using a api-key auth #1613

Merged
merged 3 commits into from
Jan 26, 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
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
Loading