11
11
12
12
namespace Symfony \Component \Security \Http \Firewall ;
13
13
14
+ use Symfony \Component \Form \CsrfProvider \CsrfProviderInterface ;
15
+ use Symfony \Component \HttpFoundation \Request ;
16
+ use Symfony \Component \HttpKernel \Log \LoggerInterface ;
14
17
use Symfony \Component \Security \Http \Authentication \AuthenticationFailureHandlerInterface ;
15
18
use Symfony \Component \Security \Http \Authentication \AuthenticationSuccessHandlerInterface ;
16
19
use Symfony \Component \Security \Http \Session \SessionAuthenticationStrategyInterface ;
17
-
18
- use Symfony \Component \Security \Core \SecurityContextInterface ;
19
20
use Symfony \Component \Security \Core \Authentication \AuthenticationManagerInterface ;
20
- use Symfony \Component \HttpKernel \Log \LoggerInterface ;
21
- use Symfony \Component \HttpFoundation \Request ;
22
21
use Symfony \Component \Security \Core \Authentication \Token \UsernamePasswordToken ;
22
+ use Symfony \Component \Security \Core \Exception \InvalidCsrfTokenException ;
23
+ use Symfony \Component \Security \Core \SecurityContextInterface ;
23
24
24
25
/**
25
26
* UsernamePasswordFormAuthenticationListener is the default implementation of
29
30
*/
30
31
class UsernamePasswordFormAuthenticationListener extends AbstractAuthenticationListener
31
32
{
33
+ protected $ csrfProvider ;
34
+
32
35
/**
33
36
* {@inheritdoc}
34
37
*/
35
- public function __construct (SecurityContextInterface $ securityContext , AuthenticationManagerInterface $ authenticationManager , SessionAuthenticationStrategyInterface $ sessionStrategy , $ providerKey , array $ options = array (), AuthenticationSuccessHandlerInterface $ successHandler = null , AuthenticationFailureHandlerInterface $ failureHandler = null , LoggerInterface $ logger = null )
38
+ public function __construct (SecurityContextInterface $ securityContext , AuthenticationManagerInterface $ authenticationManager , SessionAuthenticationStrategyInterface $ sessionStrategy , $ providerKey , array $ options = array (), AuthenticationSuccessHandlerInterface $ successHandler = null , AuthenticationFailureHandlerInterface $ failureHandler = null , LoggerInterface $ logger = null , CsrfProviderInterface $ csrfProvider = null )
36
39
{
37
40
parent ::__construct ($ securityContext , $ authenticationManager , $ sessionStrategy , $ providerKey , array_merge (array (
38
41
'username_parameter ' => '_username ' ,
39
42
'password_parameter ' => '_password ' ,
43
+ 'csrf_parameter ' => '_csrf_token ' ,
44
+ 'csrf_page_id ' => 'form_login ' ,
40
45
'post_only ' => true ,
41
46
), $ options ), $ successHandler , $ failureHandler , $ logger );
47
+
48
+ $ this ->csrfProvider = $ csrfProvider ;
42
49
}
43
50
44
51
/**
@@ -54,6 +61,14 @@ protected function attemptAuthentication(Request $request)
54
61
return null ;
55
62
}
56
63
64
+ if (null !== $ this ->csrfProvider ) {
65
+ $ csrfToken = $ request ->get ($ this ->options ['csrf_parameter ' ]);
66
+
67
+ if (false === $ this ->csrfProvider ->isTokenValid ($ this ->options ['csrf_page_id ' ], $ csrfToken )) {
68
+ throw new InvalidCsrfTokenException ('Invalid CSRF token. ' );
69
+ }
70
+ }
71
+
57
72
$ username = trim ($ request ->get ($ this ->options ['username_parameter ' ]));
58
73
$ password = $ request ->get ($ this ->options ['password_parameter ' ]);
59
74
0 commit comments