How to migrate users passwords to Symfony FOSUserBundle table - symfony

I have an old website which I want to migrate to Symfony2 and use the FOSUserBundle.
My 'old' website's database stores encrypted passwords as follows:
sha1(\"$salt1$plain_text_password$salt2\")
However, I've not done this before and am not sure on how to go about doing it. Is my only option to somehow configure FOSUserBundle to use the same encryption as the old website? If so, where would I do this?

You can create a custom password encoder and override BasePasswordEncoder ::isPasswordValid() add your logic in it
example
class CustomPasswordEncoder extends BasePasswordEncoder
{
public function encodePassword($raw,$salt){
list($salt1,$salt2) = explode(",",$salt);
return sha1($salt1.$raw.$salt2); // your logic here
}
public function isPasswordValid($encoded,$raw,$salt)
{
return $this->comparePasswords(
$encoded,$this>encodePassword($raw,$salt));
}
}
make this class a service
service.yml
services:
custom-password-encoder:
class: path\to\CustomPasswordEncoder
and add this on your security.yml
security:
encoders:
FOS\UserBundle\Model\UserInterface: {id: custom-password-encoder}
you also need to change User::getSalt() to return the two salts separated by comma
example
Class User extends BaseUser
{
public function getSalt()
{
return "salt1,salt2";
}
}

Snippet for Magento migration password logic.
<?php
namespace AppBundle\Utils;
use Symfony\Component\Security\Core\Encoder\BasePasswordEncoder;
class CustomPasswordEncoder extends BasePasswordEncoder
{
public function encodePassword($raw, $salt)
{
$salt2 = base64_encode($salt.uniqid());
// logic from magento
return md5($salt2.$raw).":".$salt2;
}
public function isPasswordValid($encoded, $raw, $salt)
{
// magento logic
$hashArr = explode(':', $encoded);
$hashToValidate = md5($hashArr[1] . $raw);
return $this->comparePasswords(
$hashArr[0], // first piece of password
$hashToValidate // $salt.$password md5 hash
);
}
}

Related

How to use Symfony 4.1 Security / Authentication Providers without Doctrine / ORM

In my current app, we've chosen not to use Doctrine or an ORM. I'm attempting to use Symfony 4.1's authentication system to log people in. What I'd like to do is use PDO directly to fetch users from the database.
I'm following this guide: http://symfony.com/doc/current/security/custom_provider.html. In security.yaml, I've created a new provider entry and added it to the firewall:
providers:
db_provider:
id: App\Utility\Security\UserAuthenticationProvider
encoders:
App\Utility\Security\UserAuthenticationProvider: bcrypt
firewalls:
main:
pattern: ^/
form_login:
login_path: login
check_path: check_login
default_target_path: user
anonymous: ~
provider: db_provider
And I've created UserAuthenticationProvider:
class UserAuthenticationProvider implements UserProviderInterface {
private $config;
private $userDAO;
public function __construct() {
$this->config = new Config();
$this->userDAO = new MySqlUserDAO($this->config);
}
public function loadUserByUsername($username) {
$user = $this->userDAO->getUserByUsername();
...
return $user;
}
public function refreshUser(UserInterface $user) {
...
}
public function supportsClass($class) {
...
}
My userDAO returns an object that implements UserInterface.
So when I got to my route /login, I get my login form. However, no users are loaded from the database. I can see that my UserProviderInterface gets created (by using dump in the constructor), but loadUserByUsername does not.
Do I need to implement something else that uses my UserAuthenticationProvider and calls loadUserByUsername?
Is there perhaps a better way to do authentication in Symfony without using Doctrine?
UPDATE
I found this guide which is older but has a bit more detail / context.
I've changed my classes / configs like so (edited for brevity):
#security.yaml
security:
providers:
db_provider:
id: database_user_provider
main:
pattern: ^/
form_login:
provider: db_provider
login_path: login
check_path: check_login
default_target_path: do_some_stuff
.
#services.yaml
services:
database_user_provider:
class: App\Utility\Security\DatabaseUserProvider
.
class DatabaseUser
implements
UserInterface,
EquatableInterface
{
protected $user;
public function getUser(): User {
return $this->user;
}
public function setUser(User $user): void {
$this->user = $user;
}
public function getRoles() {
return array("ROLE_USER");
}
public function getPassword() {
return $this->getUser()->getPassword();
}
public function getUsername() {
return $this->getUser()->getUsername();
}
}
.
class DatabaseUserProvider implements UserProviderInterface {
private $config;
private $userDAO;
public function __construct() {
$this->config = new Config();
$this->userDAO = new MySqlUserDAO($this->config);
}
public function loadUserByUsername($username): UserInterface {
$user = $this->userDAO->getUserByUsername($username);
$dbUser = new DatabaseUser();
$dbUser->setUser($user);
return $dbUser;
}
public function refreshUser(UserInterface $user) {
return $this->loadUserByUsername($user->getUsername());
}
public function supportsClass($class) {
return DatabaseUser::class === $class;
}
}
So what each file is doing (in a nutshell is):
services.yaml: naming my class DatabaseUserProvider to the service name database_user_provider for use in security.yaml.
security.yaml: setting database_user_provider to the alias db_provider and adding that as the provider on the main firewall.
DatabaseUser: my 'entity' class to represent my user from the database. I have a dumb User class that just has a few properties (username, password) and getters / setters for those. It is set as a property for the DatabaseUser object.
DatabaseUserProvider: Loads my DatabaseUser objects from the database using the DAO and returns them. (more specifically, the DAO returns a user, which is added to a new DatabaseUser object, which is returned).
The DAO simply runs a sql query to get a single result from the user table. it then takes this result and populates a User Value Object and returns it.
Results
When I was using Doctrine (and following this guide for loading users from the database and this one for a login form), the login route on the SecurityController would handle both rendering of the form and processing of the login request. Somehow, Symfony / Doctrine was smart enough to automatically (using a listener maybe???) load the appropriate user entity from the DB and authenticate them against the password they provided (and then set the user token and redirect them to the page they were trying to access.)
When I bypass doctrine (and use my own DAOs) and the classes above, authentication still does not occur. I can see that an instance of DatabaseUserProvider is being created (by dumping some vars in the constructor), and the Security tab of the profilier shows database_user_provider as the provider. But that seems to be as far as it gets.
Question
It seems to me that DatabaseUserProvider::loadUserByUsername should be the next thing that should happen. From where does this method get called from? Do I need to be passing the username into the constructor and kick it off from there? Should I be using this class as a service in my Controller and call the method manually from the controller (something I did not have to do when using doctrine - none of this logic was in the controller)?

FOSUserBundle How to normalize UserName at login?

I use FOSUB with Symfony 3.2
I wish to "normalize" the username when the user logs in.
By "normalize", I mean I have a "NormalizeService" to be called before checking in DB if user exist.
The process would be :
$normalizedUsername = $NormalizeService->normalize($providedUsername);
logUser($normalizedUsername, $providedPassword); // this is a simple login as usual
What do you think is the best way to do this ? Create a custom UserProvider ? I'm not sure it's what I need.
Regards
Like the documentation of FOSUserBundle says:
FOSUserBundle stores canonicalized versions of the username and the email which are used when querying and checking for uniqueness.
You just need to create a class that implements CanonicalizerInterface
namespace AppBundle\Util;
use FOS\UserBundle\Util\CanonicalizerInterface;
class MyCanonicalizer implements CanonicalizerInterface
{
private $normalizeService;
public function __construct(NormalizeService $normalizeService)
{
$this->normalizeService = $normalizeService;
}
public function canonicalize($string)
{
if (null === $string) {
return null;
}
return $this->normalizeService->normalize($string);
}
}
register it as a service:
# app/config/services.yml
services:
app.my_canonicalizer:
class: AppBundle\Util\MyCanonicalizer
arguments: ['#normalize_service'] # your NormalizeService
public: false
and then configure FOSUserBundle to use it:
# app/config/config.yml
fos_user:
service:
email_canonicalizer: app.my_canonicalizer
You could create a PRE_SUBMIT FormEvent in which you normalize the username.
See this for more information.

symfony2: check isGranted for a route

supposed having certain route string like "/path/index.html" protected by firewall, how to chek whether current user is able to access it?
Thanks in advance!
I am sorry, I should have been more explicit: I have an array of route names and I construct a menu. A lot of users with different roles can access a page with this menu. The purpose is to show only accessible liks in this menu for a particular user.
Something like:
'security_context'->'user'->isGranted('/path/index.html')
This answer is based on your comments:
You should get the roles needed to access that route.to that you need access to the security.access_map service which is private.so it has to be injected directly.e.g: you can create a path_roles service like such that you can get the roles for a certain path:
namespace Acme\FooBundle;
class PathRoles
{
protected $accessMap;
public function __construct($accessMap)
{
$this->accessMap = $accessMap;
}
public function getRoles($path)
{ //$path is the path you want to check access to
//build a request based on path to check access
$request = Symfony\Component\HttpFoundation\Request::create($path, 'GET');
list($roles, $channel) = $this->accessMap->getPatterns($request);//get access_control for this request
return $roles;
}
}
now declare it as a service:
services:
path_roles:
class: 'Acme\FooBundle\PathRoles'
arguments: ['#security.access_map']
now use that service in your controller to get the roles for the path and construct your menu based on those roles and isGranted.i.e:
//code from controller
public function showAction(){
//do stuff and get the link path for the menu,store it in $paths
$finalPaths=array();
foreach($paths as $path){
$roles = $this->get('path_roles')->getRoles($path);
foreach($roles as $role){
$role = $role->getRole();//not sure if this is needed
if($this->get('security.context')->isGranted($role)){
$finalPaths[] = $path;
break;
}
}
//now construct your menu based on $finalPaths
}
}
You could use security.access_control configuration option:
securty:
access_control:
- { path: "^/path/index.html$", roles: ROLE_SOME_ROLE}
Or simply check that manually from within your controller:
class SomeController extends Controller {
public function indexAction() {
if (!$this->get('security.context')->isGranted(...)) {
throw new AccessDeniedException(...);
}
...
}
}

security.yml access_control match query param

I want to secure all urls that have the ?preview=true query string applied.
The following unfortunately does not work. I guess the request matcher looks at pathInfo only.
# app/config/security.yml
access_control:
- { path: (?|&)preview=true, role: ROLE_ADMIN }
Any hints are much appreciated.
Im pretty sure that access control only looks at the hierarchical part and not the query string. This is because query parameters are not meant to determine content, only to be filters on content. You will likely need to rework your URL structure to match these.
Solved it myself. As #Chausser said access control via query params is not possible in security.yml, so I've created a voter for that.
// src/Your/Bundle/Security/Voter/PreviewAccessVoter.php
namespace Your\Bundle\Security\Voter;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
/**
* voter that denies access if user has not the required role
* to access urls with preview query param set true
*/
class PreviewAccessVoter implements VoterInterface
{
protected $requestStack;
protected $requiredRole;
public function __construct(RequestStack $requestStack, $requiredRole)
{
$this->requestStack = $requestStack;
$this->requiredRole = $requiredRole;
}
public function supportsAttribute($attribute)
{
return true;
}
public function supportsClass($class)
{
return true;
}
public function vote(TokenInterface $token, $object, array $attributes)
{
if(filter_var($this->requestStack->getCurrentRequest()->query->get('preview'), FILTER_VALIDATE_BOOLEAN))
{
foreach($token->getRoles() as $role) {
if($this->requiredRole === $role->getRole()) {
return VoterInterface::ACCESS_GRANTED;
}
}
return VoterInterface::ACCESS_DENIED;
}
return VoterInterface::ACCESS_ABSTAIN;
}
}
Add voter as (hidden) service
# src/Your/Bundle/Resources/config/services.yml
# preview voter that denies access for users without the required role
security.access.preview.voter:
class: Your\BundleBundle\Security\Voter\PreviewAccessVoter
arguments: [ #request_stack , ROLE_ADMIN ]
tags: [ { name: security.voter } ]
public: false
Changing the Access Decision Strategy
# app/config/security.yml
access_decision_manager:
strategy: unanimous # can be: affirmative, unanimous or consensus
see: http://symfony.com/doc/current/cookbook/security/voters.html as a reference
https://github.com/symfony/Security/blob/master/Core/Authorization/Voter/RoleVoter.php might also be helpful

Check login on User with multiple property in Symfony2

I'm building a multi-tenant application in Symfony2. For the secure "admin" area I have a custom entity provider (see: http://symfony.com/doc/current/cookbook/security/entity_provider.html)
However, it seems that Symfony2 only supports checking the entity on a single property.
my_entity_provider:
entity:
class: SecurityBundle:User
property: email
However, in my app a single User can have multiple accounts with the same email address. What I need is to also check for a tenant ID property when logging in.
my_entity_provider:
entity:
class: SecurityBundle:User
property: email, tenantID
I'm not sure how to accomplish this in Symfony2. I've been able to override the loadUsername method when creating a new User, but this isn't used by the login_check in Symfony2 security (and it is really ugly).
public function loadUserByUsername($username)
{
/* we create a concatenated string in the User entity to pass both
the email and tenantId values as the "username" */
$user_parts = explode("|", $username);
$q = $this->createQueryBuilder('u')
->where('u.tenantId = :tenantid AND u.email = :email')
->setParameter('tenantID', $user_parts[1])
->setParameter('email', $user_parts[0])
->getQuery();
try {
$user = $q->getSingleResult();
} catch (NoResultException $e) {
throw new UsernameNotFoundException(sprintf('Unable to find an active User object identified by "%s".', $username), null, 0, $e);
}
return $user;
}
Any guidance on implementing a custom security provider with multiple properties? Thanks!
You say you use a custom authentication provider, but I don't see where.
I think you use the default entity provider, which indeed, uses the associated entity Repositiory.
If you defined a property in configuration, it will use the repository ``findOneBy` method.
Otherwise, if you defined a custom repositoryClass, and this custom respository implements UserPrviderInterface, symfony will call loadUserByUsername
So, for you example to call you loadUserByUsername method, just remove property: email from your security.yml
Otherwise, there are many cleaner solutions that come to my mind, but the best to my mind is to
create your own user provider
It's a simple process:
create a class that implements UserProviderInterface
create a service for that class (f.e with id: app.security.user.provider)
configure security.yml to use this service
the class: (pseudo code, don't forget to add use statements, ...)
class UserProvider implements UserProviderInterface
{
public function __construct(ManagerRegistry $doctrine, $tenantIdProvider)
{
$this->doctrine = $doctrine;
$this->tenantIdProvider = $tenantIdProvider;
}
public function loadUserByUsername($username)
{
$this->doctrine->getRepository('App\Entity\User')->findOneBy([
'email' => $username,
'tenant' => $this->tenantIdProvider->getId(), // HERE $tenantIdProvider can be your listener for example.
]);
}
// more methods maybe?
}
the service (services.yml, app/config/config.yml, ... where you want)
services:
app.security.user.provider:
class: UserProvider
arguments:
- '#doctrine'
- '#app.tenant_id.provider' #maybe?
the security config
security:
providers:
user:
id: app.security.user.provider
I think overriding loadUserByUsername in your UserRepository, like you do, is the good thing to do in your case.
Maybe your problem is in the configuration :
Using the attribute "property", symfony doesn't use your own loadUserByUsername
So, simply remove the "property" line, and i hope all works :
my_entity_provider:
entity:
class: SecurityBundle:User
You've seen custom Authentication I assume? http://symfony.com/doc/current/cookbook/security/custom_authentication_provider.html#the-listener
Ignoring that this is for WSSE, this would allow you to create your own custom handle working with the tenant id and email address manually authenticating

Resources