Skip to content

Fix Authenticator Class (getCredentials) example #7065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions security/guard_authentication.rst
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,16 @@ This requires you to implement six methods::
class TokenAuthenticator extends AbstractGuardAuthenticator
{
/**
* Called on every request. Return whatever credentials you want,
* or null to stop authentication.
* Called on every request. Return whatever credentials you want, which
* will be passed to getUser(). Returning null skips all other authentication
* steps. Throwing an AuthenticationException will cause authentication to fail,
* calling onAuthenticationFailure().
*/
public function getCredentials(Request $request)
{
if (!$token = $request->headers->get('X-AUTH-TOKEN')) {
// no token? Return null and no other methods will be called
return;
// No token? Cause authentication to fail.
throw new AuthenticationException();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about setting $token to null then?

Copy link
Contributor

@ogizanagi ogizanagi Oct 31, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think throwing the exception in getCredentials is more explicit about the fact this authenticator will not allow any request not having this header (and won't let other authenticators try).

}

// What you return here will be passed to getUser() as $credentials
Expand Down