@@ -106,7 +106,6 @@ set an authenticated token in the security context if successful.
106
106
use Symfony\Component\Security\Core\Exception\AuthenticationException;
107
107
use Symfony\Component\Security\Core\SecurityContextInterface;
108
108
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
109
- use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
110
109
use Acme\DemoBundle\Security\Authentication\Token\WsseUserToken;
111
110
112
111
class WsseListener implements ListenerInterface
@@ -124,35 +123,35 @@ set an authenticated token in the security context if successful.
124
123
{
125
124
$request = $event->getRequest();
126
125
127
- if ($request->headers->has('x-wsse')) {
126
+ $wsseRegex = '/UsernameToken Username="([^"]+)", PasswordDigest="([^"]+)", Nonce="([^"]+)", Created="([^"]+)"/';
127
+ if (!$request->headers->has('x-wsse') || 1 !== preg_match($wsseRegex, $request->headers->get('x-wsse'), $matches)) {
128
+ return;
129
+ }
128
130
129
- $wsseRegex = '/UsernameToken Username="([^"]+)", PasswordDigest="([^"]+)", Nonce="([^"]+)", Created="([^"]+)"/';
131
+ $token = new WsseUserToken();
132
+ $token->setUser($matches[1]);
130
133
131
- if (preg_match($wsseRegex, $request->headers->get('x-wsse'), $ matches)) {
132
- $token = new WsseUserToken() ;
133
- $token->setUser( $matches[1]) ;
134
+ $token->digest = $ matches[2];
135
+ $token->nonce = $matches[3] ;
136
+ $token->created = $matches[4] ;
134
137
135
- $token->digest = $matches[2];
136
- $token->nonce = $matches[3];
137
- $token->created = $matches[4];
138
+ try {
139
+ $authToken = $this->authenticationManager->authenticate($token);
138
140
139
- try {
140
- $returnValue = $this->authenticationManager->authenticate($token);
141
+ $this->securityContext->setToken($authToken);
142
+ } catch (AuthenticationException $failed) {
143
+ // you might log something here
141
144
142
- if ($returnValue instanceof TokenInterface) {
143
- return $this->securityContext->setToken($returnValue);
144
- } elseif ($returnValue instanceof Response) {
145
- return $event->setResponse($returnValue);
146
- }
147
- } catch (AuthenticationException $e) {
148
- // you might log something here
149
- }
150
- }
151
- }
145
+ // To deny the authentication clear the token. This will redirect to the login page.
146
+ // $this->securityContext->setToken(null);
147
+ // return;
152
148
153
- $response = new Response();
154
- $response->setStatusCode(403);
155
- $event->setResponse($response);
149
+ // Deny authentication with a '403 Forbidden' HTTP response
150
+ $response = new Response();
151
+ $response->setStatusCode(403);
152
+ $event->setResponse($response);
153
+
154
+ }
156
155
}
157
156
}
158
157
0 commit comments