Gestion des mots de passes

dans Symfony 4.x

Robin Chalas

  • @Les-Tilleuls.coop
  • Symfony Core Team

 

GitHub : @chalasr

Twitter : @chalas_r

Built-in "encoders"

  • PBKDF2
  • BCrypt Native
  • Argon2i Sodium

NativePasswordEncoder

# config/packages/security.yaml

security:
    encoders:
        App\Security\User: 'native'
  • Utilise la fonction native password_hash()
  • Prend le meilleur algorithme disponible
    (Argon2id, Argon2i, BCrypt)
  • Remplace BCryptPasswordEncoder ("bcrypt")

SodiumPasswordEncoder

# config/packages/security.yaml

security:
    encoders:
        App\Security\User: 'sodium'
  • Utilise l'API bas niveau de l'extension PHP sodium
  • Remplace Argon2iPasswordEncoder ("argon2i")

Lequel choisir ?

Utiliser "auto"

et laisser Symfony faire le meilleur choix à votre place

 

# config/packages/security.yaml

security:
    encoders:
        App\Security\User: 'auto'
  • Uses sodium if supported, falls back to native

Built-in Opportunistic

Password Migrations

MigratingPasswordEncoder

  • Hash les passwords en utilisant le meilleur encoder disponible
  • Valide les passwords grâce à une chaine d'encoders
  • Câblé automatiquement si l'encoder est configuré à "auto"

PasswordUpgraderInterface

<?php

/**
 * @author Nicolas Grekas <p@tchwork.com>
 */
interface PasswordUpgraderInterface
{
    /**
     * Upgrades the encoded password of a user, typically for using a better hash algorithm.
     */
    public function upgradePassword(UserInterface $user, string $newEncodedPassword): void;
}
  • Cible les implémentations de UserProviderInterface
  • Implémenté par tous les user providers built-in Symfony

Utilisation dans Guard

<?php

use Symfony\Component\Security\Guard\AuthenticatorInterface;
use Symfony\Component\Security\Guard\PasswordAuthenticatedInterface;

class FormLoginAuthenticator implements PasswordAuthenticatedInterface
{
    // ...

    public function getPassword($credentials): ?string
    {        
        return $credentials['password'];
    }
}
  • Implémenter PasswordAuthenticatedInterface dans vos authenticators pour bénéficier automatiquement des migrations de passwords

Merci !

Symfony Security 4.x New Features

By Robin Chalas

Symfony Security 4.x New Features

  • 894