Commit 8b776dfb authored by Fabien Potencier's avatar Fabien Potencier

fixed authentication providers registration

parent 9022e420
...@@ -129,13 +129,23 @@ class SecurityServiceProvider implements ServiceProviderInterface ...@@ -129,13 +129,23 @@ class SecurityServiceProvider implements ServiceProviderInterface
$app['security.authentication.'.$name.'.'.$type] = $app['security.authentication.'.$type.'._proto']($name, $options); $app['security.authentication.'.$name.'.'.$type] = $app['security.authentication.'.$type.'._proto']($name, $options);
} }
return array($app['security.authentication.'.$name.'.'.$type], $app['security.entry_point.'.$name.'.'.$entryPoint], $type); if (!isset($app['security.authentication_provider.'.$name])) {
$app['security.authentication_provider.'.$name] = $app['security.authentication_provider.'.('anonymous' == $name ? 'anonymous' : 'dao').'._proto']($name);
}
return array(
'security.authentication_provider.'.$name,
'security.authentication.'.$name.'.'.$type,
'security.entry_point.'.$name.'.'.$entryPoint,
$type
);
}); });
} }
$app['security.firewall_map'] = $app->share(function () use ($app) { $app['security.firewall_map'] = $app->share(function () use ($app) {
$map = new FirewallMap();
$positions = array('logout', 'pre_auth', 'form', 'http', 'remember_me', 'anonymous'); $positions = array('logout', 'pre_auth', 'form', 'http', 'remember_me', 'anonymous');
$providers = array();
$configs = array();
foreach ($app['security.firewalls'] as $name => $firewall) { foreach ($app['security.firewalls'] as $name => $firewall) {
$entryPoint = 'form'; $entryPoint = 'form';
$pattern = isset($firewall['pattern']) ? $firewall['pattern'] : null; $pattern = isset($firewall['pattern']) ? $firewall['pattern'] : null;
...@@ -144,7 +154,7 @@ class SecurityServiceProvider implements ServiceProviderInterface ...@@ -144,7 +154,7 @@ class SecurityServiceProvider implements ServiceProviderInterface
$protected = count($firewall); $protected = count($firewall);
$listeners = array($app['security.channel_listener']); $listeners = array('security.channel_listener');
if ($protected) { if ($protected) {
if (!isset($app['security.context_listener.'.$name])) { if (!isset($app['security.context_listener.'.$name])) {
...@@ -152,13 +162,10 @@ class SecurityServiceProvider implements ServiceProviderInterface ...@@ -152,13 +162,10 @@ class SecurityServiceProvider implements ServiceProviderInterface
$app['security.user_provider.'.$name] = is_array($users) ? $app['security.user_provider.inmemory._proto']($users) : $users; $app['security.user_provider.'.$name] = is_array($users) ? $app['security.user_provider.inmemory._proto']($users) : $users;
} }
$app['security.context_listener.'.$name] = $app['security.context_listener._proto']( $app['security.context_listener.'.$name] = $app['security.context_listener._proto']($name, array($app['security.user_provider.'.$name]));
$name,
array($app['security.user_provider.'.$name])
);
} }
$listeners[] = $app['security.context_listener.'.$name]; $listeners[] = 'security.context_listener.'.$name;
$factories = array(); $factories = array();
foreach ($positions as $position) { foreach ($positions as $position) {
...@@ -179,9 +186,14 @@ class SecurityServiceProvider implements ServiceProviderInterface ...@@ -179,9 +186,14 @@ class SecurityServiceProvider implements ServiceProviderInterface
throw new \LogicException(sprintf('The "%s" authentication entry is not registered.', $type)); throw new \LogicException(sprintf('The "%s" authentication entry is not registered.', $type));
} }
list($listener, $entryPoint, $position) = $app['security.authentication.factory.'.$type]($name, $options); list($providerId, $listenerId, $entryPointId, $position) = $app['security.authentication.factory.'.$type]($name, $options);
if (null !== $entryPointId) {
$entryPoint = $entryPointId;
}
$factories[$position][] = $listener; $factories[$position][] = $listenerId;
$providers[] = $providerId;
} }
foreach ($positions as $position) { foreach ($positions as $position) {
...@@ -190,10 +202,12 @@ class SecurityServiceProvider implements ServiceProviderInterface ...@@ -190,10 +202,12 @@ class SecurityServiceProvider implements ServiceProviderInterface
} }
} }
$listeners[] = $app['security.access_listener']; $listeners[] = 'security.access_listener';
if (isset($firewall['switch_user'])) { if (isset($firewall['switch_user'])) {
$listeners[] = $app['security.authentication.switch_user._proto']($name, $firewall['switch_user']); $app['security.switch_user.'.$name] = $app['security.authentication.switch_user._proto']($name, $firewall['switch_user']);
$listeners[] = 'security.switch_user.'.$name;
} }
if (!isset($app['security.exception_listener.'.$name])) { if (!isset($app['security.exception_listener.'.$name])) {
...@@ -201,33 +215,21 @@ class SecurityServiceProvider implements ServiceProviderInterface ...@@ -201,33 +215,21 @@ class SecurityServiceProvider implements ServiceProviderInterface
} }
} }
$map->add( $configs[] = array($pattern, $listeners, $protected);
is_string($pattern) ? new RequestMatcher($pattern) : $pattern,
$listeners,
$protected ? $app['security.exception_listener.'.$name] : null
);
} }
return $map; $app['security.authentication_providers'] = array_map(function ($provider) use ($app) { return $app[$provider]; }, $providers);
});
$app['security.authentication_providers'] = $app->share(function () use ($app) {
$providers = array();
foreach ($app['security.firewalls'] as $name => $firewall) {
unset($firewall['pattern'], $firewall['users']);
if (!count($firewall)) { $map = new FirewallMap();
continue; foreach ($configs as $config) {
} $map->add(
is_string($config[0]) ? new RequestMatcher($config[0]) : $config[0],
if (!isset($app['security.authentication_provider.'.$name])) { array_map(function ($listener) use ($app) { return $app[$listener]; }, $config[1]),
$a = 'anonymous' == $name ? 'anonymous' : 'dao'; $config[2] ? $app['security.exception_listener.'.$name] : null
$app['security.authentication_provider.'.$name] = $app['security.authentication_provider.'.$a.'._proto']($name); );
}
$providers[] = $app['security.authentication_provider.'.$name];
} }
return $providers; return $map;
}); });
$app['security.access_listener'] = $app->share(function () use ($app) { $app['security.access_listener'] = $app->share(function () use ($app) {
...@@ -283,6 +285,7 @@ class SecurityServiceProvider implements ServiceProviderInterface ...@@ -283,6 +285,7 @@ class SecurityServiceProvider implements ServiceProviderInterface
// prototypes (used by the Firewall Map) // prototypes (used by the Firewall Map)
$app['security.context_listener._proto'] = $app->protect(function ($providerKey, $userProviders) use ($app) { $app['security.context_listener._proto'] = $app->protect(function ($providerKey, $userProviders) use ($app) {
return $app->share(function () use ($app, $userProviders, $providerKey) {
return new ContextListener( return new ContextListener(
$app['security'], $app['security'],
$userProviders, $userProviders,
...@@ -291,8 +294,10 @@ class SecurityServiceProvider implements ServiceProviderInterface ...@@ -291,8 +294,10 @@ class SecurityServiceProvider implements ServiceProviderInterface
$app['dispatcher'] $app['dispatcher']
); );
}); });
});
$app['security.user_provider.inmemory._proto'] = $app->protect(function ($params) use ($app) { $app['security.user_provider.inmemory._proto'] = $app->protect(function ($params) use ($app) {
return $app->share(function () use ($app, $params) {
$users = array(); $users = array();
foreach ($params as $name => $user) { foreach ($params as $name => $user) {
$users[$name] = array('roles' => (array) $user[0], 'password' => $user[1]); $users[$name] = array('roles' => (array) $user[0], 'password' => $user[1]);
...@@ -300,20 +305,24 @@ class SecurityServiceProvider implements ServiceProviderInterface ...@@ -300,20 +305,24 @@ class SecurityServiceProvider implements ServiceProviderInterface
return new InMemoryUserProvider($users); return new InMemoryUserProvider($users);
}); });
});
$app['security.exception_listener._proto'] = $app->protect(function ($entryPoint, $name) use ($app) { $app['security.exception_listener._proto'] = $app->protect(function ($entryPoint, $name) use ($app) {
return $app->share(function () use ($app, $entryPoint, $name) {
return new ExceptionListener( return new ExceptionListener(
$app['security'], $app['security'],
$app['security.trust_resolver'], $app['security.trust_resolver'],
$app['security.http_utils'], $app['security.http_utils'],
$entryPoint, $app[$entryPoint],
null, // errorPage null, // errorPage
null, // AccessDeniedHandlerInterface null, // AccessDeniedHandlerInterface
$app['logger'] $app['logger']
); );
}); });
});
$app['security.authentication.form._proto'] = $app->protect(function ($providerKey, $options) use ($app, $that) { $app['security.authentication.form._proto'] = $app->protect(function ($providerKey, $options) use ($app, $that) {
return $app->share(function () use ($app, $providerKey, $options, $that) {
$that->addFakeRoute(array('post', $tmp = isset($options['check_path']) ? $options['check_path'] : '/login_check', str_replace('/', '_', ltrim($tmp, '/')))); $that->addFakeRoute(array('post', $tmp = isset($options['check_path']) ? $options['check_path'] : '/login_check', str_replace('/', '_', ltrim($tmp, '/'))));
return new UsernamePasswordFormAuthenticationListener( return new UsernamePasswordFormAuthenticationListener(
...@@ -330,8 +339,10 @@ class SecurityServiceProvider implements ServiceProviderInterface ...@@ -330,8 +339,10 @@ class SecurityServiceProvider implements ServiceProviderInterface
isset($options['with_csrf']) && $options['with_csrf'] && isset($app['form.csrf_provider']) ? $app['form.csrf_provider'] : null isset($options['with_csrf']) && $options['with_csrf'] && isset($app['form.csrf_provider']) ? $app['form.csrf_provider'] : null
); );
}); });
});
$app['security.authentication.http._proto'] = $app->protect(function ($providerKey, $options) use ($app) { $app['security.authentication.http._proto'] = $app->protect(function ($providerKey, $options) use ($app) {
return $app->share(function () use ($app, $providerKey, $options) {
return new BasicAuthenticationListener( return new BasicAuthenticationListener(
$app['security'], $app['security'],
$app['security.authentication_manager'], $app['security.authentication_manager'],
...@@ -340,16 +351,20 @@ class SecurityServiceProvider implements ServiceProviderInterface ...@@ -340,16 +351,20 @@ class SecurityServiceProvider implements ServiceProviderInterface
$app['logger'] $app['logger']
); );
}); });
});
$app['security.authentication.anonymous._proto'] = $app->protect(function ($providerKey, $options) use ($app) { $app['security.authentication.anonymous._proto'] = $app->protect(function ($providerKey, $options) use ($app) {
return $app->share(function () use ($app, $providerKey, $options) {
return new AnonymousAuthenticationListener( return new AnonymousAuthenticationListener(
$app['security'], $app['security'],
$providerKey, $providerKey,
$app['logger'] $app['logger']
); );
}); });
});
$app['security.authentication.logout._proto'] = $app->protect(function ($providerKey, $options) use ($app, $that) { $app['security.authentication.logout._proto'] = $app->protect(function ($providerKey, $options) use ($app, $that) {
return $app->share(function () use ($app, $providerKey, $options, $that) {
$that->addFakeRoute(array('get', $tmp = isset($options['logout_path']) ? $options['logout_path'] : '/logout', str_replace('/', '_', ltrim($tmp, '/')))); $that->addFakeRoute(array('get', $tmp = isset($options['logout_path']) ? $options['logout_path'] : '/logout', str_replace('/', '_', ltrim($tmp, '/'))));
$listener = new LogoutListener( $listener = new LogoutListener(
...@@ -364,8 +379,10 @@ class SecurityServiceProvider implements ServiceProviderInterface ...@@ -364,8 +379,10 @@ class SecurityServiceProvider implements ServiceProviderInterface
return $listener; return $listener;
}); });
});
$app['security.authentication.switch_user._proto'] = $app->protect(function ($name, $options) use ($app, $that) { $app['security.authentication.switch_user._proto'] = $app->protect(function ($name, $options) use ($app, $that) {
return $app->share(function () use ($app, $name, $options, $that) {
return new SwitchUserListener( return new SwitchUserListener(
$app['security'], $app['security'],
$app['security.user_provider.'.$name], $app['security.user_provider.'.$name],
...@@ -378,16 +395,22 @@ class SecurityServiceProvider implements ServiceProviderInterface ...@@ -378,16 +395,22 @@ class SecurityServiceProvider implements ServiceProviderInterface
$app['dispatcher'] $app['dispatcher']
); );
}); });
});
$app['security.entry_point.form._proto'] = $app->protect(function ($name, $loginPath = '/login', $useForward = false) use ($app) { $app['security.entry_point.form._proto'] = $app->protect(function ($name, $loginPath = '/login', $useForward = false) use ($app) {
return $app->share(function () use ($app, $loginPath, $useForward) {
return new FormAuthenticationEntryPoint($app, $app['security.http_utils'], $loginPath, $useForward); return new FormAuthenticationEntryPoint($app, $app['security.http_utils'], $loginPath, $useForward);
}); });
});
$app['security.entry_point.http._proto'] = $app->protect(function ($name, $realName = 'Secured') use ($app) { $app['security.entry_point.http._proto'] = $app->protect(function ($name, $realName = 'Secured') use ($app) {
return $app->share(function () use ($app, $name, $realName) {
return new BasicAuthenticationEntryPoint($realName); return new BasicAuthenticationEntryPoint($realName);
}); });
});
$app['security.authentication_provider.dao._proto'] = $app->protect(function ($name) use ($app) { $app['security.authentication_provider.dao._proto'] = $app->protect(function ($name) use ($app) {
return $app->share(function () use ($app, $name) {
return new DaoAuthenticationProvider( return new DaoAuthenticationProvider(
$app['security.user_provider.'.$name], $app['security.user_provider.'.$name],
$app['security.user_checker'], $app['security.user_checker'],
...@@ -395,10 +418,13 @@ class SecurityServiceProvider implements ServiceProviderInterface ...@@ -395,10 +418,13 @@ class SecurityServiceProvider implements ServiceProviderInterface
$app['security.encoder_factory'] $app['security.encoder_factory']
); );
}); });
});
$app['security.authentication_provider.anonymous._proto'] = $app->protect(function ($name) use ($app) { $app['security.authentication_provider.anonymous._proto'] = $app->protect(function ($name) use ($app) {
return $app->share(function () use ($app, $name) {
return new AnonymousAuthenticationProvider($name); return new AnonymousAuthenticationProvider($name);
}); });
});
} }
public function boot(Application $app) public function boot(Application $app)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment