1- How to create a custom Security Extension
2- =========================================
1+ .. index ::
2+ single: Security; Custom Authentication Provider
3+
4+ How to create a custom Authentication Provider
5+ ==============================================
36
47If you have read the chapter on :doc: `/book/security `, you understand the
58distinction Symfony2 makes between authentication and authorization in the
69implementation of security. This chapter discusses the core classes involved
7- in the authorization process, and how to implement a custom authorization
8- extension . Because authentication and authorization are separate concepts,
10+ in the authentication process, and how to implement a custom authentication
11+ provider . Because authentication and authorization are separate concepts,
912this extension will be user-provider agnostic, and will function with your
1013application's user providers, may they be based in memory, a database, or
1114wherever else you choose to store them.
@@ -14,7 +17,7 @@ Meet WSSE
1417---------
1518
1619The following chapter demonstrates how to create a custom authentication
17- extension for WSSE authentication. The security protocol for WSSE provides
20+ provider for WSSE authentication. The security protocol for WSSE provides
1821several security benefits:
1922
20231. Username / Password encryption
@@ -101,8 +104,7 @@ an authenticated token in the security context if successful.
101104 protected $securityContext;
102105 protected $authenticationManager;
103106
104- public function __construct(SecurityContextInterface $securityContext,
105- AuthenticationManagerInterface $authenticationManager)
107+ public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager)
106108 {
107109 $this->securityContext = $securityContext;
108110 $this->authenticationManager = $authenticationManager;
@@ -112,6 +114,10 @@ an authenticated token in the security context if successful.
112114 {
113115 $request = $event->getRequest();
114116
117+ if (!$request->headers->has('x-wsse')) {
118+ return;
119+ }
120+
115121 $wsseRegex = '/UsernameToken Username="([^"]+)", PasswordDigest="([^"]+)", Nonce="([^"]+)", Created="([^"]+)"/';
116122
117123 if (preg_match($wsseRegex, $request->headers->get('x-wsse'), $matches)) {
@@ -152,7 +158,7 @@ a 403 Response is returned.
152158 A class not used above, the
153159 :class: `Symfony\\ Component\\ Security\\ Http\\ Firewall\\ AbstractAuthenticationListener `
154160 class, is a very useful base class which provides commonly needed functionality
155- for authentication extensions. This includes maintaining the token in
161+ for security extensions. This includes maintaining the token in
156162 the session, providing success / failure handlers, login form urls,
157163 and more. As WSSE does not require maintaining authentication sessions
158164 or login forms, it won't be used for this example.
@@ -191,8 +197,7 @@ the ``PasswordDigest`` header value matches with the user's password.
191197 {
192198 $user = $this->userProvider->loadUserByUsername($token->getUsername());
193199
194- if($user && $this->validateDigest($token->digest, $token->nonce, $token->created, $user->getPassword()))
195- {
200+ if ($user && $this->validateDigest($token->digest, $token->nonce, $token->created, $user->getPassword())) {
196201 $token->setUser($user);
197202 return $token;
198203 }
@@ -237,10 +242,10 @@ The Factory
237242-----------
238243
239244You have created a custom token, custom listener, and custom provider.
240- Now you need to tie them all together. How do you make your extension
245+ Now you need to tie them all together. How do you make your provider
241246available to your security configuration? The answer is by using a
242247``factory ``. A factory is where you hook in to the security component,
243- telling it the name of your extension and any configuration options available
248+ telling it the name of your provider and any configuration options available
244249for it. First, you must create a class which implements
245250:class: `Symfony\\ Bundle\\ SecurityBundle\\ DependencyInjection\\ Security\\ Factory\\ SecurityFactoryInterface `.
246251
@@ -293,6 +298,7 @@ position at which the provider is called, a ``getKey`` method which
293298defines the configuration key used to reference the provider, and an
294299``addConfiguration `` method, which is used to define the configuration
295300options underneath the configuration key in your security configuration.
301+ Setting configuration options are explained later in this chapter.
296302
297303.. note ::
298304
@@ -402,14 +408,14 @@ to import it.
402408 # app/config/security.yml
403409 security :
404410 factories :
405- - " %kernel.root_dir%/../vendor/bundles /Acme/DemoBundle/Resources/config/security_factories.xml"
411+ - " %kernel.root_dir%/../src /Acme/DemoBundle/Resources/config/security_factories.xml"
406412
407413 .. code-block :: xml
408414
409415 <!-- app/config/security.xml -->
410416 <config >
411417 <factories >
412- "%kernel.root_dir%/../vendor/bundles /Acme/DemoBundle/Resources/config/security_factories.xml
418+ "%kernel.root_dir%/../src /Acme/DemoBundle/Resources/config/security_factories.xml
413419 </factories >
414420 </config >
415421
@@ -418,7 +424,7 @@ to import it.
418424 // app/config/security.php
419425 $container->loadFromExtension('security', array(
420426 'factories' => array(
421- "%kernel.root_dir%/../vendor/bundles /Acme/DemoBundle/Resources/config/security_factories.xml"
427+ "%kernel.root_dir%/../src /Acme/DemoBundle/Resources/config/security_factories.xml"
422428 ),
423429 ));
424430
@@ -433,16 +439,23 @@ protection.
433439 pattern : /api/.*
434440 wsse : true
435441
436- Congratulations! You have written your very own security extension!
442+ Congratulations! You have written your very own custom security authentication
443+ provider!
437444
438445A Little Extra
439446--------------
440447
441- How about making your WSSE security extension a bit more exciting? The
442- possibilities are endless. You can start by adding options under the
443- ``wsse `` key in your security configuration. For instance, the time allowed
444- before expiring the Created header item, by default, is 5 minutes. Make this
445- configurable, so different firewalls can have different timeout lengths.
448+ How about making your WSSE authentication provider a bit more exciting? The
449+ possibilities are endless. Why don't you start by adding some spackle
450+ to that shine?
451+
452+ Configuration
453+ ~~~~~~~~~~~~~
454+
455+ You can add custom options under the ``wsse `` key in your security configuration.
456+ For instance, the time allowed before expiring the Created header item,
457+ by default, is 5 minutes. Make this configurable, so different firewalls
458+ can have different timeout lengths.
446459
447460You will first need to edit ``WsseFactory `` and define the new option in
448461the ``addConfiguration `` method.
@@ -455,9 +468,11 @@ the ``addConfiguration`` method.
455468
456469 public function addConfiguration(NodeDefinition $node)
457470 {
458- $builder = $node->children();
459-
460- $builder->scalarNode('lifetime')->defaultValue(300);
471+ $node
472+ ->children()
473+ ->scalarNode('lifetime')->defaultValue(300)
474+ ->end()
475+ ;
461476 }
462477 }
463478
@@ -470,8 +485,7 @@ to your authentication provider in order to put it to use.
470485
471486 class WsseFactory implements SecurityFactoryInterface
472487 {
473- public function create(ContainerBuilder $container, $id,
474- $config, $userProvider, $defaultEntryPoint)
488+ public function create(ContainerBuilder $container, $id, $config, $userProvider, $defaultEntryPoint)
475489 {
476490 $providerId = 'security.authentication.provider.wsse.'.$id;
477491 $container
0 commit comments