WebAuthnRecoveryController.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. * Returns the credential creation options to the user.
  31. *
  32. * @param \Illuminate\Http\Request $request
  33. *
  34. * @return \Illuminate\Http\JsonResponse
  35. */
  36. public function options(Request $request): JsonResponse
  37. {
  38. $user = WebAuthn::getUser($request->validate($this->rules()));
  39. // We will proceed only if the broker can find the user and the token is valid.
  40. // If the user doesn't exists or the token is invalid, we will bail out with a
  41. // HTTP 401 code because the user doing the request is not authorized for it.
  42. abort_unless(WebAuthn::tokenExists($user, $request->input('token')), 401, __('auth.webauthn.invalid_recovery_token'));
  43. return response()->json(WebAuthn::generateAttestation($user));
  44. }
  45. /**
  46. * Get the response for a successful account recovery.
  47. *
  48. * @param \Illuminate\Http\Request $request
  49. * @param string $response
  50. *
  51. * @return \Illuminate\Http\JsonResponse
  52. *
  53. * @codeCoverageIgnore - already covered by larapass test
  54. */
  55. protected function sendRecoveryResponse(Request $request, string $response): JsonResponse
  56. {
  57. return response()->json(['message' => __('auth.webauthn.device_successfully_registered')]);
  58. }
  59. /**
  60. * Get the response for a failed account recovery.
  61. *
  62. * @param \Illuminate\Http\Request $request
  63. * @param string $response
  64. *
  65. * @return \Illuminate\Http\JsonResponse|void
  66. * @throws \Illuminate\Validation\ValidationException
  67. *
  68. * @codeCoverageIgnore - already covered by larapass test
  69. */
  70. protected function sendRecoveryFailedResponse(Request $request, string $response): JsonResponse
  71. {
  72. throw ValidationException::withMessages(['email' => [trans($response)]]);
  73. }
  74. }