WebAuthnRegisterController.php 1.7 KB

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