Answer the question
In order to leave comments, you need to log in
How to properly arrange custom authentication in Symfony2?
I’ll make a reservation right away that the official documentation with an example of WSSE authentication did not really clarify the essence of the issue for me.
It is necessary to make authentication through the service API method, which looks like OpenID, but is not. Authentication mechanics:
1. Checking the user's authentication and, if he is not authenticated, then give him a message asking him to go to the authentication page. This is where my controller fires, displaying the region selection, and then redirecting the user to the desired API method.
2. On the page, he will have to select one option: region. When clicking on the button of the region he needs, the user gets to call a certain Action of the controller (or is it more correct to do it inside the authentication mechanism, but then how and where to redirect the user?), where the request is made to the API of the external service of the desired region (the region is remembered) to obtain a unique link authentication, after which the user is redirected to this link, where he will have to confirm the transfer of data to the site: an access token to the API of this very external service, a username in the external service, and an ID there.It is not clear here whether it will be correct to do this in the controller, or should this also be shifted to custom authentication? If you need to transfer, then how? After clicking on the region button from the user, nothing more is required before returning with the data to the site.
3. After confirmation, the user is redirected back to the site (in the first request, the external API is given the path where to redirect the user), to the page that receives data - token, username, ID.
4. The authentication system in the Listener creates a token, writes the incoming data to it, and sends the token for verification:
protected function attemptAuthentication(Request $request)
{
if ($request->get($this->options['status_parameter'], null, true) == 'ok') {
// Наполнение токена данными
return $this->authenticationManager->authenticate($token);
}
}
$user = $this->userProvider->loadUserByUsername($token->getUsername());
if (!$user) {
$user = new User();
$user->setUsername($token->getUser());
//...
// Setting role
//...
}
public function authenticate(TokenInterface $token)
{
// Тут вышеописанная рутина
$authenticatedToken->setUser($user);
$authenticatedToken->setAuthenticated(true);
return $authenticatedToken;
}
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
Answer the question
In order to leave comments, you need to log in
Actually, you need to make an implementation through DI, because the provider itself is described in your services? Well, inject the entity manager into it.
Get something like this:
private $_em;
public function setEntityManager(EntityManager $em) {
$this->_em = $em;
}
public function authenticate(TokenInterface $token)
{
// Тут вышеописанная рутина
$this->_em->persist($user);
$this->_em->flush();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question