Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit40a52c8

Browse files
committed
feature#5907 Updating some places to use the new CustomUserMessageAuthenticationException (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 |#5736Commits-------3d67202 tweaks thanks to the guys1eb5f23 Updating some places to use the new CustomUserMessageAuthenticationException
2 parents3843cda +3d67202 commit40a52c8

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

‎cookbook/security/api_key_authentication.rst

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ value and then a User object is created::
3737
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
3838
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
3939
use Symfony\Component\Security\Core\Exception\AuthenticationException;
40+
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
4041
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
4142
use Symfony\Component\Security\Core\User\UserProviderInterface;
4243
use Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface;
@@ -80,7 +81,9 @@ value and then a User object is created::
8081
$username = $userProvider->getUsernameForApiKey($apiKey);
8182

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

107+
..versionadded::2.8
108+
The ``CustomUserMessageAuthenticationException`` class is new in Symfony 2.8
109+
and helps you return custom authentication messages. In 2.7 or earlier, throw
110+
an ``AuthenticationException`` or any sub-class (you can still do this in 2.8).
111+
104112
Once you've:ref:`configured<cookbook-security-api-key-config>` everything,
105113
you'll be able to authenticate by adding an apikey parameter to the query
106114
string, like ``http://example.com/admin/foo?apikey=37b51d194a7513e45b56f6524f2d51f2``.
@@ -291,7 +299,11 @@ you can use to create an error ``Response``.
291299
292300
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
293301
{
294-
return new Response("Authentication Failed.", 403);
302+
return new Response(
303+
// this contains information about *why* authentication failed
304+
// use it, or return your own message
305+
strtr($exception->getMessageKey(), $exception->getMessageData())
306+
, 403)
295307
}
296308
}
297309
@@ -543,7 +555,8 @@ to see if the stored token has a valid User object that can be used::
543555
}
544556

545557
if (!$username) {
546-
throw new AuthenticationException(
558+
// this message will be returned to the client
559+
throw new CustomUserMessageAuthenticationException(
547560
sprintf('API Key "%s" does not exist.', $apiKey)
548561
);
549562
}

‎cookbook/security/custom_password_authenticator.rst

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ the user::
3939
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
4040
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
4141
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
42-
use Symfony\Component\Security\Core\Exception\AuthenticationException;
42+
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
4343
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
4444
use Symfony\Component\Security\Core\User\UserProviderInterface;
4545
use Symfony\Component\Security\Http\Authentication\SimpleFormAuthenticatorInterface;
@@ -58,15 +58,19 @@ the user::
5858
try {
5959
$user = $userProvider->loadUserByUsername($token->getUsername());
6060
} catch (UsernameNotFoundException $e) {
61-
throw new AuthenticationException('Invalid username or password');
61+
// CAUTION: this message will be returned to the client
62+
// (so don't put any un-trusted messages / error strings here)
63+
throw new CustomUserMessageAuthenticationException('Invalid username or password');
6264
}
6365

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

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

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

8692
public function supportsToken(TokenInterface $token, $providerKey)
@@ -95,6 +101,11 @@ the user::
95101
}
96102
}
97103

104+
..versionadded::2.8
105+
The ``CustomUserMessageAuthenticationException`` class is new in Symfony 2.8
106+
and helps you return custom authentication messages. In 2.7 or earlier, throw
107+
an ``AuthenticationException`` or any sub-class (you can still do this in 2.8).
108+
98109
How it Works
99110
------------
100111

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp