Skip to content

Commit 048a3bf

Browse files
committed
minor #14552 [Security] Documented passport attributes (wouterj)
This PR was merged into the 5.2 branch. Discussion ---------- [Security] Documented passport attributes Fixes #13871 Commits ------- 0eae59c Documented passport attributes
2 parents 5664506 + 0eae59c commit 048a3bf

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

security/experimental_authenticators.rst

+37
Original file line numberDiff line numberDiff line change
@@ -538,3 +538,40 @@ authenticator, you would initialize the passport like this::
538538
]);
539539
}
540540
}
541+
542+
.. tip::
543+
544+
Besides badges, passports can define attributes, which allows the
545+
``authenticate()`` method to store arbitrary information in the
546+
passport to access it from other authenticator methods (e.g.
547+
``createAuthenticatedToken()``)::
548+
549+
// ...
550+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
551+
552+
class LoginAuthenticator extends AbstractAuthenticator
553+
{
554+
// ...
555+
556+
public function authenticate(Request $request): PassportInterface
557+
{
558+
// ... process the request
559+
560+
$passport = new SelfValidatingPassport($username, []);
561+
562+
// set a custom attribute (e.g. scope)
563+
$passport->setAttribute('scope', $oauthScope);
564+
565+
return $passport;
566+
}
567+
568+
public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface
569+
{
570+
// read the attribute value
571+
return new CustomOauthToken($passport->getUser(), $passport->getAttribute('scope'));
572+
}
573+
}
574+
575+
.. versionadded:: 5.2
576+
577+
Passport attributes were introduced in Symfony 5.2.

0 commit comments

Comments
 (0)