- Added support for authenticating via access tokens

- update symfony to 6.4.*
- some other minor stuff
This commit is contained in:
2024-02-12 23:37:24 -07:00
parent 4a1c6e1a72
commit 7c45f64a73
21 changed files with 5022 additions and 8217 deletions
+36
View File
@@ -0,0 +1,36 @@
<?php
namespace App\Security;
use App\Entity\UserAccessToken;
use App\Repository\UserAccessTokenRepository;
use App\Repository\UserRepository;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use Symfony\Component\Security\Core\Exception\CredentialsExpiredException;
use Symfony\Component\Security\Http\AccessToken\AccessTokenHandlerInterface;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
class AccessTokenHandler implements AccessTokenHandlerInterface
{
public function __construct(
private UserAccessTokenRepository $repository
) {
}
public function getUserBadgeFrom(string $accessToken): UserBadge
{
/** @var $token UserAccessToken|null */
if (!$accessToken || !$token = $this->repository->findOneBy(['token' => $accessToken])) {
throw new BadCredentialsException('Invalid credentials.');
}
if($token->getDeletedAt() || ( $token->getExpiresAt() && $token->getExpiresAt() <= new \DateTime() ) ) {
throw new CredentialsExpiredException('Token expired.');
}
// and return a UserBadge object containing the user identifier from the found token
return new UserBadge($token->getOwner()->getId(), function() use($token){
return $token->getOwner();
});
}
}