@@ -8,7 +8,7 @@ Whether you need to build a traditional login form, an API token authentication
8
8
or you need to integrate with some proprietary single-sign-on system, the Guard
9
9
component can make it easy... and fun!
10
10
11
- In this example, you'll build an API token authentication system... and learn how
11
+ In this example, you'll build an API token authentication system and learn how
12
12
to work with Guard.
13
13
14
14
Create a User and a User Provider
@@ -17,7 +17,7 @@ Create a User and a User Provider
17
17
No matter how you authenticate, you need to create a User class that implements ``UserInterface ``
18
18
and configure a :doc: `user provider </cookbook/security/custom_provider >`. In this
19
19
example, users are stored in the database via Doctrine, and each user has an ``apiKey ``
20
- property they can use to access their account via the API::
20
+ property they use to access their account via the API::
21
21
22
22
// src/AppBundle/Entity/User.php
23
23
namespace AppBundle\Entity;
@@ -76,7 +76,7 @@ property they can use to access their account via the API::
76
76
This User doesn't have a password, but you can add a ``password `` property if
77
77
you also want to allow this user to login with a password (e.g. via a login form).
78
78
79
- Your ``User `` class doesn't need to be store in Doctrine: do whatever you need.
79
+ Your ``User `` class doesn't need to be stored in Doctrine: do whatever you need.
80
80
Next, make sure you've configured a "user provider" for the user:
81
81
82
82
.. configuration-block ::
@@ -124,7 +124,7 @@ Next, make sure you've configured a "user provider" for the user:
124
124
'providers' => array(
125
125
'your_db_provider' => array(
126
126
'entity' => array(
127
- 'class' => 'AppBundle:User',
127
+ 'class' => 'AppBundle:User',
128
128
),
129
129
),
130
130
),
@@ -144,7 +144,7 @@ Suppose you have an API where your clients will send an ``X-AUTH-TOKEN`` header
144
144
on each request with their API token. Your job is to read this and find the associated
145
145
user (if any).
146
146
147
- To create a custom authentication system, just create a class an make it implement
147
+ To create a custom authentication system, just create a class and make it implement
148
148
:class: `Symfony\\ Component\\ Security\\ Guard\\ GuardAuthenticatorInterface `. Or, extend
149
149
the simpler :class: `Symfony\\ Component\\ Security\\ Guard\\ AbstractGuardAuthenticator `.
150
150
This requires you to implement six methods::
@@ -181,7 +181,7 @@ This requires you to implement six methods::
181
181
return;
182
182
}
183
183
184
- // What we return here will be passed to getUser() as $credentials
184
+ // What you return here will be passed to getUser() as $credentials
185
185
return array(
186
186
'token' => $token,
187
187
);
@@ -356,7 +356,7 @@ Finally, configure your ``firewalls`` key in ``security.yml`` to use this authen
356
356
),
357
357
));
358
358
359
- You did it! You now have a fully-working API token authentication system. If you're
359
+ You did it! You now have a fully-working API token authentication system. If your
360
360
homepage required ``ROLE_USER ``, then you could test it under different conditions:
361
361
362
362
.. code-block :: bash
@@ -398,7 +398,7 @@ Each authenticator needs the following methods:
398
398
399
399
**checkCredentials($credentials, UserInterface $user) **
400
400
If ``getUser() `` returns a User object, this method is called. Your job is to
401
- verify if the credentials are correct. For a login for , this is where you would
401
+ verify if the credentials are correct. For a login form , this is where you would
402
402
check that the password is correct for the user. To pass authentication, return
403
403
``true ``. If you return *anything * else
404
404
(or throw an :ref: `AuthenticationException <guard-customize-error >`),
@@ -410,7 +410,7 @@ Each authenticator needs the following methods:
410
410
that will be sent to the client or ``null `` to continue the request
411
411
(e.g. allow the route/controller to be called like normal). Since this
412
412
is an API where each request authenticates itself, you want to return
413
- ``nul ``.
413
+ ``null ``.
414
414
415
415
**onAuthenticationFailure(Request $request, AuthenticationException $exception) **
416
416
This is called if authentication fails. Your job
@@ -421,13 +421,15 @@ Each authenticator needs the following methods:
421
421
**start **
422
422
This is called if the client accesses a URI/resource that requires authentication,
423
423
but no authentication details were sent (i.e. you returned ``null `` from
424
- ``getCredentialsFromRequest () ``). Your job is to return a
424
+ ``getCredentials () ``). Your job is to return a
425
425
:class: `Symfony\\ Component\\ HttpFoundation\\ Response ` object that helps
426
426
the user authenticate (e.g. a 401 response that says "token is missing!").
427
427
428
428
**supportsRememberMe **
429
+ If you want to support "remember me" functionality, return true from this method.
430
+ You will still need to active ``rememebe_me `` under your firewall for it to work.
429
431
Since this is a stateless API, you do not want to support "remember me"
430
- functionality.
432
+ functionality in this example .
431
433
432
434
.. _guard-customize-error :
433
435
@@ -457,9 +459,9 @@ to cause a failure::
457
459
{
458
460
// ...
459
461
460
- if ($token == 'MickyMouse ') {
462
+ if ($token == 'ILuvAPIs ') {
461
463
throw new CustomUserMessageAuthenticationException(
462
- 'MickyMouse is not a real API key: he \'s a cartoon character '
464
+ 'ILuvAPIs is not a real API key: it \'s just a silly phrase '
463
465
);
464
466
}
465
467
@@ -469,13 +471,13 @@ to cause a failure::
469
471
// ...
470
472
}
471
473
472
- In this case, since "MickyMouse " is a ridiculous API key, you could include an easter
474
+ In this case, since "ILuvAPIs " is a ridiculous API key, you could include an easter
473
475
egg to return a custom message if someone tries this:
474
476
475
477
.. code-block :: bash
476
478
477
- curl -H " X-AUTH-TOKEN: MickyMouse " http://localhost:8000/
478
- # {"message":"MickyMouse is not a real API key: he 's a cartoon character "}
479
+ curl -H " X-AUTH-TOKEN: ILuvAPIs " http://localhost:8000/
480
+ # {"message":"ILuvAPIs is not a real API key: it 's just a silly phrase "}
479
481
480
482
Frequently Asked Questions
481
483
--------------------------
@@ -485,7 +487,7 @@ Frequently Asked Questions
485
487
"entry_point". This means you'll need to choose *which * authenticator's ``start() ``
486
488
method should be called when an anonymous user tries to access a protected resource.
487
489
For example, suppose you have an ``app.form_login_authenticator `` that handles
488
- a traditional form login. When a user access a protected page anonymously, you
490
+ a traditional form login. When a user accesses a protected page anonymously, you
489
491
want to use the ``start() `` method from the form authenticator and redirect them
490
492
to the login page (instead of returning a JSON response):
491
493
@@ -564,8 +566,8 @@ Frequently Asked Questions
564
566
));
565
567
566
568
**Can I use this with ``form_login``? **
567
- Yes! ``form_login `` is *one * way to authenticator a user, so you could use
568
- it *and * then add one more more authenticators. Use a guard authenticator doesn't
569
+ Yes! ``form_login `` is *one * way to authenticate a user, so you could use
570
+ it *and * then add one or more authenticators. Using a guard authenticator doesn't
569
571
collide with other ways to authenticate.
570
572
571
573
**Can I use this with FOSUserBundle? **
0 commit comments