Files
baodev0andClaude Sonnet 5 12d5b4951c feat: popup quảng cáo, đặt lại mật khẩu bằng token 80 ký tự, 2FA email, vá lỗ hổng bảo mật
- Popup/banner quảng cáo cấu hình từ admin (loại, link, lịch hiển thị)
- Đặt lại mật khẩu: token 80 ký tự kèm check digit Luhn thay cho token+email lộ trong URL, chỉ lưu bản băm, một lần dùng, hết hạn 60 phút
- Xác thực 2 lớp qua email khi đăng nhập (mã 6 số, ghi nhớ thiết bị 90 ngày)
- ApiAuthCheck chặn ngay tài khoản bị khoá dù JWT còn hạn (trước đây dùng được tới hết TTL)
- Handler trả 404 thay vì 500 khi ModelNotFoundException không bọc HttpException
- Test ClientTamperingTest: xác nhận IDOR/giả mạo dữ liệu từ client đều bị chặn

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-13 13:23:33 +07:00

59 lines
2.1 KiB
PHP

<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class ApiAuthCheck
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next, $guard = null)
{
$checkGuard = $guard ?: 'client';
if (!Auth::guard($checkGuard)->check()) {
return response()->json([
'error' => true,
'message' => 'Vui lòng đăng nhập để thực hiện!',
'code' => 401
], 401);
}
// Token cấp trước lần đổi mật khẩu gần nhất → vô hiệu.
// ponytail: token cũ chưa có claim `pv` vẫn được nhận cho tới khi hết JWT_TTL (chỉ lúc mới deploy)
if ($checkGuard === 'client' && (int) Auth::guard('client')->user()->status !== 1) {
return response()->json([
'error' => true,
'message' => 'Tài khoản không còn hoạt động, vui lòng liên hệ hỗ trợ.',
'code' => 401
], 401);
}
if ($checkGuard === 'client' && Auth::guard('client')->getToken()) {
$pv = Auth::guard('client')->payload()->get('pv');
if ($pv !== null && !hash_equals(Auth::guard('client')->user()->passwordVersion(), (string) $pv)) {
return response()->json([
'error' => true,
'message' => 'Phiên đăng nhập đã hết hạn, vui lòng đăng nhập lại.',
'code' => 401
], 401);
}
}
// Ensure request user is set for the controller
$request->setUserResolver(function () use ($checkGuard) {
return Auth::guard($checkGuard)->user();
});
return $next($request);
}
}