Skip to content

Commit fa42d7d

Browse files
committed
token field in configuration
1 parent 0c5c352 commit fa42d7d

4 files changed

Lines changed: 16 additions & 12 deletions

File tree

DependencyInjection/Configuration.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public function getConfigTreeBuilder()
2626
->isRequired()
2727
->cannotBeEmpty()
2828
->end()
29+
->scalarNode('token_field')
30+
->defaultValue('accessToken')
31+
->cannotBeEmpty()
32+
->end()
2933
->scalarNode('login_field')
3034
->defaultValue('email')
3135
->cannotBeEmpty()
@@ -39,7 +43,6 @@ public function getConfigTreeBuilder()
3943
->canBeUnset()
4044
->children()
4145
->integerNode('invalid_token')->cannotBeEmpty()->defaultValue(401)->end()
42-
4346
->end()
4447
->end()
4548
->end();

Resources/config/services.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ services:
2323
class: Youshido\TokenAuthenticationBundle\Service\Listener\ExceptionListener
2424
tags:
2525
- { name: kernel.event_listener, event: kernel.exception }
26+
calls:
27+
- [ setContainer, [@service_container]]

Service/Listener/ExceptionListener.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class ExceptionListener
1515

1616
public function onKernelException(GetResponseForExceptionEvent $event)
1717
{
18+
1819
$response = new JsonResponse([
1920
'errors' => [
2021
[

Service/TokenAuthenticator.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ public function authenticateToken(TokenInterface $token, UserProviderInterface $
3737
);
3838
}
3939

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

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

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

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

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

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

80-
if ($apiKey) {
81-
return new PreAuthenticatedToken('anon.', $apiKey, $providerKey);
78+
if ($tokenString) {
79+
return new PreAuthenticatedToken('anon.', $tokenString, $providerKey);
8280
}
8381

8482
return null;

0 commit comments

Comments
 (0)