feat: password grant
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\EventListener;
|
||||
|
||||
use App\Security\Crypt;
|
||||
use League\Bundle\OAuth2ServerBundle\Event\UserResolveEvent;
|
||||
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
|
||||
use Symfony\Component\Security\Core\Exception\AuthenticationException;
|
||||
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
|
||||
use Symfony\Component\Security\Core\User\UserProviderInterface;
|
||||
|
||||
#[AsEventListener(event: 'league.oauth2_server.event.user_resolve', method: 'onUserResolve')]
|
||||
final class UserResolverListener
|
||||
{
|
||||
public function __construct(
|
||||
private readonly UserProviderInterface $userProvider,
|
||||
private readonly Crypt $crypt,
|
||||
) {
|
||||
}
|
||||
|
||||
public function onUserResolve(UserResolveEvent $event): void
|
||||
{
|
||||
try {
|
||||
$user = $this->userProvider->loadUserByIdentifier($event->getUsername());
|
||||
} catch (AuthenticationException $e) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$user instanceof PasswordAuthenticatedUserInterface) {
|
||||
return;
|
||||
}
|
||||
|
||||
$passwordHash = $this->crypt->decrypt($user->getPassword());
|
||||
|
||||
if ($passwordHash !== md5($event->getPassword())) {
|
||||
return;
|
||||
}
|
||||
|
||||
$event->setUser($user);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user