77
88namespace Youshido \TokenAuthenticationBundle \Service ;
99
10-
11- use Symfony \Component \DependencyInjection \ContainerAware ;
1210use Symfony \Component \DependencyInjection \ContainerAwareTrait ;
1311use Symfony \Component \HttpFoundation \Request ;
1412use Symfony \Component \HttpFoundation \Response ;
15- use Symfony \Component \Security \Core \Authentication \SimplePreAuthenticatorInterface ;
1613use Symfony \Component \Security \Core \Authentication \Token \PreAuthenticatedToken ;
1714use Symfony \Component \Security \Core \Authentication \Token \TokenInterface ;
1815use Symfony \Component \Security \Core \Exception \AuthenticationException ;
1916use Symfony \Component \Security \Core \User \UserProviderInterface ;
2017use Symfony \Component \Security \Http \Authentication \AuthenticationFailureHandlerInterface ;
18+ use Symfony \Component \Security \Http \Authentication \SimplePreAuthenticatorInterface ;
2119use Youshido \TokenAuthenticationBundle \Entity \AccessToken ;
2220use 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