<?php
/*
* Este arquivo é parte do package Login Bundle.
*
* (c) Laino Santos <lainosantos@recife.pe.gov.br>
*
* Para informações completas sobre copyright e licença, por favor ver o
* arquivo LICENCE distribuído juntamente com este código.
*/
namespace Emprel\Login\Bundle\Security\Voter;
use Emprel\OpenIdUmaClient\Connector;
use Emprel\Login\Bundle\Security\User\LoginUser;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
class OtpVoter implements VoterInterface
{
public function vote(TokenInterface $token, $subject, array $attributes)
{
$user = $token->getUser();
if (!$user instanceof LoginUser || !$subject instanceof OtpVoterSubject) {
return self::ACCESS_ABSTAIN;
}
foreach ($attributes as $attribute) {
$otpIsValid = $subject->getUmaClient()->callRelativeEndpoint(
"user/{$user->getUsername()}/otp/valid",
$user->getTokenWrapper()->getAccessToken(),
Connector::CONTENT_TYPE_APPLICATION_JSON,
Connector::VERB_POST,
[
'otp' => $attribute,
]
);
if (!$otpIsValid) {
return self::ACCESS_DENIED;
}
}
return self::ACCESS_GRANTED;
}
}