Skip to content

Commit

Permalink
feature #5907 Updating some places to use the new CustomUserMessageAu…
Browse files Browse the repository at this point in the history
…thenticationException (weaverryan)

This PR was merged into the 2.8 branch.

Discussion
----------

Updating some places to use the new CustomUserMessageAuthenticationException

| Q             | A
| ------------- | ---
| Doc fix?      | no
| New docs?     | yes
| Applies to    | 2.8+
| Fixed tickets | #5736

Commits
-------

3d67202 tweaks thanks to the guys
1eb5f23 Updating some places to use the new CustomUserMessageAuthenticationException
  • Loading branch information
weaverryan committed Nov 30, 2015
2 parents 3843cda + 3d67202 commit 40a52c8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
19 changes: 16 additions & 3 deletions cookbook/security/api_key_authentication.rst
Expand Up @@ -37,6 +37,7 @@ value and then a User object is created::
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface;
Expand Down Expand Up @@ -80,7 +81,9 @@ value and then a User object is created::
$username = $userProvider->getUsernameForApiKey($apiKey);

if (!$username) {
throw new AuthenticationException(
// CAUTION: this message will be returned to the client
// (so don't put any un-trusted messages / error strings here)
throw new CustomUserMessageAuthenticationException(
sprintf('API Key "%s" does not exist.', $apiKey)
);
}
Expand All @@ -101,6 +104,11 @@ value and then a User object is created::
}
}

.. versionadded:: 2.8
The ``CustomUserMessageAuthenticationException`` class is new in Symfony 2.8
and helps you return custom authentication messages. In 2.7 or earlier, throw
an ``AuthenticationException`` or any sub-class (you can still do this in 2.8).

Once you've :ref:`configured <cookbook-security-api-key-config>` everything,
you'll be able to authenticate by adding an apikey parameter to the query
string, like ``http://example.com/admin/foo?apikey=37b51d194a7513e45b56f6524f2d51f2``.
Expand Down Expand Up @@ -291,7 +299,11 @@ you can use to create an error ``Response``.
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
return new Response("Authentication Failed.", 403);
return new Response(
// this contains information about *why* authentication failed
// use it, or return your own message
strtr($exception->getMessageKey(), $exception->getMessageData())
, 403)
}
}
Expand Down Expand Up @@ -543,7 +555,8 @@ to see if the stored token has a valid User object that can be used::
}

if (!$username) {
throw new AuthenticationException(
// this message will be returned to the client
throw new CustomUserMessageAuthenticationException(
sprintf('API Key "%s" does not exist.', $apiKey)
);
}
Expand Down
19 changes: 15 additions & 4 deletions cookbook/security/custom_password_authenticator.rst
Expand Up @@ -39,7 +39,7 @@ the user::
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Http\Authentication\SimpleFormAuthenticatorInterface;
Expand All @@ -58,15 +58,19 @@ the user::
try {
$user = $userProvider->loadUserByUsername($token->getUsername());
} catch (UsernameNotFoundException $e) {
throw new AuthenticationException('Invalid username or password');
// CAUTION: this message will be returned to the client
// (so don't put any un-trusted messages / error strings here)
throw new CustomUserMessageAuthenticationException('Invalid username or password');
}

$passwordValid = $this->encoder->isPasswordValid($user, $token->getCredentials());

if ($passwordValid) {
$currentHour = date('G');
if ($currentHour < 14 || $currentHour > 16) {
throw new AuthenticationException(
// CAUTION: this message will be returned to the client
// (so don't put any un-trusted messages / error strings here)
throw new CustomUserMessageAuthenticationException(
'You can only log in between 2 and 4!',
100
);
Expand All @@ -80,7 +84,9 @@ the user::
);
}

throw new AuthenticationException('Invalid username or password');
// CAUTION: this message will be returned to the client
// (so don't put any un-trusted messages / error strings here)
throw new CustomUserMessageAuthenticationException('Invalid username or password');
}

public function supportsToken(TokenInterface $token, $providerKey)
Expand All @@ -95,6 +101,11 @@ the user::
}
}

.. versionadded:: 2.8
The ``CustomUserMessageAuthenticationException`` class is new in Symfony 2.8
and helps you return custom authentication messages. In 2.7 or earlier, throw
an ``AuthenticationException`` or any sub-class (you can still do this in 2.8).

How it Works
------------

Expand Down

0 comments on commit 40a52c8

Please sign in to comment.