-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #595 from systemli/feat/keycloak_api
feat: Add keycloak API endpoints
- Loading branch information
Showing
12 changed files
with
312 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
namespace App\Controller; | ||
|
||
use App\Dto\KeycloakUserValidateDto; | ||
use App\Entity\Domain; | ||
use App\Entity\User; | ||
use App\Handler\UserAuthenticationHandler; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Symfony\Bridge\Doctrine\Attribute\MapEntity; | ||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Attribute\MapQueryParameter; | ||
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
|
||
class KeycloakController extends AbstractController | ||
{ | ||
public function __construct(private readonly EntityManagerInterface $manager, private readonly UserAuthenticationHandler $handler) | ||
{} | ||
|
||
#[Route(path: '/api/keycloak/{domainUrl}', name: 'api_keycloak_index', methods: ['GET'], stateless: true)] | ||
public function getUsersSearch( | ||
#[MapEntity(mapping: ['domainUrl' => 'name'])] Domain $domain, | ||
#[MapQueryParameter] string $search = '', | ||
#[MapQueryParameter] int $max = 10, | ||
#[MapQueryParameter] int $first = 0, | ||
): Response | ||
{ | ||
$users = $this->manager->getRepository(User::class)->findUsersByString($domain, $search, $max, $first)->map(function (User $user) { | ||
return [ | ||
'id' => explode('@', $user->getEmail())[0], | ||
'email' => $user->getEmail(), | ||
]; | ||
}); | ||
return $this->json($users); | ||
} | ||
|
||
#[Route(path: '/api/keycloak/{domainUrl}/count', name: 'api_keycloak_count', methods: ['GET'], stateless: true)] | ||
public function getUsersCount(#[MapEntity(mapping: ['domainUrl' => 'name'])] Domain $domain): Response | ||
{ | ||
return $this->json($this->manager->getRepository(User::class)->countDomainUsers($domain)); | ||
} | ||
|
||
#[Route(path: '/api/keycloak/{domainUrl}/user/{email}', name: 'api_keycloak_user', methods: ['GET'], stateless: true)] | ||
public function getOneUser( | ||
#[MapEntity(mapping: ['domainUrl' => 'name'])] Domain $domain, | ||
string $email, | ||
): Response | ||
{ | ||
if (!str_contains($email, '@')) { | ||
$email .= '@' . $domain->getName(); | ||
} | ||
|
||
if (null === $foundUser = $this->manager->getRepository(User::class)->findByDomainAndEmail($domain, $email)) { | ||
return $this->json([ | ||
'message' => 'user not found', | ||
], Response::HTTP_NOT_FOUND); | ||
} | ||
|
||
return $this->json([ | ||
'id' => explode('@', $foundUser->getEmail())[0], | ||
'email' => $foundUser->getEmail(), | ||
]); | ||
} | ||
|
||
#[Route(path: '/api/keycloak/{domainUrl}/validate/{email}', name: 'api_keycloak_user_validate', methods: ['POST'], stateless: true)] | ||
public function postUserValidate( | ||
#[MapEntity(mapping: ['domainUrl' => 'name'])] Domain $domain, | ||
#[MapRequestPayload] KeycloakUserValidateDto $requestData, | ||
string $email, | ||
): Response | ||
{ | ||
if (null === $user = $this->manager->getRepository(User::class)->findByDomainAndEmail($domain, $email)) { | ||
return $this->json([ | ||
'message' => 'authentication failed', | ||
], Response::HTTP_FORBIDDEN); | ||
} | ||
|
||
if ($this->handler->authenticate($user, $requestData->getPassword()) === null) { | ||
return $this->json([ | ||
'message' => 'authentication failed', | ||
], Response::HTTP_FORBIDDEN); | ||
} | ||
|
||
return $this->json([ | ||
'message' => 'success', | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace App\Dto; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class KeycloakUserValidateDto { | ||
#[Assert\NotBlank] | ||
private string $password = ''; | ||
|
||
public function getPassword(): string { | ||
return $this->password; | ||
} | ||
|
||
public function setPassword(string $password): void { | ||
$this->password = $password; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace App\Security; | ||
|
||
use Symfony\Component\Security\Core\Exception\BadCredentialsException; | ||
use Symfony\Component\Security\Http\AccessToken\AccessTokenHandlerInterface; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; | ||
|
||
class KeycloakAccessTokenHandler implements AccessTokenHandlerInterface | ||
{ | ||
public function __construct(private string $keycloakApiAccessToken) | ||
{ | ||
} | ||
|
||
public function getUserBadgeFrom(#[\SensitiveParameter] string $accessToken): UserBadge { | ||
if ($accessToken !== $this->keycloakApiAccessToken) { | ||
throw new BadCredentialsException('Invalid access token'); | ||
} | ||
|
||
return new UserBadge('keycloak'); | ||
} | ||
} |
Oops, something went wrong.