Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitee40718

Browse files
committed
[SecurityBundle] Register the UsernamePasswordJsonAuthenticationListener class
1 parent57f0407 commitee40718

File tree

11 files changed

+397
-49
lines changed

11 files changed

+397
-49
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
namespaceSymfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory;
13+
14+
useSymfony\Component\DependencyInjection\ContainerBuilder;
15+
useSymfony\Component\DependencyInjection\DefinitionDecorator;
16+
useSymfony\Component\DependencyInjection\Reference;
17+
18+
/**
19+
* JsonLoginFactory creates services for JSON login authentication.
20+
*
21+
* @author Kévin Dunglas <dunglas@gmail.com>
22+
*/
23+
class JsonLoginFactoryextends AbstractFactory
24+
{
25+
publicfunction__construct()
26+
{
27+
$this->addOption('username_path','username');
28+
$this->addOption('password_path','password');
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
publicfunctiongetPosition()
35+
{
36+
return'form';
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
publicfunctiongetKey()
43+
{
44+
return'json-login';
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*/
50+
protectedfunctioncreateAuthProvider(ContainerBuilder$container,$id,$config,$userProviderId)
51+
{
52+
$provider ='security.authentication.provider.dao.'.$id;
53+
$container
54+
->setDefinition($provider,newDefinitionDecorator('security.authentication.provider.dao'))
55+
->replaceArgument(0,newReference($userProviderId))
56+
->replaceArgument(1,newReference('security.user_checker.'.$id))
57+
->replaceArgument(2,$id)
58+
;
59+
60+
return$provider;
61+
}
62+
63+
/**
64+
* {@inheritdoc}
65+
*/
66+
protectedfunctiongetListenerId()
67+
{
68+
return'security.authentication.listener.json';
69+
}
70+
71+
/**
72+
* {@inheritdoc}
73+
*/
74+
protectedfunctionisRememberMeAware($config)
75+
{
76+
returnfalse;
77+
}
78+
79+
/**
80+
* {@inheritdoc}
81+
*/
82+
protectedfunctioncreateListener($container,$id,$config,$userProvider)
83+
{
84+
$listenerId =$this->getListenerId();
85+
$listener =newDefinitionDecorator($listenerId);
86+
$listener->replaceArgument(2,$id);
87+
$listener->replaceArgument(3,newReference($this->createAuthenticationSuccessHandler($container,$id,$config)));
88+
$listener->replaceArgument(4,newReference($this->createAuthenticationFailureHandler($container,$id,$config)));
89+
$listener->replaceArgument(5,array_intersect_key($config,$this->options));
90+
91+
$listenerId .='.'.$id;
92+
$container->setDefinition($listenerId,$listener);
93+
94+
return$listenerId;
95+
}
96+
}

‎src/Symfony/Bundle/SecurityBundle/Resources/config/security_listeners.xml‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,20 @@
140140
<argument /><!-- x509 user-->
141141
<argument /><!-- x509 credentials-->
142142
<argumenttype="service"id="logger"on-invalid="null" />
143-
<argumenttype="service"id="event_dispatcher"on-invalid="null"/>
143+
<argumenttype="service"id="event_dispatcher"on-invalid="null" />
144+
</service>
145+
146+
<serviceid="security.authentication.listener.json"class="Symfony\Component\Security\Http\Firewall\UsernamePasswordJsonAuthenticationListener"public="false"abstract="true">
147+
<tagname="monolog.logger"channel="security" />
148+
<argumenttype="service"id="security.token_storage" />
149+
<argumenttype="service"id="security.authentication.manager" />
150+
<argument /><!-- Provider-shared Key-->
151+
<argumenttype="service"id="security.authentication.success_handler" />
152+
<argumenttype="service"id="security.authentication.failure_handler" />
153+
<argumenttype="collection" /><!-- Options-->
154+
<argumenttype="service"id="logger"on-invalid="null" />
155+
<argumenttype="service"id="event_dispatcher"on-invalid="null" />
156+
<argumenttype="service"id="property_accessor"on-invalid="null" />
144157
</service>
145158

146159
<serviceid="security.authentication.listener.remote_user"class="Symfony\Component\Security\Http\Firewall\RemoteUserAuthenticationListener"public="false"abstract="true">

‎src/Symfony/Bundle/SecurityBundle/SecurityBundle.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespaceSymfony\Bundle\SecurityBundle;
1313

14+
useSymfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\JsonLoginFactory;
1415
useSymfony\Component\HttpKernel\Bundle\Bundle;
1516
useSymfony\Component\DependencyInjection\ContainerBuilder;
1617
useSymfony\Bundle\SecurityBundle\DependencyInjection\Compiler\AddSecurityVotersPass;
@@ -42,6 +43,7 @@ public function build(ContainerBuilder $container)
4243
$extension =$container->getExtension('security');
4344
$extension->addSecurityListenerFactory(newFormLoginFactory());
4445
$extension->addSecurityListenerFactory(newFormLoginLdapFactory());
46+
$extension->addSecurityListenerFactory(newJsonLoginFactory());
4547
$extension->addSecurityListenerFactory(newHttpBasicFactory());
4648
$extension->addSecurityListenerFactory(newHttpBasicLdapFactory());
4749
$extension->addSecurityListenerFactory(newHttpDigestFactory());
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
namespaceSymfony\Bundle\SecurityBundle\Tests\Functional\Bundle\JsonLoginBundle\Controller;
13+
14+
/**
15+
* @author Kévin Dunglas <dunglas@gmail.com>
16+
*/
17+
class TestController
18+
{
19+
publicfunctionloginCheckAction()
20+
{
21+
thrownew \RuntimeException(sprintf('%s should never be called.',__FUNCTION__));
22+
}
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
namespaceSymfony\Bundle\SecurityBundle\Tests\Functional\Bundle\JsonLoginBundle;
13+
14+
useSymfony\Component\HttpKernel\Bundle\Bundle;
15+
16+
/**
17+
* @author Kévin Dunglas <dunglas@gmail.com>
18+
*/
19+
class JsonLoginBundleextends Bundle
20+
{
21+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
namespaceSymfony\Bundle\SecurityBundle\Tests\Functional;
13+
14+
/**
15+
* @author Kévin Dunglas <dunglas@gmail.com>
16+
*/
17+
class JsonLoginTestextends WebTestCase
18+
{
19+
publicfunctiontestJsonLoginSuccess()
20+
{
21+
$client =$this->createClient(array('test_case' =>'JsonLogin','root_config' =>'config.yml'));
22+
$client->request('POST','/chk',array(),array(),array(),'{"user": {"login": "dunglas", "password": "foo"}}');
23+
$this->assertEquals('http://localhost/',$client->getResponse()->headers->get('location'));
24+
}
25+
26+
publicfunctiontestJsonLoginFailure()
27+
{
28+
$client =$this->createClient(array('test_case' =>'JsonLogin','root_config' =>'config.yml'));
29+
$client->request('POST','/chk',array(),array(),array(),'{"user": {"login": "dunglas", "password": "bad"}}');
30+
$this->assertEquals('http://localhost/login',$client->getResponse()->headers->get('location'));
31+
}
32+
}
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+
returnarray(
13+
newSymfony\Bundle\SecurityBundle\SecurityBundle(),
14+
newSymfony\Bundle\FrameworkBundle\FrameworkBundle(),
15+
newSymfony\Bundle\SecurityBundle\Tests\Functional\Bundle\JsonLoginBundle\JsonLoginBundle(),
16+
);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
imports:
2+
-{ resource: ./../config/framework.yml }
3+
4+
security:
5+
encoders:
6+
Symfony\Component\Security\Core\User\User:plaintext
7+
8+
providers:
9+
in_memory:
10+
memory:
11+
users:
12+
dunglas:{ password: foo, roles: [ROLE_USER] }
13+
14+
firewalls:
15+
main:
16+
pattern:^/
17+
anonymous:true
18+
json_login:
19+
check_path:/mychk
20+
username_path:user.login
21+
password_path:user.password
22+
23+
access_control:
24+
-{ path: ^/foo, roles: ROLE_USER }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
login_check:
2+
path:/chk
3+
defaults:{ _controller: JsonLoginBundle:Test:loginCheck }

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp