Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 810599d

Browse files
committed
feature #42198 [Security] Deprecate PassportInterface (chalasr)
This PR was merged into the 5.4 branch. Discussion ---------- [Security] Deprecate `PassportInterface` | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | no | Deprecations? | yes | Tickets | - | License | MIT | Doc PR | - As explained in #42181, the right extension point is badges, not passports. Also renames `AuthenticatorInterface::createAuthenticatedToken()` to `createToken()` because of the signature change and the recent abandon of the `authenticated` state for tokens. Commits ------- a446030 [Security] Deprecate `PassportInterface`
2 parents bb68baf + a446030 commit 810599d

18 files changed

+192
-27
lines changed

UPGRADE-5.4.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,26 @@ Security
5252
* Deprecate `DeauthenticatedEvent`, use `TokenDeauthenticatedEvent` instead
5353
* Deprecate `CookieClearingLogoutHandler`, `SessionLogoutHandler` and `CsrfTokenClearingLogoutHandler`.
5454
Use `CookieClearingLogoutListener`, `SessionLogoutListener` and `CsrfTokenClearingLogoutListener` instead
55+
* Deprecate `AuthenticatorInterface::createAuthenticatedToken()`, use `AuthenticatorInterface::createToken()` instead
56+
* Deprecate `PassportInterface` and `UserPassportInterface`, use `Passport` instead.
57+
As such, the return type declaration of `AuthenticatorInterface::authenticate()` will change to `Passport` in 6.0
58+
59+
Before:
60+
```php
61+
class MyAuthenticator implements AuthenticatorInterface
62+
{
63+
public function authenticate(Request $request): PassportInterface
64+
{
65+
}
66+
}
67+
```
68+
69+
After:
70+
```php
71+
class MyAuthenticator implements AuthenticatorInterface
72+
{
73+
public function authenticate(Request $request): Passport
74+
{
75+
}
76+
}
77+
```

UPGRADE-6.0.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,29 @@ Security
328328
* Remove `DeauthenticatedEvent`, use `TokenDeauthenticatedEvent` instead
329329
* Remove `CookieClearingLogoutHandler`, `SessionLogoutHandler` and `CsrfTokenClearingLogoutHandler`.
330330
Use `CookieClearingLogoutListener`, `SessionLogoutListener` and `CsrfTokenClearingLogoutListener` instead
331+
* Remove `AuthenticatorInterface::createAuthenticatedToken()`, use `AuthenticatorInterface::createToken()` instead
332+
* Remove `PassportInterface` and `UserPassportInterface`, use `Passport` instead.
333+
Also, the return type declaration of `AuthenticatorInterface::authenticate()` was changed to `Passport`
334+
335+
Before:
336+
```php
337+
class MyAuthenticator implements AuthenticatorInterface
338+
{
339+
public function authenticate(Request $request): PassportInterface
340+
{
341+
}
342+
}
343+
```
344+
345+
After:
346+
```php
347+
class MyAuthenticator implements AuthenticatorInterface
348+
{
349+
public function authenticate(Request $request): Passport
350+
{
351+
}
352+
}
353+
```
331354

332355
SecurityBundle
333356
--------------

src/Symfony/Component/Ldap/Security/LdapAuthenticator.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1717
use Symfony\Component\Security\Core\Exception\AuthenticationException;
1818
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
19+
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
1920
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
2021

2122
/**
@@ -53,17 +54,31 @@ public function supports(Request $request): ?bool
5354
return $this->authenticator->supports($request);
5455
}
5556

56-
public function authenticate(Request $request): PassportInterface
57+
public function authenticate(Request $request): Passport
5758
{
5859
$passport = $this->authenticator->authenticate($request);
5960
$passport->addBadge(new LdapBadge($this->ldapServiceId, $this->dnString, $this->searchDn, $this->searchPassword, $this->queryString));
6061

6162
return $passport;
6263
}
6364

65+
/**
66+
* @deprecated since Symfony 5.4, use {@link createToken()} instead
67+
*/
6468
public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface
6569
{
66-
return $this->authenticator->createAuthenticatedToken($passport, $firewallName);
70+
trigger_deprecation('symfony/ldap', '5.4', 'Method "%s()" is deprecated, use "%s::createToken()" instead.', __METHOD__, __CLASS__);
71+
72+
return $this->createToken($passport, $firewallName);
73+
}
74+
75+
public function createToken(PassportInterface $passport, string $firewallName): TokenInterface
76+
{
77+
// @deprecated since Symfony 5.4, in 6.0 change to:
78+
// return $this->authenticator->createToken($passport, $firewallName);
79+
return method_exists($this->authenticator, 'createToken')
80+
? $this->authenticator->createToken($passport, $firewallName)
81+
: $this->authenticator->createAuthenticatedToken($passport, $firewallName);
6782
}
6883

