Quantcast
Channel: Active questions tagged symfony4 - Stack Overflow
Viewing all articles
Browse latest Browse all 3925

How to inject Response object to Symfony service during authentication

$
0
0

I am writing Symfony Cookie based Authenticator. After getting response from configured UserProvider (remote service call) I would need to set cookie in the final Response. I don't know how can I access Response object to add new Cookie to it's headers at this stage.

The code for adding a Cookie is normally like this:

$cookie = new Cookie('foo', 'bar', strtotime('Wed, 28-Dec-2016 15:00:00 +0100'), '/', '.example.com', true, true, true),$response->headers->setCookie(new Cookie('foo', 'bar'));

I need reference to $response

I do not want to create my own instance of Response and return it, since I would like to leave Response creation as it is, but I would only need this one cookie to be added to Response. What is best way to achieve this in Symfony 5?

This is simplified Authenticator code I am using:

<?phpnamespace App\Security;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;use Symfony\Component\Security\Core\Exception\AuthenticationException;use Symfony\Component\Security\Core\User\UserInterface;use Symfony\Component\Security\Core\User\UserProviderInterface;use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;class SessionCookieAuthenticator extends AbstractGuardAuthenticator{    public function supports(Request $request)    {        // check if php-sid cookie is provided        return !empty($request->cookies->get('php-sid'));    }    /**     * Credentials are global cookies     * @param Request $request     * @return mixed|string     */    public function getCredentials(Request $request)    {        $cookie = implode('; ', array_map(            function($k, $v) {                return $k . '=' . $v;            },            array_keys($_COOKIE),            $_COOKIE        ));        return $cookie;    }    public function getUser($credentials, UserProviderInterface $userProvider)    {        return $userProvider->loadUserByCookie($credentials);    }    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)    {        return null; // @todo set Cookie here for example. Can I get Response here?    }}

Viewing all articles
Browse latest Browse all 3925

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>