support multi password hash verify

This commit is contained in:
Tokumeikoi
2020-01-31 21:54:17 +08:00
parent 9ff815f853
commit 241cbd3016
3 changed files with 20 additions and 2 deletions

View File

@ -93,7 +93,11 @@ class AuthController extends Controller
if (!$user) {
abort(500, '用户名或密码错误');
}
if (!password_verify($password, $user->password)) {
if (!$this->multiPasswordVerify(
$user->password_algo,
$password,
$user->password)
) {
abort(500, '用户名或密码错误');
}
@ -173,6 +177,7 @@ class AuthController extends Controller
}
$user = User::where('email', $request->input('email'))->first();
$user->password = password_hash($request->input('password'), PASSWORD_DEFAULT);
$user->password_algo = NULL;
if (!$user->save()) {
abort(500, '重置失败');
}
@ -181,4 +186,13 @@ class AuthController extends Controller
'data' => true
]);
}
private function multiPasswordVerify($algo, $password, $hash)
{
switch($algo) {
case 'md5': return md5($password) === $hash;
case 'sha256': return hash('sha256', $password) === $hash;
default: return password_hash($password, PASSWORD_DEFAULT) === $hash;
}
}
}