Files
portal-api/app/Http/Controllers/Api/Client/Concerns/HandlesClientPasswordReset.php
T
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

102 lines
3.2 KiB
PHP

<?php
namespace App\Http\Controllers\Api\Client\Concerns;
use App\Http\Requests\Api\Client\ClientChangePasswordRequest;
use App\Support\PasswordResetToken;
use Illuminate\Http\Request;
use Illuminate\Validation\Rules\Password;
trait HandlesClientPasswordReset
{
public function forgotPassword(Request $request)
{
$request->validate([
'email' => ['required', 'email', 'max:255'],
]);
$customer = $this->authService->sendPasswordResetLink($request->input('email'));
if ($customer) {
$this->activityLogger->log(
event: 'password_forgot',
logName: 'Customer',
description: "Customer {$customer->email} yêu cầu link đặt lại mật khẩu",
subject: $customer,
);
}
// Do not disclose whether the email exists.
return response()->json([
'error' => false,
'message' => 'Nếu email tồn tại, link đặt lại mật khẩu đã được gửi đến hộp thư của bạn.',
]);
}
public function showResetToken(string $token)
{
$customer = PasswordResetToken::findCustomer($token);
if (!$customer) {
return response()->json([
'error' => true,
'message' => 'Link đặt lại mật khẩu không hợp lệ hoặc đã hết hạn.',
], 404);
}
return response()->json(['error' => false, 'data' => ['email' => $customer->email]]);
}
public function resetPassword(Request $request)
{
$data = $request->validate([
'token' => ['required', 'string', 'size:' . PasswordResetToken::LENGTH],
'password' => ['required', Password::min(10), 'confirmed'],
]);
$customer = $this->authService->resetPassword($data['token'], $data['password']);
$this->activityLogger->log(
event: 'password_reset',
logName: 'Customer',
description: "Customer {$customer->email} đặt lại mật khẩu qua email",
subject: $customer,
);
return response()->json([
'error' => false,
'message' => 'Đặt lại mật khẩu thành công, vui lòng đăng nhập lại.',
]);
}
public function changePassword(ClientChangePasswordRequest $request)
{
$user = auth('client')->user();
try {
$this->authService->changePassword(
$user,
$request->input('current_password'),
$request->input('password'),
);
} catch (\Illuminate\Validation\ValidationException $e) {
return response()->json([
'error' => true,
'message' => collect($e->errors())->flatten()->first(),
], 422);
}
$this->activityLogger->log(
event: 'password_change',
logName: 'Customer',
description: "Customer {$user->email} đã đổi mật khẩu",
subject: $user,
);
return response()->json([
'error' => false,
'message' => 'Đổi mật khẩu thành công',
]);
}
}