8000 feature #27650 [SecurityBundle] Add json login ldap (Rudy Onfroy) · symfony/symfony@6cefd88 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6cefd88

Browse files
author
Robin Chalas
committed
feature #27650 [SecurityBundle] Add json login ldap (Rudy Onfroy)
This PR was squashed before being merged into the 4.2-dev branch (closes #27650). Discussion ---------- [SecurityBundle] Add json login ldap | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT Add a simple from_login_ldap on firewall types to let authenticate with ldap with json API Commits ------- 2b2dfd2 [SecurityBundle] Add json login ldap
2 parents 02daeb2 + 2b2dfd2 commit 6cefd88

File tree

6 files changed

+144
-1
lines changed

6 files changed

+144
-1
lines changed

src/Symfony/Bundle/SecurityBundle/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ CHANGELOG
1010
custom tokens extend the existing `Symfony\Component\Security\Core\Authentication\Token\AnonymousToken`
1111
or `Symfony\Component\Security\Core\Authentication\Token\RememberMeToken`.
1212
* Added `Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\AddExpressionLanguageProvidersPass`
13-
13+
* Added `json_login_ldap` authentication provider to use LDAP authentication with a REST API.
14+
1415
4.1.0
1516
-----
1617

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory;
13+
14+
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
15+
use Symfony\Component\DependencyInjection\ChildDefinition;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Reference;
18+
19+
/**
20+
* JsonLoginLdapFactory creates services for json login ldap authentication.
21+
*/
22+
class JsonLoginLdapFactory extends JsonLoginFactory
23+
{
24+
public function getKey()
25+
{
26+
return 'json-login-ldap';
27+
}
28+
29+
protected function createAuthProvider(ContainerBuilder $container, $id, $config, $userProviderId)
30+
{
31+
$provider = 'security.authentication.provider.ldap_bind.'.$id;
32+
$definition = $container
33+
->setDefinition($provider, new ChildDefinition('security.authentication.provider.ldap_bind'))
34+
->replaceArgument(0, new Reference($userProviderId))
35+
->replaceArgument(1, new Reference('security.user_checker.'.$id))
36+
->replaceArgument(2, $id)
37+
->replaceArgument(3, new Reference($config['service']))
38+
->replaceArgument(4, $config['dn_string'])
39+
;
40+
41+
if (!empty($config['query_string'])) {
42+
$definition->addMethodCall('setQueryString', array($config['query_string']));
43+
}
44+
45+
return $provider;
46+
}
47+
48+
public function addConfiguration(NodeDefinition $node)
49+
{
50+
parent::addConfiguration($node);
51+
52+
$node
53+
->children()
54+
->scalarNode('service')->defaultValue('ldap')->end()
55+
->scalarNode('dn_string')->defaultValue('{username}')->end()
56+
->scalarNode('query_string')->end()
57+
->end()
58+
;
59+
}
60+
}

src/Symfony/Bundle/SecurityBundle/SecurityBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\AddExpressionLanguageProvidersPass;
1515
use Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\RegisterCsrfTokenClearingLogoutHandlerPass;
1616
use Symfony\Bundle\SecurityBundle\Dep 57AE endencyInjection\Security\Factory\JsonLoginFactory;
17+
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\JsonLoginLdapFactory;
1718
use Symfony\Component\HttpKernel\Bundle\Bundle;
1819
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
1920
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -47,6 +48,7 @@ public function build(ContainerBuilder $container)
4748
$extension->addSecurityListenerFactory(new FormLoginFactory());
4849
$extension->addSecurityListenerFactory(new FormLoginLdapFactory());
4950
$extension->addSecurityListenerFactory(new JsonLoginFactory());
51+
$extension->addSecurityListenerFactory(new JsonLoginLdapFactory());
5052
$extension->addSecurityListenerFactory(new HttpBasicFactory());
5153
$extension->addSecurityListenerFactory(new HttpBasicLdapFactory());
5254
$extension->addSecurityListenerFactory(new RememberMeFactory());
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\SecurityBundle\Tests\Functional;
13+
14+
use Symfony\Component\HttpKernel\Kernel;
15+
16+
class JsonLoginLdapTest extends WebTestCase
17+
{
18+
public function testKernelBoot()
19+
{
20+
$kernel = self::createKernel(array('test_case' => 'JsonLoginLdap', 'root_config' => 'config.yml'));
21+
$kernel->boot();
22+
23+
$this->assertInstanceOf(Kernel::class, $kernel);
24+
}
25+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
return array(
13+
new Symfony\Bundle\SecurityBundle\SecurityBundle(),
14+
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
15+
new Symfony\Bundle\TwigBundle\TwigBundle(),
16+
);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
imports:
2+
- { resource: ./../config/default.yml }
3+
services:
4+
Symfony\Component\Ldap\Ldap:
5+
arguments: ['@Symfony\Component\Ldap\Adapter\ExtLdap\Adapter']
6+
7+
Symfony\Component\Ldap\Adapter\ExtLdap\Adapter:
8+
arguments:
9+
- host: 'localhost'
10+
port: 389
11+
options:
12+
protocol_version: 3
13+
referrals: false
14+
security:
15+
providers:
16+
ldap:
17+
ldap:
18+
service: Symfony\Component\Ldap\Ldap
19+
base_dn: 'dc=onfroy,dc=net'
20+
search_dn: ''
21+
search_password: ''
22+
default_roles: ROLE_USER
23+
uid 8D1B _key: uid
24+
25+
firewalls:
26+
main:
27+
pattern: ^/login
28+
stateless: true
29+
anonymous: true
30+
json_login_ldap:
31+
check_path: /login
32+
require_previous_session: false
33+
service: Symfony\Component\Ldap\Ldap
34+
dn_string: ''
35+
username_path: user.login
36+
password_path: user.password
37+
38+
access_control:
39+
- { path: ^/, roles: ROLE_USER }

0 commit comments

Comments
 (0)
0