Skip to content

Commit de4085a

Browse files
committed
Merge pull request #1686 from dol/minor_fix_in_custom_authentication_provider
Minor improvement: Cleaned up the WsseListener example
2 parents beecde8 + 8785f3f commit de4085a

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

cookbook/security/custom_authentication_provider.rst

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ set an authenticated token in the security context if successful.
106106
use Symfony\Component\Security\Core\Exception\AuthenticationException;
107107
use Symfony\Component\Security\Core\SecurityContextInterface;
108108
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
109-
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
110109
use Acme\DemoBundle\Security\Authentication\Token\WsseUserToken;
111110
112111
class WsseListener implements ListenerInterface
@@ -124,35 +123,35 @@ set an authenticated token in the security context if successful.
124123
{
125124
$request = $event->getRequest();
126125
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+
}
128130
129-
$wsseRegex = '/UsernameToken Username="([^"]+)", PasswordDigest="([^"]+)", Nonce="([^"]+)", Created="([^"]+)"/';
131+
$token = new WsseUserToken();
132+
$token->setUser($matches[1]);
130133
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];
134137
135-
$token->digest = $matches[2];
136-
$token->nonce = $matches[3];
137-
$token->created = $matches[4];
138+
try {
139+
$authToken = $this->authenticationManager->authenticate($token);
138140
139-
try {
140-
$returnValue = $this->authenticationManager->authenticate($token);
141+
$this->securityContext->setToken($authToken);
142+
} catch (AuthenticationException $failed) {
143+
// you might log something here
141144
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;
152148
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+
}
156155
}
157156
}
158157

0 commit comments

Comments
 (0)