6984
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response

src/Symfony/Component/Security/Core/Exception/AuthenticationExpiredException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Component\Security\Core\Exception;
1313

1414
/**
15-
* AuthenticationExpiredException is thrown when an authenticated token becomes un-authenticated between requests.
15+
* AuthenticationExpiredException is thrown when an authentication token becomes un-authenticated between requests.
1616
*
1717
* In practice, this is due to the User changing between requests (e.g. password changes),
1818
* causes the token to become un-authenticated.

src/Symfony/Component/Security/Http/Authentication/AuthenticatorManager.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,12 @@ public function __construct(iterable $authenticators, TokenStorageInterface $tok
7474
*/
7575
public function authenticateUser(UserInterface $user, AuthenticatorInterface $authenticator, Request $request, array $badges = []): ?Response
7676
{
77-
// create an authenticated token for the User
77+
// create an authentication token for the User
7878
// @deprecated since 5.3, change to $user->getUserIdentifier() in 6.0
79-
$token = $authenticator->createAuthenticatedToken($passport = new SelfValidatingPassport(new UserBadge(method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername(), function () use ($user) { return $user; }), $badges), $this->firewallName);
79+
$passport = new SelfValidatingPassport(new UserBadge(method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername(), function () use ($user) { return $user; }), $badges);
80+
$token = method_exists($authenticator, 'createToken') ? $authenticator->createToken($passport, $this->firewallName) : $authenticator->createAuthenticatedToken($passport, $this->firewallName);
8081

81-
// announce the authenticated token
82+
// announce the authentication token
8283
$token = $this->eventDispatcher->dispatch(new AuthenticationTokenCreatedEvent($token, $passport))->getAuthenticatedToken();
8384

8485
// authenticate this in the system
@@ -189,10 +190,10 @@ private function executeAuthenticator(AuthenticatorInterface $authenticator, Req
189190
throw new BadCredentialsException(sprintf('Authentication failed; Some badges marked as required by the firewall config are not available on the passport: "%s".', implode('", "', $missingRequiredBadges)));
190191
}
191192

192-
// create the authenticated token
193-
$authenticatedToken = $authenticator->createAuthenticatedToken($passport, $this->firewallName);
193+
// create the authentication token
194+
$authenticatedToken = method_exists($authenticator, 'createToken') ? $authenticator->createToken($passport, $this->firewallName) : $authenticator->createAuthenticatedToken($passport, $this->firewallName);
194195

195-
// announce the authenticated token
196+
// announce the authentication token
196197
$authenticatedToken = $this->eventDispatcher->dispatch(new AuthenticationTokenCreatedEvent($authenticatedToken, $passport))->getAuthenticatedToken();
197198

198199
if (true === $this->eraseCredentials) {

src/Symfony/Component/Security/Http/Authenticator/AbstractAuthenticator.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1515
use Symfony\Component\Security\Core\Exception\LogicException;
16+
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
1617
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
1718
use Symfony\Component\Security\Http\Authenticator\Passport\UserPassportInterface;
1819
use Symfony\Component\Security\Http\Authenticator\Token\PostAuthenticationToken;
@@ -30,12 +31,23 @@ abstract class AbstractAuthenticator implements AuthenticatorInterface
3031
*
3132
* @return PostAuthenticationToken
3233
*/
34+
public function createToken(Passport $passport, string $firewallName): TokenInterface
35+
{
36+
return new PostAuthenticationToken($passport->getUser(), $firewallName, $passport->getUser()->getRoles());
37+
}
38+
39+
/**
40+
* @deprecated since Symfony 5.4, use {@link createToken()} instead
41+
*/
3342
public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface
3443
{
44+
// @deprecated since Symfony 5.4
3545
if (!$passport instanceof UserPassportInterface) {
36-
throw new LogicException(sprintf('Passport does not contain a user, overwrite "createAuthenticatedToken()" in "%s" to create a custom authenticated token.', static::class));
46+
throw new LogicException(sprintf('Passport does not contain a user, overwrite "createToken()" in "%s" to create a custom authentication token.', static::class));
3747
}
3848

39-
return new PostAuthenticationToken($passport->getUser(), $firewallName, $passport->getUser()->getRoles());
49+
trigger_deprecation('symfony/security-http', '5.4', 'Method "%s()" is deprecated, use "%s::createToken()" instead.', __METHOD__, __CLASS__);
50+
51+
return $this->createToken($passport, $firewallName);
4052
}
4153
}

src/Symfony/Component/Security/Http/Authenticator/AbstractPreAuthenticatedAuthenticator.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\Security\Core\User\UserProviderInterface;
2323
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PreAuthenticatedUserBadge;
2424
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
25+
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
2526
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
2627
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
2728

@@ -84,7 +85,7 @@ public function supports(Request $request): ?bool
8485
return true;
8586
}
8687

