WebAuthnRegisterController.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Contracts\Support\Responsable;
  5. use Illuminate\Http\Response;
  6. use Illuminate\Support\Facades\Log;
  7. use Laragear\WebAuthn\Http\Requests\AttestationRequest;
  8. use Laragear\WebAuthn\Http\Requests\AttestedRequest;
  9. use Laragear\WebAuthn\WebAuthn;
  10. class WebAuthnRegisterController extends Controller
  11. {
  12. /**
  13. * Returns a challenge to be verified by the user device.
  14. *
  15. * @param \Laragear\WebAuthn\Http\Requests\AttestationRequest $request
  16. * @return \Illuminate\Contracts\Support\Responsable
  17. */
  18. public function options(AttestationRequest $request) : Responsable
  19. {
  20. switch (config('webauthn.user_verification')) {
  21. case WebAuthn::USER_VERIFICATION_DISCOURAGED:
  22. $request = $request->fastRegistration(); // Makes the authenticator to only check for user presence on registration
  23. break;
  24. case WebAuthn::USER_VERIFICATION_REQUIRED:
  25. $request = $request->secureRegistration(); // Makes the authenticator to always verify the user thoroughly on registration
  26. break;
  27. }
  28. return $request
  29. // ->allowDuplicates() // Allows the device to create multiple credentials for the same user for this app
  30. // ->userless() // Tells the authenticator use this credential to login instantly, instead of asking for one
  31. ->toCreate();
  32. }
  33. /**
  34. * Registers a device for further WebAuthn authentication.
  35. *
  36. * @param \Laragear\WebAuthn\Http\Requests\AttestedRequest $request
  37. * @return \Illuminate\Http\Response
  38. */
  39. public function register(AttestedRequest $request) : Response
  40. {
  41. $request->save();
  42. Log::info(sprintf('User ID #%s registered a new security device', $request->user()->id));
  43. return response()->noContent();
  44. }
  45. }