8000 added information on AuthenticationFailureHandlerInterface by samsamm777 · Pull Request #3565 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

added information on AuthenticationFailureHandlerInterface #3565

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
8000
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
28 changes: 28 additions & 0 deletions cookbook/security/api_key_authentication.rst
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,34 @@ exception in ``refreshUser()``.
If you *do* want to store authentication data in the session so that
the key doesn't need to be sent on every request, see :ref:`cookbook-security-api-key-session`.

Handling Authentication Failure
-------------------------------

In order for your ``ApiKeyAuthentication`` to correctly display a 403
http status when either bad credentials or authentication fails you will
need to implement the :class:`Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface` on your
Authenticator. This will provide a method ``onAuthenticationFailure`` which
you can use to create an error ``Response``.

// src/Acme/HelloBundle/Security/ApiKeyAuthenticator.php
namespace Acme\HelloBundle\Security;

use Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;

class ApiKeyAuthenticator implements SimplePreAuthenticatorInterface, AuthenticationFailureHandlerInterface
{
//...

public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
Copy link
Member

Choose a reason for hiding this comment

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

use statements for Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface, Symfony\Component\HttpFoundation\Request and Symfony\Component\Security\Core\Exception\AuthenticationException are missing.

{
return new Response("Authentication Failed.", 403);
}
}

.. _cookbook-security-api-key-config:

Configuration
Expand Down
0