Skip to content

Commit 529675a

Browse files
BourotBenjaminwouterj
authored andcommitted
Check for api_key in request
1 parent 59e4bd4 commit 529675a

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

cookbook/security/api_key_authentication.rst

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,21 @@ value and then a User object is created::
4545

4646
public function createToken(Request $request, $providerKey)
4747
{
48-
if ($request->query->has('apikey')) {
49-
return new PreAuthenticatedToken(
50-
'anon.',
51-
$request->query->get('apikey'),
52-
$providerKey
53-
);
54-
}
55-
else if($request->request->has('apikey'))
56-
{
57-
return new PreAuthenticatedToken(
58-
'anon.',
59-
$request->request->get('apikey'),
60-
$providerKey
61-
);
62-
}
63-
else
64-
{
48+
// look for an apikey query parameter
49+
$apiKey = $request->query->get('apikey');
50+
51+
// or if you want to use an "apikey" header, then do something like this:
52+
// $apiKey = $request->headers->get('apikey');
53+
54+
if (!$apiKey) {
6555
throw new BadCredentialsException('No API key found');
66-
}
56+
}
57+
58+
return new PreAuthenticatedToken(
59+
'anon.',
60+
$apiKey,
61+
$providerKey
62+
);
6763
}
6864

6965
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)

0 commit comments

Comments
 (0)