You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.3 KiB
49 lines
1.3 KiB
<?php
|
|
|
|
namespace App\Auth;
|
|
|
|
use Laravel\Passport\Bridge\UserRepository;
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface;
|
|
use Laravel\Passport\Bridge\User;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use App\Models\User as UserModel;
|
|
|
|
class PassportUserRepository extends UserRepository
|
|
{
|
|
public function getUserEntityByUserCredentials(
|
|
$username,
|
|
$password,
|
|
$grantType,
|
|
ClientEntityInterface $clientEntity
|
|
) {
|
|
$user = UserModel::where('email', $username)->first();
|
|
|
|
if (!$user) {
|
|
return null;
|
|
}
|
|
|
|
// Tránh lỗi Hash::check() với MD5
|
|
if ($this->isBcryptHash($user->password)) {
|
|
if (Hash::check($password, $user->password)) {
|
|
return new User($user->id);
|
|
}
|
|
} else {
|
|
// Hash không phải bcrypt, kiểm tra MD5 thủ công
|
|
if (md5($password) === $user->password) {
|
|
// Nâng cấp mật khẩu lên bcrypt
|
|
$user->password = Hash::make($password);
|
|
$user->save();
|
|
|
|
return new User($user->id);
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
// Thêm method kiểm tra thuật toán hash
|
|
protected function isBcryptHash($hashedPassword): bool
|
|
{
|
|
return password_get_info($hashedPassword)['algo'] === PASSWORD_BCRYPT;
|
|
}
|
|
}
|
|
|