4

I just went through this tutorial: http://symfony.com/doc/current/cookbook/security/api_key_authentication.html (including "Storing Authentication in the Session")

It works and authorizes users by an api key and successfully stores authentication in the Session.

But, I've no any ideas how to programmatically authenticate user through that authentication method.

I've tried something like:

$user = new User(
    'admin',
    null,
    ['ROLE_ADMIN']
);

$token = new PreAuthenticatedToken($user, null, "secured_area", $user->getRoles());
$this->get("security.token_storage")->setToken($token);

$request = $this->get("request");
$event = new InteractiveLoginEvent($request, $token);
$this->get("event_dispatcher")->dispatch("security.interactive_login", $event);

but it seems like it used wrong authentication provider.

Can please someone tell me what I doing wrong? (:

Updated:

When authentication was done by method above, in session token is stored under "default" firewall.

security:
    providers:
        api_key_user_provider:
            id: api_key_user_provider

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt|error)|css|images|js)/
            security: false

        secured_area:
            pattern: ^/admin
            simple_preauth:
                authenticator: apikey_authenticator

        default:
            anonymous: ~

Why instead of using "secured_area" firewall it uses "default"? How to properly force "secured_area" usage?

x2df2na
  • 55
  • 1
  • 5
  • Possible duplicate of [How to programmatically login/authenticate a user?](https://stackoverflow.com/questions/9550079/how-to-programmatically-login-authenticate-a-user) – Oleg Abrazhaev Jul 27 '17 at 12:30

1 Answers1

6

your user creation is not correct , you should use the user manager:

$userManager = $this->container->get('fos_user.user_manager');

// Create our user and set details
$user = $userManager->createUser();
$user->setUsername('username');
$user->setEmail('email@domain.com');
$user->setPlainPassword('password');
//$user->setPassword('encrypted_password');
$user->setEnabled(true);
$user->setRoles(array('ROLE_ADMIN'));

// Update the user
$userManager->updateUser($user, true);

Then you can authenticate user with this :

$token = new UsernamePasswordToken(
    $user,
    $user->getPassword(),
    'secured_area',
    $user->getRoles()
);

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

$request->getSession()->set('_security_secured_area', serialize($token));

Edit :

$token = new UsernamePasswordToken($user, $user->getPassword(), "secured_area", $user->getRoles());
$this->get("security.context")->setToken($token);

$event = new InteractiveLoginEvent($request, $token);
$this->get("event_dispatcher")->dispatch("security.interactive_login", $event);

You can do it like this in a more conventional way, let me know if it helps getting the right firewall.

btw i am not sure if this is already in your symfony version yet, but there is an easier way :

https://github.com/symfony/symfony/pull/13062

martin
  • 93,354
  • 25
  • 191
  • 226
Nawfal Serrar
  • 2,213
  • 1
  • 14
  • 22
  • or direct write to the session is the only way to solve the problem? And there are no any other ways to access different from current firewall? – x2df2na Mar 23 '15 at 19:58
  • I dont know any other programming way they do it, this is what people uses normally , this is how you can programically authenticate in Symfony2 – Nawfal Serrar Jan 19 '16 at 17:39
  • 3
    in the meantime `security.context` got deprecated so use `security.token_storage` check more here: http://symfony.com/blog/new-in-symfony-2-6-security-component-improvements – Herr Nentu' Sep 14 '16 at 15:57