Skip to content

Commit bad6664

Browse files
committed
feature #7081 JSON authentication listener docs (dunglas, javiereguiluz)
This PR was merged into the master branch. Discussion ---------- JSON authentication listener docs Docs for symfony/symfony#18952. Commits ------- b192ab3 Fixed a syntax issue 16ae3f6 Reworded and simplified the article 820f28e Fixed the name of the "username" property 6d018f6 Fixed a syntax error ab5259b Removed a tip which is not too relevant for the article d2dd895 Fixed the JSON format (this time for real) 6ff24da Fixed the JSON format 0961128 Show the simple example first and then explain the complex use case 68bd9a5 JSON authentication listener docs
2 parents 9353c49 + b192ab3 commit bad6664

File tree

1 file changed

+200
-0
lines changed

1 file changed

+200
-0
lines changed

security/json_login_setup.rst

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
How to Build a JSON Authentication Endpoint
2+
===========================================
3+
4+
In this entry, you'll build a JSON endpoint to log in your users. Of course, when the
5+
user logs in, you can load your users from anywhere - like the database.
6+
See :ref:`security-user-providers` for details.
7+
8+
First, enable the JSON login under your firewall:
9+
10+
.. configuration-block::
11+
12+
.. code-block:: yaml
13+
14+
# app/config/security.yml
15+
security:
16+
# ...
17+
18+
firewalls:
19+
main:
20+
anonymous: ~
21+
json_login:
22+
check_path: /login
23+
24+
.. code-block:: xml
25+
26+
<!-- app/config/security.xml -->
27+
<?xml version="1.0" encoding="UTF-8"?>
28+
<srv:container xmlns="http://symfony.com/schema/dic/security"
29+
xmlns:srv="http://symfony.com/schema/dic/services"
30+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
31+
xsi:schemaLocation="http://symfony.com/schema/dic/services
32+
http://symfony.com/schema/dic/services/services-1.0.xsd">
33+
34+
<config>
35+
<firewall name="main">
36+
<anonymous />
37+
<json-login check-path="/login" />
38+
</firewall>
39+
</config>
40+
</srv:container>
41+
42+
.. code-block:: php
43+
44+
// app/config/security.php
45+
$container->loadFromExtension('security', array(
46+
'firewalls' => array(
47+
'main' => array(
48+
'anonymous' => null,
49+
'json_login' => array(
50+
'check_path' => '/login',
51+
),
52+
),
53+
),
54+
));
55+
56+
.. tip::
57+
58+
The ``check_path`` can also be a route name (but cannot have mandatory wildcards - e.g.
59+
``/login/{foo}`` where ``foo`` has no default value).
60+
61+
Now, when a request is made to the ``/login`` URL, the security system initiates
62+
the authentication process. You just need to define anywhere in your application
63+
an empty controller associated with that URL:
64+
65+
.. configuration-block::
66+
67+
.. code-block:: php-annotations
68+
69+
// src/AppBundle/Controller/SecurityController.php
70+
71+
// ...
72+
use Symfony\Component\HttpFoundation\Request;
73+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
74+
75+
class SecurityController extends Controller
76+
{
77+
/**
78+
* @Route("/login", name="login")
79+
*/
80+
public function loginAction(Request $request)
81+
{
82+
}
83+
}
84+
85+
.. code-block:: yaml
86+
87+
# app/config/routing.yml
88+
login:
89+
path: /login
90+
defaults: { _controller: AppBundle:Security:login }
91+
92+
.. code-block:: xml
93+
94+
<!-- app/config/routing.xml -->
95+
<?xml version="1.0" encoding="UTF-8" ?>
96+
<routes xmlns="http://symfony.com/schema/routing"
97+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
98+
xsi:schemaLocation="http://symfony.com/schema/routing
99+
http://symfony.com/schema/routing/routing-1.0.xsd">
100+
101+
<route id="login" path="/login">
102+
<default key="_controller">AppBundle:Security:login</default>
103+
</route>
104+
</routes>
105+
106+
.. code-block:: php
107+
108+
// app/config/routing.php
109+
use Symfony\Component\Routing\RouteCollection;
110+
use Symfony\Component\Routing\Route;
111+
112+
$collection = new RouteCollection();
113+
$collection->add('login', new Route('/login', array(
114+
'_controller' => 'AppBundle:Security:login',
115+
)));
116+
117+
return $collection;
118+
119+
Don't let this empty controller confuse you. When you submit a ``POST`` request
120+
to the ``/login`` URL with the following JSON document as body, the security
121+
system automatically handles it and takes care of checking the submitted
122+
username and password and authenticating the user or throwing an error:
123+
124+
.. code-block:: json
125+
126+
{
127+
"username": "dunglas",
128+
"password": "MyPassword"
129+
}
130+
131+
If the JSON document has a different structure, you can specify the path to
132+
access to the user and password properties using the ``username_path`` and
133+
``password_path`` keys (they default respectively to ``username`` and ``password``).
134+
135+
For example, if the JSON document has the following structure:
136+
137+
.. code-block:: json
138+
139+
{
140+
"security": {
141+
"credentials": {
142+
"login": "dunglas",
143+
"password": "MyPassword"
144+
}
145+
}
146+
}
147+
148+
The security configuration should be:
149+
150+
.. configuration-block::
151+
152+
.. code-block:: yaml
153+
154+
# app/config/security.yml
155+
security:
156+
# ...
157+
158+
firewalls:
159+
main:
160+
anonymous: ~
161+
json_login:
162+
check_path: login
163+
username_path: security.credentials.login
164+
password_path: security.credentials.password
165+
166+
.. code-block:: xml
167+
168+
<!-- app/config/security.xml -->
169+
<?xml version="1.0" encoding="UTF-8"?>
170+
<srv:container xmlns="http://symfony.com/schema/dic/security"
171+
xmlns:srv="http://symfony.com/schema/dic/services"
172+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
173+
xsi:schemaLocation="http://symfony.com/schema/dic/services
174+
http://symfony.com/schema/dic/services/services-1.0.xsd">
175+
176+
<config>
177+
<firewall name="main">
178+
<anonymous />
179+
<json-login check-path="login"
180+
username-path="security.credentials.login"
181+
password-path="security.credentials.password" />
182+
</firewall>
183+
</config>
184+
</srv:container>
185+
186+
.. code-block:: php
187+
188+
// app/config/security.php
189+
$container->loadFromExtension('security', array(
190+
'firewalls' => array(
191+
'main' => array(
192+
'anonymous' => null,
193+
'json_login' => array(
194+
'check_path' => 'login',
195+
'username_path' => 'security.credentials.login',
196+
'password_path' => 'security.credentials.password',
197+
),
198+
),
199+
),
200+
));

0 commit comments

Comments
 (0)