Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manual authenticate user

Tags:

symfony

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.

like image 788
alexfv Avatar asked Dec 26 '11 15:12

alexfv


People also ask

What is an authenticate user?

User Authentication is a process that verifies a person's identity allowing them access to an online service, connected device, or other resource. Authenticating users occurs differently across services as business logic and risk profiles at enterprises can vary markedly.

What are 3 ways to authenticate a user?

There are three common factors used for authentication: Something you know (such as a password) Something you have (such as a smart card) Something you are (such as a fingerprint or other biometric method)

How do I manually set an authenticated user in Spring Security?

Simply put, Spring Security hold the principal information of each authenticated user in a ThreadLocal – represented as an Authentication object. In order to construct and set this Authentication object – we need to use the same approach Spring Security typically uses to build the object on a standard authentication.


1 Answers

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));

}
like image 190
Dmitriy Avatar answered Oct 12 '22 01:10

Dmitriy