I am using Symfony 2.6.6 and I am currently trying to use a salt on my database users. In the registration process I do the following hash while persist the user information via Doctrine.
src/AppBundle/Form/RegisterFormType.php
<?php
$account->setSalt(base_convert(sha1(uniqid(mt_rand(), true)), 16, 36));
$account->setPassword($this->encodePassword($account, $account->getPlainPassword()));
In that same file I have this function:
<?php
private function encodePassword(Account $account, $plainPassword)
{
$encoder = $this->container->get('security.encoder_factory')->getEncoder($account);
return $encoder->encodePassword($plainPassword, $account->getSalt());
}
This is my security.yml file:
app/config/security.yml
security:
encoders:
AppBundle\Entity\Account: bcrypt
providers:
database_users:
entity: { class: AppBundle:Account }
role_hierarchy:
ROLE_ADMIN: [ROLE_USER, ROLE_ALLOWED_TO_SWITCH]
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
firewalls:
dev:
pattern: ^/(_(profiler|wdt|error)|css|images|js)/
security: false
prod:
pattern: ^/
form_login:
check_path: account_login_check
login_path: account_login
csrf_provider: form.csrf_provider
logout:
path: account_logout
target: home
anonymous: ~
switch_user: ~
remember_me:
key: "%secret%"
access_control:
- { path: ^/account/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/account/register, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/account/logout, roles: IS_AUTHENTICATED_FULLY }
In my Entity, these are the salt keys:
src/AppBundle/Entity/Account.php
<?php // src/AppBundle/Entity/Account.php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\Role\Role;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Serializable;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Table(name="accounts")
* @ORM\Entity(repositoryClass="AppBundle\Entity\AccountRepository")
* @UniqueEntity(fields="username", message="That username is taken!")
* @UniqueEntity(fields="email", message="That email is taken!")
*/
class Account implements AdvancedUserInterface, Serializable
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(name="username", type="string", length=30)
* @Assert\NotBlank(message="Give us at least 3 characters")
* @Assert\Length(min=3, minMessage="Give us at least 3 characters!")
*/
private $username;
/**
* @ORM\Column(name="password", type="string", length=255)
*/
private $password;
/**
* @ORM\Column(name="salt", type="string")
*/
private $salt;
/**
* @ORM\Column(type="string", length=120)
* @Assert\NotBlank
* @Assert\Email
*/
private $email;
/**
* @ORM\Column(type="json_array")
*/
private $roles = array();
/**
* @ORM\Column(type="boolean")
*/
private $isActive = false;
/**
* @Assert\NotBlank
* @Assert\Regex(
* pattern="/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*$/",
* message="Use 1 upper case letter, 1 lower case letter, and 1 number"
* )
*/
private $plainPassword;
/**
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* @param string $username
* @return Account
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* @param string $email
* @return Account
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* @param string $password
* @return Account
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* @return string
*/
public function getPlainPassword()
{
return $this->plainPassword;
}
/**
* @param $plainPassword
* @return string
*/
public function setPlainPassword($plainPassword)
{
$this->plainPassword = $plainPassword;
return $this;
}
/**
* @return array Role
*/
public function getRoles()
{
$roles = $this->roles;
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
/**
* @param array $roles
* @return Role
*/
public function setRoles(array $roles)
{
$this->roles = $roles;
return $this;
}
/**
* Removes sensitive data from the user
*/
public function eraseCredentials()
{
$this->setPlainPassword(null);
}
/**
* @return string
*/
public function getSalt()
{
return $this->salt;
}
/**
* @param $salt
* @return Account
*/
public function setSalt($salt)
{
$this->salt = $salt;
return $this;
}
/**
* @return boolean
*/
public function getIsActive()
{
return $this->isActive;
}
/**
* @param boolean $isActive
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
}
public function isAccountNonExpired()
{
return true;
}
public function isAccountNonLocked()
{
return true;
}
public function isCredentialsNonExpired()
{
return true;
}
public function isEnabled()
{
return $this->getIsActive();
}
public function serialize()
{
return serialize(array(
$this->id,
$this->username,
$this->password,
$this->salt
));
}
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->password,
$this->salt
) = unserialize($serialized);
}
}
When I login, it works, but the Profiler tells me that Authenticated?
says no. When I change the salt to something different in the database, the user can still login.
Is there something I am missing from the Symfony documentation or do I need to change the whole way of salting?
EDIT: I am sorry I did not include that I serialize and unserialize $this->password, $this->salt
.
EDIT 2: Included app/config/security.yml
and posted full src/AppBundle/Entity/Account.php
file by user:ghanbari's request.
Thank you for reading my question.
First, your User
object must implement Serializable
interface, and you must serialize id & salt.
Read this.
EDIT: your entity class must be like this:
<?php // src/AppBundle/Entity/Account.php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\Role\Role;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Serializable;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Table(name="accounts")
* @ORM\Entity(repositoryClass="AppBundle\Entity\AccountRepository")
* @UniqueEntity(fields="username", message="That username is taken!")
* @UniqueEntity(fields="email", message="That email is taken!")
*/
class Account implements AdvancedUserInterface, Serializable
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(name="username", type="string", length=30)
* @Assert\NotBlank(message="Give us at least 3 characters")
* @Assert\Length(min=3, minMessage="Give us at least 3 characters!")
*/
private $username;
/**
* @ORM\Column(name="password", type="string", length=255)
*/
private $password;
/**
* @ORM\Column(name="salt", type="string")
*/
private $salt;
/**
* @ORM\Column(type="string", length=120)
* @Assert\NotBlank
* @Assert\Email
*/
private $email;
/**
* @ORM\Column(type="json_array")
*/
private $roles = array();
/**
* @ORM\Column(type="boolean")
*/
private $isActive = false;
/**
* @Assert\NotBlank
* @Assert\Regex(
* pattern="/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*$/",
* message="Use 1 upper case letter, 1 lower case letter, and 1 number"
* )
*/
private $plainPassword;
/**
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* @param string $username
* @return Account
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* @param string $email
* @return Account
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* @param string $password
* @return Account
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* @return string
*/
public function getPlainPassword()
{
return $this->plainPassword;
}
/**
* @param $plainPassword
* @return string
*/
public function setPlainPassword($plainPassword)
{
$this->plainPassword = $plainPassword;
return $this;
}
/**
* @return array Role
*/
public function getRoles()
{
$roles = $this->roles;
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
/**
* @param array $roles
* @return Role
*/
public function setRoles(array $roles)
{
$this->roles = $roles;
return $this;
}
/**
* Removes sensitive data from the user
*/
public function eraseCredentials()
{
$this->setPlainPassword(null);
}
/**
* @return string
*/
public function getSalt()
{
return null;
}
/**
* @param $salt
* @return Account
*/
public function setSalt($salt)
{
$this->salt = $salt;
return $this;
}
/**
* @return boolean
*/
public function getIsActive()
{
return $this->isActive;
}
/**
* @param boolean $isActive
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
}
public function isAccountNonExpired()
{
return true;
}
public function isAccountNonLocked()
{
return true;
}
public function isCredentialsNonExpired()
{
return true;
}
public function isEnabled()
{
return $this->getIsActive();
}
public function serialize()
{
return serialize(array(
$this->id,
$this->username,
$this->password,
$this->salt,
$this->isActive
));
}
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->password,
$this->salt,
$this->isActive
) = unserialize($serialized);
}
}