13

I try to authenticate user:

<?php 

/**
 * @Route("/testLogin", name="testLogin")
 */
public function testLoginAction()
{
    $em = $this->getDoctrine()->getEntityManager();
    $user = $em->getRepository('ApplicationDefaultBundle:User')->findOneBy(array('id' => 126));

    $providerKey = 'main';
    $token = new UsernamePasswordToken($user, null, $providerKey, $user->getRoles());

    $this->container->get('security.context')->setToken($token);

    return $this->redirect($this->generateUrl('testCheck'));
}

/**
 * @Route("/testCheck", name="testCheck")
 */
public function testCheckAction()
{
    if (false === $this->get('security.context')->isGranted(
        'IS_AUTHENTICATED_REMEMBERED'
    )) {
        return new Response('Not logged');
    }
    $user = $this->container->get('security.context')->getToken()->getUser();

    return new Response($user->getUsername.' is logged');
}

But I get permanent 302 redirect to /login page.

security:
    encoders:
        Application\Bundle\DefaultBundle\Entity\User:
            algorithm:   sha512
            iterations: 24
            encode_as_base64: true

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SPECIALIST:  ROLE_USER
        ROLE_EMPLOYER:    ROLE_USER
        ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

    providers:
        main:
            entity: { class: Application\Bundle\DefaultBundle\Entity\User, property: username }

    firewalls:   
        secured_area:
            remember_me:
                key:      MySecretKeyBlablabla
                lifetime: 36000000
                path:     /
                domain:   ~
            pattern:    ^/
            form_login:
                check_path: /login_check
                login_path: /login
                provider: main
            logout:
                path:   /logout
                target: /
            anonymous: true

    access_control:
        - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }

Code for authenticate I take from https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Controller/RegistrationController.php

Error in app/logs/dev.log:

redirecting to authentication entry point (No Authentication Provider found for token of class "Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken".) [] []

I can't access to site until I clean cookies.

j0k
  • 22,600
  • 28
  • 79
  • 90
alexfv
  • 1,826
  • 2
  • 19
  • 22

2 Answers2

15

Try code from this answer. In your case firewall name is secured_area:

// your controller action
public function myAction()
{

    // Authenticating user
    $token = new UsernamePasswordToken($user, null, 'secured_area', $user->getRoles());
    $this->get('security.token_storage')->setToken($token);
    //For Symfony <= 2.3
    //$this->get('security.context')->setToken($token);
    $this->get('session')->set('_security_secured_area', serialize($token));

}
tenbits
  • 7,568
  • 5
  • 34
  • 53
Dmytro
  • 5,443
  • 2
  • 52
  • 50
0

Verify that you have configured encoders section in security.yml, as configuration there has changed not very long time ago and it affects your problem.

Anton Babenko
  • 6,586
  • 2
  • 36
  • 44