Skip to content

Commit

Permalink
token field in configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
viniychuk committed Feb 5, 2016
1 parent 0c5c352 commit fa42d7d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
5 changes: 4 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public function getConfigTreeBuilder()
->isRequired()
->cannotBeEmpty()
->end()
->scalarNode('token_field')
->defaultValue('accessToken')
->cannotBeEmpty()
->end()
->scalarNode('login_field')
->defaultValue('email')
->cannotBeEmpty()
Expand All @@ -39,7 +43,6 @@ public function getConfigTreeBuilder()
->canBeUnset()
->children()
->integerNode('invalid_token')->cannotBeEmpty()->defaultValue(401)->end()

->end()
->end()
->end();
Expand Down
2 changes: 2 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ services:
class: Youshido\TokenAuthenticationBundle\Service\Listener\ExceptionListener
tags:
- { name: kernel.event_listener, event: kernel.exception }
calls:
- [ setContainer, [@service_container]]
1 change: 1 addition & 0 deletions Service/Listener/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ExceptionListener

public function onKernelException(GetResponseForExceptionEvent $event)
{

$response = new JsonResponse([
'errors' => [
[
Expand Down
20 changes: 9 additions & 11 deletions Service/TokenAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public function authenticateToken(TokenInterface $token, UserProviderInterface $
);
}

$errorCode = $this->container->getParameter('token_authenticator.error_codes')['invalid_token'];
$apiKey = $token->getCredentials();
$token = $userProvider->findTokenByApiKey($apiKey);
$errorCode = $this->container->getParameter('token_authenticator.error_codes')['invalid_token'];
$tokenString = $token->getCredentials();
$token = $userProvider->findTokenByApiKey($tokenString);

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

if ($token->getStatus() == AccessToken::STATUS_DENIED) {
Expand All @@ -62,7 +62,7 @@ public function authenticateToken(TokenInterface $token, UserProviderInterface $
throw new NotValidTokenException('User of this token not exist', $errorCode);
}

return new PreAuthenticatedToken($user, $apiKey, $providerKey, $user->getRoles());
return new PreAuthenticatedToken($user, $tokenString, $providerKey, $user->getRoles());
}

public function supportsToken(TokenInterface $token, $providerKey)
Expand All @@ -72,13 +72,11 @@ public function supportsToken(TokenInterface $token, $providerKey)

public function createToken(Request $request, $providerKey)
{
$apiKey = $request->headers->get('apikey');
if (!$apiKey) {
$apiKey = $request->headers->get('accesstoken');
}
$tokenField = $this->container->getParameter('token_authenticator.token_field');
$tokenString = $request->headers->get($tokenField);

if ($apiKey) {
return new PreAuthenticatedToken('anon.', $apiKey, $providerKey);
if ($tokenString) {
return new PreAuthenticatedToken('anon.', $tokenString, $providerKey);
}

return null;
Expand Down

0 comments on commit fa42d7d

Please sign in to comment.