Skip to content

[Security] Documented passport attributes #14552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions security/experimental_authenticators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -502,3 +502,40 @@ authenticator, you would initialize the passport like this::
]);
}
}

.. tip::

Besides badges, passports can define attributes, which allows the
``authenticate()`` method to store arbitrary information in the
passport to access it from other authenticator methods (e.g.
``createAuthenticatedToken()``)::

// ...
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

class LoginAuthenticator extends AbstractAuthenticator
{
// ...

public function authenticate(Request $request): PassportInterface
{
// ... process the request

$passport = new SelfValidatingPassport($username, []);

// set a custom attribute (e.g. scope)
$passport->setAttribute('scope', $oauthScope);

return $passport;
}

public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface
{
// read the attribute value
return new CustomOauthToken($passport->getUser(), $passport->getAttribute('scope'));
}
}

.. versionadded:: 5.2

Passport attributes were introduced in Symfony 5.2.