dimanche 23 septembre 2018

Symfony - Login on testing

I have issues trying to login on testing. Following the documentation, I did it this way :

private function logIn()
    {
        $session = $this->client->getContainer()->get('session');

        $firewallName = 'main';
        // if you don't define multiple connected firewalls, the context defaults to the firewall name
        // See https://symfony.com/doc/current/reference/configuration/security.html#firewall-context
        $firewallContext = 'main';

        // you may need to use a different token class depending on your application.
        // for example, when using Guard authentication you must instantiate PostAuthenticationGuardToken
        $token = new UsernamePasswordToken('login', 'password&', $firewallName, array('ROLE_ADMIN'));
        $session->set('_security_'.$firewallContext, serialize($token));
        $session->save();

        $cookie = new Cookie($session->getName(), $session->getId());
        $this->client->getCookieJar()->set($cookie);
    }

Then in my testing function, I call the login function, request a private page of the website, but it does not work. When I print the crawler, I can see there is something wrong.

$this->logIn($encoder);
$crawler = $this->client->request('GET', '/projects');
var_dump($crawler); 
die();

Here is what I get in the cmd tool :

object(Symfony\Component\HttpFoundation\Session\Session)#683 (5) {
  ["storage":protected]=>
  object(Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage                                                                                                             )#558 (8) {
    ["savePath":"Symfony\Component\HttpFoundation\Session\Storage\MockFileSessio                                                                                                             nStorage":private]=>
    string(58) "C:\cygwin64\home\www\navitia-tools\var\cache\test/sessions"
    ["id":protected]=>
    string(64) "734637027e955d1004e3231434f77395fba31b2dca8d8e3f1cd1ac288b1d4e94                                                                                                             "
    ["name":protected]=>
    string(10) "MOCKSESSID"
    ["started":protected]=>
    bool(false)
    ["closed":protected]=>
    bool(false)
    ["data":protected]=>
    array(2) {
      ["_sf2_attributes"]=>
      &array(2) {
        ["_security.main.target_path"]=>
        string(17) "http://localhost/"
        ["_security_main"]=>
        string(324) "C:74:"Symfony\Component\Security\Core\Authentication\Token\                                                                                                             UsernamePasswordToken":236:{a:3:{i:0;s:10:"password";i:1;s:4:"main";i:2;s:180:                                                                                                             "a:4:{i:0;s:5:"admin";i:1;b:1;i:2;a:1:{i:0;O:41:"Symfony\Component\Security\Core                                                                                                             \Role\Role":1:{s:47:"Symfony\Component\Security\Core\Role\Rolerole";s:10:"ROLE_A                                                                                                             DMIN";}}i:3;a:0:{}}";}}"
      }
      ["_sf2_meta"]=>
      &array(3) {
        ["u"]=>
        int(1537770433)
        ["c"]=>
        int(1537770433)
        ["l"]=>
        string(1) "0"
      }
    }
    ["metadataBag":protected]=>
    object(Symfony\Component\HttpFoundation\Session\Storage\MetadataBag)#719 (5)                                                                                                              {
      ["name":"Symfony\Component\HttpFoundation\Session\Storage\MetadataBag":pri                                                                                                             vate]=>
      string(10) "__metadata"
      ["storageKey":"Symfony\Component\HttpFoundation\Session\Storage\MetadataBa                                                                                                             g":private]=>
      string(9) "_sf2_meta"
      ["meta":protected]=>
      &array(3) {
        ["u"]=>
        int(1537770433)
        ["c"]=>
        int(1537770433)
        ["l"]=>
        string(1) "0"
      }
      ["lastUsed":"Symfony\Component\HttpFoundation\Session\Storage\MetadataBag"                                                                                                             :private]=>
      int(1537770433)
      ["updateThreshold":"Symfony\Component\HttpFoundation\Session\Storage\Metad                                                                                                             ataBag":private]=>
      int(0)
    }
    ["bags":protected]=>
    array(2) {
      ["attributes"]=>
      object(Symfony\Component\HttpFoundation\Session\SessionBagProxy)#684 (3) {
        ["bag":"Symfony\Component\HttpFoundation\Session\SessionBagProxy":privat                                                                                                             e]=>
        object(Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag)#                                                                                                             724 (3) {
          ["name":"Symfony\Component\HttpFoundation\Session\Attribute\AttributeB                                                                                                             ag":private]=>
          string(10) "attributes"
          ["storageKey":"Symfony\Component\HttpFoundation\Session\Attribute\Attr                                                                                                             ibuteBag":private]=>
          string(15) "_sf2_attributes"
          ["attributes":protected]=>
          &array(2) {
            ["_security.main.target_path"]=>
            string(17) "http://localhost/"
            ["_security_main"]=>
            string(324) "C:74:"Symfony\Component\Security\Core\Authentication\To                                                                                                             ken\UsernamePasswordToken":236:{a:3:{i:0;s:10:"password";i:1;s:4:"main";i:2;s:                                                                                                             180:"a:4:{i:0;s:5:"admin";i:1;b:1;i:2;a:1:{i:0;O:41:"Symfony\Component\Security\                                                                                                             Core\Role\Role":1:{s:47:"Symfony\Component\Security\Core\Role\Rolerole";s:10:"RO                                                                                                             LE_ADMIN";}}i:3;a:0:{}}";}}"
          }
        }
        ["data":"Symfony\Component\HttpFoundation\Session\SessionBagProxy":priva                                                                                                             te]=>
        &array(2) {
          ["_sf2_attributes"]=>
          &array(2) {
            ["_security.main.target_path"]=>
            string(17) "http://localhost/"
            ["_security_main"]=>
            string(324) "C:74:"Symfony\Component\Security\Core\Authentication\To                                                                                                             ken\UsernamePasswordToken":236:{a:3:{i:0;s:10:"password";i:1;s:4:"main";i:2;s:                                                                                                             180:"a:4:{i:0;s:5:"admin";i:1;b:1;i:2;a:1:{i:0;O:41:"Symfony\Component\Security\                                                                                                             Core\Role\Role":1:{s:47:"Symfony\Component\Security\Core\Role\Rolerole";s:10:"RO                                                                                                             LE_ADMIN";}}i:3;a:0:{}}";}}"
          }
          ["_symfony_flashes"]=>
          &array(0) {
          }
        }
        ["usageIndex":"Symfony\Component\HttpFoundation\Session\SessionBagProxy"                                                                                                             :private]=>
        &int(9)
      }
      ["flashes"]=>
      object(Symfony\Component\HttpFoundation\Session\SessionBagProxy)#717 (3) {
        ["bag":"Symfony\Component\HttpFoundation\Session\SessionBagProxy":privat                                                                                                             e]=>
        object(Symfony\Component\HttpFoundation\Session\Flash\FlashBag)#725 (3)                                                                                                              {
          ["name":"Symfony\Component\HttpFoundation\Session\Flash\FlashBag":priv                                                                                                             ate]=>
          string(7) "flashes"
          ["flashes":"Symfony\Component\HttpFoundation\Session\Flash\FlashBag":p                                                                                                             rivate]=>
          &array(0) {
          }
          ["storageKey":"Symfony\Component\HttpFoundation\Session\Flash\FlashBag                                                                                                             ":private]=>
          string(16) "_symfony_flashes"
        }
        ["data":"Symfony\Component\HttpFoundation\Session\SessionBagProxy":priva                                                                                                             te]=>
        &array(2) {
          ["_sf2_attributes"]=>
          &array(2) {
            ["_security.main.target_path"]=>
            string(17) "http://localhost/"
            ["_security_main"]=>
            string(324) "C:74:"Symfony\Component\Security\Core\Authentication\To                                                                                                             ken\UsernamePasswordToken":236:{a:3:{i:0;s:10:"password";i:1;s:4:"main";i:2;s:                                                                                                             180:"a:4:{i:0;s:5:"admin";i:1;b:1;i:2;a:1:{i:0;O:41:"Symfony\Component\Security\                                                                                                             Core\Role\Role":1:{s:47:"Symfony\Component\Security\Core\Role\Rolerole";s:10:"RO                                                                                                             LE_ADMIN";}}i:3;a:0:{}}";}}"
          }
          ["_symfony_flashes"]=>
          &array(0) {
          }
        }
        ["usageIndex":"Symfony\Component\HttpFoundation\Session\SessionBagProxy"                                                                                                             :private]=>
        &int(9)
      }
    }
  }
  ["flashName":"Symfony\Component\HttpFoundation\Session\Session":private]=>
  string(7) "flashes"
  ["attributeName":"Symfony\Component\HttpFoundation\Session\Session":private]=>
  string(10) "attributes"
  ["data":"Symfony\Component\HttpFoundation\Session\Session":private]=>
  &array(2) {
    ["_sf2_attributes"]=>
    &array(2) {
      ["_security.main.target_path"]=>
      string(17) "http://localhost/"
      ["_security_main"]=>
      string(324) "C:74:"Symfony\Component\Security\Core\Authentication\Token\Us                                                                                                             ernamePasswordToken":236:{a:3:{i:0;s:10:"password";i:1;s:4:"main";i:2;s:180:"a                                                                                                             :4:{i:0;s:5:"admin";i:1;b:1;i:2;a:1:{i:0;O:41:"Symfony\Component\Security\Core\R                                                                                                             ole\Role":1:{s:47:"Symfony\Component\Security\Core\Role\Rolerole";s:10:"ROLE_ADM                                                                                                             IN";}}i:3;a:0:{}}";}}"
    }
    ["_symfony_flashes"]=>
    &array(0) {
    }
  }
  ["usageIndex":"Symfony\Component\HttpFoundation\Session\Session":private]=>
  &int(9)
}

My security setup if this can help :

security:
    encoders:
        App\Entity\User:
            algorithm: bcrypt
    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
    providers:
        user:
            entity:
                class: App\Entity\User
                property: username
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            # pattern:   ^/
            anonymous: ~
            provider: user
            form_login:
                login_path: login
                check_path: login
                provider: user
            logout:
                path:   /logout
                target: /
            access_denied_handler: App\Security\AccessDeniedHandler
    access_control:
        - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/, roles: [ROLE_ADMIN, ROLE_USER] }

Your help would be very welcome,

Thanks for your support,

Aucun commentaire:

Enregistrer un commentaire