87-
public function authenticate(Request $request): PassportInterface
88+
public function authenticate(Request $request): Passport
8889
{
8990
// @deprecated since 5.3, change to $this->userProvider->loadUserByIdentifier() in 6.0
9091
$method = 'loadUserByIdentifier';
@@ -100,7 +101,17 @@ public function authenticate(Request $request): PassportInterface
100101
);
101102
}
102103

104+
/**
105+
* @deprecated since Symfony 5.4, use {@link createToken()} instead
106+
*/
103107
public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface
108+
{
109+
trigger_deprecation('symfony/security-http', '5.4', 'Method "%s()" is deprecated, use "%s::createToken()" instead.', __METHOD__, __CLASS__);
110+
111+
return $this->createToken($passport, $firewallName);
112+
}
113+
114+
public function createToken(Passport $passport, string $firewallName): TokenInterface
104115
{
105116
return new PreAuthenticatedToken($passport->getUser(), null, $firewallName, $passport->getUser()->getRoles());
106117
}

src/Symfony/Component/Security/Http/Authenticator/AuthenticatorInterface.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\HttpFoundation\Response;
1616
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1717
use Symfony\Component\Security\Core\Exception\AuthenticationException;
18+
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
1819
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
1920

2021
/**
@@ -23,6 +24,10 @@
2324
* @author Ryan Weaver <[email protected]>
2425
* @author Amaury Leroux de Lens <[email protected]>
2526
* @author Wouter de Jong <[email protected]>
27+
*
28+
* @method TokenInterface createToken(Passport $passport, string $firewallName) Creates a token for the given user.
29+
* If you don't care about which token class is used, you can skip this method by extending
30+
* the AbstractAuthenticator class from your authenticator.
2631
*/
2732
interface AuthenticatorInterface
2833
{
@@ -47,8 +52,10 @@ public function supports(Request $request): ?bool;
4752
* a UserNotFoundException when the user cannot be found).
4853
*
4954
* @throws AuthenticationException
55+
*
56+
* @return Passport
5057
*/
51-
public function authenticate(Request $request): PassportInterface;
58+
public function authenticate(Request $request); /*: Passport;*/
5259

5360
/**
5461
* Create an authenticated token for the given user.
@@ -60,6 +67,8 @@ public function authenticate(Request $request): PassportInterface;
6067
* @see AbstractAuthenticator
6168
*
6269
* @param PassportInterface $passport The passport returned from authenticate()
70+
*
71+
* @deprecated since Symfony 5.4, use {@link createToken()} instead
6372
*/
6473
public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface;
6574

src/Symfony/Component/Security/Http/Authenticator/FormLoginAuthenticator.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function supports(Request $request): bool
7777
&& $this->httpUtils->checkRequestPath($request, $this->options['check_path']);
7878
}
7979

80-
public function authenticate(Request $request): PassportInterface
80+
public function authenticate(Request $request): Passport
8181
{
8282
$credentials = $this->getCredentials($request);
8383

@@ -106,9 +106,19 @@ public function authenticate(Request $request): PassportInterface
106106
}
107107

108108
/**
109-
* @param Passport $passport
109+
* @deprecated since Symfony 5.4, use {@link createToken()} instead
110110
*/
111111
public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface
112+
{
113+
trigger_deprecation('symfony/security-http', '5.4', 'Method "%s()" is deprecated, use "%s::createToken()" instead.', __METHOD__, __CLASS__);
114+
115+
return $this->createToken($passport, $firewallName);
116+
}
117+
118+
/**
119+
* @return UsernamePasswordToken
120+
*/
121+
public function createToken(Passport $passport, string $firewallName): TokenInterface
112122
{
113123
return new UsernamePasswordToken($passport->getUser(), null, $firewallName, $passport->getUser()->getRoles());
114124
}

src/Symfony/Component/Security/Http/Authenticator/HttpBasicAuthenticator.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,16 @@ public function authenticate(Request $request): PassportInterface
8484
}
8585

