私は次のようなLoginControllerクラスを持っています:
class LoginController
{
protected $db;
public function __construct(PDO $db)
{
$this->db = $db;
}
public function login($username, $password)
{
//Query the database, and get back the password_hash, salt, and user id.
//Hash the incoming password with the salt.
//Compare the two hashes.
//return the logged in user object.
}
}
したがって、データベースからpassword_hashとsaltを取得した後、着信パスワードをハッシュする必要があるステップがあります。これは私には、別のコンポーネントによって処理されるべき2番目の責任のように思えます。おそらくPasswordHashingComponent(または持っている場合はより良い名前)。
abstract class PasswordHashingComponentAbstract
{
public abstract function hash($data, $salt = "");
public function verify($hash, $data, $salt = "")
{
return $this->hash($data, $salt) == $hash;
}
}
class MD5PasswordHashingComponent extends PasswordHashingComponentAbstract
{
public function hash($data, $salt = "")
{
return md5($data . md5($salt));
}
}
class SHA1PasswordHashingComponent extends PasswordHashingComponentAbstract
{
public function hash($data, $salt = "")
{
return sha1($data . sha1($salt));
}
}
次に、LoginControllerに依存性注入することができます。
public function __construct(PDO $db, PasswordHashingComponentAbstract $passwordhasher);
と
$loginController = new LoginController($db, new SHA1PasswordHashingComponent());
$loginController->login("username", "password");
だから私の質問は、これがやり過ぎかどうかです。
LoginControllerがログインとパスワードのハッシュおよび比較の両方を処理する場合、これは主要なSRP違反ですか?
私の見方では、このクラスが変更される可能性がある理由は2つあります。
- ログインの処理方法が変わる場合。
- パスワードをハッシュして比較する方法が変更された場合。
それで、それはSRPに違反しませんか?パスワードハッシュ部分の抽象化を処理できるより良い方法はありますか?
ありがとう!