Mapping Model User authentication to Sunday English

master
sundayenglish 4 weeks ago
parent 69a403fd68
commit b3fab2a4b6
  1. 35
      app/Auth/CustomUserProvider.php
  2. 49
      app/Auth/PassportUserRepository.php
  3. 16
      app/Providers/AuthServiceProvider.php
  4. 3
      config/auth.php

@ -0,0 +1,35 @@
<?php
namespace App\Auth;
use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Contracts\Auth\Authenticatable;
class CustomUserProvider extends EloquentUserProvider
{
public function validateCredentials(Authenticatable $user, array $credentials): bool
{
$plain = $credentials['password'];
$hashed = $user->getAuthPassword();
if ($this->isBcryptHash($hashed)) {
if ($this->hasher->check($plain, $hashed)) {
return true;
}
} else {
if (md5($plain) === $hashed) {
// Upgrade lên bcrypt
$user->password = $this->hasher->make($plain);
$user->save();
return true;
}
}
return false;
}
protected function isBcryptHash($hashedPassword): bool
{
return password_get_info($hashedPassword)['algo'] === PASSWORD_BCRYPT;
}
}

@ -0,0 +1,49 @@
<?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;
}
}

@ -5,8 +5,10 @@ namespace App\Providers;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Laravel\Passport\Passport; use Laravel\Passport\Passport;
use League\OAuth2\Server\AuthorizationServer; use League\OAuth2\Server\AuthorizationServer;
use Laravel\Passport\Bridge\UserRepository; use App\Auth\PassportUserRepository; // thêm dòng này
use Laravel\Passport\Bridge\RefreshTokenRepository; use Laravel\Passport\Bridge\RefreshTokenRepository;
use Illuminate\Support\Facades\Auth;
use App\Auth\CustomUserProvider; // thêm dòng này
use DateInterval; use DateInterval;
class AuthServiceProvider extends ServiceProvider class AuthServiceProvider extends ServiceProvider
@ -18,14 +20,20 @@ class AuthServiceProvider extends ServiceProvider
Passport::tokensExpireIn(now()->addHour()); Passport::tokensExpireIn(now()->addHour());
Passport::refreshTokensExpireIn(now()->addMonth()); Passport::refreshTokensExpireIn(now()->addMonth());
// Đăng ký CustomUserProvider cho login web
Auth::provider('custom', function ($app, array $config) {
return new CustomUserProvider($app['hash'], $config['model']);
});
// Passport custom repository cho API login
$this->app->afterResolving(AuthorizationServer::class, function ($server) { $this->app->afterResolving(AuthorizationServer::class, function ($server) {
$grant = new \League\OAuth2\Server\Grant\PasswordGrant( $grant = new \League\OAuth2\Server\Grant\PasswordGrant(
app(UserRepository::class), app(PassportUserRepository::class), // Custom Passport user repository
app(RefreshTokenRepository::class) app(RefreshTokenRepository::class)
); );
$grant->setRefreshTokenTTL(new DateInterval('P1M')); // 1 tháng $grant->setRefreshTokenTTL(new DateInterval('P1M')); // 1 tháng refresh token
$server->enableGrantType($grant, new DateInterval('PT1H')); // 1 giờ $server->enableGrantType($grant, new DateInterval('PT1H')); // 1 giờ access token
}); });
} }
} }

@ -66,7 +66,8 @@ return [
'providers' => [ 'providers' => [
'users' => [ 'users' => [
'driver' => 'eloquent', // 'driver' => 'eloquent',
'driver' => 'custom',
'model' => env('AUTH_MODEL', App\Models\User::class), 'model' => env('AUTH_MODEL', App\Models\User::class),
], ],

Loading…
Cancel
Save