2022-03-15 14:47:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
|
2022-11-14 17:13:24 +01:00
|
|
|
use App\Extensions\WebauthnCredentialBroker;
|
2022-11-22 15:15:52 +01:00
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Http\Requests\WebauthnDeviceLostRequest;
|
2022-11-14 17:13:24 +01:00
|
|
|
use Illuminate\Foundation\Auth\ResetsPasswords;
|
2022-11-22 15:15:52 +01:00
|
|
|
use Illuminate\Http\Request;
|
2022-11-14 17:13:24 +01:00
|
|
|
use Illuminate\Support\Facades\Password;
|
2022-11-22 15:15:52 +01:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2022-03-15 14:47:07 +01:00
|
|
|
|
|
|
|
class WebAuthnDeviceLostController extends Controller
|
|
|
|
{
|
2022-11-14 17:13:24 +01:00
|
|
|
use ResetsPasswords;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a recovery email to the user.
|
|
|
|
*
|
2022-11-22 15:15:52 +01:00
|
|
|
* @param \App\Http\Requests\WebauthnDeviceLostRequest $request
|
2022-11-14 17:13:24 +01:00
|
|
|
* @param \App\Extensions\WebauthnCredentialBroker $broker
|
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
|
2022-11-22 15:15:52 +01:00
|
|
|
*
|
2022-11-14 17:13:24 +01:00
|
|
|
* @throws \Illuminate\Validation\ValidationException
|
|
|
|
*/
|
|
|
|
public function sendRecoveryEmail(WebauthnDeviceLostRequest $request, WebauthnCredentialBroker $broker)
|
|
|
|
{
|
|
|
|
$credentials = $request->validated();
|
|
|
|
|
|
|
|
$response = $broker->sendResetLink($credentials);
|
|
|
|
|
|
|
|
return $response === Password::RESET_LINK_SENT
|
|
|
|
? $this->sendRecoveryLinkResponse($request, $response)
|
|
|
|
: $this->sendRecoveryLinkFailedResponse($request, $response);
|
|
|
|
}
|
|
|
|
|
2022-03-15 14:47:07 +01:00
|
|
|
/**
|
2022-11-14 17:13:24 +01:00
|
|
|
* Get the response for a failed account recovery link.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param string $response
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
|
2022-11-22 15:15:52 +01:00
|
|
|
*
|
2022-11-14 17:13:24 +01:00
|
|
|
* @throws \Illuminate\Validation\ValidationException
|
2022-03-15 14:47:07 +01:00
|
|
|
*/
|
2022-11-14 17:13:24 +01:00
|
|
|
protected function sendRecoveryLinkFailedResponse(Request $request, string $response)
|
2022-03-15 14:47:07 +01:00
|
|
|
{
|
2022-11-14 17:13:24 +01:00
|
|
|
if ($request->wantsJson()) {
|
|
|
|
throw ValidationException::withMessages(['email' => [trans($response)]]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return back()
|
|
|
|
->withInput($request->only('email'))
|
|
|
|
->withErrors(['email' => trans($response)]);
|
2022-03-15 14:47:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the response for a successful account recovery link.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param string $response
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
|
|
|
|
*/
|
|
|
|
protected function sendRecoveryLinkResponse(Request $request, string $response)
|
|
|
|
{
|
|
|
|
return response()->json(['message' => __('auth.webauthn.account_recovery_email_sent')]);
|
|
|
|
}
|
2022-11-22 15:15:52 +01:00
|
|
|
}
|