WebAuthnLoginController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\User;
  5. use Carbon\Carbon;
  6. use Illuminate\Contracts\Support\Responsable;
  7. use Illuminate\Http\JsonResponse;
  8. use Illuminate\Http\Response;
  9. use Illuminate\Support\Arr;
  10. use Illuminate\Support\Facades\Log;
  11. use Laragear\WebAuthn\Http\Requests\AssertedRequest;
  12. use Laragear\WebAuthn\Http\Requests\AssertionRequest;
  13. use Laragear\WebAuthn\WebAuthn;
  14. class WebAuthnLoginController extends Controller
  15. {
  16. /*
  17. |--------------------------------------------------------------------------
  18. | WebAuthn Login Controller
  19. |--------------------------------------------------------------------------
  20. |
  21. | This controller allows the WebAuthn user device to request a login and
  22. | return the correctly signed challenge. Most of the hard work is done
  23. | by your Authentication Guard once the user is attempting to login.
  24. |
  25. */
  26. /**
  27. * Returns the challenge to assertion.
  28. *
  29. * @param \Laragear\WebAuthn\Http\Requests\AssertionRequest $request
  30. * @return \Illuminate\Contracts\Support\Responsable|\Illuminate\Http\JsonResponse
  31. */
  32. public function options(AssertionRequest $request) : Responsable|JsonResponse
  33. {
  34. switch (config('webauthn.user_verification')) {
  35. case WebAuthn::USER_VERIFICATION_DISCOURAGED:
  36. $request = $request->fastLogin(); // Makes the authenticator to only check for user presence on registration
  37. break;
  38. case WebAuthn::USER_VERIFICATION_REQUIRED:
  39. $request = $request->secureLogin(); // Makes the authenticator to always verify the user thoroughly on registration
  40. break;
  41. }
  42. return $request->toVerify($request->validate([
  43. 'email' => [
  44. 'required',
  45. 'email',
  46. new \App\Rules\CaseInsensitiveEmailExists,
  47. ],
  48. ]));
  49. }
  50. /**
  51. * Log the user in.
  52. *
  53. * @param \Laragear\WebAuthn\Http\Requests\AssertedRequest $request
  54. * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
  55. */
  56. public function login(AssertedRequest $request)
  57. {
  58. Log::info('User login via webauthn requested');
  59. if ($request->has('response')) {
  60. $response = $request->response;
  61. // Some authenticators do not send a userHandle so we hack the response to be compliant
  62. // with Laragear\WebAuthn implementation that waits for a userHandle
  63. if (! Arr::exists($response, 'userHandle') || blank($response['userHandle'])) {
  64. $response['userHandle'] = User::getFromCredentialId($request->id)?->userHandle();
  65. $request->merge(['response' => $response]);
  66. }
  67. }
  68. /**
  69. * @var \App\Models\User|null
  70. */
  71. $user = $request->login();
  72. if ($user) {
  73. $this->authenticated($user);
  74. return response()->json([
  75. 'message' => 'authenticated',
  76. 'name' => $user->name,
  77. 'preferences' => $user->preferences,
  78. ], Response::HTTP_OK);
  79. }
  80. return response()->noContent(422);
  81. }
  82. /**
  83. * The user has been authenticated.
  84. *
  85. * @param mixed $user
  86. * @return void|\Illuminate\Http\JsonResponse
  87. */
  88. protected function authenticated($user)
  89. {
  90. $user->last_seen_at = Carbon::now()->format('Y-m-d H:i:s');
  91. $user->save();
  92. Log::info(sprintf('User ID #%s authenticated using webauthn', $user->id));
  93. }
  94. }