Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually encrypt user password in Symfony php

I have a need to manually encrypt the password in the same way Symfony does it.

$user->setPlainPassword($password);
$userManager->updateUser($user);

This is application code that saves the user password. But it is obviously encrypted (not plain as method says).

How can I manually get the same result for given password?

EDIT1:

The problem is that I want to authenticate user manually in users controller code. I have readable username and password that comes as parameters. I want to return 401 if they to not exist in database.

like image 779
eomeroff Avatar asked Nov 27 '22 07:11

eomeroff


2 Answers

Symfony comes with a useful command to encode a password the same way it does:

bin/console security:encode-password 'your_plain_password' 'AppBundle\Entity\YourUserClass'
like image 51
Kévin Dunglas Avatar answered Nov 30 '22 22:11

Kévin Dunglas


Try this in your controller action:

$encoder = $this->get('security.encoder_factory')->getEncoder($userClass);
$encodedPassword = $encoder->encodePassword($plainPassword);
like image 21
miikes Avatar answered Dec 01 '22 00:12

miikes