2FAuth/app/Http/Controllers/Auth/WebAuthnRecoveryController.php

120 lines
4.1 KiB
PHP
Raw Normal View History

2022-03-15 14:47:07 +01:00
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Http\Requests\WebauthnRecoveryRequest;
use App\Extensions\WebauthnCredentialBroker;
use App\Facades\Settings;
use Illuminate\Auth\AuthenticationException;
2022-03-15 14:47:07 +01:00
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Support\Facades\Password;
use Illuminate\Support\Facades\App;
2022-03-15 14:47:07 +01:00
class WebAuthnRecoveryController extends Controller
{
use ResetsPasswords;
2022-03-15 14:47:07 +01:00
/**
* Let the user regain access to his account using email+password by resetting
* the "use webauthn only" setting.
*
* @param \App\Http\Requests\WebauthnRecoveryRequest $request
* @param \App\Extensions\WebauthnCredentialBroker $broker
2022-03-15 14:47:07 +01:00
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
* @throws \Illuminate\Validation\ValidationException
2022-03-15 14:47:07 +01:00
*/
public function recover(WebauthnRecoveryRequest $request, WebauthnCredentialBroker $broker)
{
$credentials = $request->validated();
$response = $broker->reset(
$credentials,
function ($user) use ($request) {
// At this time, the WebAuthnUserProvider is already registered in the Laravel Service Container,
// with a password_fallback value set using the useWebauthnOnly user setting (see AuthServiceProvider.php).
// To ensure user login with email+pwd credentials, we replace the registered WebAuthnUserProvider instance
// with a new instance configured with password_fallback On.
$provider = new \Laragear\WebAuthn\Auth\WebAuthnUserProvider(
app()->make('hash'),
\App\Models\User::class,
app()->make(\Laragear\WebAuthn\Assertion\Validator\AssertionValidator::class),
true,
);
Auth::guard()->setProvider($provider);
if (Auth::attempt($request->only('email', 'password'))) {
if ($this->shouldRevokeAllCredentials($request)) {
$user->flushCredentials();
}
Settings::delete('useWebauthnOnly');
}
else throw new AuthenticationException();
}
);
return $response === Password::PASSWORD_RESET
? $this->sendRecoveryResponse($request, $response)
: $this->sendRecoveryFailedResponse($request, $response);
}
2022-03-15 14:47:07 +01:00
/**
* Check if the user has set to revoke all credentials.
2022-03-15 14:47:07 +01:00
*
* @param \App\Http\Requests\WebauthnRecoveryRequest $request
2022-03-15 14:47:07 +01:00
*
* @return bool|mixed
2022-03-15 14:47:07 +01:00
*/
protected function shouldRevokeAllCredentials(WebauthnRecoveryRequest $request): mixed
2022-03-15 14:47:07 +01:00
{
return filter_var($request->header('WebAuthn-Unique'), FILTER_VALIDATE_BOOLEAN)
?: $request->input('revokeAll', true);
2022-03-15 14:47:07 +01:00
}
2022-03-15 14:47:07 +01:00
/**
* Get the response for a successful account recovery.
*
* @param \Illuminate\Http\Request $request
* @param string $response
*
* @return \Illuminate\Http\JsonResponse
2022-03-31 08:38:35 +02:00
*
2022-03-15 14:47:07 +01:00
*/
protected function sendRecoveryResponse(Request $request, string $response): JsonResponse
{
return response()->json(['message' => __('auth.webauthn.webauthn_login_disabled')]);
2022-03-15 14:47:07 +01:00
}
2022-03-15 14:47:07 +01:00
/**
* Get the response for a failed account recovery.
*
* @param \Illuminate\Http\Request $request
* @param string $response
*
* @return \Illuminate\Http\JsonResponse
2022-03-15 14:47:07 +01:00
* @throws \Illuminate\Validation\ValidationException
2022-03-31 08:38:35 +02:00
*
2022-03-15 14:47:07 +01:00
*/
protected function sendRecoveryFailedResponse(Request $request, string $response): JsonResponse
{
switch ($response) {
case Password::INVALID_TOKEN:
throw ValidationException::withMessages(['token' => [__('auth.webauthn.invalid_reset_token')]]);
default:
throw ValidationException::withMessages(['email' => [trans($response)]]);
}
2022-03-15 14:47:07 +01:00
}
}