Files
portal-api/app/Http/Controllers/Api/Admin/Auth/AuthController.php
T
baodev0andClaude Opus 5.5 ea5697cb81 feat(security): argon2id password hashing and AES for SignRSA secrets
- Hash driver read from HASH_DRIVER (argon2id in production); bcrypt
  hashes still verify and are rehashed on successful admin/client login
  since the JWT guard and guard->validate() never rehash on their own.
- Client registration uses Hash::make instead of bcrypt(), which always
  forced bcrypt regardless of the configured driver.
- SignRSA now encrypts with Crypt (AES-256-CBC + HMAC, APP_KEY) instead
  of 3DES in ECB mode without IV. Legacy 3DES values are still readable
  via RSA_KEY/RSA_PUBLIC until `secrets:reencrypt` migrates them;
  RSA_PRIVATE is no longer used.
- LegacyPortalImporter stored module_vmware.password in plaintext; it is
  now encrypted, and a migration encrypts existing plaintext rows.

Deploy: php artisan migrate, then secrets:reencrypt --dry-run and
secrets:reencrypt; remove RSA_* from env once it exits 0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 19:56:38 +07:00

134 lines
4.3 KiB
PHP

<?php
namespace App\Http\Controllers\Api\Admin\Auth;
use App\Http\Controllers\Controller;
use App\Http\Requests\UpdateProfileRequest;
use App\Http\Resources\Api\Admin\AdminUserResource;
use App\Services\ActivityLogger;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\RateLimiter;
use Tymon\JWTAuth\Facades\JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
class AuthController extends Controller
{
private const LOGIN_MAX_ATTEMPTS = 5;
private const LOGIN_DECAY_SECONDS = 60;
public function __construct(private readonly ActivityLogger $activityLogger) {}
public function login(Request $request): JsonResponse
{
$request->validate([
'username' => 'required|string',
'password' => 'required|string',
]);
$throttleKey = 'admin-login:' . strtolower($request->string('username')->toString()) . '|' . $request->ip();
if (RateLimiter::tooManyAttempts($throttleKey, self::LOGIN_MAX_ATTEMPTS)) {
return response()->json([
'error' => true,
'message' => 'Bạn đã đăng nhập sai quá nhiều lần. Vui lòng thử lại sau ít phút.',
], 429);
}
$token = Auth::guard('admin')->attempt($request->only('username', 'password'));
if (!$token) {
RateLimiter::hit($throttleKey, self::LOGIN_DECAY_SECONDS);
$this->activityLogger->log(
event: 'login_failed',
logName: 'auth',
description: '[admin] Đăng nhập thất bại với username: ' . $request->username,
);
return response()->json([
'error' => true,
'message' => 'Thông tin đăng nhập không chính xác.',
], 401);
}
RateLimiter::clear($throttleKey);
$user = Auth::guard('admin')->user();
Auth::guard('admin')->getProvider()->rehashPasswordIfRequired($user, $request->only('password'));
$this->activityLogger->logAuth('login', 'admin', $user->id, $user->username ?? $user->email);
return response()->json([
'error' => false,
'data' => [
'token' => $token,
'token_type' => 'bearer',
'expires_in' => config('jwt.ttl') * 60,
'user' => new AdminUserResource($user),
],
]);
}
public function me(): JsonResponse
{
$user = Auth::guard('admin')->user();
return response()->json([
'error' => false,
'data' => ['user' => new AdminUserResource($user)],
]);
}
public function updateProfile(UpdateProfileRequest $request): JsonResponse
{
$user = Auth::guard('admin')->user();
$user->update($request->validated());
return response()->json([
'error' => false,
'message' => 'Cập nhật hồ sơ thành công.',
'data' => ['user' => new AdminUserResource($user->fresh())],
]);
}
public function refresh(): JsonResponse
{
try {
$newToken = JWTAuth::parseToken()->refresh();
$user = JWTAuth::setToken($newToken)->toUser();
return response()->json([
'error' => false,
'data' => [
'token' => $newToken,
'token_type' => 'bearer',
'expires_in' => config('jwt.ttl') * 60,
'user' => new AdminUserResource($user),
],
]);
} catch (JWTException) {
return response()->json([
'error' => true,
'message' => 'Không thể làm mới phiên đăng nhập. Vui lòng đăng nhập lại.',
], 401);
}
}
public function logout(): JsonResponse
{
$user = Auth::guard('admin')->user();
if ($user) {
$this->activityLogger->logAuth('logout', 'admin', $user->id, $user->username ?? $user->email);
}
Auth::guard('admin')->logout();
return response()->json([
'error' => false,
'message' => 'Đã đăng xuất thành công.',
]);
}
}