From 92fb4b12cc310b683f99cabf6add192d267a9b9d Mon Sep 17 00:00:00 2001 From: Sam Hudson Date: Tue, 11 Feb 2014 09:37:08 +0000 Subject: [PATCH 1/5] added information on AuthenticationFailureHandlerInterface in api keys docs --- cookbook/security/api_key_authentication.rst | 21 ++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/cookbook/security/api_key_authentication.rst b/cookbook/security/api_key_authentication.rst index c412bd234d6..9bf8ebf729a 100644 --- a/cookbook/security/api_key_authentication.rst +++ b/cookbook/security/api_key_authentication.rst @@ -210,6 +210,27 @@ 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 Exceptions +------------------- + +In order for you're ``ApiKeyAuthentication`` to correctly display a 403 http status when either bad credentials, or authentication fails you will need to implement the ``AuthenticationFailureHandlerInterface`` on your Authenticator. This will provide a method ``onAuthenticationFailure`` which you can then return a ``Response`` with. + + // src/Acme/HelloBundle/Security/ApiKeyAuthenticator.php + namespace Acme\HelloBundle\Security; + + use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface; + use Symfony\Component\HttpFoundation\Response; + + class ApiKeyAuthenticator implements SimplePreAuthenticatorInterface, AuthenticationFailureHandlerInterface + { + //... + + public function onAuthenticationFailure(Request $request, AuthenticationException $exception) + { + return new Response("Authentication Failed.", 403); + } + } + .. _cookbook-security-api-key-config: Configuration From 698825ffcc88d65539322d0ce053c8440239a038 Mon Sep 17 00:00:00 2001 From: Sam Hudson Date: Tue, 11 Feb 2014 14:10:12 +0000 Subject: [PATCH 2/5] fixed line wrapping --- cookbook/security/api_key_authentication.rst | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cookbook/security/api_key_authentication.rst b/cookbook/security/api_key_authentication.rst index 9bf8ebf729a..ccfcc82d13f 100644 --- a/cookbook/security/api_key_authentication.rst +++ b/cookbook/security/api_key_authentication.rst @@ -213,7 +213,11 @@ exception in ``refreshUser()``. Handling Exceptions ------------------- -In order for you're ``ApiKeyAuthentication`` to correctly display a 403 http status when either bad credentials, or authentication fails you will need to implement the ``AuthenticationFailureHandlerInterface`` on your Authenticator. This will provide a method ``onAuthenticationFailure`` which you can then return a ``Response`` with. +In order for you're ``ApiKeyAuthentication`` to correctly display a 403 +http status when either bad credentials, or authentication fails you will +need to implement the ``AuthenticationFailureHandlerInterface`` on your +Authenticator. This will provide a method ``onAuthenticationFailure`` which +you can then return a ``Response`` with. // src/Acme/HelloBundle/Security/ApiKeyAuthenticator.php namespace Acme\HelloBundle\Security; From c5a87fa30dfd9bdd26fc5201421cba7fd964764e Mon Sep 17 00:00:00 2001 From: Sam Hudson Date: Wed, 26 Feb 2014 22:09:05 +0000 Subject: [PATCH 3/5] fixed authentication failed header --- cookbook/security/api_key_authentication.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cookbook/security/api_key_authentication.rst b/cookbook/security/api_key_authentication.rst index ccfcc82d13f..46a920bcf83 100644 --- a/cookbook/security/api_key_authentication.rst +++ b/cookbook/security/api_key_authentication.rst @@ -210,11 +210,11 @@ 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 Exceptions -------------------- +Handling Authentication Failure +------------------------------- -In order for you're ``ApiKeyAuthentication`` to correctly display a 403 -http status when either bad credentials, or authentication fails you will +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 ``AuthenticationFailureHandlerInterface`` on your Authenticator. This will provide a method ``onAuthenticationFailure`` which you can then return a ``Response`` with. From 9445676209b39508a55de770482df021857bd998 Mon Sep 17 00:00:00 2001 From: Sam Hudson Date: Wed, 5 Mar 2014 16:46:17 +0000 Subject: [PATCH 4/5] recommendations by xabbuh --- .gitignore | 1 + cookbook/security/api_key_authentication.rst | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000000..e43b0f98895 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/cookbook/security/api_key_authentication.rst b/cookbook/security/api_key_authentication.rst index 46a920bcf83..9c659744673 100644 --- a/cookbook/security/api_key_authentication.rst +++ b/cookbook/security/api_key_authentication.rst @@ -213,17 +213,20 @@ exception in ``refreshUser()``. 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 ``AuthenticationFailureHandlerInterface`` on your +In order for you're ``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 then return a ``Response`` with. +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 { From e915162ad48189edb311908b66b34f2084fe0eac Mon Sep 17 00:00:00 2001 From: Sam Hudson Date: Wed, 5 Mar 2014 16:52:04 +0000 Subject: [PATCH 5/5] fixed typo --- cookbook/security/api_key_authentication.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cookbook/security/api_key_authentication.rst b/cookbook/security/api_key_authentication.rst index 9c659744673..e27ef087c90 100644 --- a/cookbook/security/api_key_authentication.rst +++ b/cookbook/security/api_key_authentication.rst @@ -213,7 +213,7 @@ exception in ``refreshUser()``. Handling Authentication Failure ------------------------------- -In order for you're ``ApiKeyAuthentication`` to correctly display a 403 +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