Skip to content

Commit e3a16eb

Browse files
committed
fixes + refactor
1 parent 6ddca86 commit e3a16eb

2 files changed

Lines changed: 21 additions & 13 deletions

File tree

Service/Helper/AccessTokenHelper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
namespace Youshido\TokenAuthenticationBundle\Service\Helper;
99

1010

11-
use Symfony\Component\DependencyInjection\ContainerAware;
11+
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
1212
use Youshido\DoctrineExtensionBundle\Traits\Service\ServiceHelperTrait;
1313
use Youshido\TokenAuthenticationBundle\Entity\AccessToken;
1414

15-
class AccessTokenHelper extends ContainerAware
15+
class AccessTokenHelper
1616
{
17-
use ServiceHelperTrait;
17+
use ServiceHelperTrait, ContainerAwareTrait;
1818

1919
/** @var int */
2020
protected $tokenLifetime;
@@ -26,7 +26,7 @@ class AccessTokenHelper extends ContainerAware
2626
*/
2727
public function checkExpires(AccessToken $token)
2828
{
29-
if (time() < $token->getCreatedAt()->getTimestamp() + $this->tokenLifetime) {
29+
if (time() > ($token->getCreatedAt()->getTimestamp() + $this->tokenLifetime)) {
3030
$token->setStatus(AccessToken::STATUS_EXPIRED);
3131

3232
$this->persist($token);

Service/TokenAuthenticator.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@
77

88
namespace Youshido\TokenAuthenticationBundle\Service;
99

10-
11-
use Symfony\Component\DependencyInjection\ContainerAware;
1210
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
1311
use Symfony\Component\HttpFoundation\Request;
1412
use Symfony\Component\HttpFoundation\Response;
15-
use Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface;
1613
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
1714
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1815
use Symfony\Component\Security\Core\Exception\AuthenticationException;
1916
use Symfony\Component\Security\Core\User\UserProviderInterface;
2017
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
18+
use Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface;
2119
use Youshido\TokenAuthenticationBundle\Entity\AccessToken;
2220
use Youshido\TokenAuthenticationBundle\Service\Exception\NotValidTokenException;
2321

@@ -37,22 +35,32 @@ public function authenticateToken(TokenInterface $token, UserProviderInterface $
3735
);
3836
}
3937

40-
$errorCode = $this->container->getParameter('token_authentication.error_codes')['invalid_token'];
4138
$tokenString = $token->getCredentials();
42-
$token = $userProvider->findTokenByApiKey($tokenString);
39+
$user = $this->validateTokenAndGetUser($userProvider, $tokenString);
40+
41+
return new PreAuthenticatedToken($user, $tokenString, $providerKey, $user->getRoles());
42+
}
43+
44+
public function validateTokenAndGetUser(TokenUserProvider $userProvider, $tokenString)
45+
{
46+
$token = $userProvider->findTokenByApiKey($tokenString);
47+
48+
$errorCode = $this->container->getParameter('token_authentication.error_codes')['invalid_token'];
4349

4450
if (!$token) {
4551
throw new NotValidTokenException(sprintf('API Key "%s" does not exist.', $tokenString), $errorCode);
4652
}
4753

48-
if ($token->getStatus() == AccessToken::STATUS_DENIED) {
54+
if ($token->getStatus() != AccessToken::STATUS_VALID) {
4955
throw new NotValidTokenException('Access denied for this token.', $errorCode);
5056
}
5157

52-
if ($this->container->get('access_token_helper')->checkExpires($token)) {
53-
$em = $this->container->get('doctrine')->getEntityManager();
58+
if (!$this->container->get('access_token_helper')->checkExpires($token)) {
59+
$em = $this->container->get('doctrine')->getManager();
60+
5461
$em->remove($token);
5562
$em->flush();
63+
5664
throw new NotValidTokenException('Token expired. Please login again.', $errorCode);
5765
}
5866

@@ -62,7 +70,7 @@ public function authenticateToken(TokenInterface $token, UserProviderInterface $
6270
throw new NotValidTokenException('User of this token not exist', $errorCode);
6371
}
6472

65-
return new PreAuthenticatedToken($user, $tokenString, $providerKey, $user->getRoles());
73+
return $user;
6674
}
6775

6876
public function supportsToken(TokenInterface $token, $providerKey)

0 commit comments

Comments
 (0)