8686
/**
87-
* @param Passport $passport
87+
* @deprecated since Symfony 5.4, use {@link createToken()} instead
8888
*/
8989
public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface
90+
{
91+
trigger_deprecation('symfony/security-http', '5.4', 'Method "%s()" is deprecated, use "%s::createToken()" instead.', __METHOD__, __CLASS__);
92+
93+
return $this->createToken($passport, $firewallName);
94+
}
95+
96+
public function createToken(Passport $passport, string $firewallName): TokenInterface
9097
{
9198
return new UsernamePasswordToken($passport->getUser(), null, $firewallName, $passport->getUser()->getRoles());
9299
}

src/Symfony/Component/Security/Http/Authenticator/JsonLoginAuthenticator.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,17 @@ public function authenticate(Request $request): PassportInterface
110110
return $passport;
111111
}
112112

113+
/**
114+
* @deprecated since Symfony 5.4, use {@link createToken()} instead
115+
*/
113116
public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface
117+
{
118+
trigger_deprecation('symfony/security-http', '5.4', 'Method "%s()" is deprecated, use "%s::createToken()" instead.', __METHOD__, __CLASS__);
119+
120+
return $this->createToken($passport, $firewallName);
121+
}
122+
123+
public function createToken(Passport $passport, string $firewallName): TokenInterface
114124
{
115125
return new UsernamePasswordToken($passport->getUser(), null, $firewallName, $passport->getUser()->getRoles());
116126
}

src/Symfony/Component/Security/Http/Authenticator/Passport/PassportInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* passport.
2222
*
2323
* @author Wouter de Jong <[email protected]>
24+
*
25+
* @deprecated since Symfony 5.4, use {@link Passport} instead
2426
*/
2527
interface PassportInterface
2628
{

src/Symfony/Component/Security/Http/Authenticator/Passport/UserPassportInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* Represents a passport for a Security User.
1919
*
2020
* @author Wouter de Jong <[email protected]>
21+
*
22+
* @deprecated since Symfony 5.4, use {@link Passport} instead
2123
*/
2224
interface UserPassportInterface extends PassportInterface
2325
{

src/Symfony/Component/Security/Http/Authenticator/RememberMeAuthenticator.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
2323
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
2424
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
25+
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
2526
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
2627
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
2728
use Symfony\Component\Security\Http\RememberMe\RememberMeDetails;
@@ -95,7 +96,17 @@ public function authenticate(Request $request): PassportInterface
9596
}));
9697
}
9798

99+
/**
100+
* @deprecated since Symfony 5.4, use {@link createToken()} instead
101+
*/
98102
public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface
103+
{
104+
trigger_deprecation('symfony/security-http', '5.4', 'Method "%s()" is deprecated, use "%s::createToken()" instead.', __METHOD__, __CLASS__);
105+
106+
return $this->createToken($passport, $firewallName);
107+
}
108+
109+
public function createToken(Passport $passport, string $firewallName): TokenInterface
99110
{
100111
return new RememberMeToken($passport->getUser(), $firewallName, $this->secret);
101112
}

src/Symfony/Component/Security/Http/Authenticator/Token/PostAuthenticationToken.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function __construct(UserInterface $user, string $firewallName, array $ro
4040
}
4141

4242
/**
43-
* This is meant to be only an authenticated token, where credentials
43+
* This is meant to be only a token, where credentials
4444
* have already been used and are thus cleared.
4545
*
4646
* {@inheritdoc}

src/Symfony/Component/Security/Http/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* Deprecate `DeauthenticatedEvent`, use `TokenDeauthenticatedEvent` instead
99
* Deprecate `CookieClearingLogoutHandler`, `SessionLogoutHandler` and `CsrfTokenClearingLogoutHandler`.
1010
Use `CookieClearingLogoutListener`, `SessionLogoutListener` and `CsrfTokenClearingLogoutListener` instead
11+
* Deprecate `PassportInterface` and `UserPassportInterface`, use `Passport` instead
1112

1213
5.3
1314
---

src/Symfony/Component/Security/Http/Event/LoginSuccessEvent.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
/**
2525
* This event is dispatched after authentication has successfully completed.
2626
*
27-
* At this stage, the authenticator created an authenticated token
28-
* and generated an authentication success response. Listeners to
27+
* At this stage, the authenticator created a token and
28+
* generated an authentication success response. Listeners to
2929
* this event can do actions related to successful authentication
3030
* (such as migrating the password).
3131
*

0 commit comments

Comments
 (0)