WebAuthnRecoveryController.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Http\Controllers\Controller;
  4. use App\Providers\RouteServiceProvider;
  5. use DarkGhostHunter\Larapass\Http\RecoversWebAuthn;
  6. use DarkGhostHunter\Larapass\Facades\WebAuthn;
  7. use Illuminate\Http\JsonResponse;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Validation\ValidationException;
  10. class WebAuthnRecoveryController extends Controller
  11. {
  12. use RecoversWebAuthn;
  13. /*
  14. |--------------------------------------------------------------------------
  15. | WebAuthn Recovery Controller
  16. |--------------------------------------------------------------------------
  17. |
  18. | When an user loses his device he will reach this controller to attach a
  19. | new device. The user will attach a new device, and optionally, disable
  20. | all others. Then he will be authenticated and redirected to your app.
  21. |
  22. */
  23. /**
  24. * Where to redirect users after resetting their password.
  25. *
  26. * @var string
  27. */
  28. protected $redirectTo = RouteServiceProvider::HOME;
  29. /**
  30. * Create a new controller instance.
  31. *
  32. * @return void
  33. */
  34. public function __construct()
  35. {
  36. // $this->middleware('guest');
  37. // $this->middleware('throttle:10,1')->only('options', 'recover');
  38. }
  39. /**
  40. * Returns the credential creation options to the user.
  41. *
  42. * @param \Illuminate\Http\Request $request
  43. *
  44. * @return \Illuminate\Http\JsonResponse
  45. */
  46. public function options(Request $request): JsonResponse
  47. {
  48. $user = WebAuthn::getUser($request->validate($this->rules()));
  49. // We will proceed only if the broker can find the user and the token is valid.
  50. // If the user doesn't exists or the token is invalid, we will bail out with a
  51. // HTTP 401 code because the user doing the request is not authorized for it.
  52. abort_unless(WebAuthn::tokenExists($user, $request->input('token')), 401, __('auth.webauthn.invalid_recovery_token'));
  53. return response()->json(WebAuthn::generateAttestation($user));
  54. }
  55. /**
  56. * Get the response for a successful account recovery.
  57. *
  58. * @param \Illuminate\Http\Request $request
  59. * @param string $response
  60. *
  61. * @return \Illuminate\Http\JsonResponse
  62. */
  63. protected function sendRecoveryResponse(Request $request, string $response): JsonResponse
  64. {
  65. return response()->json(['message' => __('auth.webauthn.device_successfully_registered')]);
  66. }
  67. /**
  68. * Get the response for a failed account recovery.
  69. *
  70. * @param \Illuminate\Http\Request $request
  71. * @param string $response
  72. *
  73. * @return \Illuminate\Http\JsonResponse|void
  74. * @throws \Illuminate\Validation\ValidationException
  75. */
  76. protected function sendRecoveryFailedResponse(Request $request, string $response): JsonResponse
  77. {
  78. throw ValidationException::withMessages(['email' => [trans($response)]]);
  79. }
  80. }