0

How to manage Full authentication is required to access this resource.? I want to redirect user when he is not authenticated. I have custom uthenticater which authenticate user depending on session data, and i want to redirect user when hes not authenticatet.

My authenticator class:

/**
 * @Service("sso_authenticator")
 */
class SsoAuthenticator implements SimplePreAuthenticatorInterface
{

    /**
     * @var SsoUserProvider
     */
    protected $userProvider;

    /**
     * @InjectParams({
     *      "userProvider" = @Inject("sso_user_provider")
     * })
     */
    public function __construct(SsoUserProvider $userProvider)
    {
        $this->userProvider = $userProvider;
    }

    public function createToken(Request $request, $providerKey)
    {
        $user = $request->getSession()->get('sso_user');

        if (!$user) {
            throw new BadCredentialsException('No user found');
        }

        return new PreAuthenticatedToken(
                'anon.', $user, $providerKey
        );
    }

    public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
    {
        $user = $token->getCredentials();
        if (!is_array($user)) {
            $user = $token->getUser();
        }

        if (!$user) {
            throw new AuthenticationException('User does not exist.');
        }

        $ssoUser = $this->userProvider->loadUser($user);

        return new PreAuthenticatedToken(
                $ssoUser, $user, $providerKey, $ssoUser->getRoles()
        );
    }

    public function supportsToken(TokenInterface $token, $providerKey)
    {
        return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey;
    }

}

1 Answer 1

2

i set the login path to logout path like this:

secured_area:           
    form_login: 
        login_path : main_user_logout

then i wrote custom logout handler:

/**
 * @Service("sso_authentication_handler")
 */
class SsoAuthenticationHandler implements LogoutSuccessHandlerInterface
{
    /**
     * @var Router
     */
    private $router;

    /**
     * @var array
     */
    protected $ssoUrls;

    /**
     * @InjectParams({
     *      "ssoUrls" = @Inject("%wordpress_sso%"),
     *      "router" = @Inject("router")
     * })
     */
    public function __construct(array $ssoUrls, Router $router)
    {
        $this->ssoUrls = $ssoUrls;
        $this->router = $router;
    }

    public function onLogoutSuccess(Request $request)
    {
        $locale = $request->getLocale();
        if ($locale === 'pl') {
            $url = $this->ssoUrls[$locale];
        } else {
            $url = $this->ssoUrls['en'];
        }

        $url .= '?returnUrl=' . $this->router->generate('main');

        return new RedirectResponse($url);
    }

}

so with this combination i achive behavior like when youser is not authenticated or when he logout i will redirect him to other site to login, in my example to wordpress.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.