Skip to content

Commit 2d2a16f

Browse files
committed
[Security] Added return types
1 parent 586d1b0 commit 2d2a16f

15 files changed

+71
-66
lines changed

security.rst

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ You can deny access from inside a controller::
644644
// src/Controller/AdminController.php
645645
// ...
646646

647-
public function adminDashboard()
647+
public function adminDashboard(): Response
648648
{
649649
$this->denyAccessUnlessGranted('ROLE_ADMIN');
650650

@@ -688,7 +688,7 @@ using annotations:
688688
+ *
689689
+ * @IsGranted("ROLE_ADMIN")
690690
+ */
691-
public function adminDashboard()
691+
public function adminDashboard(): Response
692692
{
693693
// ...
694694
}
@@ -735,7 +735,7 @@ role::
735735

736736
// ...
737737

738-
public function adminDashboard()
738+
public function adminDashboard(): Response
739739
{
740740
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
741741

@@ -770,7 +770,7 @@ like this:
770770
After authentication, the ``User`` object of the current user can be accessed
771771
via the ``getUser()`` shortcut::
772772

773-
public function index()
773+
public function index(): Response
774774
{
775775
// usually you'll want to make sure the user is authenticated first
776776
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
@@ -811,6 +811,8 @@ If you need to get the logged in user from a service, use the
811811
{
812812
// returns User object or null if not authenticated
813813
$user = $this->security->getUser();
814+
815+
// ...
814816
}
815817
}
816818

@@ -901,7 +903,7 @@ Next, you'll need to create a route for this URL (but not a controller):
901903
/**
902904
* @Route("/logout", name="app_logout", methods={"GET"})
903905
*/
904-
public function logout()
906+
public function logout(): void
905907
{
906908
// controller can be blank: it will never be executed!
907909
throw new \Exception('Don\'t forget to activate logout in security.yaml');

security/access_denied_handler.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ response)::
123123

124124
class AccessDeniedHandler implements AccessDeniedHandlerInterface
125125
{
126-
public function handle(Request $request, AccessDeniedException $accessDeniedException)
126+
public function handle(Request $request, AccessDeniedException $accessDeniedException): ?Response
127127
{
128128
// ...
129129

security/csrf.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ this can be customized on a form-by-form basis::
8585

8686
// src/Form/TaskType.php
8787
namespace App\Form;
88-
88+
8989
// ...
9090
use App\Entity\Task;
9191
use Symfony\Component\OptionsResolver\OptionsResolver;
@@ -94,7 +94,7 @@ this can be customized on a form-by-form basis::
9494
{
9595
// ...
9696

97-
public function configureOptions(OptionsResolver $resolver)
97+
public function configureOptions(OptionsResolver $resolver): void
9898
{
9999
$resolver->setDefaults([
100100
'data_class' => Task::class,
@@ -153,10 +153,11 @@ Then, get the value of the CSRF token in the controller action and use the
153153
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController::isCsrfTokenValid`
154154
to check its validity::
155155

156+
use Symfony\Component\HttpFoundation\Response;
156157
use Symfony\Component\HttpFoundation\Request;
157158
// ...
158159

159-
public function delete(Request $request)
160+
public function delete(Request $request): Response
160161
{
161162
$submittedToken = $request->request->get('token');
162163

security/custom_authentication_provider.rst

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ provider::
7878
$this->setAuthenticated(count($roles) > 0);
7979
}
8080

81-
public function getCredentials()
81+
public function getCredentials(): string
8282
{
8383
return '';
8484
}
@@ -123,7 +123,7 @@ set an authenticated token in the token storage if successful::
123123
$this->authenticationManager = $authenticationManager;
124124
}
125125

126-
public function __invoke(RequestEvent $event)
126+
public function __invoke(RequestEvent $event): void
127127
{
128128
$request = $event->getRequest();
129129

@@ -216,7 +216,7 @@ the ``PasswordDigest`` header value matches with the user's password::
216216
$this->cachePool = $cachePool;
217217
}
218218

219-
public function authenticate(TokenInterface $token)
219+
public function authenticate(TokenInterface $token): WsseUserToken
220220
{
221221
$user = $this->userProvider->loadUserByUsername($token->getUsername());
222222

@@ -236,7 +236,7 @@ the ``PasswordDigest`` header value matches with the user's password::
236236
* For more information specific to the logic here, see
237237
* https://github.com/symfony/symfony-docs/pull/3134#issuecomment-27699129
238238
*/
239-
protected function validateDigest($digest, $nonce, $created, $secret)
239+
protected function validateDigest($digest, $nonce, $created, $secret): bool
240240
{
241241
// Check created time is not in the future
242242
if (strtotime($created) > time()) {
@@ -269,7 +269,7 @@ the ``PasswordDigest`` header value matches with the user's password::
269269
return hash_equals($expected, $digest);
270270
}
271271

272-
public function supports(TokenInterface $token)
272+
public function supports(TokenInterface $token): bool
273273
{
274274
return $token instanceof WsseUserToken;
275275
}
@@ -307,7 +307,7 @@ create a class which implements
307307

308308
class WsseFactory implements SecurityFactoryInterface
309309
{
310-
public function create(ContainerBuilder $container, $id, $config, $userProvider, $defaultEntryPoint)
310+
public function create(ContainerBuilder $container, $id, $config, $userProvider, $defaultEntryPoint): array
311311
{
312312
$providerId = 'security.authentication.provider.wsse.'.$id;
313313
$container
@@ -321,17 +321,17 @@ create a class which implements
321321
return [$providerId, $listenerId, $defaultEntryPoint];
322322
}
323323

324-
public function getPosition()
324+
public function getPosition(): string
325325
{
326326
return 'pre_auth';
327327
}
328328

329-
public function getKey()
329+
public function getKey(): string
330330
{
331331
return 'wsse';
332332
}
333333

334-
public function addConfiguration(NodeDefinition $node)
334+
public function addConfiguration(NodeDefinition $node): void
335335
{
336336
}
337337
}
@@ -455,7 +455,7 @@ factory in the kernel::
455455

456456
class Kernel extends BaseKernel
457457
{
458-
public function build(ContainerBuilder $container)
458+
public function build(ContainerBuilder $container): void
459459
{
460460
$extension = $container->getExtension('security');
461461
$extension->addSecurityListenerFactory(new WsseFactory());
@@ -547,7 +547,7 @@ the ``addConfiguration()`` method::
547547
{
548548
// ...
549549

550-
public function addConfiguration(NodeDefinition $node)
550+
public function addConfiguration(NodeDefinition $node): void
551551
{
552552
$node
553553
->children()
@@ -563,12 +563,12 @@ in order to put it to use::
563563

564564
// src/DependencyInjection/Security/Factory/WsseFactory.php
565565
namespace App\DependencyInjection\Security\Factory;
566-
566+
567567
use App\Security\Authentication\Provider\WsseProvider;
568568

569569
class WsseFactory implements SecurityFactoryInterface
570570
{
571-
public function create(ContainerBuilder $container, $id, $config, $userProvider, $defaultEntryPoint)
571+
public function create(ContainerBuilder $container, $id, $config, $userProvider, $defaultEntryPoint): array
572572
{
573573
$providerId = 'security.authentication.provider.wsse.'.$id;
574574
$container

security/expressions.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ accepts an :class:`Symfony\\Component\\ExpressionLanguage\\Expression` object::
1515
use Symfony\Component\ExpressionLanguage\Expression;
1616
// ...
1717

18-
public function index()
18+
public function index(): Response
1919
{
2020
$this->denyAccessUnlessGranted(new Expression(
2121
'"ROLE_ADMIN" in roles or (not is_anonymous() and user.isSuperAdmin())'
@@ -80,7 +80,7 @@ Additionally, you have access to a number of functions inside the expression:
8080
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
8181
// ...
8282

83-
public function index(AuthorizationCheckerInterface $authorizationChecker)
83+
public function index(AuthorizationCheckerInterface $authorizationChecker): Response
8484
{
8585
$access1 = $authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED');
8686

security/form_login.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ configuration (``login``):
103103
/**
104104
* @Route("/login", name="login", methods={"GET", "POST"})
105105
*/
106-
public function login()
106+
public function login(): Response
107107
{
108108
}
109109
}
@@ -146,7 +146,7 @@ Great! Next, add the logic to ``login()`` that displays the login form::
146146
// src/Controller/SecurityController.php
147147
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
148148

149-
public function login(AuthenticationUtils $authenticationUtils)
149+
public function login(AuthenticationUtils $authenticationUtils): Response
150150
{
151151
// get the login error if there is one
152152
$error = $authenticationUtils->getLastAuthenticationError();

security/form_login_setup.rst

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class that processes the login submit and 4) updates the main security config fi
8080
/**
8181
* @Route("/logout", name="app_logout")
8282
*/
83-
public function logout()
83+
public function logout(): void
8484
{
8585
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
8686
}
@@ -196,6 +196,7 @@ a traditional HTML form that submits to ``/login``:
196196
use App\Entity\User;
197197
use Doctrine\ORM\EntityManagerInterface;
198198
use Symfony\Component\HttpFoundation\RedirectResponse;
199+
use Symfony\Component\HttpFoundation\Response;
199200
use Symfony\Component\HttpFoundation\Request;
200201
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
201202
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
@@ -230,7 +231,7 @@ a traditional HTML form that submits to ``/login``:
230231
$this->passwordEncoder = $passwordEncoder;
231232
}
232233

233-
public function supports(Request $request)
234+
public function supports(Request $request): bool
234235
{
235236
return self::LOGIN_ROUTE === $request->attributes->get('_route')
236237
&& $request->isMethod('POST');
@@ -251,7 +252,7 @@ a traditional HTML form that submits to ``/login``:
251252
return $credentials;
252253
}
253254

254-
public function getUser($credentials, UserProviderInterface $userProvider)
255+
public function getUser($credentials, UserProviderInterface $userProvider): ?User
255256
{
256257
$token = new CsrfToken('authenticate', $credentials['csrf_token']);
257258
if (!$this->csrfTokenManager->isTokenValid($token)) {
@@ -268,7 +269,7 @@ a traditional HTML form that submits to ``/login``:
268269
return $user;
269270
}
270271

271-
public function checkCredentials($credentials, UserInterface $user)
272+
public function checkCredentials($credentials, UserInterface $user): bool
272273
{
273274
return $this->passwordEncoder->isPasswordValid($user, $credentials['password']);
274275
}
@@ -281,7 +282,7 @@ a traditional HTML form that submits to ``/login``:
281282
return $credentials['password'];
282283
}
283284

284-
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
285+
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey): ?Response
285286
{
286287
if ($targetPath = $this->getTargetPath($request->getSession(), $providerKey)) {
287288
return new RedirectResponse($targetPath);
@@ -291,7 +292,7 @@ a traditional HTML form that submits to ``/login``:
291292
throw new \Exception('TODO: provide a valid redirect inside '.__FILE__);
292293
}
293294

294-
protected function getLoginUrl()
295+
protected function getLoginUrl(): string
295296
{
296297
return $this->urlGenerator->generate(self::LOGIN_ROUTE);
297298
}
@@ -380,7 +381,7 @@ be redirected after success:
380381
// src/Security/LoginFormAuthenticator.php
381382
382383
// ...
383-
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
384+
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey): Response
384385
{
385386
// ...
386387
@@ -503,7 +504,7 @@ whenever the user browses a page::
503504
$this->saveTargetPath($this->session, 'main', $request->getUri());
504505
}
505506

506-
public static function getSubscribedEvents()
507+
public static function getSubscribedEvents(): array
507508
{
508509
return [
509510
KernelEvents::REQUEST => ['onKernelRequest']

0 commit comments

Comments
 (0)