Skip to content

Commit 0eae59c

Browse files
committed
Documented passport attributes
1 parent ca8621d commit 0eae59c

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

security/experimental_authenticators.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,3 +502,40 @@ authenticator, you would initialize the passport like this::
502502
]);
503503
}
504504
}
505+
506+
.. tip::
507+
508+
Besides badges, passports can define attributes, which allows the
509+
``authenticate()`` method to store arbitrary information in the
510+
passport to access it from other authenticator methods (e.g.
511+
``createAuthenticatedToken()``)::
512+
513+
// ...
514+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
515+
516+
class LoginAuthenticator extends AbstractAuthenticator
517+
{
518+
// ...
519+
520+
public function authenticate(Request $request): PassportInterface
521+
{
522+
// ... process the request
523+
524+
$passport = new SelfValidatingPassport($username, []);
525+
526+
// set a custom attribute (e.g. scope)
527+
$passport->setAttribute('scope', $oauthScope);
528+
529+
return $passport;
530+
}
531+
532+
public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface
533+
{
534+
// read the attribute value
535+
return new CustomOauthToken($passport->getUser(), $passport->getAttribute('scope'));
536+
}
537+
}
538+
539+
.. versionadded:: 5.2
540+
541+
Passport attributes were introduced in Symfony 5.2.

0 commit comments

Comments
 (0)