How to attach a password encoder to a User Interface in Silex?

岁酱吖の 提交于 2019-12-07 05:59:24

问题


So I am trying to create a new Silex application and use the Security bundle included. For simplicities sake I was going to go with the basic password encoding.

Per the Silex documentation http://silex.sensiolabs.org/doc/providers/security.html I have created a custom User Provider. However this user interface does not seem to use the default password encoding.

I can successfully get a password out of

$password = $app['security.encoder.digest']->encodePassword('foo');

However when I use the example

// find the encoder for a UserInterface instance
$encoder = $app['security.encoder_factory']->getEncoder($user);

// compute the encoded password for foo
$password = $encoder->encodePassword('foo', $user->getSalt());

I get the

RuntimeException: No encoder has been configured for account

In symfony2, I would use something like the following

encoders:
        somename:
            class: Acme\DemoBundle\Entity\User
        Acme\DemoBundle\Entity\User: sha512
        Acme\DemoBundle\Entity\User: plaintext
        Acme\DemoBundle\Entity\User:
            algorithm: sha512
            encode_as_base64: true
            iterations: 5000
        Acme\DemoBundle\Entity\User:
            id: my.custom.encoder.service.id

But that doesnt seem to be the case here. I can't seem to find any type of setEncoder method so I am a bit stumped.


回答1:


You need to reconstruct the EncoderFactory to add your custom implementation:

<?php

$app = new Silex\Application();
$app['myapp.encoder.base64'] = new Base64PasswordEncoder();
$app['security.encoder_factory'] = $app->share(function ($app) {
    return new EncoderFactory(
        array(
            'Symfony\Component\Security\Core\User\UserInterface' => $app['security.encoder.digest'],
            'MyApp\Model\UserInterface'                          => $app['myapp.encoder.base64'],
        )
    );
});

(oh and please, don't use a Base64Encoder() for password ;))




回答2:


I was able to use the accepted answer to fix my problem, but I couldn't assign it to security.encoder_factory directly, so I'm just sharing what I found to work.

instead of:

$app['security.encoder_factory'] = $app->share(function($app) {
    //..see above...//
});

I had to use:

$app->register(new Silex\Provider\SecurityServiceProvider(),array(
    'security.encoder_factory' => $app->share(function($app) {
    //... same as above ...//
    })
));

I'm too new to Silex to know why it didn't work as above for me. My initial guess would be a version difference (question was asked over two years ago). I can assign to security.provider.default before the call to register the module, but I can't seem to assign to security.encoder_factory . I also seem to have to put security.firewalls in the register call.



来源:https://stackoverflow.com/questions/14220229/how-to-attach-a-password-encoder-to-a-user-interface-in-silex

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!